Skip to content

fix: add proper checks for Hierarchical PartionKeys #1283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/cosmos-db/cosmos-db-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class AzureCosmosDbCoreModule {
const connectionProvider = {
provide: cosmosConnectionName,
useFactory: async (cosmosModuleOptions: AzureCosmosDbOptions): Promise<any> => {
const { dbName, retryAttempts, retryDelay, connectionName, ...cosmosOptions } = cosmosModuleOptions;
const { dbName, retryAttempts, retryDelay, ...cosmosOptions } = cosmosModuleOptions;
Copy link
Preview

Copilot AI Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connectionName is no longer excluded and will be spread into cosmosOptions, potentially passing an unsupported option to the Azure SDK. Re-introduce connectionName in the destructuring exclusion or explicitly delete it from cosmosOptions.

Suggested change
const { dbName, retryAttempts, retryDelay, ...cosmosOptions } = cosmosModuleOptions;
const { dbName, retryAttempts, retryDelay, connectionName, ...cosmosOptions } = cosmosModuleOptions;

Copilot uses AI. Check for mistakes.


return await defer(async () => {
cosmosOptions.userAgentSuffix = await getuserAgentSuffix();
Expand Down
5 changes: 1 addition & 4 deletions lib/cosmos-db/cosmos-db.decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { AZURE_COSMOS_DB_ENTITY, CosmosPartitionKey } from './cosmos-db.decorato
import { PartitionKeyDefinitionVersion, PartitionKeyKind } from '@azure/cosmos';

describe('Azure CosmosDB Decorators', () => {
beforeEach(() => {
// tslint:disable-next-line: no-empty
function MockEntity() {}
});
beforeEach(() => {});

describe('@CosmosPartitionKey()', () => {
it('should add a PartitionKey ', () => {
Expand Down
14 changes: 11 additions & 3 deletions lib/cosmos-db/cosmos-db.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ export function createAzureCosmosDbProviders(
}

if (partitionKey != null) {
containerOptions.partitionKey = {
paths: [`/${partitionKey}`],
};
if (typeof partitionKey === 'string') {
containerOptions.partitionKey = {
paths: [`/${partitionKey}`],
};
}
else if (typeof partitionKey === 'object') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the partition key might be an array, which will still resolve to object and in this case will work just fine. Looks good to me :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the confusion here, but partitionKey here represents actually entityDescriptor.partitionKey object, not the partition keys values (paths).

containerOptions.partitionKey = partitionKey;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found one issue, it throws an error if the property path is an empty array

@CosmosPartitionKey({
  paths: [],
  version: PartitionKeyDefinitionVersion.V2,
  kind: PartitionKeyKind.MultiHash,
})

} else {
throw new Error(`Invalid PartitionKey definition for model ${model.dto.name}`);
}

}
const coResponse = await database.containers.createIfNotExists(containerOptions);

Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"dependencies": {
"@azure/cosmos": "^4.0.0",
"@azure/data-tables": "^13.2.2",
"@nestjs/common": "^11.0.0",
"@nestjs/core": "^11.0.0"
"@nestjs/common": "^10.0.0 || ^11.0.0",
"@nestjs/core": "^10.0.0 || ^11.0.0"
},
"devDependencies": {
"@commitlint/cli": "19.8.1",
Expand All @@ -45,7 +45,7 @@
"@nestjs/testing": "11.1.3",
"@types/jest": "29.5.14",
"@types/node": "22.13.8",
"azurite": "3.34.0",
"azurite": "^3.34.0",
"dotenv": "16.5.0",
"eslint": "9.28.0",
"eslint-config-prettier": "10.1.5",
Expand Down
Loading