Skip to content

fix: add missing partition key kind for simple partition keys (#1342) - #1429

Open
kauanmodolo wants to merge 1 commit into
nestjs:masterfrom
kauanmodolo:fix/partition-key-kind-missing
Open

fix: add missing partition key kind for simple partition keys (#1342)#1429
kauanmodolo wants to merge 1 commit into
nestjs:masterfrom
kauanmodolo:fix/partition-key-kind-missing

Conversation

@kauanmodolo

Copy link
Copy Markdown

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

When using a simple partition key with the @CosmosPartitionKey decorator (e.g., @CosmosPartitionKey('userId')), the application fails at runtime with the following error:

Error: Missing partition key kind in the partition key path policy

This happens because the latest Azure Cosmos DB SDK requires the kind and version properties to be explicitly defined in the partition key configuration. The current implementation only sets the paths property for simple partition keys, which is no longer sufficient.

Example that fails:

@CosmosPartitionKey('userId')
class TestDto {
  userId: string;
  dummy: string;
}

Hierarchical partition keys work correctly because they already include the required properties:

@CosmosPartitionKey({
  paths: ['/name', '/type/label'],
  version: PartitionKeyDefinitionVersion.V2,
  kind: PartitionKeyKind.MultiHash,
})
class Event {
  name: string;
  type: { label: string };
}

Issue Number: #1342

What is the new behavior?

The fix automatically adds the required kind and version properties when a simple string partition key is used:

Simple partition keys now default to:

kind: PartitionKeyKind.Hash
version: PartitionKeyDefinitionVersion.V1

Hierarchical partition keys continue to work exactly as before (no changes)

After the fix, both scenarios work:

✅ Simple partition key:

@CosmosPartitionKey('userId')
class User {
  userId: string;
  name: string;
}
// Generates: { paths: ['/userId'], kind: Hash, version: V1 }

✅ Hierarchical partition key:

@CosmosPartitionKey({
  paths: ['/country', '/city'],
  version: PartitionKeyDefinitionVersion.V2,
  kind: PartitionKeyKind.MultiHash,
})
class Location {
  country: string;
  city: string;
}
// Preserves the exact configuration provided

Does this PR introduce a breaking change?

  • Yes
  • No

This is a backward-compatible bug fix. All existing code continues to work:

  • Simple partition keys that were broken are now fixed

  • Hierarchical partition keys continue to work as before

  • No API changes or behavioral changes for existing working code

Other information

Changes made:

Updated lib/cosmos-db/cosmos-db.providers.ts:

  • Added imports for PartitionKeyKind and PartitionKeyDefinitionVersion

  • Added logic to detect if partition key is a simple string or hierarchical object

  • Automatically add kind and version for simple partition keys

  • Updated PartitionKeyValues interface to support both string and object types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant