@twin.org/entity-storage-connector-cosmosdb 0.9.2-next.6 → 0.9.2-next.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@
3
3
  import { BulkOperationType, CosmosClient, PartitionKeyKind } from "@azure/cosmos";
4
4
  import { HealthCategory, HealthStatus } from "@twin.org/api-models";
5
5
  import { ContextIdHelper, ContextIdStore } from "@twin.org/context";
6
- import { BaseError, Coerce, ComponentFactory, GeneralError, Guards, Is, ObjectHelper, Validation } from "@twin.org/core";
6
+ import { BaseError, Coerce, ComponentFactory, ConflictError, GeneralError, Guards, Is, Mutex, ObjectHelper, Validation } from "@twin.org/core";
7
7
  import { ComparisonOperator, EntitySchemaFactory, EntitySchemaHelper, LogicalOperator, SortDirection } from "@twin.org/entity";
8
8
  import { EntityStorageHelper } from "@twin.org/entity-storage-models";
9
9
  /**
@@ -49,11 +49,21 @@ export class CosmosDbEntityStorageConnector {
49
49
  * @internal
50
50
  */
51
51
  _primaryKey;
52
+ /**
53
+ * The name of the version property, if any.
54
+ * @internal
55
+ */
56
+ _versionKey;
52
57
  /**
53
58
  * The configuration for the connector.
54
59
  * @internal
55
60
  */
56
61
  _config;
62
+ /**
63
+ * Milliseconds to wait for optimistic-lock mutexes before throwing.
64
+ * @internal
65
+ */
66
+ _mutexTimeoutMs;
57
67
  /**
58
68
  * The Cosmos DB client.
59
69
  * @internal
@@ -80,7 +90,9 @@ export class CosmosDbEntityStorageConnector {
80
90
  this._entitySchemaName = options.entitySchema;
81
91
  this._partitionContextIds = options.partitionContextIds;
82
92
  this._primaryKey = EntitySchemaHelper.getPrimaryKey(this._entitySchema);
93
+ this._versionKey = EntitySchemaHelper.findVersionProperty(this._entitySchema);
83
94
  this._config = options.config;
95
+ this._mutexTimeoutMs = Coerce.integer(options.config.mutexTimeoutMs);
84
96
  this._client = new CosmosClient({
85
97
  endpoint: this._config.endpoint,
86
98
  key: this._config.key,
@@ -257,6 +269,9 @@ export class CosmosDbEntityStorageConnector {
257
269
  const { resource: item } = await this._container
258
270
  .item(id, partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE)
259
271
  .read();
272
+ if (Is.empty(item)) {
273
+ return undefined;
274
+ }
260
275
  return this.itemToEntity(item);
261
276
  }
262
277
  const conditionValues = [];
@@ -318,25 +333,65 @@ export class CosmosDbEntityStorageConnector {
318
333
  EntityStorageHelper.validateConditions(this._entitySchema, conditions);
319
334
  const contextIds = await ContextIdStore.getContextIds();
320
335
  const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
336
+ const submittedVersion = Is.stringValue(this._versionKey)
337
+ ? ObjectHelper.propertyGet(entity, this._versionKey)
338
+ : undefined;
339
+ const hasVersionCheck = !Is.empty(this._versionKey) && !Is.empty(submittedVersion) && submittedVersion > 0;
321
340
  const prepared = EntityStorageHelper.prepareEntity(entity, this._entitySchema, undefined, {
322
341
  nullBehavior: "omit"
323
342
  });
324
343
  const id = prepared[this._primaryKey.property];
344
+ const optimisticMutexKey = Is.stringValue(this._versionKey)
345
+ ? this.buildOptimisticMutexKey(partitionKey, id)
346
+ : undefined;
347
+ if (Is.stringValue(optimisticMutexKey)) {
348
+ await Mutex.lock(optimisticMutexKey, {
349
+ throwOnTimeout: true,
350
+ timeoutMs: this._mutexTimeoutMs
351
+ });
352
+ }
325
353
  try {
326
- if (Is.arrayValue(conditions)) {
354
+ let itemEtag;
355
+ if (Is.stringValue(this._versionKey) || Is.arrayValue(conditions)) {
327
356
  const item = this._container.item(id, partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE);
328
357
  const { resource: itemData } = await item.read();
329
- if (Is.notEmpty(itemData) && !this.verifyConditions(conditions, itemData)) {
330
- return;
358
+ if (!Is.empty(itemData)) {
359
+ if (hasVersionCheck) {
360
+ const storedVersion = ObjectHelper.propertyGet(itemData, this._versionKey) ?? 0;
361
+ if (storedVersion !== submittedVersion) {
362
+ throw new ConflictError(CosmosDbEntityStorageConnector.CLASS_NAME, "optimisticLockFailed", id);
363
+ }
364
+ }
365
+ if (Is.arrayValue(conditions) && !this.verifyConditions(conditions, itemData)) {
366
+ if (Is.stringValue(this._versionKey)) {
367
+ throw new ConflictError(CosmosDbEntityStorageConnector.CLASS_NAME, "conditionFailed", id);
368
+ }
369
+ return;
370
+ }
371
+ itemEtag = ObjectHelper.propertyGet(itemData, "_etag");
372
+ }
373
+ if (Is.stringValue(this._versionKey)) {
374
+ const storedVersion = !Is.empty(itemData)
375
+ ? (ObjectHelper.propertyGet(itemData, this._versionKey) ?? 0)
376
+ : 0;
377
+ ObjectHelper.propertySet(prepared, this._versionKey, storedVersion + 1);
331
378
  }
332
379
  }
333
380
  await this._container.items.upsert({
334
381
  id,
335
382
  [CosmosDbEntityStorageConnector._PARTITION_KEY]: partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE,
336
383
  ...prepared
337
- });
384
+ }, Is.stringValue(itemEtag)
385
+ ? { accessCondition: { type: "IfMatch", condition: itemEtag } }
386
+ : undefined);
338
387
  }
339
388
  catch (err) {
389
+ if (BaseError.isErrorName(err, ConflictError.CLASS_NAME)) {
390
+ throw err;
391
+ }
392
+ if (Is.object(err) && err.code === 412) {
393
+ throw new ConflictError(CosmosDbEntityStorageConnector.CLASS_NAME, "optimisticLockFailed", id);
394
+ }
340
395
  if (BaseError.isAggregateError(err)) {
341
396
  const errors = BaseError.fromAggregate(err);
342
397
  if (BaseError.someErrorCode(errors, "ResourceNotFoundException")) {
@@ -349,6 +404,11 @@ export class CosmosDbEntityStorageConnector {
349
404
  id
350
405
  }, err);
351
406
  }
407
+ finally {
408
+ if (Is.stringValue(optimisticMutexKey)) {
409
+ Mutex.unlock(optimisticMutexKey);
410
+ }
411
+ }
352
412
  }
353
413
  /**
354
414
  * Set multiple entities in a batch.
@@ -421,17 +481,32 @@ export class CosmosDbEntityStorageConnector {
421
481
  EntityStorageHelper.validateConditions(this._entitySchema, conditions);
422
482
  const contextIds = await ContextIdStore.getContextIds();
423
483
  const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
484
+ const optimisticMutexKey = Is.stringValue(this._versionKey)
485
+ ? this.buildOptimisticMutexKey(partitionKey, id)
486
+ : undefined;
487
+ if (Is.stringValue(optimisticMutexKey)) {
488
+ await Mutex.lock(optimisticMutexKey, {
489
+ throwOnTimeout: true,
490
+ timeoutMs: this._mutexTimeoutMs
491
+ });
492
+ }
424
493
  try {
425
494
  const item = this._container.item(id, partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE);
426
495
  const { resource: itemData } = await item.read();
427
496
  if (Is.notEmpty(itemData)) {
428
497
  if (Is.arrayValue(conditions) && !this.verifyConditions(conditions, itemData)) {
498
+ if (Is.stringValue(this._versionKey)) {
499
+ throw new ConflictError(CosmosDbEntityStorageConnector.CLASS_NAME, "conditionFailed", id);
500
+ }
429
501
  return;
430
502
  }
431
503
  await item.delete();
432
504
  }
433
505
  }
434
506
  catch (err) {
507
+ if (BaseError.isErrorName(err, ConflictError.CLASS_NAME)) {
508
+ throw err;
509
+ }
435
510
  if (BaseError.fromError(err) &&
436
511
  Is.object(err) &&
437
512
  err.body?.code === "NotFound") {
@@ -441,6 +516,11 @@ export class CosmosDbEntityStorageConnector {
441
516
  id
442
517
  }, err);
443
518
  }
519
+ finally {
520
+ if (Is.stringValue(optimisticMutexKey)) {
521
+ Mutex.unlock(optimisticMutexKey);
522
+ }
523
+ }
444
524
  }
445
525
  /**
446
526
  * Remove multiple entities by id.
@@ -963,6 +1043,16 @@ export class CosmosDbEntityStorageConnector {
963
1043
  verifyConditions(conditions, obj) {
964
1044
  return conditions.every(condition => ObjectHelper.propertyGet(obj, condition.property) === condition.value);
965
1045
  }
1046
+ /**
1047
+ * Build a mutex key for optimistic-locking critical sections.
1048
+ * @param partitionKey The resolved partition key.
1049
+ * @param id The entity id.
1050
+ * @returns The mutex key.
1051
+ * @internal
1052
+ */
1053
+ buildOptimisticMutexKey(partitionKey, id) {
1054
+ return `${CosmosDbEntityStorageConnector.CLASS_NAME}:optimistic:${this._config.databaseId}:${this._config.containerId}:${partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE}:${id}`;
1055
+ }
966
1056
  /**
967
1057
  * Convert an entity to an item.
968
1058
  * @param item The item to convert.
@@ -1033,7 +1123,14 @@ export class CosmosDbEntityStorageConnector {
1033
1123
  }
1034
1124
  }
1035
1125
  }
1036
- return compositeIndexes.length > 0 ? { compositeIndexes } : undefined;
1126
+ return compositeIndexes.length > 0
1127
+ ? {
1128
+ indexingMode: "consistent",
1129
+ automatic: true,
1130
+ includedPaths: [{ path: "/*" }],
1131
+ compositeIndexes
1132
+ }
1133
+ : undefined;
1037
1134
  }
1038
1135
  /**
1039
1136
  * Check if the container exists.
@@ -1 +1 @@
1
- {"version":3,"file":"cosmosDbEntityStorageConnector.js","sourceRoot":"","sources":["../../src/cosmosDbEntityStorageConnector.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EACN,iBAAiB,EAGjB,YAAY,EAMZ,gBAAgB,EAIhB,MAAM,eAAe,CAAC;AACvB,OAAO,EACN,cAAc,EACd,YAAY,EAGZ,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,cAAc,EAAoB,MAAM,mBAAmB,CAAC;AACtF,OAAO,EACN,SAAS,EACT,MAAM,EACN,gBAAgB,EAChB,YAAY,EACZ,MAAM,EACN,EAAE,EAEF,YAAY,EACZ,UAAU,EACV,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,kBAAkB,EAElB,mBAAmB,EACnB,kBAAkB,EAKlB,eAAe,EACf,aAAa,EACb,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACN,mBAAmB,EAInB,MAAM,iCAAiC,CAAC;AAMzC;;GAEG;AACH,MAAM,OAAO,8BAA8B;IAG1C;;OAEG;IACI,MAAM,CAAU,UAAU,oCAAoD;IA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I,CAAC,eAAe,EAAE,CAAC;YACrD,IAAI,eAAe,EAAE,CAAC;gBACrB,MAAM;YACP,CAAC;YACD,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;QACxD,CAAC;IACF,CAAC;IAED;;;;OAIG;IACK,KAAK,CAAC,yBAAyB;QACtC,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,GAAG,EAAE,EAAE,OAAO,EAAE,EAAE,CAAC;YAC/C,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;YACrD,IAAI,CAAC,eAAe,EAAE,CAAC;gBACtB,MAAM;YACP,CAAC;YACD,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;QACxD,CAAC;IACF,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tBulkOperationType,\n\ttype CompositePath,\n\ttype Container,\n\tCosmosClient,\n\ttype FeedOptions,\n\ttype IndexingPolicy,\n\ttype ItemDefinition,\n\ttype JSONValue,\n\ttype OperationInput,\n\tPartitionKeyKind,\n\ttype Resource,\n\ttype SqlParameter,\n\ttype SqlQuerySpec\n} from \"@azure/cosmos\";\nimport {\n\tHealthCategory,\n\tHealthStatus,\n\ttype IHealth,\n\ttype IHealthProviderComponent\n} from \"@twin.org/api-models\";\nimport { ContextIdHelper, ContextIdStore, type IContextIds } from \"@twin.org/context\";\nimport {\n\tBaseError,\n\tCoerce,\n\tComponentFactory,\n\tGeneralError,\n\tGuards,\n\tIs,\n\ttype IValidationFailure,\n\tObjectHelper,\n\tValidation\n} from \"@twin.org/core\";\nimport {\n\tComparisonOperator,\n\ttype EntityCondition,\n\tEntitySchemaFactory,\n\tEntitySchemaHelper,\n\ttype EntitySchemaPropertyType,\n\ttype IComparator,\n\ttype IEntitySchema,\n\ttype IEntitySchemaProperty,\n\tLogicalOperator,\n\tSortDirection\n} from \"@twin.org/entity\";\nimport {\n\tEntityStorageHelper,\n\ttype IEntityStorageConnector,\n\ttype IEntityStorageMigrationConnector,\n\ttype IMigrationOptions\n} from \"@twin.org/entity-storage-models\";\nimport type { ILoggingComponent } from \"@twin.org/logging-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { ICosmosDbEntityStorageConnectorConfig } from \"./models/ICosmosDbEntityStorageConnectorConfig.js\";\nimport type { ICosmosDbEntityStorageConnectorConstructorOptions } from \"./models/ICosmosDbEntityStorageConnectorConstructorOptions.js\";\n\n/**\n * Class for performing entity storage operations using Cosmos DB.\n */\nexport class CosmosDbEntityStorageConnector<T = unknown>\n\timplements IEntityStorageMigrationConnector<T>, IHealthProviderComponent\n{\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<CosmosDbEntityStorageConnector>();\n\n\t/**\n\t * Limit the number of entities when finding.\n\t * @internal\n\t */\n\tprivate static readonly _DEFAULT_LIMIT: number = 40;\n\n\t/**\n\t * Partition id field name.\n\t * @internal\n\t */\n\tprivate static readonly _PARTITION_KEY: string = \"partitionId\";\n\n\t/**\n\t * Partition id field value.\n\t * @internal\n\t */\n\tprivate static readonly _PARTITION_KEY_VALUE: string = \"root\";\n\n\t/**\n\t * The name for the schema.\n\t * @internal\n\t */\n\tprivate readonly _entitySchemaName: string;\n\n\t/**\n\t * The schema for the entity.\n\t * @internal\n\t */\n\tprivate readonly _entitySchema: IEntitySchema<T>;\n\n\t/**\n\t * The keys to use from the context ids to create partitions.\n\t * @internal\n\t */\n\tprivate readonly _partitionContextIds?: string[];\n\n\t/**\n\t * The primary key.\n\t * @internal\n\t */\n\tprivate readonly _primaryKey: IEntitySchemaProperty<T>;\n\n\t/**\n\t * The configuration for the connector.\n\t * @internal\n\t */\n\tprivate readonly _config: ICosmosDbEntityStorageConnectorConfig;\n\n\t/**\n\t * The Cosmos DB client.\n\t * @internal\n\t */\n\tprivate readonly _client: CosmosClient;\n\n\t/**\n\t * Container user for the data storage.\n\t * @internal\n\t */\n\tprivate readonly _container: Container;\n\n\t/**\n\t * Create a new instance of CosmosDbEntityStorageConnector.\n\t * @param options The options for the connector.\n\t */\n\tconstructor(options: ICosmosDbEntityStorageConnectorConstructorOptions) {\n\t\tGuards.object(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(options), options);\n\t\tGuards.stringValue(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.entitySchema),\n\t\t\toptions.entitySchema\n\t\t);\n\t\tGuards.object<ICosmosDbEntityStorageConnectorConfig>(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config),\n\t\t\toptions.config\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.endpoint),\n\t\t\toptions.config.endpoint\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.key),\n\t\t\toptions.config.key\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.databaseId),\n\t\t\toptions.config.databaseId\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.containerId),\n\t\t\toptions.config.containerId\n\t\t);\n\n\t\tthis._entitySchema = EntitySchemaFactory.get(options.entitySchema);\n\t\tthis._entitySchemaName = options.entitySchema;\n\t\tthis._partitionContextIds = options.partitionContextIds;\n\n\t\tthis._primaryKey = EntitySchemaHelper.getPrimaryKey<T>(this._entitySchema);\n\n\t\tthis._config = options.config;\n\n\t\tthis._client = new CosmosClient({\n\t\t\tendpoint: this._config.endpoint,\n\t\t\tkey: this._config.key,\n\t\t\tconnectionPolicy: {\n\t\t\t\tenableEndpointDiscovery: !this._config.disableEndpointDiscovery\n\t\t\t}\n\t\t});\n\n\t\tthis._container = this._client\n\t\t\t.database(this._config.databaseId)\n\t\t\t.container(this._config.containerId);\n\t}\n\n\t/**\n\t * Initialize the Cosmos DB environment.\n\t * @param nodeLoggingComponentType Optional type of the logging component.\n\t * @returns A promise that resolves to a boolean indicating success.\n\t */\n\tpublic async bootstrap(nodeLoggingComponentType?: string): Promise<boolean> {\n\t\tconst nodeLogging = ComponentFactory.getIfExists<ILoggingComponent>(nodeLoggingComponentType);\n\n\t\t// Create the database if it does not exist\n\t\ttry {\n\t\t\tconst databaseExists = await this.databaseExists();\n\n\t\t\tif (databaseExists) {\n\t\t\t\tawait nodeLogging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"databaseExists\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tdatabaseId: this._config.databaseId\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tawait nodeLogging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"databaseCreating\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tdatabaseId: this._config.databaseId\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tawait this._client.databases.create({\n\t\t\t\t\tid: this._config.databaseId\n\t\t\t\t});\n\n\t\t\t\tawait this.waitForDatabaseExists();\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tawait nodeLogging?.log({\n\t\t\t\tlevel: \"error\",\n\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"databaseCreateFailed\",\n\t\t\t\terror: BaseError.fromError(error),\n\t\t\t\tdata: {\n\t\t\t\t\tdatabaseId: this._config.databaseId\n\t\t\t\t}\n\t\t\t});\n\t\t\treturn false;\n\t\t}\n\n\t\t// Create the container if it does not exist\n\t\ttry {\n\t\t\tconst containerExists = await this.containerExists();\n\n\t\t\tif (containerExists) {\n\t\t\t\tawait nodeLogging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"containerExists\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tcontainerId: this._config.containerId\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tawait nodeLogging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"containerCreating\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tcontainerId: this._config.containerId\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tawait this._client.database(this._config.databaseId).containers.create(\n\t\t\t\t\t{\n\t\t\t\t\t\tid: this._config.containerId,\n\t\t\t\t\t\tpartitionKey: {\n\t\t\t\t\t\t\tkind: PartitionKeyKind.Hash,\n\t\t\t\t\t\t\tpaths: [`/${CosmosDbEntityStorageConnector._PARTITION_KEY}`]\n\t\t\t\t\t\t},\n\t\t\t\t\t\tindexingPolicy: this.buildIndexingPolicy()\n\t\t\t\t\t},\n\t\t\t\t\t{ offerThroughput: this._config.offerThroughput }\n\t\t\t\t);\n\n\t\t\t\tawait this.waitForContainerExists();\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tawait nodeLogging?.log({\n\t\t\t\tlevel: \"error\",\n\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"containerCreateFailed\",\n\t\t\t\terror: BaseError.fromError(error),\n\t\t\t\tdata: {\n\t\t\t\t\tcontainerId: this._config.containerId\n\t\t\t\t}\n\t\t\t});\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn CosmosDbEntityStorageConnector.CLASS_NAME;\n\t}\n\n\t/**\n\t * Returns the health status of the component.\n\t * @returns The health status of the component.\n\t */\n\tpublic async health(): Promise<IHealth[]> {\n\t\ttry {\n\t\t\tawait this._client\n\t\t\t\t.database(this._config.databaseId)\n\t\t\t\t.container(this._config.containerId)\n\t\t\t\t.read();\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tcategory: HealthCategory.Connectivity,\n\t\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\t\tdescription: \"healthDescription\",\n\t\t\t\t\tdata: { databaseId: this._config.databaseId, containerId: this._config.containerId }\n\t\t\t\t}\n\t\t\t];\n\t\t} catch {\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tcategory: HealthCategory.Connectivity,\n\t\t\t\t\tstatus: HealthStatus.Error,\n\t\t\t\t\tdescription: \"healthDescription\",\n\t\t\t\t\tmessage: \"connectionFailed\",\n\t\t\t\t\tdata: { databaseId: this._config.databaseId, containerId: this._config.containerId }\n\t\t\t\t}\n\t\t\t];\n\t\t}\n\t}\n\n\t/**\n\t * Get the schema for the entities.\n\t * @returns The schema for the entities.\n\t */\n\tpublic getSchema(): IEntitySchema {\n\t\treturn this._entitySchema as IEntitySchema;\n\t}\n\n\t/**\n\t * Get an entity from Cosmos DB.\n\t * @param id The id of the entity to get, or the index value if secondaryIndex is set.\n\t * @param secondaryIndex Get the item using a secondary index.\n\t * @param conditions The optional conditions to match for the entities.\n\t * @returns The object if it can be found or undefined.\n\t */\n\tpublic async get(\n\t\tid: string,\n\t\tsecondaryIndex?: keyof T,\n\t\tconditions?: { property: keyof T; value: unknown }[]\n\t): Promise<T | undefined> {\n\t\tGuards.stringValue(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(id), id);\n\t\tEntityStorageHelper.validateConditions(this._entitySchema, conditions);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\ttry {\n\t\t\t// No secondary index or conditions\n\t\t\tif (Is.empty(secondaryIndex) && !Is.arrayValue(conditions)) {\n\t\t\t\tconst { resource: item } = await this._container\n\t\t\t\t\t.item(id, partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE)\n\t\t\t\t\t.read<ItemDefinition>();\n\t\t\t\treturn this.itemToEntity(item);\n\t\t\t}\n\n\t\t\tconst conditionValues: SqlParameter[] = [];\n\n\t\t\tconst whereQuery: string[] = [\n\t\t\t\t`c.${CosmosDbEntityStorageConnector._PARTITION_KEY} = @partitionKey`\n\t\t\t];\n\t\t\tconditionValues.push({\n\t\t\t\tname: \"@partitionKey\",\n\t\t\t\tvalue: partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t});\n\n\t\t\t// With a secondary index\n\t\t\tif (Is.stringValue(secondaryIndex)) {\n\t\t\t\tconst secIndex = secondaryIndex.toString();\n\t\t\t\twhereQuery.push(`c.${secIndex} = @id`);\n\t\t\t} else {\n\t\t\t\twhereQuery.push(`c.${this._primaryKey.property as string} = @id`);\n\t\t\t}\n\t\t\tconditionValues.push({ name: \"@id\", value: id });\n\n\t\t\t// With conditions\n\t\t\tif (Is.arrayValue(conditions)) {\n\t\t\t\tfor (const c of conditions) {\n\t\t\t\t\twhereQuery.push(`c.${c.property as string} = @${c.property as string}`);\n\t\t\t\t\tconditionValues.push({\n\t\t\t\t\t\tname: `@${c.property as string}`,\n\t\t\t\t\t\tvalue: c.value as JSONValue\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst query: SqlQuerySpec = {\n\t\t\t\tquery: `SELECT * FROM c WHERE ${whereQuery.join(\" AND \")}`,\n\t\t\t\tparameters: conditionValues\n\t\t\t};\n\n\t\t\tconst { resources: items } = await this._container.items.query(query).fetchAll();\n\n\t\t\tif (items.length === 1) {\n\t\t\t\treturn this.itemToEntity(items[0]);\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tif (BaseError.isErrorCode(err, \"NotFound\")) {\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\"containerDoesNotExist\",\n\t\t\t\t\t{\n\t\t\t\t\t\tcontainerId: this._config.containerId\n\t\t\t\t\t},\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"getFailed\",\n\t\t\t\t{\n\t\t\t\t\tid\n\t\t\t\t},\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Set an entity.\n\t * @param entity The entity to set.\n\t * @param conditions The optional conditions to match for the entities.\n\t * @returns The id of the entity.\n\t */\n\tpublic async set(entity: T, conditions?: { property: keyof T; value: unknown }[]): Promise<void> {\n\t\tGuards.object<T>(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(entity), entity);\n\t\tEntityStorageHelper.validateConditions(this._entitySchema, conditions);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tconst prepared = EntityStorageHelper.prepareEntity(entity, this._entitySchema, undefined, {\n\t\t\tnullBehavior: \"omit\"\n\t\t});\n\n\t\tconst id = prepared[this._primaryKey.property] as string;\n\n\t\ttry {\n\t\t\tif (Is.arrayValue(conditions)) {\n\t\t\t\tconst item = this._container.item(\n\t\t\t\t\tid,\n\t\t\t\t\tpartitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t\t);\n\t\t\t\tconst { resource: itemData } = await item.read<ItemDefinition>();\n\t\t\t\tif (Is.notEmpty(itemData) && !this.verifyConditions(conditions, itemData as T)) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tawait this._container.items.upsert({\n\t\t\t\tid,\n\t\t\t\t[CosmosDbEntityStorageConnector._PARTITION_KEY]:\n\t\t\t\t\tpartitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE,\n\t\t\t\t...prepared\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tif (BaseError.isAggregateError(err)) {\n\t\t\t\tconst errors = BaseError.fromAggregate(err);\n\t\t\t\tif (BaseError.someErrorCode(errors, \"ResourceNotFoundException\")) {\n\t\t\t\t\tthrow new GeneralError(\n\t\t\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\t\"containerDoesNotExist\",\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcontainerId: this._config.containerId\n\t\t\t\t\t\t},\n\t\t\t\t\t\terr\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"setFailed\",\n\t\t\t\t{\n\t\t\t\t\tid\n\t\t\t\t},\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Set multiple entities in a batch.\n\t * @param entities The entities to set.\n\t * @returns Nothing.\n\t */\n\tpublic async setBatch(entities: T[]): Promise<void> {\n\t\tGuards.arrayValue(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(entities), entities);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tconst preparedEntities = entities.map(entity =>\n\t\t\tEntityStorageHelper.prepareEntity(entity, this._entitySchema, undefined, {\n\t\t\t\tnullBehavior: \"omit\"\n\t\t\t})\n\t\t);\n\n\t\ttry {\n\t\t\tawait this._container.items.executeBulkOperations(\n\t\t\t\tpreparedEntities.map(\n\t\t\t\t\tprepared =>\n\t\t\t\t\t\t({\n\t\t\t\t\t\t\toperationType: BulkOperationType.Upsert,\n\t\t\t\t\t\t\tpartitionKey: partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE,\n\t\t\t\t\t\t\tresourceBody: {\n\t\t\t\t\t\t\t\tid: prepared[this._primaryKey.property] as string,\n\t\t\t\t\t\t\t\t[CosmosDbEntityStorageConnector._PARTITION_KEY]:\n\t\t\t\t\t\t\t\t\tpartitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE,\n\t\t\t\t\t\t\t\t...(prepared as { [key: string]: unknown })\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}) as OperationInput\n\t\t\t\t)\n\t\t\t);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"setBatchFailed\",\n\t\t\t\tundefined,\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Empty all entities from the storage.\n\t * @returns Nothing.\n\t */\n\tpublic async empty(): Promise<void> {\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\t\tconst pk = partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE;\n\n\t\ttry {\n\t\t\tlet continuationToken: string | undefined;\n\t\t\tdo {\n\t\t\t\tconst feedOptions: FeedOptions = { maxItemCount: 100, continuationToken };\n\t\t\t\tconst { resources, continuationToken: nextToken } = await this._container.items\n\t\t\t\t\t.query<{ id: string }>(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tquery: `SELECT c.id FROM c WHERE c.${CosmosDbEntityStorageConnector._PARTITION_KEY} = @pk`,\n\t\t\t\t\t\t\tparameters: [{ name: \"@pk\", value: pk }]\n\t\t\t\t\t\t},\n\t\t\t\t\t\tfeedOptions\n\t\t\t\t\t)\n\t\t\t\t\t.fetchNext();\n\n\t\t\t\tcontinuationToken = nextToken;\n\n\t\t\t\tif (Is.arrayValue(resources)) {\n\t\t\t\t\tconst operations: OperationInput[] = resources.map(r => ({\n\t\t\t\t\t\toperationType: BulkOperationType.Delete,\n\t\t\t\t\t\tid: r.id,\n\t\t\t\t\t\tpartitionKey: pk\n\t\t\t\t\t}));\n\t\t\t\t\tawait this._container.items.executeBulkOperations(operations);\n\t\t\t\t}\n\t\t\t} while (Is.stringValue(continuationToken));\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"emptyFailed\",\n\t\t\t\tundefined,\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Remove the entity.\n\t * @param id The id of the entity to remove.\n\t * @param conditions The optional conditions to match for the entities.\n\t * @returns Nothing.\n\t */\n\tpublic async remove(\n\t\tid: string,\n\t\tconditions?: { property: keyof T; value: unknown }[]\n\t): Promise<void> {\n\t\tGuards.stringValue(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(id), id);\n\t\tEntityStorageHelper.validateConditions(this._entitySchema, conditions);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\ttry {\n\t\t\tconst item = this._container.item(\n\t\t\t\tid,\n\t\t\t\tpartitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t);\n\t\t\tconst { resource: itemData } = await item.read<ItemDefinition>();\n\t\t\tif (Is.notEmpty(itemData)) {\n\t\t\t\tif (Is.arrayValue(conditions) && !this.verifyConditions(conditions, itemData as T)) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tawait item.delete();\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tif (\n\t\t\t\tBaseError.fromError(err) &&\n\t\t\t\tIs.object<{ body?: { code?: string } }>(err) &&\n\t\t\t\terr.body?.code === \"NotFound\"\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"removeFailed\",\n\t\t\t\t{\n\t\t\t\t\tid\n\t\t\t\t},\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Remove multiple entities by id.\n\t * @param ids The ids of the entities to remove.\n\t * @returns Nothing.\n\t */\n\tpublic async removeBatch(ids: string[]): Promise<void> {\n\t\tGuards.arrayValue(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(ids), ids);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\ttry {\n\t\t\tconst operations: OperationInput[] = ids.map(id => ({\n\t\t\t\toperationType: BulkOperationType.Delete,\n\t\t\t\tid,\n\t\t\t\tpartitionKey: partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t}));\n\n\t\t\tawait this._container.items.executeBulkOperations(operations);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"removeBatchFailed\",\n\t\t\t\tundefined,\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Teardown the storage by deleting the underlying container.\n\t * @param nodeLoggingComponentType The node logging component type.\n\t * @returns True if the teardown process was successful.\n\t */\n\tpublic async teardown(nodeLoggingComponentType?: string): Promise<boolean> {\n\t\tconst nodeLogging = ComponentFactory.getIfExists<ILoggingComponent>(nodeLoggingComponentType);\n\n\t\tawait nodeLogging?.log({\n\t\t\tlevel: \"info\",\n\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tts: Date.now(),\n\t\t\tmessage: \"containerDeleting\",\n\t\t\tdata: { containerId: this._config.containerId }\n\t\t});\n\n\t\ttry {\n\t\t\tif (await this.containerExists()) {\n\t\t\t\tawait this._container.delete();\n\t\t\t\tawait this.waitForContainerNotExists();\n\t\t\t}\n\n\t\t\tawait nodeLogging?.log({\n\t\t\t\tlevel: \"info\",\n\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"containerDeleted\",\n\t\t\t\tdata: { containerId: this._config.containerId }\n\t\t\t});\n\n\t\t\treturn true;\n\t\t} catch (err) {\n\t\t\tawait nodeLogging?.log({\n\t\t\t\tlevel: \"error\",\n\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"teardownFailed\",\n\t\t\t\terror: BaseError.fromError(err)\n\t\t\t});\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Find all the entities which match the conditions.\n\t * @param conditions The conditions to match for the entities.\n\t * @param sortProperties The optional sort order.\n\t * @param properties The optional properties to return, defaults to all.\n\t * @param cursor The cursor to request the next chunk of entities.\n\t * @param limit The suggested number of entities to return in each chunk, in some scenarios can return a different amount.\n\t * @returns All the entities for the storage matching the conditions,\n\t * and a cursor which can be used to request more entities.\n\t */\n\tpublic async query(\n\t\tconditions?: EntityCondition<T>,\n\t\tsortProperties?: { property: keyof T; sortDirection: SortDirection }[],\n\t\tproperties?: (keyof T)[],\n\t\tcursor?: string,\n\t\tlimit?: number\n\t): Promise<{ entities: Partial<T>[]; cursor?: string }> {\n\t\tlet sql = \"\";\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tEntityStorageHelper.validateSortProperties(this._entitySchema, sortProperties);\n\t\tEntityStorageHelper.validateProperties(this._entitySchema, properties);\n\t\tEntityStorageHelper.validateConditionProperties(this._entitySchema, conditions);\n\n\t\t// Only the sort shapes covered by the composite indexes from buildIndexingPolicy are accepted.\n\t\tif (Is.arrayValue(sortProperties)) {\n\t\t\tconst nonPrimarySorts = sortProperties.filter(\n\t\t\t\tsortProperty => sortProperty.property !== this._primaryKey.property\n\t\t\t);\n\t\t\tif (nonPrimarySorts.length > 1) {\n\t\t\t\tthrow new GeneralError(CosmosDbEntityStorageConnector.CLASS_NAME, \"sortUnsupported\", {\n\t\t\t\t\tproperties: sortProperties.map(sortProperty => sortProperty.property),\n\t\t\t\t\tprimaryKey: this._primaryKey.property\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (!Is.empty(limit)) {\n\t\t\tconst validationFailures: IValidationFailure[] = [];\n\t\t\tValidation.integer(nameof(limit), limit, validationFailures, undefined, { minValue: 1 });\n\t\t\tValidation.asValidationError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"query\",\n\t\t\t\tvalidationFailures\n\t\t\t);\n\t\t}\n\n\t\ttry {\n\t\t\tconst returnSize = limit ?? CosmosDbEntityStorageConnector._DEFAULT_LIMIT;\n\n\t\t\tlet orderByClause: string = \"\";\n\t\t\tif (Is.arrayValue(sortProperties)) {\n\t\t\t\t// The primary key is unique so no property after its first occurrence can affect\n\t\t\t\t// the order, truncating keeps the ORDER BY within the provisioned composite indexes.\n\t\t\t\tconst primaryKeyIndex = sortProperties.findIndex(\n\t\t\t\t\tsortProperty => sortProperty.property === this._primaryKey.property\n\t\t\t\t);\n\t\t\t\tconst effectiveSorts =\n\t\t\t\t\tprimaryKeyIndex === -1 ? sortProperties : sortProperties.slice(0, primaryKeyIndex + 1);\n\t\t\t\tconst orderClauses = effectiveSorts.map(sortProperty => {\n\t\t\t\t\tconst direction = sortProperty.sortDirection === SortDirection.Ascending ? \"asc\" : \"desc\";\n\t\t\t\t\treturn `c.${String(sortProperty.property)} ${direction}`;\n\t\t\t\t});\n\t\t\t\torderByClause = `ORDER BY ${orderClauses.join(\", \")}`;\n\t\t\t}\n\n\t\t\tconst attributeNames: { [id: string]: string } = {};\n\t\t\tconst attributeValues: { [id: string]: unknown } = {};\n\t\t\tlet queryClause = this.buildQueryParameters(\"\", conditions, attributeNames, attributeValues);\n\n\t\t\tif (queryClause.length > 0) {\n\t\t\t\tqueryClause = ` AND ${queryClause}`;\n\t\t\t}\n\n\t\t\tconst selectClause = properties ? properties.map(p => `c.${p as string}`).join(\", \") : \"*\";\n\t\t\tconst baseQuery = `SELECT ${selectClause} FROM c WHERE c.${CosmosDbEntityStorageConnector._PARTITION_KEY} = @partitionId${queryClause}`;\n\t\t\tconst queryParameters: SqlParameter[] = [\n\t\t\t\t{\n\t\t\t\t\tname: \"@partitionId\",\n\t\t\t\t\tvalue: partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t\t},\n\t\t\t\t...Object.keys(attributeValues).map(\n\t\t\t\t\tkey => ({ name: `@${key}`, value: attributeValues[key] }) as SqlParameter\n\t\t\t\t)\n\t\t\t];\n\t\t\tconst queryPartitionKey = partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE;\n\n\t\t\t// For sorted queries use OFFSET LIMIT - CosmosDB continuation tokens are\n\t\t\t// not reliably returned for ORDER BY queries across all service versions.\n\t\t\t// The cursor is a numeric offset encoded as a string (same as SQL connectors).\n\t\t\tconst startIndex = Coerce.number(cursor) ?? 0;\n\t\t\tsql = `${baseQuery} ${orderByClause} OFFSET ${startIndex} LIMIT ${returnSize + 1}`;\n\t\t\tconst sortedQuerySpecs: SqlQuerySpec = { query: sql, parameters: queryParameters };\n\t\t\tconst sortedResponse = await this._container.items\n\t\t\t\t.query(sortedQuerySpecs, {\n\t\t\t\t\tpartitionKey: queryPartitionKey,\n\t\t\t\t\tmaxItemCount: returnSize + 1\n\t\t\t\t})\n\t\t\t\t.fetchNext();\n\t\t\tconst allItems = sortedResponse.resources;\n\t\t\tconst hasMore = allItems.length > returnSize;\n\t\t\treturn {\n\t\t\t\tentities: (hasMore ? allItems.slice(0, returnSize) : allItems).map(i =>\n\t\t\t\t\tthis.itemToEntity(i)\n\t\t\t\t),\n\t\t\t\tcursor: hasMore ? Coerce.string(startIndex + returnSize) : undefined\n\t\t\t};\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"queryFailed\",\n\t\t\t\t{ sql },\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Count all the entities which match the conditions.\n\t * @param conditions The optional conditions to match for the entities.\n\t * @returns The total count of entities in the storage.\n\t */\n\tpublic async count(conditions?: EntityCondition<T>): Promise<number> {\n\t\tEntityStorageHelper.validateConditionProperties(this._entitySchema, conditions);\n\t\ttry {\n\t\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\t\tconst partitionKey = ContextIdHelper.combinedContextKey(\n\t\t\t\tcontextIds,\n\t\t\t\tthis._partitionContextIds\n\t\t\t);\n\n\t\t\tconst attributeNames: { [id: string]: string } = {};\n\t\t\tconst attributeValues: { [id: string]: unknown } = {};\n\t\t\tlet queryClause = Is.empty(conditions)\n\t\t\t\t? \"\"\n\t\t\t\t: this.buildQueryParameters(\"\", conditions, attributeNames, attributeValues);\n\n\t\t\tif (queryClause.length > 0) {\n\t\t\t\tqueryClause = ` AND ${queryClause}`;\n\t\t\t}\n\n\t\t\tconst querySpec: SqlQuerySpec = {\n\t\t\t\tquery: `SELECT VALUE COUNT(1) FROM c WHERE c.${CosmosDbEntityStorageConnector._PARTITION_KEY} = @partitionId${queryClause}`,\n\t\t\t\tparameters: [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: \"@partitionId\",\n\t\t\t\t\t\tvalue: partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t\t\t},\n\t\t\t\t\t...Object.keys(attributeValues).map(\n\t\t\t\t\t\tkey => ({ name: `@${key}`, value: attributeValues[key] }) as SqlParameter\n\t\t\t\t\t)\n\t\t\t\t]\n\t\t\t};\n\t\t\tconst { resources } = await this._container.items.query(querySpec).fetchAll();\n\t\t\treturn resources[0] ?? 0;\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"countFailed\",\n\t\t\t\tundefined,\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Get a unique list of all the context ids from the storage.\n\t * @returns The list of unique context ids.\n\t */\n\tpublic async getPartitionContextIds(): Promise<IContextIds[] | undefined> {\n\t\tconst partitionContextIds = this._partitionContextIds;\n\t\tif (!Is.arrayValue(partitionContextIds)) {\n\t\t\treturn undefined;\n\t\t}\n\t\ttry {\n\t\t\tconst { resources: partitionIds } = await this._container.items\n\t\t\t\t.query<string>({\n\t\t\t\t\tquery: `SELECT DISTINCT VALUE c.${CosmosDbEntityStorageConnector._PARTITION_KEY} FROM c`\n\t\t\t\t})\n\t\t\t\t.fetchAll();\n\t\t\treturn partitionIds\n\t\t\t\t.filter(id => Is.stringValue(id))\n\t\t\t\t.map(id => ContextIdHelper.shortSplit(partitionContextIds, id));\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"getPartitionContextIdsFailed\",\n\t\t\t\tundefined,\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Create the target connector for performing the migration using a temporary container.\n\t * @param newEntitySchema The name of the new entity schema to create the connector for.\n\t * @returns Connector for performing the migration.\n\t */\n\tpublic async createTargetConnector<U>(\n\t\tnewEntitySchema: string\n\t): Promise<IEntityStorageConnector<U>> {\n\t\tconst migrationContainerId = `${this._config.containerId}Migration${Date.now()}`;\n\t\treturn new CosmosDbEntityStorageConnector<U>({\n\t\t\tentitySchema: newEntitySchema,\n\t\t\tconfig: { ...this._config, containerId: migrationContainerId },\n\t\t\tpartitionContextIds: this._partitionContextIds\n\t\t});\n\t}\n\n\t/**\n\t * Finalize the migration by tearing down the old container and replacing it with the target container.\n\t * @param targetConnector The target connector to finalize the migration with.\n\t * @param options The options to control how the migration is finalized.\n\t * @param loggingComponentType The optional component type to use for logging.\n\t * @returns The final connector pointing at the original container id.\n\t */\n\tpublic async finalizeMigration<U>(\n\t\ttargetConnector: CosmosDbEntityStorageConnector<U>,\n\t\toptions?: IMigrationOptions,\n\t\tloggingComponentType?: string\n\t): Promise<CosmosDbEntityStorageConnector<U>> {\n\t\t// There is no rename operation in DynamoDB so we have to create a new table with the original name and copy the data over\n\n\t\t// Teardown the existing table with the original name to free up the name for the new table\n\t\tawait this.teardown(loggingComponentType);\n\n\t\t// Create a new connector with the original table name but with the new schema\n\t\t// and copy the data from the migration table to the new table using batch operations\n\t\tconst finalConnector = new CosmosDbEntityStorageConnector<U>({\n\t\t\tentitySchema: targetConnector._entitySchemaName,\n\t\t\tconfig: this._config,\n\t\t\tpartitionContextIds: this._partitionContextIds\n\t\t});\n\t\tif (await finalConnector.bootstrap(loggingComponentType)) {\n\t\t\t// Since there is no rename, we need to copy the data from the migration table to the new table\n\t\t\tconst partitions = await targetConnector.getPartitionContextIds();\n\t\t\tconst batchSize = options?.batchSize ?? CosmosDbEntityStorageConnector._DEFAULT_LIMIT;\n\t\t\tawait this.bulkCopy(targetConnector, finalConnector, partitions, batchSize);\n\n\t\t\tawait targetConnector.teardown(loggingComponentType);\n\n\t\t\treturn finalConnector;\n\t\t}\n\n\t\tthrow new GeneralError(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\"finalizeMigrationFailedBootstrap\"\n\t\t);\n\t}\n\n\t/**\n\t * Cleanup the migration if a migration fails or needs to be aborted.\n\t * @param targetConnector The target connector to cleanup.\n\t * @param options The options to control how the migration is cleaned up.\n\t * @param loggingComponentType The optional component type to use for logging.\n\t */\n\tpublic async cleanupMigration<U>(\n\t\ttargetConnector: IEntityStorageConnector<U> | undefined,\n\t\toptions?: IMigrationOptions,\n\t\tloggingComponentType?: string\n\t): Promise<void> {\n\t\t// If something failed the only thing to cleanup is the migration table\n\t\tawait targetConnector?.teardown?.(loggingComponentType);\n\t}\n\n\t/**\n\t * Copy all entities from sourceConnector to destConnector, paging through each partition.\n\t * @param sourceConnector The connector to read entities from.\n\t * @param destConnector The connector to write entities to.\n\t * @param partitions The partition list returned by getPartitionContextIds.\n\t * @param batchSize The number of entities to read per page.\n\t * @internal\n\t */\n\tprivate async bulkCopy<U>(\n\t\tsourceConnector: CosmosDbEntityStorageConnector<U>,\n\t\tdestConnector: CosmosDbEntityStorageConnector<U>,\n\t\tpartitions: IContextIds[] | undefined,\n\t\tbatchSize: number\n\t): Promise<void> {\n\t\t// undefined → not partitioned: one pass with no partition key.\n\t\t// [] → partitioned but empty: nothing to copy, return early.\n\t\t// [{…}, …] → partitioned with data: iterate over each partition.\n\t\tif (partitions?.length === 0) {\n\t\t\treturn;\n\t\t}\n\t\tconst partitionList = partitions ?? [{}];\n\n\t\tfor (let i = 0; i < partitionList.length; i++) {\n\t\t\t// Values from getPartitionContextIds are already short-form, so we join them\n\t\t\t// directly rather than using combinedContextKey, which expects long-form input\n\t\t\t// and calls guardAll (throwing if a registered handler rejects short-form values).\n\t\t\tconst partitionKey = Is.arrayValue(sourceConnector._partitionContextIds)\n\t\t\t\t? sourceConnector._partitionContextIds.map(k => partitionList[i][k]).join(\"/\")\n\t\t\t\t: CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE;\n\n\t\t\tlet continuationToken: string | undefined;\n\t\t\tdo {\n\t\t\t\tconst feedOptions: FeedOptions = { maxItemCount: batchSize, continuationToken };\n\t\t\t\tconst { resources, continuationToken: nextToken } = await sourceConnector._container.items\n\t\t\t\t\t.query<ItemDefinition & Resource>(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tquery: `SELECT * FROM c WHERE c.${CosmosDbEntityStorageConnector._PARTITION_KEY} = @partitionId`,\n\t\t\t\t\t\t\tparameters: [{ name: \"@partitionId\", value: partitionKey }]\n\t\t\t\t\t\t},\n\t\t\t\t\t\tfeedOptions\n\t\t\t\t\t)\n\t\t\t\t\t.fetchNext();\n\n\t\t\t\tcontinuationToken = nextToken;\n\n\t\t\t\tif (Is.arrayValue(resources)) {\n\t\t\t\t\tconst operations: OperationInput[] = resources.map(\n\t\t\t\t\t\t({\n\t\t\t\t\t\t\t_rid: rid,\n\t\t\t\t\t\t\t_self: self,\n\t\t\t\t\t\t\t_ts: ts,\n\t\t\t\t\t\t\t_etag: etag,\n\t\t\t\t\t\t\t_attachments: attachments,\n\t\t\t\t\t\t\t...rest\n\t\t\t\t\t\t}) => ({\n\t\t\t\t\t\t\toperationType: BulkOperationType.Upsert,\n\t\t\t\t\t\t\tpartitionKey,\n\t\t\t\t\t\t\tresourceBody: rest\n\t\t\t\t\t\t})\n\t\t\t\t\t);\n\t\t\t\t\tawait destConnector._container.items.executeBulkOperations(operations);\n\t\t\t\t}\n\t\t\t} while (Is.stringValue(continuationToken));\n\t\t}\n\t}\n\n\t/**\n\t * Create an SQL condition clause.\n\t * @param objectPath The path for the nested object.\n\t * @param condition The conditions to create the query from.\n\t * @param attributeNames The attribute names to use in the query.\n\t * @param attributeValues The attribute values to use in the query.\n\t * @returns The condition clause.\n\t * @internal\n\t */\n\tprivate buildQueryParameters(\n\t\tobjectPath: string,\n\t\tcondition: EntityCondition<T> | undefined,\n\t\tattributeNames: { [id: string]: string },\n\t\tattributeValues: { [id: string]: unknown }\n\t): string {\n\t\t// If no conditions are defined then return empty string\n\t\tif (Is.undefined(condition)) {\n\t\t\treturn \"\";\n\t\t}\n\n\t\tif (\"conditions\" in condition) {\n\t\t\tif (condition.conditions.length === 0) {\n\t\t\t\treturn \"\";\n\t\t\t}\n\t\t\t// It's a group of comparisons, so check the individual items and combine with the logical operator\n\t\t\tconst joinConditions: string[] = condition.conditions.map(c =>\n\t\t\t\tthis.buildQueryParameters(objectPath, c, attributeNames, attributeValues)\n\t\t\t);\n\n\t\t\tconst logicalOperator = this.mapConditionalOperator(condition.logicalOperator);\n\t\t\tconst queryClause = joinConditions\n\t\t\t\t.filter(j => j.length > 0)\n\t\t\t\t.map(j => j)\n\t\t\t\t.join(` ${logicalOperator} `);\n\n\t\t\treturn Is.stringValue(queryClause) ? ` (${queryClause}) ` : \"\";\n\t\t}\n\n\t\tconst schemaProp = this._entitySchema.properties?.find(p => p.property === condition.property);\n\n\t\t// It's a single value so just create the property comparison for the condition\n\t\tconst comparison = this.mapComparisonOperator(\n\t\t\tobjectPath,\n\t\t\tcondition,\n\t\t\tschemaProp?.type,\n\t\t\tattributeNames,\n\t\t\tattributeValues\n\t\t);\n\n\t\treturn comparison;\n\t}\n\n\t/**\n\t * Map the framework comparison operators to those in CosmosDB.\n\t * @param objectPath The prefix to use for the condition.\n\t * @param comparator The operator to map.\n\t * @param type The type of the property.\n\t * @param attributeNames The attribute names to use in the query.\n\t * @returns The comparison expression.\n\t * @throws GeneralError if the comparison operator is not supported.\n\t * @internal\n\t */\n\tprivate mapComparisonOperator(\n\t\tobjectPath: string,\n\t\tcomparator: IComparator,\n\t\ttype: EntitySchemaPropertyType | undefined,\n\t\tattributeNames: { [id: string]: string },\n\t\tattributeValues: { [id: string]: unknown }\n\t): string {\n\t\tlet prop = objectPath;\n\t\tif (prop.length > 0) {\n\t\t\tprop += \".\";\n\t\t}\n\t\tprop += comparator.property;\n\n\t\tlet attributeName = this.populateAttributeNames(prop, attributeNames);\n\t\tlet propName = `${attributeName.replace(/\\./g, \"\").replace(/@/g, \"\")}`;\n\n\t\tif (\n\t\t\t(comparator.comparison === ComparisonOperator.Equals ||\n\t\t\t\tcomparator.comparison === ComparisonOperator.NotEquals) &&\n\t\t\t(comparator.value === null || comparator.value === undefined)\n\t\t) {\n\t\t\t// Cosmos DB SQL null semantics mirror standard SQL: any comparison using = or <>\n\t\t\t// against null evaluates to UNKNOWN, not TRUE, so no rows are returned.\n\t\t\t// IS_NULL() and IS_DEFINED() must be used instead (no bound parameter needed).\n\t\t\tif (comparator.comparison === ComparisonOperator.Equals) {\n\t\t\t\treturn `(IS_NULL(c.${attributeName}) OR NOT IS_DEFINED(c.${attributeName}))`;\n\t\t\t}\n\t\t\treturn `(IS_DEFINED(c.${attributeName}) AND NOT IS_NULL(c.${attributeName}))`;\n\t\t} else if (Is.array(comparator.value)) {\n\t\t\tconst dbValues = comparator.value.map(v => this.propertyToDbValue(v, type));\n\t\t\tif (dbValues.length === 0 && comparator.comparison === ComparisonOperator.In) {\n\t\t\t\t// CosmosDB rejects `IN ()` - return always-false sentinel (#141).\n\t\t\t\treturn \"1=0\";\n\t\t\t}\n\t\t\tconst arrAttributeNames = [];\n\t\t\tfor (let i = 0; i < dbValues.length; i++) {\n\t\t\t\tconst arrAttributeName = `${propName}${i}`;\n\t\t\t\tattributeValues[arrAttributeName] = dbValues[i];\n\t\t\t\tarrAttributeNames.push(arrAttributeName);\n\t\t\t}\n\t\t\tpropName = attributeName;\n\t\t\tattributeName = `(${arrAttributeNames.map(name => `@${name}`).join(\", \")})`;\n\t\t} else if (\n\t\t\tIs.object(comparator.value) &&\n\t\t\t(comparator.comparison === ComparisonOperator.Equals ||\n\t\t\t\tcomparator.comparison === ComparisonOperator.NotEquals)\n\t\t) {\n\t\t\t// CosmosDB SQL does not support object equality with =; expand to per-property comparisons.\n\t\t\tconst op = comparator.comparison === ComparisonOperator.Equals ? \"=\" : \"<>\";\n\t\t\tconst join = comparator.comparison === ComparisonOperator.Equals ? \" AND \" : \" OR \";\n\t\t\tconst clauses: string[] = [];\n\t\t\tfor (const [key, val] of Object.entries(comparator.value)) {\n\t\t\t\tconst paramName = `${propName}${key}`;\n\t\t\t\tattributeValues[paramName] = val;\n\t\t\t\tclauses.push(`c.${attributeName}.${key} ${op} @${paramName}`);\n\t\t\t}\n\t\t\treturn clauses.length === 1 ? clauses[0] : `(${clauses.join(join)})`;\n\t\t} else {\n\t\t\t// Avoid parameter name conflicts by ensuring unique parameter names in the query\n\t\t\tlet counter = 1;\n\t\t\twhile (propName in attributeValues) {\n\t\t\t\tpropName = `${attributeName.replace(/\\./g, \"\").replace(/@/g, \"\")}${counter}`;\n\t\t\t\tcounter++;\n\t\t\t}\n\t\t\tattributeValues[propName] = comparator.value;\n\t\t}\n\n\t\tconst matches = attributeName.split(\".\").length;\n\t\tif (matches && comparator.comparison === ComparisonOperator.Equals) {\n\t\t\tattributeName = attributeName\n\t\t\t\t.split(\".\")\n\t\t\t\t.map(part => `[\"${part}\"]`)\n\t\t\t\t.join(\"\");\n\t\t\treturn `c${attributeName} = @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.Equals) {\n\t\t\treturn `c.${attributeName} = @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.NotEquals) {\n\t\t\treturn `c.${attributeName} <> @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.GreaterThan) {\n\t\t\treturn `c.${attributeName} > @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.LessThan) {\n\t\t\treturn `c.${attributeName} < @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.GreaterThanOrEqual) {\n\t\t\treturn `c.${attributeName} >= @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.LessThanOrEqual) {\n\t\t\treturn `c.${attributeName} <= @${propName}`;\n\t\t} else if (\n\t\t\tIs.object(attributeValues[propName]) &&\n\t\t\tcomparator.comparison === ComparisonOperator.Includes\n\t\t) {\n\t\t\treturn `ARRAY_CONTAINS(c.${attributeName}, @${propName}, true)`;\n\t\t} else if (\n\t\t\t(type === \"array\" || type === \"object\") &&\n\t\t\tcomparator.comparison === ComparisonOperator.Includes\n\t\t) {\n\t\t\treturn `ARRAY_CONTAINS(c.${attributeName}, @${propName})`;\n\t\t} else if (\n\t\t\t(type === \"array\" || type === \"object\") &&\n\t\t\tcomparator.comparison === ComparisonOperator.NotIncludes\n\t\t) {\n\t\t\treturn `NOT ARRAY_CONTAINS(c.${attributeName}, @${propName})`;\n\t\t} else if (comparator.comparison === ComparisonOperator.Includes) {\n\t\t\treturn `CONTAINS(c.${attributeName}, @${propName})`;\n\t\t} else if (comparator.comparison === ComparisonOperator.NotIncludes) {\n\t\t\treturn `NOT CONTAINS(c.${attributeName}, @${propName})`;\n\t\t} else if (comparator.comparison === ComparisonOperator.In) {\n\t\t\treturn `c.${propName} IN ${attributeName}`;\n\t\t}\n\n\t\tthrow new GeneralError(CosmosDbEntityStorageConnector.CLASS_NAME, \"comparisonNotSupported\", {\n\t\t\tcomparison: comparator.comparison\n\t\t});\n\t}\n\n\t/**\n\t * Format a value to insert into DB.\n\t * @param value The value to format.\n\t * @param type The type for the property.\n\t * @returns The value after conversion.\n\t * @internal\n\t */\n\tprivate propertyToDbValue(value: unknown, type?: EntitySchemaPropertyType): unknown {\n\t\tif (Is.object(value)) {\n\t\t\tconst map: { [id: string]: unknown } = {};\n\t\t\tfor (const key in value) {\n\t\t\t\tmap[key] = this.propertyToDbValue(value[key]);\n\t\t\t}\n\t\t\treturn map;\n\t\t}\n\n\t\tif (type === \"string\") {\n\t\t\treturn `${Coerce.string(value)}`;\n\t\t} else if (type === \"integer\" || type === \"number\") {\n\t\t\treturn Coerce.number(value) ?? 0;\n\t\t} else if (type === \"boolean\") {\n\t\t\treturn Coerce.boolean(value) ?? false;\n\t\t}\n\n\t\treturn Coerce.string(value) ?? \"\";\n\t}\n\n\t/**\n\t * Create a unique name for the attribute.\n\t * @param name The name to create a unique name for.\n\t * @param attributeNames The attribute names to use in the query.\n\t * @returns The unique name.\n\t * @internal\n\t */\n\tprivate populateAttributeNames(name: string, attributeNames: { [id: string]: string }): string {\n\t\tconst parts = name.split(\".\");\n\t\tconst attributeNameParts: string[] = [];\n\n\t\tfor (const part of parts) {\n\t\t\tconst hashPart = `${part}`;\n\t\t\tif (Is.empty(attributeNames[hashPart])) {\n\t\t\t\tattributeNames[hashPart] = part;\n\t\t\t}\n\t\t\tattributeNameParts.push(hashPart);\n\t\t}\n\n\t\treturn attributeNameParts.join(\".\");\n\t}\n\n\t/**\n\t * Map the framework conditional operators to those in CosmosDB.\n\t * @param operator The operator to map.\n\t * @returns The conditional operator.\n\t * @throws GeneralError if the conditional operator is not supported.\n\t * @internal\n\t */\n\tprivate mapConditionalOperator(operator?: LogicalOperator): string {\n\t\tif ((operator ?? LogicalOperator.And) === LogicalOperator.And) {\n\t\t\treturn \"AND\";\n\t\t} else if (operator === LogicalOperator.Or) {\n\t\t\treturn \"OR\";\n\t\t}\n\n\t\tthrow new GeneralError(CosmosDbEntityStorageConnector.CLASS_NAME, \"conditionalNotSupported\", {\n\t\t\toperator\n\t\t});\n\t}\n\n\t/**\n\t * Verify the conditions for the entity.\n\t * @param conditions The conditions to verify.\n\t * @param obj The object to verify the conditions against.\n\t * @returns True if all conditions are met, false otherwise.\n\t * @internal\n\t */\n\tprivate verifyConditions(\n\t\tconditions: { property: keyof T; value: unknown }[],\n\t\tobj: { [key in keyof T]: unknown }\n\t): boolean {\n\t\treturn conditions.every(\n\t\t\tcondition => ObjectHelper.propertyGet(obj, condition.property as string) === condition.value\n\t\t);\n\t}\n\n\t/**\n\t * Convert an entity to an item.\n\t * @param item The item to convert.\n\t * @returns The entity.\n\t * @internal\n\t */\n\tprivate itemToEntity(item: (ItemDefinition & Resource) | undefined): T {\n\t\treturn EntityStorageHelper.unPrepareEntity<T>(item as T, [\n\t\t\tCosmosDbEntityStorageConnector._PARTITION_KEY,\n\t\t\t\"_attachments\",\n\t\t\t\"_etag\",\n\t\t\t\"_rid\",\n\t\t\t\"_self\",\n\t\t\t\"_ts\"\n\t\t]);\n\t}\n\n\t/**\n\t * Check if the database exists.\n\t * @returns True if the database exists, false otherwise.\n\t * @internal\n\t */\n\tprivate async databaseExists(): Promise<boolean> {\n\t\ttry {\n\t\t\tconst { resources: databaseList } = await this._client.databases.readAll().fetchAll();\n\n\t\t\treturn databaseList.some((db: Resource) => db.id === this._config.databaseId);\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Wait for a database to exist.\n\t * @returns Nothing.\n\t * @internal\n\t */\n\tprivate async waitForDatabaseExists(): Promise<void> {\n\t\tfor (let attempt = 0; attempt < 20; attempt++) {\n\t\t\tconst databaseExists = await this.databaseExists();\n\t\t\tif (databaseExists) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tawait new Promise(resolve => setTimeout(resolve, 250));\n\t\t}\n\t}\n\n\t/**\n\t * Build the composite indexes needed to serve multi-property ORDER BY queries.\n\t * Pairing each sortable property with the primary key in both directions covers all\n\t * four direction combinations, as Cosmos DB also serves each index reversed.\n\t * @returns The indexing policy for the container, or undefined if the schema has no\n\t * sortable properties.\n\t * @internal\n\t */\n\tprivate buildIndexingPolicy(): IndexingPolicy | undefined {\n\t\tconst primaryKeyPath = `/${this._primaryKey.property as string}`;\n\t\tconst compositeIndexes: CompositePath[][] = [];\n\n\t\tif (Is.arrayValue(this._entitySchema.properties)) {\n\t\t\tfor (const prop of this._entitySchema.properties) {\n\t\t\t\tif (!prop.isPrimary && (Is.stringValue(prop.sortDirection) || prop.isSecondary)) {\n\t\t\t\t\tconst propertyPath = `/${prop.property as string}`;\n\t\t\t\t\tcompositeIndexes.push([\n\t\t\t\t\t\t{ path: propertyPath, order: \"ascending\" },\n\t\t\t\t\t\t{ path: primaryKeyPath, order: \"ascending\" }\n\t\t\t\t\t]);\n\t\t\t\t\tcompositeIndexes.push([\n\t\t\t\t\t\t{ path: propertyPath, order: \"ascending\" },\n\t\t\t\t\t\t{ path: primaryKeyPath, order: \"descending\" }\n\t\t\t\t\t]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn compositeIndexes.length > 0 ? { compositeIndexes } : undefined;\n\t}\n\n\t/**\n\t * Check if the container exists.\n\t * @returns True if the container exists, false otherwise.\n\t * @internal\n\t */\n\tprivate async containerExists(): Promise<boolean> {\n\t\ttry {\n\t\t\tconst { resources: containers } = await this._client\n\t\t\t\t.database(this._config.databaseId)\n\t\t\t\t.containers.readAll()\n\t\t\t\t.fetchAll();\n\n\t\t\treturn containers.some((c: Resource) => c.id === this._config.containerId);\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Wait for a container to exist.\n\t * @returns Nothing.\n\t * @internal\n\t */\n\tprivate async waitForContainerExists(): Promise<void> {\n\t\tfor (let attempt = 0; attempt < 20; attempt++) {\n\t\t\tconst containerExists = await this.containerExists();\n\t\t\tif (containerExists) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tawait new Promise(resolve => setTimeout(resolve, 250));\n\t\t}\n\t}\n\n\t/**\n\t * Wait for a container to not exist.\n\t * @returns Nothing.\n\t * @internal\n\t */\n\tprivate async waitForContainerNotExists(): Promise<void> {\n\t\tfor (let attempt = 0; attempt < 20; attempt++) {\n\t\t\tconst containerExists = await this.containerExists();\n\t\t\tif (!containerExists) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tawait new Promise(resolve => setTimeout(resolve, 250));\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"cosmosDbEntityStorageConnector.js","sourceRoot":"","sources":["../../src/cosmosDbEntityStorageConnector.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EACN,iBAAiB,EAGjB,YAAY,EAMZ,gBAAgB,EAIhB,MAAM,eAAe,CAAC;AACvB,OAAO,EACN,cAAc,EACd,YAAY,EAGZ,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,cAAc,EAAoB,MAAM,mBAAmB,CAAC;AACtF,OAAO,EACN,SAAS,EACT,MAAM,EACN,gBAAgB,EAChB,aAAa,EACb,YAAY,EACZ,MAAM,EACN,EAAE,EACF,KAAK,EAEL,YAAY,EACZ,UAAU,EACV,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,kBAAkB,EA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AAO;iBAClD,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC;iBACjC,UAAU,CAAC,OAAO,EAAE;iBACpB,QAAQ,EAAE,CAAC;YAEb,OAAO,UAAU,CAAC,IAAI,CAAC,CAAC,CAAW,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;QAC5E,CAAC;QAAC,MAAM,CAAC;YACR,OAAO,KAAK,CAAC;QACd,CAAC;IACF,CAAC;IAED;;;;OAIG;IACK,KAAK,CAAC,sBAAsB;QACnC,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,GAAG,EAAE,EAAE,OAAO,EAAE,EAAE,CAAC;YAC/C,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;YACrD,IAAI,eAAe,EAAE,CAAC;gBACrB,MAAM;YACP,CAAC;YACD,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;QACxD,CAAC;IACF,CAAC;IAED;;;;OAIG;IACK,KAAK,CAAC,yBAAyB;QACtC,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,GAAG,EAAE,EAAE,OAAO,EAAE,EAAE,CAAC;YAC/C,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;YACrD,IAAI,CAAC,eAAe,EAAE,CAAC;gBACtB,MAAM;YACP,CAAC;YACD,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;QACxD,CAAC;IACF,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tBulkOperationType,\n\ttype CompositePath,\n\ttype Container,\n\tCosmosClient,\n\ttype FeedOptions,\n\ttype IndexingPolicy,\n\ttype ItemDefinition,\n\ttype JSONValue,\n\ttype OperationInput,\n\tPartitionKeyKind,\n\ttype Resource,\n\ttype SqlParameter,\n\ttype SqlQuerySpec\n} from \"@azure/cosmos\";\nimport {\n\tHealthCategory,\n\tHealthStatus,\n\ttype IHealth,\n\ttype IHealthProviderComponent\n} from \"@twin.org/api-models\";\nimport { ContextIdHelper, ContextIdStore, type IContextIds } from \"@twin.org/context\";\nimport {\n\tBaseError,\n\tCoerce,\n\tComponentFactory,\n\tConflictError,\n\tGeneralError,\n\tGuards,\n\tIs,\n\tMutex,\n\ttype IValidationFailure,\n\tObjectHelper,\n\tValidation\n} from \"@twin.org/core\";\nimport {\n\tComparisonOperator,\n\ttype EntityCondition,\n\tEntitySchemaFactory,\n\tEntitySchemaHelper,\n\ttype EntitySchemaPropertyType,\n\ttype IComparator,\n\ttype IEntitySchema,\n\ttype IEntitySchemaProperty,\n\tLogicalOperator,\n\tSortDirection\n} from \"@twin.org/entity\";\nimport {\n\tEntityStorageHelper,\n\ttype IEntityStorageConnector,\n\ttype IEntityStorageMigrationConnector,\n\ttype IMigrationOptions\n} from \"@twin.org/entity-storage-models\";\nimport type { ILoggingComponent } from \"@twin.org/logging-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport type { ICosmosDbEntityStorageConnectorConfig } from \"./models/ICosmosDbEntityStorageConnectorConfig.js\";\nimport type { ICosmosDbEntityStorageConnectorConstructorOptions } from \"./models/ICosmosDbEntityStorageConnectorConstructorOptions.js\";\n\n/**\n * Class for performing entity storage operations using Cosmos DB.\n */\nexport class CosmosDbEntityStorageConnector<T = unknown>\n\timplements IEntityStorageMigrationConnector<T>, IHealthProviderComponent\n{\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<CosmosDbEntityStorageConnector>();\n\n\t/**\n\t * Limit the number of entities when finding.\n\t * @internal\n\t */\n\tprivate static readonly _DEFAULT_LIMIT: number = 40;\n\n\t/**\n\t * Partition id field name.\n\t * @internal\n\t */\n\tprivate static readonly _PARTITION_KEY: string = \"partitionId\";\n\n\t/**\n\t * Partition id field value.\n\t * @internal\n\t */\n\tprivate static readonly _PARTITION_KEY_VALUE: string = \"root\";\n\n\t/**\n\t * The name for the schema.\n\t * @internal\n\t */\n\tprivate readonly _entitySchemaName: string;\n\n\t/**\n\t * The schema for the entity.\n\t * @internal\n\t */\n\tprivate readonly _entitySchema: IEntitySchema<T>;\n\n\t/**\n\t * The keys to use from the context ids to create partitions.\n\t * @internal\n\t */\n\tprivate readonly _partitionContextIds?: string[];\n\n\t/**\n\t * The primary key.\n\t * @internal\n\t */\n\tprivate readonly _primaryKey: IEntitySchemaProperty<T>;\n\n\t/**\n\t * The name of the version property, if any.\n\t * @internal\n\t */\n\tprivate readonly _versionKey?: string;\n\n\t/**\n\t * The configuration for the connector.\n\t * @internal\n\t */\n\tprivate readonly _config: ICosmosDbEntityStorageConnectorConfig;\n\n\t/**\n\t * Milliseconds to wait for optimistic-lock mutexes before throwing.\n\t * @internal\n\t */\n\tprivate readonly _mutexTimeoutMs?: number;\n\n\t/**\n\t * The Cosmos DB client.\n\t * @internal\n\t */\n\tprivate readonly _client: CosmosClient;\n\n\t/**\n\t * Container user for the data storage.\n\t * @internal\n\t */\n\tprivate readonly _container: Container;\n\n\t/**\n\t * Create a new instance of CosmosDbEntityStorageConnector.\n\t * @param options The options for the connector.\n\t */\n\tconstructor(options: ICosmosDbEntityStorageConnectorConstructorOptions) {\n\t\tGuards.object(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(options), options);\n\t\tGuards.stringValue(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.entitySchema),\n\t\t\toptions.entitySchema\n\t\t);\n\t\tGuards.object<ICosmosDbEntityStorageConnectorConfig>(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config),\n\t\t\toptions.config\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.endpoint),\n\t\t\toptions.config.endpoint\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.key),\n\t\t\toptions.config.key\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.databaseId),\n\t\t\toptions.config.databaseId\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.containerId),\n\t\t\toptions.config.containerId\n\t\t);\n\n\t\tthis._entitySchema = EntitySchemaFactory.get(options.entitySchema);\n\t\tthis._entitySchemaName = options.entitySchema;\n\t\tthis._partitionContextIds = options.partitionContextIds;\n\n\t\tthis._primaryKey = EntitySchemaHelper.getPrimaryKey<T>(this._entitySchema);\n\t\tthis._versionKey = EntitySchemaHelper.findVersionProperty(this._entitySchema);\n\n\t\tthis._config = options.config;\n\t\tthis._mutexTimeoutMs = Coerce.integer(options.config.mutexTimeoutMs);\n\n\t\tthis._client = new CosmosClient({\n\t\t\tendpoint: this._config.endpoint,\n\t\t\tkey: this._config.key,\n\t\t\tconnectionPolicy: {\n\t\t\t\tenableEndpointDiscovery: !this._config.disableEndpointDiscovery\n\t\t\t}\n\t\t});\n\n\t\tthis._container = this._client\n\t\t\t.database(this._config.databaseId)\n\t\t\t.container(this._config.containerId);\n\t}\n\n\t/**\n\t * Initialize the Cosmos DB environment.\n\t * @param nodeLoggingComponentType Optional type of the logging component.\n\t * @returns A promise that resolves to a boolean indicating success.\n\t */\n\tpublic async bootstrap(nodeLoggingComponentType?: string): Promise<boolean> {\n\t\tconst nodeLogging = ComponentFactory.getIfExists<ILoggingComponent>(nodeLoggingComponentType);\n\n\t\t// Create the database if it does not exist\n\t\ttry {\n\t\t\tconst databaseExists = await this.databaseExists();\n\n\t\t\tif (databaseExists) {\n\t\t\t\tawait nodeLogging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"databaseExists\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tdatabaseId: this._config.databaseId\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tawait nodeLogging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"databaseCreating\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tdatabaseId: this._config.databaseId\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tawait this._client.databases.create({\n\t\t\t\t\tid: this._config.databaseId\n\t\t\t\t});\n\n\t\t\t\tawait this.waitForDatabaseExists();\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tawait nodeLogging?.log({\n\t\t\t\tlevel: \"error\",\n\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"databaseCreateFailed\",\n\t\t\t\terror: BaseError.fromError(error),\n\t\t\t\tdata: {\n\t\t\t\t\tdatabaseId: this._config.databaseId\n\t\t\t\t}\n\t\t\t});\n\t\t\treturn false;\n\t\t}\n\n\t\t// Create the container if it does not exist\n\t\ttry {\n\t\t\tconst containerExists = await this.containerExists();\n\n\t\t\tif (containerExists) {\n\t\t\t\tawait nodeLogging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"containerExists\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tcontainerId: this._config.containerId\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tawait nodeLogging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"containerCreating\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\tcontainerId: this._config.containerId\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tawait this._client.database(this._config.databaseId).containers.create(\n\t\t\t\t\t{\n\t\t\t\t\t\tid: this._config.containerId,\n\t\t\t\t\t\tpartitionKey: {\n\t\t\t\t\t\t\tkind: PartitionKeyKind.Hash,\n\t\t\t\t\t\t\tpaths: [`/${CosmosDbEntityStorageConnector._PARTITION_KEY}`]\n\t\t\t\t\t\t},\n\t\t\t\t\t\tindexingPolicy: this.buildIndexingPolicy()\n\t\t\t\t\t},\n\t\t\t\t\t{ offerThroughput: this._config.offerThroughput }\n\t\t\t\t);\n\n\t\t\t\tawait this.waitForContainerExists();\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tawait nodeLogging?.log({\n\t\t\t\tlevel: \"error\",\n\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"containerCreateFailed\",\n\t\t\t\terror: BaseError.fromError(error),\n\t\t\t\tdata: {\n\t\t\t\t\tcontainerId: this._config.containerId\n\t\t\t\t}\n\t\t\t});\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn CosmosDbEntityStorageConnector.CLASS_NAME;\n\t}\n\n\t/**\n\t * Returns the health status of the component.\n\t * @returns The health status of the component.\n\t */\n\tpublic async health(): Promise<IHealth[]> {\n\t\ttry {\n\t\t\tawait this._client\n\t\t\t\t.database(this._config.databaseId)\n\t\t\t\t.container(this._config.containerId)\n\t\t\t\t.read();\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tcategory: HealthCategory.Connectivity,\n\t\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\t\tdescription: \"healthDescription\",\n\t\t\t\t\tdata: { databaseId: this._config.databaseId, containerId: this._config.containerId }\n\t\t\t\t}\n\t\t\t];\n\t\t} catch {\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tcategory: HealthCategory.Connectivity,\n\t\t\t\t\tstatus: HealthStatus.Error,\n\t\t\t\t\tdescription: \"healthDescription\",\n\t\t\t\t\tmessage: \"connectionFailed\",\n\t\t\t\t\tdata: { databaseId: this._config.databaseId, containerId: this._config.containerId }\n\t\t\t\t}\n\t\t\t];\n\t\t}\n\t}\n\n\t/**\n\t * Get the schema for the entities.\n\t * @returns The schema for the entities.\n\t */\n\tpublic getSchema(): IEntitySchema {\n\t\treturn this._entitySchema as IEntitySchema;\n\t}\n\n\t/**\n\t * Get an entity from Cosmos DB.\n\t * @param id The id of the entity to get, or the index value if secondaryIndex is set.\n\t * @param secondaryIndex Get the item using a secondary index.\n\t * @param conditions The optional conditions to match for the entities.\n\t * @returns The object if it can be found or undefined.\n\t */\n\tpublic async get(\n\t\tid: string,\n\t\tsecondaryIndex?: keyof T,\n\t\tconditions?: { property: keyof T; value: unknown }[]\n\t): Promise<T | undefined> {\n\t\tGuards.stringValue(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(id), id);\n\t\tEntityStorageHelper.validateConditions(this._entitySchema, conditions);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\ttry {\n\t\t\t// No secondary index or conditions\n\t\t\tif (Is.empty(secondaryIndex) && !Is.arrayValue(conditions)) {\n\t\t\t\tconst { resource: item } = await this._container\n\t\t\t\t\t.item(id, partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE)\n\t\t\t\t\t.read<ItemDefinition>();\n\t\t\t\tif (Is.empty(item)) {\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t\treturn this.itemToEntity(item);\n\t\t\t}\n\n\t\t\tconst conditionValues: SqlParameter[] = [];\n\n\t\t\tconst whereQuery: string[] = [\n\t\t\t\t`c.${CosmosDbEntityStorageConnector._PARTITION_KEY} = @partitionKey`\n\t\t\t];\n\t\t\tconditionValues.push({\n\t\t\t\tname: \"@partitionKey\",\n\t\t\t\tvalue: partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t});\n\n\t\t\t// With a secondary index\n\t\t\tif (Is.stringValue(secondaryIndex)) {\n\t\t\t\tconst secIndex = secondaryIndex.toString();\n\t\t\t\twhereQuery.push(`c.${secIndex} = @id`);\n\t\t\t} else {\n\t\t\t\twhereQuery.push(`c.${this._primaryKey.property as string} = @id`);\n\t\t\t}\n\t\t\tconditionValues.push({ name: \"@id\", value: id });\n\n\t\t\t// With conditions\n\t\t\tif (Is.arrayValue(conditions)) {\n\t\t\t\tfor (const c of conditions) {\n\t\t\t\t\twhereQuery.push(`c.${c.property as string} = @${c.property as string}`);\n\t\t\t\t\tconditionValues.push({\n\t\t\t\t\t\tname: `@${c.property as string}`,\n\t\t\t\t\t\tvalue: c.value as JSONValue\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst query: SqlQuerySpec = {\n\t\t\t\tquery: `SELECT * FROM c WHERE ${whereQuery.join(\" AND \")}`,\n\t\t\t\tparameters: conditionValues\n\t\t\t};\n\n\t\t\tconst { resources: items } = await this._container.items.query(query).fetchAll();\n\n\t\t\tif (items.length === 1) {\n\t\t\t\treturn this.itemToEntity(items[0]);\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tif (BaseError.isErrorCode(err, \"NotFound\")) {\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\"containerDoesNotExist\",\n\t\t\t\t\t{\n\t\t\t\t\t\tcontainerId: this._config.containerId\n\t\t\t\t\t},\n\t\t\t\t\terr\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"getFailed\",\n\t\t\t\t{\n\t\t\t\t\tid\n\t\t\t\t},\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Set an entity.\n\t * @param entity The entity to set.\n\t * @param conditions The optional conditions to match for the entities.\n\t * @returns The id of the entity.\n\t */\n\tpublic async set(entity: T, conditions?: { property: keyof T; value: unknown }[]): Promise<void> {\n\t\tGuards.object<T>(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(entity), entity);\n\t\tEntityStorageHelper.validateConditions(this._entitySchema, conditions);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tconst submittedVersion = Is.stringValue(this._versionKey)\n\t\t\t? ObjectHelper.propertyGet<number>(entity, this._versionKey)\n\t\t\t: undefined;\n\t\tconst hasVersionCheck =\n\t\t\t!Is.empty(this._versionKey) && !Is.empty(submittedVersion) && submittedVersion > 0;\n\n\t\tconst prepared = EntityStorageHelper.prepareEntity(entity, this._entitySchema, undefined, {\n\t\t\tnullBehavior: \"omit\"\n\t\t});\n\n\t\tconst id = prepared[this._primaryKey.property] as string;\n\t\tconst optimisticMutexKey = Is.stringValue(this._versionKey)\n\t\t\t? this.buildOptimisticMutexKey(partitionKey, id)\n\t\t\t: undefined;\n\n\t\tif (Is.stringValue(optimisticMutexKey)) {\n\t\t\tawait Mutex.lock(optimisticMutexKey, {\n\t\t\t\tthrowOnTimeout: true,\n\t\t\t\ttimeoutMs: this._mutexTimeoutMs\n\t\t\t});\n\t\t}\n\n\t\ttry {\n\t\t\tlet itemEtag: string | undefined;\n\n\t\t\tif (Is.stringValue(this._versionKey) || Is.arrayValue(conditions)) {\n\t\t\t\tconst item = this._container.item(\n\t\t\t\t\tid,\n\t\t\t\t\tpartitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t\t);\n\t\t\t\tconst { resource: itemData } = await item.read<ItemDefinition>();\n\t\t\t\tif (!Is.empty(itemData)) {\n\t\t\t\t\tif (hasVersionCheck) {\n\t\t\t\t\t\tconst storedVersion = ObjectHelper.propertyGet<number>(itemData, this._versionKey) ?? 0;\n\t\t\t\t\t\tif (storedVersion !== submittedVersion) {\n\t\t\t\t\t\t\tthrow new ConflictError(\n\t\t\t\t\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\t\t\t\"optimisticLockFailed\",\n\t\t\t\t\t\t\t\tid\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (Is.arrayValue(conditions) && !this.verifyConditions(conditions, itemData as T)) {\n\t\t\t\t\t\tif (Is.stringValue(this._versionKey)) {\n\t\t\t\t\t\t\tthrow new ConflictError(\n\t\t\t\t\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\t\t\t\"conditionFailed\",\n\t\t\t\t\t\t\t\tid\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\titemEtag = ObjectHelper.propertyGet(itemData, \"_etag\");\n\t\t\t\t}\n\t\t\t\tif (Is.stringValue(this._versionKey)) {\n\t\t\t\t\tconst storedVersion = !Is.empty(itemData)\n\t\t\t\t\t\t? (ObjectHelper.propertyGet<number>(itemData, this._versionKey) ?? 0)\n\t\t\t\t\t\t: 0;\n\t\t\t\t\tObjectHelper.propertySet(prepared, this._versionKey, storedVersion + 1);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tawait this._container.items.upsert(\n\t\t\t\t{\n\t\t\t\t\tid,\n\t\t\t\t\t[CosmosDbEntityStorageConnector._PARTITION_KEY]:\n\t\t\t\t\t\tpartitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE,\n\t\t\t\t\t...prepared\n\t\t\t\t},\n\t\t\t\tIs.stringValue(itemEtag)\n\t\t\t\t\t? { accessCondition: { type: \"IfMatch\", condition: itemEtag } }\n\t\t\t\t\t: undefined\n\t\t\t);\n\t\t} catch (err) {\n\t\t\tif (BaseError.isErrorName(err, ConflictError.CLASS_NAME)) {\n\t\t\t\tthrow err;\n\t\t\t}\n\t\t\tif (Is.object<{ code?: number }>(err) && err.code === 412) {\n\t\t\t\tthrow new ConflictError(\n\t\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\"optimisticLockFailed\",\n\t\t\t\t\tid\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (BaseError.isAggregateError(err)) {\n\t\t\t\tconst errors = BaseError.fromAggregate(err);\n\t\t\t\tif (BaseError.someErrorCode(errors, \"ResourceNotFoundException\")) {\n\t\t\t\t\tthrow new GeneralError(\n\t\t\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\t\"containerDoesNotExist\",\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcontainerId: this._config.containerId\n\t\t\t\t\t\t},\n\t\t\t\t\t\terr\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"setFailed\",\n\t\t\t\t{\n\t\t\t\t\tid\n\t\t\t\t},\n\t\t\t\terr\n\t\t\t);\n\t\t} finally {\n\t\t\tif (Is.stringValue(optimisticMutexKey)) {\n\t\t\t\tMutex.unlock(optimisticMutexKey);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Set multiple entities in a batch.\n\t * @param entities The entities to set.\n\t * @returns Nothing.\n\t */\n\tpublic async setBatch(entities: T[]): Promise<void> {\n\t\tGuards.arrayValue(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(entities), entities);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tconst preparedEntities = entities.map(entity =>\n\t\t\tEntityStorageHelper.prepareEntity(entity, this._entitySchema, undefined, {\n\t\t\t\tnullBehavior: \"omit\"\n\t\t\t})\n\t\t);\n\n\t\ttry {\n\t\t\tawait this._container.items.executeBulkOperations(\n\t\t\t\tpreparedEntities.map(\n\t\t\t\t\tprepared =>\n\t\t\t\t\t\t({\n\t\t\t\t\t\t\toperationType: BulkOperationType.Upsert,\n\t\t\t\t\t\t\tpartitionKey: partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE,\n\t\t\t\t\t\t\tresourceBody: {\n\t\t\t\t\t\t\t\tid: prepared[this._primaryKey.property] as string,\n\t\t\t\t\t\t\t\t[CosmosDbEntityStorageConnector._PARTITION_KEY]:\n\t\t\t\t\t\t\t\t\tpartitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE,\n\t\t\t\t\t\t\t\t...(prepared as { [key: string]: unknown })\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}) as OperationInput\n\t\t\t\t)\n\t\t\t);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"setBatchFailed\",\n\t\t\t\tundefined,\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Empty all entities from the storage.\n\t * @returns Nothing.\n\t */\n\tpublic async empty(): Promise<void> {\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\t\tconst pk = partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE;\n\n\t\ttry {\n\t\t\tlet continuationToken: string | undefined;\n\t\t\tdo {\n\t\t\t\tconst feedOptions: FeedOptions = { maxItemCount: 100, continuationToken };\n\t\t\t\tconst { resources, continuationToken: nextToken } = await this._container.items\n\t\t\t\t\t.query<{ id: string }>(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tquery: `SELECT c.id FROM c WHERE c.${CosmosDbEntityStorageConnector._PARTITION_KEY} = @pk`,\n\t\t\t\t\t\t\tparameters: [{ name: \"@pk\", value: pk }]\n\t\t\t\t\t\t},\n\t\t\t\t\t\tfeedOptions\n\t\t\t\t\t)\n\t\t\t\t\t.fetchNext();\n\n\t\t\t\tcontinuationToken = nextToken;\n\n\t\t\t\tif (Is.arrayValue(resources)) {\n\t\t\t\t\tconst operations: OperationInput[] = resources.map(r => ({\n\t\t\t\t\t\toperationType: BulkOperationType.Delete,\n\t\t\t\t\t\tid: r.id,\n\t\t\t\t\t\tpartitionKey: pk\n\t\t\t\t\t}));\n\t\t\t\t\tawait this._container.items.executeBulkOperations(operations);\n\t\t\t\t}\n\t\t\t} while (Is.stringValue(continuationToken));\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"emptyFailed\",\n\t\t\t\tundefined,\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Remove the entity.\n\t * @param id The id of the entity to remove.\n\t * @param conditions The optional conditions to match for the entities.\n\t * @returns Nothing.\n\t */\n\tpublic async remove(\n\t\tid: string,\n\t\tconditions?: { property: keyof T; value: unknown }[]\n\t): Promise<void> {\n\t\tGuards.stringValue(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(id), id);\n\t\tEntityStorageHelper.validateConditions(this._entitySchema, conditions);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\t\tconst optimisticMutexKey = Is.stringValue(this._versionKey)\n\t\t\t? this.buildOptimisticMutexKey(partitionKey, id)\n\t\t\t: undefined;\n\n\t\tif (Is.stringValue(optimisticMutexKey)) {\n\t\t\tawait Mutex.lock(optimisticMutexKey, {\n\t\t\t\tthrowOnTimeout: true,\n\t\t\t\ttimeoutMs: this._mutexTimeoutMs\n\t\t\t});\n\t\t}\n\n\t\ttry {\n\t\t\tconst item = this._container.item(\n\t\t\t\tid,\n\t\t\t\tpartitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t);\n\t\t\tconst { resource: itemData } = await item.read<ItemDefinition>();\n\t\t\tif (Is.notEmpty(itemData)) {\n\t\t\t\tif (Is.arrayValue(conditions) && !this.verifyConditions(conditions, itemData as T)) {\n\t\t\t\t\tif (Is.stringValue(this._versionKey)) {\n\t\t\t\t\t\tthrow new ConflictError(\n\t\t\t\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\t\t\"conditionFailed\",\n\t\t\t\t\t\t\tid\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tawait item.delete();\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tif (BaseError.isErrorName(err, ConflictError.CLASS_NAME)) {\n\t\t\t\tthrow err;\n\t\t\t}\n\t\t\tif (\n\t\t\t\tBaseError.fromError(err) &&\n\t\t\t\tIs.object<{ body?: { code?: string } }>(err) &&\n\t\t\t\terr.body?.code === \"NotFound\"\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"removeFailed\",\n\t\t\t\t{\n\t\t\t\t\tid\n\t\t\t\t},\n\t\t\t\terr\n\t\t\t);\n\t\t} finally {\n\t\t\tif (Is.stringValue(optimisticMutexKey)) {\n\t\t\t\tMutex.unlock(optimisticMutexKey);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Remove multiple entities by id.\n\t * @param ids The ids of the entities to remove.\n\t * @returns Nothing.\n\t */\n\tpublic async removeBatch(ids: string[]): Promise<void> {\n\t\tGuards.arrayValue(CosmosDbEntityStorageConnector.CLASS_NAME, nameof(ids), ids);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\ttry {\n\t\t\tconst operations: OperationInput[] = ids.map(id => ({\n\t\t\t\toperationType: BulkOperationType.Delete,\n\t\t\t\tid,\n\t\t\t\tpartitionKey: partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t}));\n\n\t\t\tawait this._container.items.executeBulkOperations(operations);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"removeBatchFailed\",\n\t\t\t\tundefined,\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Teardown the storage by deleting the underlying container.\n\t * @param nodeLoggingComponentType The node logging component type.\n\t * @returns True if the teardown process was successful.\n\t */\n\tpublic async teardown(nodeLoggingComponentType?: string): Promise<boolean> {\n\t\tconst nodeLogging = ComponentFactory.getIfExists<ILoggingComponent>(nodeLoggingComponentType);\n\n\t\tawait nodeLogging?.log({\n\t\t\tlevel: \"info\",\n\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\tts: Date.now(),\n\t\t\tmessage: \"containerDeleting\",\n\t\t\tdata: { containerId: this._config.containerId }\n\t\t});\n\n\t\ttry {\n\t\t\tif (await this.containerExists()) {\n\t\t\t\tawait this._container.delete();\n\t\t\t\tawait this.waitForContainerNotExists();\n\t\t\t}\n\n\t\t\tawait nodeLogging?.log({\n\t\t\t\tlevel: \"info\",\n\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"containerDeleted\",\n\t\t\t\tdata: { containerId: this._config.containerId }\n\t\t\t});\n\n\t\t\treturn true;\n\t\t} catch (err) {\n\t\t\tawait nodeLogging?.log({\n\t\t\t\tlevel: \"error\",\n\t\t\t\tsource: CosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"teardownFailed\",\n\t\t\t\terror: BaseError.fromError(err)\n\t\t\t});\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Find all the entities which match the conditions.\n\t * @param conditions The conditions to match for the entities.\n\t * @param sortProperties The optional sort order.\n\t * @param properties The optional properties to return, defaults to all.\n\t * @param cursor The cursor to request the next chunk of entities.\n\t * @param limit The suggested number of entities to return in each chunk, in some scenarios can return a different amount.\n\t * @returns All the entities for the storage matching the conditions,\n\t * and a cursor which can be used to request more entities.\n\t */\n\tpublic async query(\n\t\tconditions?: EntityCondition<T>,\n\t\tsortProperties?: { property: keyof T; sortDirection: SortDirection }[],\n\t\tproperties?: (keyof T)[],\n\t\tcursor?: string,\n\t\tlimit?: number\n\t): Promise<{ entities: Partial<T>[]; cursor?: string }> {\n\t\tlet sql = \"\";\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tEntityStorageHelper.validateSortProperties(this._entitySchema, sortProperties);\n\t\tEntityStorageHelper.validateProperties(this._entitySchema, properties);\n\t\tEntityStorageHelper.validateConditionProperties(this._entitySchema, conditions);\n\n\t\t// Only the sort shapes covered by the composite indexes from buildIndexingPolicy are accepted.\n\t\tif (Is.arrayValue(sortProperties)) {\n\t\t\tconst nonPrimarySorts = sortProperties.filter(\n\t\t\t\tsortProperty => sortProperty.property !== this._primaryKey.property\n\t\t\t);\n\t\t\tif (nonPrimarySorts.length > 1) {\n\t\t\t\tthrow new GeneralError(CosmosDbEntityStorageConnector.CLASS_NAME, \"sortUnsupported\", {\n\t\t\t\t\tproperties: sortProperties.map(sortProperty => sortProperty.property),\n\t\t\t\t\tprimaryKey: this._primaryKey.property\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif (!Is.empty(limit)) {\n\t\t\tconst validationFailures: IValidationFailure[] = [];\n\t\t\tValidation.integer(nameof(limit), limit, validationFailures, undefined, { minValue: 1 });\n\t\t\tValidation.asValidationError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"query\",\n\t\t\t\tvalidationFailures\n\t\t\t);\n\t\t}\n\n\t\ttry {\n\t\t\tconst returnSize = limit ?? CosmosDbEntityStorageConnector._DEFAULT_LIMIT;\n\n\t\t\tlet orderByClause: string = \"\";\n\t\t\tif (Is.arrayValue(sortProperties)) {\n\t\t\t\t// The primary key is unique so no property after its first occurrence can affect\n\t\t\t\t// the order, truncating keeps the ORDER BY within the provisioned composite indexes.\n\t\t\t\tconst primaryKeyIndex = sortProperties.findIndex(\n\t\t\t\t\tsortProperty => sortProperty.property === this._primaryKey.property\n\t\t\t\t);\n\t\t\t\tconst effectiveSorts =\n\t\t\t\t\tprimaryKeyIndex === -1 ? sortProperties : sortProperties.slice(0, primaryKeyIndex + 1);\n\t\t\t\tconst orderClauses = effectiveSorts.map(sortProperty => {\n\t\t\t\t\tconst direction = sortProperty.sortDirection === SortDirection.Ascending ? \"asc\" : \"desc\";\n\t\t\t\t\treturn `c.${String(sortProperty.property)} ${direction}`;\n\t\t\t\t});\n\t\t\t\torderByClause = `ORDER BY ${orderClauses.join(\", \")}`;\n\t\t\t}\n\n\t\t\tconst attributeNames: { [id: string]: string } = {};\n\t\t\tconst attributeValues: { [id: string]: unknown } = {};\n\t\t\tlet queryClause = this.buildQueryParameters(\"\", conditions, attributeNames, attributeValues);\n\n\t\t\tif (queryClause.length > 0) {\n\t\t\t\tqueryClause = ` AND ${queryClause}`;\n\t\t\t}\n\n\t\t\tconst selectClause = properties ? properties.map(p => `c.${p as string}`).join(\", \") : \"*\";\n\t\t\tconst baseQuery = `SELECT ${selectClause} FROM c WHERE c.${CosmosDbEntityStorageConnector._PARTITION_KEY} = @partitionId${queryClause}`;\n\t\t\tconst queryParameters: SqlParameter[] = [\n\t\t\t\t{\n\t\t\t\t\tname: \"@partitionId\",\n\t\t\t\t\tvalue: partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t\t},\n\t\t\t\t...Object.keys(attributeValues).map(\n\t\t\t\t\tkey => ({ name: `@${key}`, value: attributeValues[key] }) as SqlParameter\n\t\t\t\t)\n\t\t\t];\n\t\t\tconst queryPartitionKey = partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE;\n\n\t\t\t// For sorted queries use OFFSET LIMIT - CosmosDB continuation tokens are\n\t\t\t// not reliably returned for ORDER BY queries across all service versions.\n\t\t\t// The cursor is a numeric offset encoded as a string (same as SQL connectors).\n\t\t\tconst startIndex = Coerce.number(cursor) ?? 0;\n\t\t\tsql = `${baseQuery} ${orderByClause} OFFSET ${startIndex} LIMIT ${returnSize + 1}`;\n\t\t\tconst sortedQuerySpecs: SqlQuerySpec = { query: sql, parameters: queryParameters };\n\t\t\tconst sortedResponse = await this._container.items\n\t\t\t\t.query(sortedQuerySpecs, {\n\t\t\t\t\tpartitionKey: queryPartitionKey,\n\t\t\t\t\tmaxItemCount: returnSize + 1\n\t\t\t\t})\n\t\t\t\t.fetchNext();\n\t\t\tconst allItems = sortedResponse.resources;\n\t\t\tconst hasMore = allItems.length > returnSize;\n\t\t\treturn {\n\t\t\t\tentities: (hasMore ? allItems.slice(0, returnSize) : allItems).map(i =>\n\t\t\t\t\tthis.itemToEntity(i)\n\t\t\t\t),\n\t\t\t\tcursor: hasMore ? Coerce.string(startIndex + returnSize) : undefined\n\t\t\t};\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"queryFailed\",\n\t\t\t\t{ sql },\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Count all the entities which match the conditions.\n\t * @param conditions The optional conditions to match for the entities.\n\t * @returns The total count of entities in the storage.\n\t */\n\tpublic async count(conditions?: EntityCondition<T>): Promise<number> {\n\t\tEntityStorageHelper.validateConditionProperties(this._entitySchema, conditions);\n\t\ttry {\n\t\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\t\tconst partitionKey = ContextIdHelper.combinedContextKey(\n\t\t\t\tcontextIds,\n\t\t\t\tthis._partitionContextIds\n\t\t\t);\n\n\t\t\tconst attributeNames: { [id: string]: string } = {};\n\t\t\tconst attributeValues: { [id: string]: unknown } = {};\n\t\t\tlet queryClause = Is.empty(conditions)\n\t\t\t\t? \"\"\n\t\t\t\t: this.buildQueryParameters(\"\", conditions, attributeNames, attributeValues);\n\n\t\t\tif (queryClause.length > 0) {\n\t\t\t\tqueryClause = ` AND ${queryClause}`;\n\t\t\t}\n\n\t\t\tconst querySpec: SqlQuerySpec = {\n\t\t\t\tquery: `SELECT VALUE COUNT(1) FROM c WHERE c.${CosmosDbEntityStorageConnector._PARTITION_KEY} = @partitionId${queryClause}`,\n\t\t\t\tparameters: [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: \"@partitionId\",\n\t\t\t\t\t\tvalue: partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t\t\t},\n\t\t\t\t\t...Object.keys(attributeValues).map(\n\t\t\t\t\t\tkey => ({ name: `@${key}`, value: attributeValues[key] }) as SqlParameter\n\t\t\t\t\t)\n\t\t\t\t]\n\t\t\t};\n\t\t\tconst { resources } = await this._container.items.query(querySpec).fetchAll();\n\t\t\treturn resources[0] ?? 0;\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"countFailed\",\n\t\t\t\tundefined,\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Get a unique list of all the context ids from the storage.\n\t * @returns The list of unique context ids.\n\t */\n\tpublic async getPartitionContextIds(): Promise<IContextIds[] | undefined> {\n\t\tconst partitionContextIds = this._partitionContextIds;\n\t\tif (!Is.arrayValue(partitionContextIds)) {\n\t\t\treturn undefined;\n\t\t}\n\t\ttry {\n\t\t\tconst { resources: partitionIds } = await this._container.items\n\t\t\t\t.query<string>({\n\t\t\t\t\tquery: `SELECT DISTINCT VALUE c.${CosmosDbEntityStorageConnector._PARTITION_KEY} FROM c`\n\t\t\t\t})\n\t\t\t\t.fetchAll();\n\t\t\treturn partitionIds\n\t\t\t\t.filter(id => Is.stringValue(id))\n\t\t\t\t.map(id => ContextIdHelper.shortSplit(partitionContextIds, id));\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"getPartitionContextIdsFailed\",\n\t\t\t\tundefined,\n\t\t\t\terr\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Create the target connector for performing the migration using a temporary container.\n\t * @param newEntitySchema The name of the new entity schema to create the connector for.\n\t * @returns Connector for performing the migration.\n\t */\n\tpublic async createTargetConnector<U>(\n\t\tnewEntitySchema: string\n\t): Promise<IEntityStorageConnector<U>> {\n\t\tconst migrationContainerId = `${this._config.containerId}Migration${Date.now()}`;\n\t\treturn new CosmosDbEntityStorageConnector<U>({\n\t\t\tentitySchema: newEntitySchema,\n\t\t\tconfig: { ...this._config, containerId: migrationContainerId },\n\t\t\tpartitionContextIds: this._partitionContextIds\n\t\t});\n\t}\n\n\t/**\n\t * Finalize the migration by tearing down the old container and replacing it with the target container.\n\t * @param targetConnector The target connector to finalize the migration with.\n\t * @param options The options to control how the migration is finalized.\n\t * @param loggingComponentType The optional component type to use for logging.\n\t * @returns The final connector pointing at the original container id.\n\t */\n\tpublic async finalizeMigration<U>(\n\t\ttargetConnector: CosmosDbEntityStorageConnector<U>,\n\t\toptions?: IMigrationOptions,\n\t\tloggingComponentType?: string\n\t): Promise<CosmosDbEntityStorageConnector<U>> {\n\t\t// There is no rename operation in DynamoDB so we have to create a new table with the original name and copy the data over\n\n\t\t// Teardown the existing table with the original name to free up the name for the new table\n\t\tawait this.teardown(loggingComponentType);\n\n\t\t// Create a new connector with the original table name but with the new schema\n\t\t// and copy the data from the migration table to the new table using batch operations\n\t\tconst finalConnector = new CosmosDbEntityStorageConnector<U>({\n\t\t\tentitySchema: targetConnector._entitySchemaName,\n\t\t\tconfig: this._config,\n\t\t\tpartitionContextIds: this._partitionContextIds\n\t\t});\n\t\tif (await finalConnector.bootstrap(loggingComponentType)) {\n\t\t\t// Since there is no rename, we need to copy the data from the migration table to the new table\n\t\t\tconst partitions = await targetConnector.getPartitionContextIds();\n\t\t\tconst batchSize = options?.batchSize ?? CosmosDbEntityStorageConnector._DEFAULT_LIMIT;\n\t\t\tawait this.bulkCopy(targetConnector, finalConnector, partitions, batchSize);\n\n\t\t\tawait targetConnector.teardown(loggingComponentType);\n\n\t\t\treturn finalConnector;\n\t\t}\n\n\t\tthrow new GeneralError(\n\t\t\tCosmosDbEntityStorageConnector.CLASS_NAME,\n\t\t\t\"finalizeMigrationFailedBootstrap\"\n\t\t);\n\t}\n\n\t/**\n\t * Cleanup the migration if a migration fails or needs to be aborted.\n\t * @param targetConnector The target connector to cleanup.\n\t * @param options The options to control how the migration is cleaned up.\n\t * @param loggingComponentType The optional component type to use for logging.\n\t */\n\tpublic async cleanupMigration<U>(\n\t\ttargetConnector: IEntityStorageConnector<U> | undefined,\n\t\toptions?: IMigrationOptions,\n\t\tloggingComponentType?: string\n\t): Promise<void> {\n\t\t// If something failed the only thing to cleanup is the migration table\n\t\tawait targetConnector?.teardown?.(loggingComponentType);\n\t}\n\n\t/**\n\t * Copy all entities from sourceConnector to destConnector, paging through each partition.\n\t * @param sourceConnector The connector to read entities from.\n\t * @param destConnector The connector to write entities to.\n\t * @param partitions The partition list returned by getPartitionContextIds.\n\t * @param batchSize The number of entities to read per page.\n\t * @internal\n\t */\n\tprivate async bulkCopy<U>(\n\t\tsourceConnector: CosmosDbEntityStorageConnector<U>,\n\t\tdestConnector: CosmosDbEntityStorageConnector<U>,\n\t\tpartitions: IContextIds[] | undefined,\n\t\tbatchSize: number\n\t): Promise<void> {\n\t\t// undefined → not partitioned: one pass with no partition key.\n\t\t// [] → partitioned but empty: nothing to copy, return early.\n\t\t// [{…}, …] → partitioned with data: iterate over each partition.\n\t\tif (partitions?.length === 0) {\n\t\t\treturn;\n\t\t}\n\t\tconst partitionList = partitions ?? [{}];\n\n\t\tfor (let i = 0; i < partitionList.length; i++) {\n\t\t\t// Values from getPartitionContextIds are already short-form, so we join them\n\t\t\t// directly rather than using combinedContextKey, which expects long-form input\n\t\t\t// and calls guardAll (throwing if a registered handler rejects short-form values).\n\t\t\tconst partitionKey = Is.arrayValue(sourceConnector._partitionContextIds)\n\t\t\t\t? sourceConnector._partitionContextIds.map(k => partitionList[i][k]).join(\"/\")\n\t\t\t\t: CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE;\n\n\t\t\tlet continuationToken: string | undefined;\n\t\t\tdo {\n\t\t\t\tconst feedOptions: FeedOptions = { maxItemCount: batchSize, continuationToken };\n\t\t\t\tconst { resources, continuationToken: nextToken } = await sourceConnector._container.items\n\t\t\t\t\t.query<ItemDefinition & Resource>(\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tquery: `SELECT * FROM c WHERE c.${CosmosDbEntityStorageConnector._PARTITION_KEY} = @partitionId`,\n\t\t\t\t\t\t\tparameters: [{ name: \"@partitionId\", value: partitionKey }]\n\t\t\t\t\t\t},\n\t\t\t\t\t\tfeedOptions\n\t\t\t\t\t)\n\t\t\t\t\t.fetchNext();\n\n\t\t\t\tcontinuationToken = nextToken;\n\n\t\t\t\tif (Is.arrayValue(resources)) {\n\t\t\t\t\tconst operations: OperationInput[] = resources.map(\n\t\t\t\t\t\t({\n\t\t\t\t\t\t\t_rid: rid,\n\t\t\t\t\t\t\t_self: self,\n\t\t\t\t\t\t\t_ts: ts,\n\t\t\t\t\t\t\t_etag: etag,\n\t\t\t\t\t\t\t_attachments: attachments,\n\t\t\t\t\t\t\t...rest\n\t\t\t\t\t\t}) => ({\n\t\t\t\t\t\t\toperationType: BulkOperationType.Upsert,\n\t\t\t\t\t\t\tpartitionKey,\n\t\t\t\t\t\t\tresourceBody: rest\n\t\t\t\t\t\t})\n\t\t\t\t\t);\n\t\t\t\t\tawait destConnector._container.items.executeBulkOperations(operations);\n\t\t\t\t}\n\t\t\t} while (Is.stringValue(continuationToken));\n\t\t}\n\t}\n\n\t/**\n\t * Create an SQL condition clause.\n\t * @param objectPath The path for the nested object.\n\t * @param condition The conditions to create the query from.\n\t * @param attributeNames The attribute names to use in the query.\n\t * @param attributeValues The attribute values to use in the query.\n\t * @returns The condition clause.\n\t * @internal\n\t */\n\tprivate buildQueryParameters(\n\t\tobjectPath: string,\n\t\tcondition: EntityCondition<T> | undefined,\n\t\tattributeNames: { [id: string]: string },\n\t\tattributeValues: { [id: string]: unknown }\n\t): string {\n\t\t// If no conditions are defined then return empty string\n\t\tif (Is.undefined(condition)) {\n\t\t\treturn \"\";\n\t\t}\n\n\t\tif (\"conditions\" in condition) {\n\t\t\tif (condition.conditions.length === 0) {\n\t\t\t\treturn \"\";\n\t\t\t}\n\t\t\t// It's a group of comparisons, so check the individual items and combine with the logical operator\n\t\t\tconst joinConditions: string[] = condition.conditions.map(c =>\n\t\t\t\tthis.buildQueryParameters(objectPath, c, attributeNames, attributeValues)\n\t\t\t);\n\n\t\t\tconst logicalOperator = this.mapConditionalOperator(condition.logicalOperator);\n\t\t\tconst queryClause = joinConditions\n\t\t\t\t.filter(j => j.length > 0)\n\t\t\t\t.map(j => j)\n\t\t\t\t.join(` ${logicalOperator} `);\n\n\t\t\treturn Is.stringValue(queryClause) ? ` (${queryClause}) ` : \"\";\n\t\t}\n\n\t\tconst schemaProp = this._entitySchema.properties?.find(p => p.property === condition.property);\n\n\t\t// It's a single value so just create the property comparison for the condition\n\t\tconst comparison = this.mapComparisonOperator(\n\t\t\tobjectPath,\n\t\t\tcondition,\n\t\t\tschemaProp?.type,\n\t\t\tattributeNames,\n\t\t\tattributeValues\n\t\t);\n\n\t\treturn comparison;\n\t}\n\n\t/**\n\t * Map the framework comparison operators to those in CosmosDB.\n\t * @param objectPath The prefix to use for the condition.\n\t * @param comparator The operator to map.\n\t * @param type The type of the property.\n\t * @param attributeNames The attribute names to use in the query.\n\t * @returns The comparison expression.\n\t * @throws GeneralError if the comparison operator is not supported.\n\t * @internal\n\t */\n\tprivate mapComparisonOperator(\n\t\tobjectPath: string,\n\t\tcomparator: IComparator,\n\t\ttype: EntitySchemaPropertyType | undefined,\n\t\tattributeNames: { [id: string]: string },\n\t\tattributeValues: { [id: string]: unknown }\n\t): string {\n\t\tlet prop = objectPath;\n\t\tif (prop.length > 0) {\n\t\t\tprop += \".\";\n\t\t}\n\t\tprop += comparator.property;\n\n\t\tlet attributeName = this.populateAttributeNames(prop, attributeNames);\n\t\tlet propName = `${attributeName.replace(/\\./g, \"\").replace(/@/g, \"\")}`;\n\n\t\tif (\n\t\t\t(comparator.comparison === ComparisonOperator.Equals ||\n\t\t\t\tcomparator.comparison === ComparisonOperator.NotEquals) &&\n\t\t\t(comparator.value === null || comparator.value === undefined)\n\t\t) {\n\t\t\t// Cosmos DB SQL null semantics mirror standard SQL: any comparison using = or <>\n\t\t\t// against null evaluates to UNKNOWN, not TRUE, so no rows are returned.\n\t\t\t// IS_NULL() and IS_DEFINED() must be used instead (no bound parameter needed).\n\t\t\tif (comparator.comparison === ComparisonOperator.Equals) {\n\t\t\t\treturn `(IS_NULL(c.${attributeName}) OR NOT IS_DEFINED(c.${attributeName}))`;\n\t\t\t}\n\t\t\treturn `(IS_DEFINED(c.${attributeName}) AND NOT IS_NULL(c.${attributeName}))`;\n\t\t} else if (Is.array(comparator.value)) {\n\t\t\tconst dbValues = comparator.value.map(v => this.propertyToDbValue(v, type));\n\t\t\tif (dbValues.length === 0 && comparator.comparison === ComparisonOperator.In) {\n\t\t\t\t// CosmosDB rejects `IN ()` - return always-false sentinel (#141).\n\t\t\t\treturn \"1=0\";\n\t\t\t}\n\t\t\tconst arrAttributeNames = [];\n\t\t\tfor (let i = 0; i < dbValues.length; i++) {\n\t\t\t\tconst arrAttributeName = `${propName}${i}`;\n\t\t\t\tattributeValues[arrAttributeName] = dbValues[i];\n\t\t\t\tarrAttributeNames.push(arrAttributeName);\n\t\t\t}\n\t\t\tpropName = attributeName;\n\t\t\tattributeName = `(${arrAttributeNames.map(name => `@${name}`).join(\", \")})`;\n\t\t} else if (\n\t\t\tIs.object(comparator.value) &&\n\t\t\t(comparator.comparison === ComparisonOperator.Equals ||\n\t\t\t\tcomparator.comparison === ComparisonOperator.NotEquals)\n\t\t) {\n\t\t\t// CosmosDB SQL does not support object equality with =; expand to per-property comparisons.\n\t\t\tconst op = comparator.comparison === ComparisonOperator.Equals ? \"=\" : \"<>\";\n\t\t\tconst join = comparator.comparison === ComparisonOperator.Equals ? \" AND \" : \" OR \";\n\t\t\tconst clauses: string[] = [];\n\t\t\tfor (const [key, val] of Object.entries(comparator.value)) {\n\t\t\t\tconst paramName = `${propName}${key}`;\n\t\t\t\tattributeValues[paramName] = val;\n\t\t\t\tclauses.push(`c.${attributeName}.${key} ${op} @${paramName}`);\n\t\t\t}\n\t\t\treturn clauses.length === 1 ? clauses[0] : `(${clauses.join(join)})`;\n\t\t} else {\n\t\t\t// Avoid parameter name conflicts by ensuring unique parameter names in the query\n\t\t\tlet counter = 1;\n\t\t\twhile (propName in attributeValues) {\n\t\t\t\tpropName = `${attributeName.replace(/\\./g, \"\").replace(/@/g, \"\")}${counter}`;\n\t\t\t\tcounter++;\n\t\t\t}\n\t\t\tattributeValues[propName] = comparator.value;\n\t\t}\n\n\t\tconst matches = attributeName.split(\".\").length;\n\t\tif (matches && comparator.comparison === ComparisonOperator.Equals) {\n\t\t\tattributeName = attributeName\n\t\t\t\t.split(\".\")\n\t\t\t\t.map(part => `[\"${part}\"]`)\n\t\t\t\t.join(\"\");\n\t\t\treturn `c${attributeName} = @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.Equals) {\n\t\t\treturn `c.${attributeName} = @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.NotEquals) {\n\t\t\treturn `c.${attributeName} <> @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.GreaterThan) {\n\t\t\treturn `c.${attributeName} > @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.LessThan) {\n\t\t\treturn `c.${attributeName} < @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.GreaterThanOrEqual) {\n\t\t\treturn `c.${attributeName} >= @${propName}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.LessThanOrEqual) {\n\t\t\treturn `c.${attributeName} <= @${propName}`;\n\t\t} else if (\n\t\t\tIs.object(attributeValues[propName]) &&\n\t\t\tcomparator.comparison === ComparisonOperator.Includes\n\t\t) {\n\t\t\treturn `ARRAY_CONTAINS(c.${attributeName}, @${propName}, true)`;\n\t\t} else if (\n\t\t\t(type === \"array\" || type === \"object\") &&\n\t\t\tcomparator.comparison === ComparisonOperator.Includes\n\t\t) {\n\t\t\treturn `ARRAY_CONTAINS(c.${attributeName}, @${propName})`;\n\t\t} else if (\n\t\t\t(type === \"array\" || type === \"object\") &&\n\t\t\tcomparator.comparison === ComparisonOperator.NotIncludes\n\t\t) {\n\t\t\treturn `NOT ARRAY_CONTAINS(c.${attributeName}, @${propName})`;\n\t\t} else if (comparator.comparison === ComparisonOperator.Includes) {\n\t\t\treturn `CONTAINS(c.${attributeName}, @${propName})`;\n\t\t} else if (comparator.comparison === ComparisonOperator.NotIncludes) {\n\t\t\treturn `NOT CONTAINS(c.${attributeName}, @${propName})`;\n\t\t} else if (comparator.comparison === ComparisonOperator.In) {\n\t\t\treturn `c.${propName} IN ${attributeName}`;\n\t\t}\n\n\t\tthrow new GeneralError(CosmosDbEntityStorageConnector.CLASS_NAME, \"comparisonNotSupported\", {\n\t\t\tcomparison: comparator.comparison\n\t\t});\n\t}\n\n\t/**\n\t * Format a value to insert into DB.\n\t * @param value The value to format.\n\t * @param type The type for the property.\n\t * @returns The value after conversion.\n\t * @internal\n\t */\n\tprivate propertyToDbValue(value: unknown, type?: EntitySchemaPropertyType): unknown {\n\t\tif (Is.object(value)) {\n\t\t\tconst map: { [id: string]: unknown } = {};\n\t\t\tfor (const key in value) {\n\t\t\t\tmap[key] = this.propertyToDbValue(value[key]);\n\t\t\t}\n\t\t\treturn map;\n\t\t}\n\n\t\tif (type === \"string\") {\n\t\t\treturn `${Coerce.string(value)}`;\n\t\t} else if (type === \"integer\" || type === \"number\") {\n\t\t\treturn Coerce.number(value) ?? 0;\n\t\t} else if (type === \"boolean\") {\n\t\t\treturn Coerce.boolean(value) ?? false;\n\t\t}\n\n\t\treturn Coerce.string(value) ?? \"\";\n\t}\n\n\t/**\n\t * Create a unique name for the attribute.\n\t * @param name The name to create a unique name for.\n\t * @param attributeNames The attribute names to use in the query.\n\t * @returns The unique name.\n\t * @internal\n\t */\n\tprivate populateAttributeNames(name: string, attributeNames: { [id: string]: string }): string {\n\t\tconst parts = name.split(\".\");\n\t\tconst attributeNameParts: string[] = [];\n\n\t\tfor (const part of parts) {\n\t\t\tconst hashPart = `${part}`;\n\t\t\tif (Is.empty(attributeNames[hashPart])) {\n\t\t\t\tattributeNames[hashPart] = part;\n\t\t\t}\n\t\t\tattributeNameParts.push(hashPart);\n\t\t}\n\n\t\treturn attributeNameParts.join(\".\");\n\t}\n\n\t/**\n\t * Map the framework conditional operators to those in CosmosDB.\n\t * @param operator The operator to map.\n\t * @returns The conditional operator.\n\t * @throws GeneralError if the conditional operator is not supported.\n\t * @internal\n\t */\n\tprivate mapConditionalOperator(operator?: LogicalOperator): string {\n\t\tif ((operator ?? LogicalOperator.And) === LogicalOperator.And) {\n\t\t\treturn \"AND\";\n\t\t} else if (operator === LogicalOperator.Or) {\n\t\t\treturn \"OR\";\n\t\t}\n\n\t\tthrow new GeneralError(CosmosDbEntityStorageConnector.CLASS_NAME, \"conditionalNotSupported\", {\n\t\t\toperator\n\t\t});\n\t}\n\n\t/**\n\t * Verify the conditions for the entity.\n\t * @param conditions The conditions to verify.\n\t * @param obj The object to verify the conditions against.\n\t * @returns True if all conditions are met, false otherwise.\n\t * @internal\n\t */\n\tprivate verifyConditions(\n\t\tconditions: { property: keyof T; value: unknown }[],\n\t\tobj: { [key in keyof T]: unknown }\n\t): boolean {\n\t\treturn conditions.every(\n\t\t\tcondition => ObjectHelper.propertyGet(obj, condition.property as string) === condition.value\n\t\t);\n\t}\n\n\t/**\n\t * Build a mutex key for optimistic-locking critical sections.\n\t * @param partitionKey The resolved partition key.\n\t * @param id The entity id.\n\t * @returns The mutex key.\n\t * @internal\n\t */\n\tprivate buildOptimisticMutexKey(partitionKey: string | undefined, id: string): string {\n\t\treturn `${CosmosDbEntityStorageConnector.CLASS_NAME}:optimistic:${this._config.databaseId}:${this._config.containerId}:${partitionKey ?? CosmosDbEntityStorageConnector._PARTITION_KEY_VALUE}:${id}`;\n\t}\n\n\t/**\n\t * Convert an entity to an item.\n\t * @param item The item to convert.\n\t * @returns The entity.\n\t * @internal\n\t */\n\tprivate itemToEntity(item: (ItemDefinition & Resource) | undefined): T {\n\t\treturn EntityStorageHelper.unPrepareEntity<T>(item as T, [\n\t\t\tCosmosDbEntityStorageConnector._PARTITION_KEY,\n\t\t\t\"_attachments\",\n\t\t\t\"_etag\",\n\t\t\t\"_rid\",\n\t\t\t\"_self\",\n\t\t\t\"_ts\"\n\t\t]);\n\t}\n\n\t/**\n\t * Check if the database exists.\n\t * @returns True if the database exists, false otherwise.\n\t * @internal\n\t */\n\tprivate async databaseExists(): Promise<boolean> {\n\t\ttry {\n\t\t\tconst { resources: databaseList } = await this._client.databases.readAll().fetchAll();\n\n\t\t\treturn databaseList.some((db: Resource) => db.id === this._config.databaseId);\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Wait for a database to exist.\n\t * @returns Nothing.\n\t * @internal\n\t */\n\tprivate async waitForDatabaseExists(): Promise<void> {\n\t\tfor (let attempt = 0; attempt < 20; attempt++) {\n\t\t\tconst databaseExists = await this.databaseExists();\n\t\t\tif (databaseExists) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tawait new Promise(resolve => setTimeout(resolve, 250));\n\t\t}\n\t}\n\n\t/**\n\t * Build the composite indexes needed to serve multi-property ORDER BY queries.\n\t * Pairing each sortable property with the primary key in both directions covers all\n\t * four direction combinations, as Cosmos DB also serves each index reversed.\n\t * @returns The indexing policy for the container, or undefined if the schema has no\n\t * sortable properties.\n\t * @internal\n\t */\n\tprivate buildIndexingPolicy(): IndexingPolicy | undefined {\n\t\tconst primaryKeyPath = `/${this._primaryKey.property as string}`;\n\t\tconst compositeIndexes: CompositePath[][] = [];\n\n\t\tif (Is.arrayValue(this._entitySchema.properties)) {\n\t\t\tfor (const prop of this._entitySchema.properties) {\n\t\t\t\tif (!prop.isPrimary && (Is.stringValue(prop.sortDirection) || prop.isSecondary)) {\n\t\t\t\t\tconst propertyPath = `/${prop.property as string}`;\n\t\t\t\t\tcompositeIndexes.push([\n\t\t\t\t\t\t{ path: propertyPath, order: \"ascending\" },\n\t\t\t\t\t\t{ path: primaryKeyPath, order: \"ascending\" }\n\t\t\t\t\t]);\n\t\t\t\t\tcompositeIndexes.push([\n\t\t\t\t\t\t{ path: propertyPath, order: \"ascending\" },\n\t\t\t\t\t\t{ path: primaryKeyPath, order: \"descending\" }\n\t\t\t\t\t]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn compositeIndexes.length > 0\n\t\t\t? {\n\t\t\t\t\tindexingMode: \"consistent\",\n\t\t\t\t\tautomatic: true,\n\t\t\t\t\tincludedPaths: [{ path: \"/*\" }],\n\t\t\t\t\tcompositeIndexes\n\t\t\t\t}\n\t\t\t: undefined;\n\t}\n\n\t/**\n\t * Check if the container exists.\n\t * @returns True if the container exists, false otherwise.\n\t * @internal\n\t */\n\tprivate async containerExists(): Promise<boolean> {\n\t\ttry {\n\t\t\tconst { resources: containers } = await this._client\n\t\t\t\t.database(this._config.databaseId)\n\t\t\t\t.containers.readAll()\n\t\t\t\t.fetchAll();\n\n\t\t\treturn containers.some((c: Resource) => c.id === this._config.containerId);\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Wait for a container to exist.\n\t * @returns Nothing.\n\t * @internal\n\t */\n\tprivate async waitForContainerExists(): Promise<void> {\n\t\tfor (let attempt = 0; attempt < 20; attempt++) {\n\t\t\tconst containerExists = await this.containerExists();\n\t\t\tif (containerExists) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tawait new Promise(resolve => setTimeout(resolve, 250));\n\t\t}\n\t}\n\n\t/**\n\t * Wait for a container to not exist.\n\t * @returns Nothing.\n\t * @internal\n\t */\n\tprivate async waitForContainerNotExists(): Promise<void> {\n\t\tfor (let attempt = 0; attempt < 20; attempt++) {\n\t\t\tconst containerExists = await this.containerExists();\n\t\t\tif (!containerExists) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tawait new Promise(resolve => setTimeout(resolve, 250));\n\t\t}\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"ICosmosDbEntityStorageConnectorConfig.js","sourceRoot":"","sources":["../../../src/models/ICosmosDbEntityStorageConnectorConfig.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Configuration for the Cosmos DB Entity Storage Connector.\n */\nexport interface ICosmosDbEntityStorageConnectorConfig {\n\t/**\n\t * The endpoint for the Cosmos DB instance.\n\t */\n\tendpoint: string;\n\n\t/**\n\t * The primary key for the Cosmos DB instance.\n\t */\n\tkey: string;\n\n\t/**\n\t * The ID of the database to be used.\n\t */\n\tdatabaseId: string;\n\n\t/**\n\t * The ID of the container for the storage.\n\t */\n\tcontainerId: string;\n\n\t/**\n\t * The offer throughput for the container.\n\t */\n\tofferThroughput?: number;\n\n\t/**\n\t * Disable endpoint discovery so the SDK always uses the configured endpoint.\n\t * Required when using the CosmosDB emulator behind a port-mapped Docker container,\n\t * because the emulator's account response advertises its internal container port\n\t * instead of the mapped host port.\n\t */\n\tdisableEndpointDiscovery?: boolean;\n}\n"]}
1
+ {"version":3,"file":"ICosmosDbEntityStorageConnectorConfig.js","sourceRoot":"","sources":["../../../src/models/ICosmosDbEntityStorageConnectorConfig.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n\n/**\n * Configuration for the Cosmos DB Entity Storage Connector.\n */\nexport interface ICosmosDbEntityStorageConnectorConfig {\n\t/**\n\t * The endpoint for the Cosmos DB instance.\n\t */\n\tendpoint: string;\n\n\t/**\n\t * The primary key for the Cosmos DB instance.\n\t */\n\tkey: string;\n\n\t/**\n\t * The ID of the database to be used.\n\t */\n\tdatabaseId: string;\n\n\t/**\n\t * The ID of the container for the storage.\n\t */\n\tcontainerId: string;\n\n\t/**\n\t * The offer throughput for the container.\n\t */\n\tofferThroughput?: number;\n\n\t/**\n\t * Disable endpoint discovery so the SDK always uses the configured endpoint.\n\t * Required when using the CosmosDB emulator behind a port-mapped Docker container,\n\t * because the emulator's account response advertises its internal container port\n\t * instead of the mapped host port.\n\t */\n\tdisableEndpointDiscovery?: boolean;\n\n\t/**\n\t * Milliseconds to wait for connector mutex locks before throwing.\n\t */\n\tmutexTimeoutMs?: number;\n}\n"]}
@@ -29,4 +29,8 @@ export interface ICosmosDbEntityStorageConnectorConfig {
29
29
  * instead of the mapped host port.
30
30
  */
31
31
  disableEndpointDiscovery?: boolean;
32
+ /**
33
+ * Milliseconds to wait for connector mutex locks before throwing.
34
+ */
35
+ mutexTimeoutMs?: number;
32
36
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.9.2-next.7](https://github.com/iotaledger/twin-entity-storage/compare/entity-storage-connector-cosmosdb-v0.9.2-next.6...entity-storage-connector-cosmosdb-v0.9.2-next.7) (2026-08-11)
4
+
5
+
6
+ ### Features
7
+
8
+ * optimistic locking ([#201](https://github.com/iotaledger/twin-entity-storage/issues/201)) ([80cbe3b](https://github.com/iotaledger/twin-entity-storage/commit/80cbe3b611c47b16328bceab02cfb8423fe1bbcd))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/entity-storage-models bumped from 0.9.2-next.6 to 0.9.2-next.7
16
+ * devDependencies
17
+ * @twin.org/entity-storage-connector-memory bumped from 0.9.2-next.6 to 0.9.2-next.7
18
+
3
19
  ## [0.9.2-next.6](https://github.com/iotaledger/twin-entity-storage/compare/entity-storage-connector-cosmosdb-v0.9.2-next.5...entity-storage-connector-cosmosdb-v0.9.2-next.6) (2026-08-07)
4
20
 
5
21
 
@@ -52,3 +52,11 @@ Disable endpoint discovery so the SDK always uses the configured endpoint.
52
52
  Required when using the CosmosDB emulator behind a port-mapped Docker container,
53
53
  because the emulator's account response advertises its internal container port
54
54
  instead of the mapped host port.
55
+
56
+ ***
57
+
58
+ ### mutexTimeoutMs? {#mutextimeoutms}
59
+
60
+ > `optional` **mutexTimeoutMs?**: `number`
61
+
62
+ Milliseconds to wait for connector mutex locks before throwing.
package/locales/en.json CHANGED
@@ -11,6 +11,8 @@
11
11
  },
12
12
  "error": {
13
13
  "cosmosDbEntityStorageConnector": {
14
+ "optimisticLockFailed": "The entity \"{conflictId}\" could not be written because the optimistic lock check failed",
15
+ "conditionFailed": "The entity \"{conflictId}\" could not be updated because the supplied conditions did not match the stored state",
14
16
  "setFailed": "Unable to set entity \"{id}\"",
15
17
  "getFailed": "Unable to get entity \"{id}\"",
16
18
  "removeFailed": "Unable to remove entity \"{id}\"",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/entity-storage-connector-cosmosdb",
3
- "version": "0.9.2-next.6",
3
+ "version": "0.9.2-next.7",
4
4
  "description": "Azure Cosmos DB connector for globally distributed persistence.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,7 +19,7 @@
19
19
  "@twin.org/context": "next",
20
20
  "@twin.org/core": "next",
21
21
  "@twin.org/entity": "next",
22
- "@twin.org/entity-storage-models": "0.9.2-next.6",
22
+ "@twin.org/entity-storage-models": "0.9.2-next.7",
23
23
  "@twin.org/logging-models": "next",
24
24
  "@twin.org/nameof": "next"
25
25
  },