@twin.org/entity-storage-connector-postgresql 0.0.3-next.13 → 0.0.3-next.14

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,6 +3,7 @@
3
3
  import { ContextIdHelper, ContextIdStore } from "@twin.org/context";
4
4
  import { BaseError, Coerce, ComponentFactory, GeneralError, Guards, HealthStatus, Is, ObjectHelper } from "@twin.org/core";
5
5
  import { ComparisonOperator, EntitySchemaFactory, EntitySchemaHelper, EntitySchemaPropertyType, LogicalOperator, SortDirection } from "@twin.org/entity";
6
+ import { EntityHelper } from "@twin.org/entity-storage-models";
6
7
  import postgres from "postgres";
7
8
  /**
8
9
  * Class for performing entity storage operations using ql.
@@ -27,6 +28,11 @@ export class PostgreSqlEntityStorageConnector {
27
28
  * @internal
28
29
  */
29
30
  static _PARTITION_KEY_VALUE = "root";
31
+ /**
32
+ * The name for the schema.
33
+ * @internal
34
+ */
35
+ _entitySchemaName;
30
36
  /**
31
37
  * The schema for the entity.
32
38
  * @internal
@@ -65,6 +71,7 @@ export class PostgreSqlEntityStorageConnector {
65
71
  Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.password", options.config.password);
66
72
  Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.database", options.config.database);
67
73
  Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.tableName", options.config.tableName);
74
+ this._entitySchemaName = options.entitySchema;
68
75
  this._entitySchema = EntitySchemaFactory.get(options.entitySchema);
69
76
  this._partitionContextIds = options.partitionContextIds;
70
77
  this._primaryKeyProperty = EntitySchemaHelper.getPrimaryKey(this._entitySchema);
@@ -258,9 +265,9 @@ export class PostgreSqlEntityStorageConnector {
258
265
  }
259
266
  }
260
267
  }
261
- const entity = ObjectHelper.removeEmptyProperties(rows[0], { removeNull: true });
262
- ObjectHelper.propertyDelete(entity, PostgreSqlEntityStorageConnector._PARTITION_KEY);
263
- return entity;
268
+ return EntityHelper.unPrepareEntity(rows[0], [
269
+ PostgreSqlEntityStorageConnector._PARTITION_KEY
270
+ ]);
264
271
  }
265
272
  }
266
273
  catch (err) {
@@ -280,8 +287,13 @@ export class PostgreSqlEntityStorageConnector {
280
287
  Guards.object(PostgreSqlEntityStorageConnector.CLASS_NAME, "entity", entity);
281
288
  const contextIds = await ContextIdStore.getContextIds();
282
289
  const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
283
- EntitySchemaHelper.validateEntity(entity, this.getSchema());
284
- const id = entity[this._primaryKeyProperty.property];
290
+ const prepared = EntityHelper.prepareEntity(entity, this._entitySchema, [
291
+ {
292
+ property: PostgreSqlEntityStorageConnector._PARTITION_KEY,
293
+ value: partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE
294
+ }
295
+ ]);
296
+ const id = prepared[this._primaryKeyProperty.property];
285
297
  try {
286
298
  if (Is.arrayValue(conditions)) {
287
299
  const itemData = await this.get(id);
@@ -289,25 +301,17 @@ export class PostgreSqlEntityStorageConnector {
289
301
  return;
290
302
  }
291
303
  }
292
- const finalEntity = ObjectHelper.clone(entity);
293
304
  const props = [...(this._entitySchema.properties ?? [])];
294
305
  props.unshift({
295
306
  property: PostgreSqlEntityStorageConnector._PARTITION_KEY,
296
307
  type: EntitySchemaPropertyType.String
297
308
  });
298
- ObjectHelper.propertySet(finalEntity, PostgreSqlEntityStorageConnector._PARTITION_KEY, partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE);
299
309
  const keys = [];
300
310
  const values = [];
301
311
  for (const prop of props) {
302
- if (!(Is.empty(finalEntity[prop.property]) && (prop.optional ?? false))) {
303
- keys.push(prop.property);
304
- if (finalEntity[prop.property] === undefined) {
305
- values.push(null);
306
- }
307
- else {
308
- values.push(finalEntity[prop.property]);
309
- }
310
- }
312
+ keys.push(prop.property);
313
+ const val = prepared[prop.property];
314
+ values.push(val ?? null);
311
315
  }
312
316
  let sql = `INSERT INTO "${this._config.tableName}"`;
313
317
  sql += ` (${keys.map(key => `"${key}"`).join(", ")})`;
@@ -332,9 +336,12 @@ export class PostgreSqlEntityStorageConnector {
332
336
  Guards.arrayValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "entities", entities);
333
337
  const contextIds = await ContextIdStore.getContextIds();
334
338
  const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
335
- for (const entity of entities) {
336
- EntitySchemaHelper.validateEntity(entity, this.getSchema());
337
- }
339
+ const preparedEntities = entities.map(entity => EntityHelper.prepareEntity(entity, this._entitySchema, [
340
+ {
341
+ property: PostgreSqlEntityStorageConnector._PARTITION_KEY,
342
+ value: partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE
343
+ }
344
+ ]));
338
345
  try {
339
346
  const props = [...(this._entitySchema.properties ?? [])];
340
347
  props.unshift({
@@ -344,12 +351,10 @@ export class PostgreSqlEntityStorageConnector {
344
351
  const keys = props.map(p => p.property);
345
352
  const allValues = [];
346
353
  const rowPlaceholders = [];
347
- for (const entity of entities) {
348
- const finalEntity = ObjectHelper.clone(entity);
349
- ObjectHelper.propertySet(finalEntity, PostgreSqlEntityStorageConnector._PARTITION_KEY, partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE);
354
+ for (const prepared of preparedEntities) {
350
355
  const rowValues = [];
351
356
  for (const prop of props) {
352
- const val = finalEntity[prop.property];
357
+ const val = prepared[prop.property];
353
358
  allValues.push(Is.empty(val) ? null : val);
354
359
  rowValues.push(`$${allValues.length}`);
355
360
  }
@@ -483,6 +488,74 @@ export class PostgreSqlEntityStorageConnector {
483
488
  return false;
484
489
  }
485
490
  }
491
+ /**
492
+ * Get all the distinct partition context ids from the storage.
493
+ * @returns An array of context id objects, one per unique partition.
494
+ */
495
+ async getPartitionContextIds() {
496
+ if (!Is.arrayValue(this._partitionContextIds)) {
497
+ return [];
498
+ }
499
+ try {
500
+ const dbConnection = await this.createConnection();
501
+ const rows = await dbConnection.unsafe(`SELECT DISTINCT "${PostgreSqlEntityStorageConnector._PARTITION_KEY}" FROM "${this._config.tableName}"`);
502
+ return rows
503
+ .map(row => row[PostgreSqlEntityStorageConnector._PARTITION_KEY])
504
+ .filter((id) => Is.stringValue(id))
505
+ .map(id => ContextIdHelper.shortSplit(this._partitionContextIds ?? [], id));
506
+ }
507
+ catch (err) {
508
+ throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "getPartitionContextIdsFailed", undefined, err);
509
+ }
510
+ }
511
+ /**
512
+ * Create a new target connector for the migration.
513
+ * @param entitySchemaName The entity schema name to use for the target connector.
514
+ * @returns A new connector configured with a migration table name.
515
+ */
516
+ async createTargetConnector(entitySchemaName) {
517
+ return new PostgreSqlEntityStorageConnector({
518
+ entitySchema: entitySchemaName,
519
+ config: {
520
+ ...this._config,
521
+ tableName: `${this._config.tableName}Migration${Date.now()}`
522
+ },
523
+ partitionContextIds: this._partitionContextIds
524
+ });
525
+ }
526
+ /**
527
+ * Finalize the migration by renaming the migration table to the original table name.
528
+ * @param targetConnector The connector pointing to the migration table.
529
+ * @param options The optional migration options.
530
+ * @param loggingComponentType The node logging component type.
531
+ * @returns A connector pointing to the final (renamed) table.
532
+ */
533
+ async finalizeMigration(targetConnector, options, loggingComponentType) {
534
+ // Teardown the existing table with the original name to free up the name for the new table
535
+ await this.teardown(loggingComponentType);
536
+ const dbConnection = await targetConnector.createConnection();
537
+ await dbConnection.unsafe(`ALTER TABLE "${targetConnector._config.tableName}" RENAME TO "${this._config.tableName}"`);
538
+ const finalConnector = new PostgreSqlEntityStorageConnector({
539
+ entitySchema: targetConnector._entitySchemaName,
540
+ config: this._config,
541
+ partitionContextIds: this._partitionContextIds
542
+ });
543
+ if (await finalConnector.bootstrap(loggingComponentType)) {
544
+ await targetConnector.stop();
545
+ return finalConnector;
546
+ }
547
+ throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "finalizeMigrationFailedBootstrap", undefined);
548
+ }
549
+ /**
550
+ * Clean up the migration by tearing down the migration table.
551
+ * @param targetConnector The connector pointing to the migration table.
552
+ * @param options The optional migration options.
553
+ * @param loggingComponentType The node logging component type.
554
+ */
555
+ async cleanupMigration(targetConnector, options, loggingComponentType) {
556
+ // If something failed the only thing to cleanup is the migration table
557
+ await targetConnector?.teardown?.(loggingComponentType);
558
+ }
486
559
  /**
487
560
  * Find all the entities which match the conditions.
488
561
  * @param conditions The conditions to match for the entities.
@@ -508,25 +581,13 @@ export class PostgreSqlEntityStorageConnector {
508
581
  }
509
582
  orderByClause = `ORDER BY ${orderClauses.join(", ")}`;
510
583
  }
511
- const whereClauses = [];
512
- const values = [];
513
- const finalConditions = {
514
- conditions: [],
515
- logicalOperator: LogicalOperator.And
516
- };
517
- finalConditions.conditions.push({
518
- property: PostgreSqlEntityStorageConnector._PARTITION_KEY,
519
- comparison: ComparisonOperator.Equals,
520
- value: partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE
521
- });
522
- if (!Is.empty(conditions)) {
523
- finalConditions.conditions.push(conditions);
524
- }
525
- this.buildQueryParameters("", finalConditions, whereClauses, values, 1);
584
+ const { whereClauses, values } = this.buildWhereClause(conditions, partitionKey);
526
585
  const startIndex = Coerce.number(cursor) ?? 0;
527
586
  sql = `SELECT ${properties ? properties.map(p => `"${String(p)}"`).join(", ") : "*"} FROM "${this._config.tableName}"`;
528
- sql += ` WHERE ${whereClauses.join(" AND ")}`;
529
- sql += ` ${orderByClause} LIMIT ${returnSize} OFFSET ${startIndex}`;
587
+ if (whereClauses.length > 0) {
588
+ sql += ` WHERE ${whereClauses.join(" AND ")}`;
589
+ }
590
+ sql += ` ${orderByClause} LIMIT ${returnSize + 1} OFFSET ${startIndex}`;
530
591
  const dbConnection = await this.createConnection();
531
592
  const rows = await dbConnection.unsafe(sql, values);
532
593
  if (this._entitySchema.properties) {
@@ -555,16 +616,17 @@ export class PostgreSqlEntityStorageConnector {
555
616
  }
556
617
  }
557
618
  }
558
- const entities = rows;
619
+ const hasMore = Is.array(rows) && rows.length > returnSize;
620
+ const resultRows = hasMore ? rows.slice(0, returnSize) : rows;
621
+ const entities = resultRows;
559
622
  for (let i = 0; i < entities.length; i++) {
560
- ObjectHelper.propertyDelete(entities[i], PostgreSqlEntityStorageConnector._PARTITION_KEY);
561
- entities[i] = ObjectHelper.removeEmptyProperties(entities[i], { removeNull: true });
623
+ entities[i] = EntityHelper.unPrepareEntity(entities[i], [
624
+ PostgreSqlEntityStorageConnector._PARTITION_KEY
625
+ ]);
562
626
  }
563
627
  return {
564
628
  entities,
565
- cursor: Is.array(rows) && rows.length === returnSize
566
- ? Coerce.string(startIndex + returnSize)
567
- : undefined
629
+ cursor: hasMore ? Coerce.string(startIndex + returnSize) : undefined
568
630
  };
569
631
  }
570
632
  catch (err) {
@@ -573,18 +635,25 @@ export class PostgreSqlEntityStorageConnector {
573
635
  }
574
636
  /**
575
637
  * Count all the entities which match the conditions.
638
+ * @param conditions The optional conditions to match for the entities.
576
639
  * @returns The total count of entities in the storage.
577
640
  */
578
- async count() {
641
+ async count(conditions) {
642
+ let queryStr;
579
643
  try {
644
+ const dbConnection = await this.createConnection();
580
645
  const contextIds = await ContextIdStore.getContextIds();
581
646
  const partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);
582
- const sql = await this.createConnection();
583
- const result = await sql `SELECT COUNT(*) AS count FROM ${sql(this._config.tableName)} WHERE "partitionId" = ${partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE}`;
647
+ const { whereClauses, values } = this.buildWhereClause(conditions, partitionKey);
648
+ queryStr = `SELECT COUNT(*) AS count FROM "${this._config.tableName}"`;
649
+ if (whereClauses.length > 0) {
650
+ queryStr += ` WHERE ${whereClauses.join(" AND ")}`;
651
+ }
652
+ const result = await dbConnection.unsafe(queryStr, values);
584
653
  return Number(result[0].count);
585
654
  }
586
655
  catch (err) {
587
- throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "countFailed", undefined, err);
656
+ throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "countFailed", { sql: queryStr }, err);
588
657
  }
589
658
  }
590
659
  /**
@@ -624,9 +693,8 @@ export class PostgreSqlEntityStorageConnector {
624
693
  async tableExists() {
625
694
  try {
626
695
  const dbConnection = await this.createConnection();
627
- const tableExistsQuery = `SELECT to_regclass('${this._config.tableName}')`;
628
- const tableExistsResult = await dbConnection.unsafe(tableExistsQuery);
629
- return tableExistsResult[0].to_regclass !== null;
696
+ const res = await dbConnection.unsafe("SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = $1 LIMIT 1", [this._config.tableName]);
697
+ return res.length > 0;
630
698
  }
631
699
  catch {
632
700
  return false;
@@ -684,6 +752,31 @@ export class PostgreSqlEntityStorageConnector {
684
752
  password: this._config.password
685
753
  };
686
754
  }
755
+ /**
756
+ * Build where clause arrays for a query, combining partition key and optional conditions.
757
+ * @param conditions The optional entity conditions to include.
758
+ * @param partitionKey The partition key value.
759
+ * @returns The where clauses and bound values.
760
+ * @internal
761
+ */
762
+ buildWhereClause(conditions, partitionKey) {
763
+ const whereClauses = [];
764
+ const values = [];
765
+ const finalConditions = {
766
+ conditions: [],
767
+ logicalOperator: LogicalOperator.And
768
+ };
769
+ finalConditions.conditions.push({
770
+ property: PostgreSqlEntityStorageConnector._PARTITION_KEY,
771
+ comparison: ComparisonOperator.Equals,
772
+ value: partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE
773
+ });
774
+ if (!Is.empty(conditions)) {
775
+ finalConditions.conditions.push(conditions);
776
+ }
777
+ this.buildQueryParameters("", finalConditions, whereClauses, values, 1);
778
+ return { whereClauses, values };
779
+ }
687
780
  /**
688
781
  * Create an SQL condition clause.
689
782
  * @param objectPath The path for the nested object.
@@ -786,6 +879,17 @@ export class PostgreSqlEntityStorageConnector {
786
879
  }
787
880
  return `LOWER(${jsonTextExpr}) ILIKE $${valueIndex}`;
788
881
  }
882
+ case ComparisonOperator.NotIncludes: {
883
+ values.pop();
884
+ values.push(`%${String(comparator.value).toLowerCase()}%`);
885
+ if (isArray) {
886
+ const elemPath = nestedParts
887
+ .map((p, i, arr) => (i === arr.length - 1 ? `->>'${p}'` : `->'${p}'`))
888
+ .join("");
889
+ return `NOT EXISTS (SELECT 1 FROM jsonb_array_elements("${rootProp}") elem WHERE LOWER(elem${elemPath}) ILIKE $${valueIndex})`;
890
+ }
891
+ return `LOWER(${jsonTextExpr}) NOT ILIKE $${valueIndex}`;
892
+ }
789
893
  case ComparisonOperator.NotEquals:
790
894
  return `${jsonTextExpr} <> $${valueIndex}`;
791
895
  case ComparisonOperator.GreaterThan:
@@ -831,6 +935,18 @@ export class PostgreSqlEntityStorageConnector {
831
935
  type
832
936
  });
833
937
  }
938
+ case ComparisonOperator.NotIncludes: {
939
+ if (type === EntitySchemaPropertyType.String) {
940
+ return `"${prop}" NOT ILIKE '%' || $${valueIndex} || '%'`;
941
+ }
942
+ if (type === EntitySchemaPropertyType.Array || type === EntitySchemaPropertyType.Object) {
943
+ return `NOT EXISTS (SELECT 1 FROM jsonb_array_elements("${prop}") elem WHERE elem @> $${valueIndex}::jsonb)`;
944
+ }
945
+ throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "comparisonNotSupported", {
946
+ comparison: comparator.comparison,
947
+ type
948
+ });
949
+ }
834
950
  default:
835
951
  throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "comparisonNotSupported", {
836
952
  comparison: comparator.comparison
@@ -1 +1 @@
1
- {"version":3,"file":"postgreSqlEntityStorageConnector.js","sourceRoot":"","sources":["../../src/postgreSqlEntityStorageConnector.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EACN,SAAS,EACT,MAAM,EACN,gBAAgB,EAChB,YAAY,EACZ,MAAM,EACN,YAAY,EACZ,EAAE,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 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ContextIdHelper, ContextIdStore } from \"@twin.org/context\";\nimport {\n\tBaseError,\n\tCoerce,\n\tComponentFactory,\n\tGeneralError,\n\tGuards,\n\tHealthStatus,\n\tIs,\n\tObjectHelper,\n\ttype IHealth\n} from \"@twin.org/core\";\nimport {\n\tComparisonOperator,\n\ttype EntityCondition,\n\tEntitySchemaFactory,\n\tEntitySchemaHelper,\n\tEntitySchemaPropertyType,\n\ttype IComparator,\n\ttype IEntitySchema,\n\ttype IEntitySchemaProperty,\n\tLogicalOperator,\n\tSortDirection\n} from \"@twin.org/entity\";\nimport type { IEntityStorageConnector } from \"@twin.org/entity-storage-models\";\nimport type { ILoggingComponent } from \"@twin.org/logging-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport postgres, { type ParameterOrJSON } from \"postgres\";\nimport type { IPostgreSqlEntityStorageConnectorConfig } from \"./models/IPostgreSqlEntityStorageConnectorConfig.js\";\nimport type { IPostgreSqlEntityStorageConnectorConstructorOptions } from \"./models/IPostgreSqlEntityStorageConnectorConstructorOptions.js\";\n\n/**\n * Class for performing entity storage operations using ql.\n */\nexport class PostgreSqlEntityStorageConnector<T = unknown> implements IEntityStorageConnector<T> {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<PostgreSqlEntityStorageConnector>();\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 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 property.\n\t * @internal\n\t */\n\tprivate readonly _primaryKeyProperty: IEntitySchemaProperty<T>;\n\n\t/**\n\t * The configuration for the connector.\n\t * @internal\n\t */\n\tprivate readonly _config: IPostgreSqlEntityStorageConnectorConfig;\n\n\t/**\n\t * The configuration for the connector.\n\t * @internal\n\t */\n\tprivate _connection?: postgres.Sql;\n\n\t/**\n\t * Create a new instance of PostgreSqlEntityStorageConnector.\n\t * @param options The options for the connector.\n\t */\n\tconstructor(options: IPostgreSqlEntityStorageConnectorConstructorOptions) {\n\t\tGuards.object(PostgreSqlEntityStorageConnector.CLASS_NAME, nameof(options), options);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.entitySchema),\n\t\t\toptions.entitySchema\n\t\t);\n\t\tGuards.object<IPostgreSqlEntityStorageConnectorConfig>(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config),\n\t\t\toptions.config\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.host),\n\t\t\toptions.config.host\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.user),\n\t\t\toptions.config.user\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.password),\n\t\t\toptions.config.password\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.database),\n\t\t\toptions.config.database\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.tableName),\n\t\t\toptions.config.tableName\n\t\t);\n\n\t\tthis._entitySchema = EntitySchemaFactory.get(options.entitySchema);\n\t\tthis._partitionContextIds = options.partitionContextIds;\n\t\tthis._primaryKeyProperty = EntitySchemaHelper.getPrimaryKey(this._entitySchema);\n\n\t\tthis._config = options.config;\n\t}\n\n\t/**\n\t * Initialize the PostgreSql 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\ttry {\n\t\t\tconst dbConnection = await this.createConnection();\n\n\t\t\tconst databaseExists = await this.databaseExists();\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: PostgreSqlEntityStorageConnector.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\tdatabaseName: this._config.database\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tawait dbConnection.unsafe(`CREATE DATABASE \"${this._config.database}\";`);\n\t\t\t\tawait this.waitForDatabaseExists();\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: PostgreSqlEntityStorageConnector.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\tdatabaseName: this._config.database\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst tableExists = await this.tableExists();\n\n\t\t\tif (!tableExists) {\n\t\t\t\tawait nodeLogging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tsource: PostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"tableCreating\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\ttableName: this._config.tableName\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tconst createTableQuery = `CREATE TABLE \"${this._config.tableName}\" (${this.mapPostgreSqlProperties(this._entitySchema)})`;\n\t\t\t\tawait dbConnection.unsafe(createTableQuery);\n\t\t\t\tawait this.waitForTableExists();\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: PostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"tableExists\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\ttableName: this._config.tableName\n\t\t\t\t\t}\n\t\t\t\t});\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: PostgreSqlEntityStorageConnector.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\tdatabaseName: this._config.database\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 PostgreSqlEntityStorageConnector.CLASS_NAME;\n\t}\n\n\t/**\n\t * Get the health of the component.\n\t * @returns The health of the component.\n\t */\n\tpublic async health(): Promise<IHealth[]> {\n\t\ttry {\n\t\t\tconst sql = await this.createConnection();\n\t\t\tawait sql`SELECT 1 FROM ${sql(this._config.tableName)} LIMIT 0`;\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tsource: PostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\t\tdescription: \"healthDescription\",\n\t\t\t\t\tdata: { tableName: this._config.tableName }\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: PostgreSqlEntityStorageConnector.CLASS_NAME,\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: { tableName: this._config.tableName }\n\t\t\t\t}\n\t\t\t];\n\t\t}\n\t}\n\n\t/**\n\t * The component needs to be stopped when the node is closed.\n\t * @returns Nothing.\n\t */\n\tpublic async stop(): Promise<void> {\n\t\tif (this._connection) {\n\t\t\tawait this._connection.end();\n\t\t\tthis._connection = undefined;\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 PostgreSql.\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(PostgreSqlEntityStorageConnector.CLASS_NAME, nameof(id), id);\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 dbConnection = await this.createConnection();\n\n\t\t\tconst whereClauses: string[] = [];\n\t\t\tconst values: unknown[] = [];\n\n\t\t\twhereClauses.push(`\"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\" = $1`);\n\t\t\tvalues.push(partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE);\n\n\t\t\tif (secondaryIndex) {\n\t\t\t\twhereClauses.push(`\"${String(secondaryIndex)}\" = $2`);\n\t\t\t\tvalues.push(id);\n\t\t\t} else {\n\t\t\t\twhereClauses.push(`\"${this._primaryKeyProperty.property as string}\" = $2`);\n\t\t\t\tvalues.push(id);\n\t\t\t}\n\n\t\t\tif (Is.arrayValue(conditions)) {\n\t\t\t\tfor (const condition of conditions) {\n\t\t\t\t\twhereClauses.push(`\"${String(condition.property)}\" = $${values.length + 1}`);\n\t\t\t\t\tvalues.push(condition.value);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst query = `SELECT * FROM \"${this._config.tableName}\" WHERE ${whereClauses.join(\" AND \")} LIMIT 1`;\n\n\t\t\tconst rows = await dbConnection.unsafe(query, values as postgres.ParameterOrJSON<never>[]);\n\n\t\t\tif (Is.array(rows) && rows.length === 1) {\n\t\t\t\tif (this._entitySchema.properties) {\n\t\t\t\t\tfor (const prop of this._entitySchema.properties) {\n\t\t\t\t\t\tconst row = rows[0] as unknown as { [key: string]: unknown };\n\t\t\t\t\t\tlet propColumn = prop.property as string;\n\t\t\t\t\t\tpropColumn = propColumn.toLowerCase();\n\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t(prop.type === EntitySchemaPropertyType.Object ||\n\t\t\t\t\t\t\t\tprop.type === EntitySchemaPropertyType.Array) &&\n\t\t\t\t\t\t\ttypeof row[propColumn] === \"string\"\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tlet value: unknown;\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tvalue = JSON.parse((rows[0] as { [key: string]: unknown })[propColumn] as string);\n\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t// If JSON.parse fails, keep the value as string\n\t\t\t\t\t\t\t\t// This handles cases where plain text was stored in Object/Array fields\n\t\t\t\t\t\t\t\tvalue = (rows[0] as { [key: string]: unknown })[propColumn];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tdelete (rows[0] as { [key: string]: unknown })[propColumn];\n\t\t\t\t\t\t\t(rows[0] as { [key: string]: unknown })[prop.property as string] = value;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (row[propColumn] === null) {\n\t\t\t\t\t\t\t(rows[0] as { [key: string]: unknown })[prop.property as string] = undefined;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst entity = ObjectHelper.removeEmptyProperties(rows[0] as T, { removeNull: true });\n\t\t\t\tObjectHelper.propertyDelete(entity, PostgreSqlEntityStorageConnector._PARTITION_KEY);\n\t\t\t\treturn entity;\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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>(PostgreSqlEntityStorageConnector.CLASS_NAME, nameof(entity), entity);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tEntitySchemaHelper.validateEntity(entity, this.getSchema());\n\n\t\tconst id = entity[this._primaryKeyProperty.property] as unknown as string;\n\n\t\ttry {\n\t\t\tif (Is.arrayValue(conditions)) {\n\t\t\t\tconst itemData = await this.get(id);\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\tconst finalEntity = ObjectHelper.clone(entity);\n\n\t\t\tconst props = [...(this._entitySchema.properties ?? [])];\n\t\t\tprops.unshift({\n\t\t\t\tproperty: PostgreSqlEntityStorageConnector._PARTITION_KEY as keyof T,\n\t\t\t\ttype: EntitySchemaPropertyType.String\n\t\t\t});\n\n\t\t\tObjectHelper.propertySet(\n\t\t\t\tfinalEntity,\n\t\t\t\tPostgreSqlEntityStorageConnector._PARTITION_KEY,\n\t\t\t\tpartitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t);\n\n\t\t\tconst keys: string[] = [];\n\t\t\tconst values: unknown[] = [];\n\n\t\t\tfor (const prop of props) {\n\t\t\t\tif (!(Is.empty(finalEntity[prop.property]) && (prop.optional ?? false))) {\n\t\t\t\t\tkeys.push(prop.property as string);\n\t\t\t\t\tif (finalEntity[prop.property] === undefined) {\n\t\t\t\t\t\tvalues.push(null);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tvalues.push(finalEntity[prop.property]);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet sql = `INSERT INTO \"${this._config.tableName}\"`;\n\t\t\tsql += ` (${keys.map(key => `\"${key}\"`).join(\", \")})`;\n\t\t\tsql += ` VALUES (${values.map((_, i) => `$${i + 1}`).join(\", \")})`;\n\t\t\tsql += ` ON CONFLICT (\"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\", \"${this._primaryKeyProperty.property as string}\")`;\n\t\t\tsql += ` DO UPDATE SET ${keys.map(key => `\"${key}\" = EXCLUDED.\"${key}\"`).join(\", \")};`;\n\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tawait dbConnection.unsafe(sql, values as ParameterOrJSON<never>[]);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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(PostgreSqlEntityStorageConnector.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\tfor (const entity of entities) {\n\t\t\tEntitySchemaHelper.validateEntity(entity, this.getSchema());\n\t\t}\n\n\t\ttry {\n\t\t\tconst props = [...(this._entitySchema.properties ?? [])];\n\t\t\tprops.unshift({\n\t\t\t\tproperty: PostgreSqlEntityStorageConnector._PARTITION_KEY as keyof T,\n\t\t\t\ttype: EntitySchemaPropertyType.String\n\t\t\t});\n\t\t\tconst keys = props.map(p => p.property as string);\n\n\t\t\tconst allValues: unknown[] = [];\n\t\t\tconst rowPlaceholders: string[] = [];\n\n\t\t\tfor (const entity of entities) {\n\t\t\t\tconst finalEntity = ObjectHelper.clone(entity);\n\t\t\t\tObjectHelper.propertySet(\n\t\t\t\t\tfinalEntity,\n\t\t\t\t\tPostgreSqlEntityStorageConnector._PARTITION_KEY,\n\t\t\t\t\tpartitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t\t);\n\n\t\t\t\tconst rowValues: string[] = [];\n\t\t\t\tfor (const prop of props) {\n\t\t\t\t\tconst val = finalEntity[prop.property];\n\t\t\t\t\tallValues.push(Is.empty(val) ? null : val);\n\t\t\t\t\trowValues.push(`$${allValues.length}`);\n\t\t\t\t}\n\t\t\t\trowPlaceholders.push(`(${rowValues.join(\", \")})`);\n\t\t\t}\n\n\t\t\tlet sql = `INSERT INTO \"${this._config.tableName}\"`;\n\t\t\tsql += ` (${keys.map(key => `\"${key}\"`).join(\", \")})`;\n\t\t\tsql += ` VALUES ${rowPlaceholders.join(\", \")}`;\n\t\t\tsql += ` ON CONFLICT (\"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\", \"${this._primaryKeyProperty.property as string}\")`;\n\t\t\tsql += ` DO UPDATE SET ${keys.map(key => `\"${key}\" = EXCLUDED.\"${key}\"`).join(\", \")};`;\n\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tawait dbConnection.unsafe(sql, allValues as ParameterOrJSON<never>[]);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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 the entities.\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\n\t\ttry {\n\t\t\tconst sql = `DELETE FROM \"${this._config.tableName}\" WHERE \"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\" = $1`;\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tawait dbConnection.unsafe(sql, [\n\t\t\t\tpartitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t]);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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(PostgreSqlEntityStorageConnector.CLASS_NAME, nameof(id), id);\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 dbConnection = await this.createConnection();\n\n\t\t\tconst itemData = await this.get(id);\n\t\t\tif (Is.notEmpty(itemData)) {\n\t\t\t\tconst values: unknown[] = [];\n\t\t\t\tconst whereClauses: string[] = [];\n\n\t\t\t\twhereClauses.push(\n\t\t\t\t\t`\"${this._primaryKeyProperty.property as string}\" = $${values.length + 1}`\n\t\t\t\t);\n\t\t\t\tvalues.push(id);\n\n\t\t\t\twhereClauses.push(\n\t\t\t\t\t`\"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\" = $${values.length + 1}`\n\t\t\t\t);\n\t\t\t\tvalues.push(partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE);\n\n\t\t\t\tif (Is.arrayValue(conditions)) {\n\t\t\t\t\twhereClauses.push(\n\t\t\t\t\t\t...conditions.map(condition => {\n\t\t\t\t\t\t\tvalues.push(condition.value);\n\t\t\t\t\t\t\treturn `\"${String(condition.property)}\" = $${values.length}`;\n\t\t\t\t\t\t})\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tconst query = `DELETE FROM \"${this._config.tableName}\" WHERE ${whereClauses.join(\" AND \")}`;\n\t\t\t\tawait dbConnection.unsafe(query, values as postgres.ParameterOrJSON<never>[]);\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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 their primary key IDs.\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(PostgreSqlEntityStorageConnector.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 sql = `DELETE FROM \"${this._config.tableName}\" WHERE \"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\" = $1 AND \"${this._primaryKeyProperty.property as string}\" = ANY($2)`;\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tawait dbConnection.unsafe(sql, [\n\t\t\t\tpartitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE,\n\t\t\t\tids\n\t\t\t] as ParameterOrJSON<never>[]);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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 entity storage by dropping the table.\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: PostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tts: Date.now(),\n\t\t\tmessage: \"tableDropping\",\n\t\t\tdata: { tableName: this._config.tableName }\n\t\t});\n\n\t\ttry {\n\t\t\tconst tableExists = await this.tableExists();\n\t\t\tif (tableExists) {\n\t\t\t\tconst dbConnection = await this.createConnection();\n\t\t\t\tawait dbConnection.unsafe(`DROP TABLE \"${this._config.tableName}\";`);\n\t\t\t\tawait this.waitForTableNotExists();\n\t\t\t}\n\n\t\t\tawait nodeLogging?.log({\n\t\t\t\tlevel: \"info\",\n\t\t\t\tsource: PostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"tableDropped\",\n\t\t\t\tdata: { tableName: this._config.tableName }\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: PostgreSqlEntityStorageConnector.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\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tlet sql = \"\";\n\t\ttry {\n\t\t\tconst returnSize = limit ?? PostgreSqlEntityStorageConnector._DEFAULT_LIMIT;\n\n\t\t\tlet orderByClause: string = \"\";\n\t\t\tif (Is.arrayValue(sortProperties)) {\n\t\t\t\tconst orderClauses: string[] = [];\n\t\t\t\tfor (const sortProperty of sortProperties) {\n\t\t\t\t\tconst direction = sortProperty.sortDirection === SortDirection.Ascending ? \"ASC\" : \"DESC\";\n\t\t\t\t\torderClauses.push(`\"${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 whereClauses: string[] = [];\n\t\t\tconst values: unknown[] = [];\n\n\t\t\tconst finalConditions: EntityCondition<T> = {\n\t\t\t\tconditions: [],\n\t\t\t\tlogicalOperator: LogicalOperator.And\n\t\t\t};\n\n\t\t\tfinalConditions.conditions.push({\n\t\t\t\tproperty: PostgreSqlEntityStorageConnector._PARTITION_KEY,\n\t\t\t\tcomparison: ComparisonOperator.Equals,\n\t\t\t\tvalue: partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t});\n\n\t\t\tif (!Is.empty(conditions)) {\n\t\t\t\tfinalConditions.conditions.push(conditions);\n\t\t\t}\n\n\t\t\tthis.buildQueryParameters(\"\", finalConditions, whereClauses, values, 1);\n\n\t\t\tconst startIndex = Coerce.number(cursor) ?? 0;\n\n\t\t\tsql = `SELECT ${properties ? properties.map(p => `\"${String(p)}\"`).join(\", \") : \"*\"} FROM \"${this._config.tableName}\"`;\n\t\t\tsql += ` WHERE ${whereClauses.join(\" AND \")}`;\n\t\t\tsql += ` ${orderByClause} LIMIT ${returnSize} OFFSET ${startIndex}`;\n\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tconst rows = await dbConnection.unsafe(sql, values as postgres.ParameterOrJSON<never>[]);\n\n\t\t\tif (this._entitySchema.properties) {\n\t\t\t\tfor (const row of rows) {\n\t\t\t\t\tfor (const prop of this._entitySchema.properties) {\n\t\t\t\t\t\tlet propColumn = prop.property as string;\n\t\t\t\t\t\tpropColumn = propColumn.toLowerCase();\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t(prop.type === EntitySchemaPropertyType.Object ||\n\t\t\t\t\t\t\t\tprop.type === EntitySchemaPropertyType.Array) &&\n\t\t\t\t\t\t\tIs.string(row[propColumn])\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tlet value: unknown;\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tvalue = JSON.parse(row[propColumn] as string);\n\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t// If JSON.parse fails, keep the value as string\n\t\t\t\t\t\t\t\t// This handles cases where plain text was stored in Object/Array fields\n\t\t\t\t\t\t\t\tvalue = row[propColumn];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tdelete row[propColumn];\n\t\t\t\t\t\t\trow[prop.property as string] = value;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (row[propColumn] === null) {\n\t\t\t\t\t\t\trow[prop.property as string] = undefined;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst entities = rows as unknown as Partial<T>[];\n\t\t\tfor (let i = 0; i < entities.length; i++) {\n\t\t\t\tObjectHelper.propertyDelete(entities[i], PostgreSqlEntityStorageConnector._PARTITION_KEY);\n\t\t\t\tentities[i] = ObjectHelper.removeEmptyProperties(entities[i], { removeNull: true });\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tentities,\n\t\t\t\tcursor:\n\t\t\t\t\tIs.array(rows) && rows.length === returnSize\n\t\t\t\t\t\t? Coerce.string(startIndex + returnSize)\n\t\t\t\t\t\t: undefined\n\t\t\t};\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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 * @returns The total count of entities in the storage.\n\t */\n\tpublic async count(): Promise<number> {\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 sql = await this.createConnection();\n\t\t\tconst result =\n\t\t\t\tawait sql`SELECT COUNT(*) AS count FROM ${sql(this._config.tableName)} WHERE \"partitionId\" = ${partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE}`;\n\t\t\treturn Number(result[0].count);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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 * 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 dbConnection = await this.createConnection();\n\t\t\tconst res = await dbConnection.unsafe(\n\t\t\t\t`SELECT datname FROM pg_catalog.pg_database WHERE datname = '${this._config.database}'`\n\t\t\t);\n\t\t\treturn res.length > 0;\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 * Check if the table exists.\n\t * @returns True if the table exists, false otherwise.\n\t * @internal\n\t */\n\tprivate async tableExists(): Promise<boolean> {\n\t\ttry {\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tconst tableExistsQuery = `SELECT to_regclass('${this._config.tableName}')`;\n\t\t\tconst tableExistsResult = await dbConnection.unsafe(tableExistsQuery);\n\t\t\treturn tableExistsResult[0].to_regclass !== null;\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Wait for a table to exist.\n\t * @returns Nothing.\n\t * @internal\n\t */\n\tprivate async waitForTableExists(): Promise<void> {\n\t\tfor (let attempt = 0; attempt < 20; attempt++) {\n\t\t\tconst tableExists = await this.tableExists();\n\t\t\tif (tableExists) {\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 table to not exist.\n\t * @returns Nothing.\n\t * @internal\n\t */\n\tprivate async waitForTableNotExists(): Promise<void> {\n\t\tfor (let attempt = 0; attempt < 20; attempt++) {\n\t\t\tconst tableExists = await this.tableExists();\n\t\t\tif (!tableExists) {\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 * Create a new DB connection.\n\t * @returns The PostgreSql connection.\n\t * @internal\n\t */\n\tprivate async createConnection(): Promise<postgres.Sql> {\n\t\tif (Is.empty(this._connection)) {\n\t\t\tthis._connection = postgres(this.createConnectionConfig());\n\t\t}\n\t\treturn this._connection;\n\t}\n\n\t/**\n\t * Create a new DB connection configuration.\n\t * @returns The PostgreSql connection configuration.\n\t * @internal\n\t */\n\tprivate createConnectionConfig(): postgres.Options<{ [key: string]: postgres.PostgresType }> {\n\t\treturn {\n\t\t\thost: this._config.host,\n\t\t\tport: this._config.port ?? 5432,\n\t\t\tuser: this._config.user,\n\t\t\tpassword: this._config.password\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 whereClauses The where clauses to use in the query.\n\t * @param values The values to use in the query.\n\t * @param valueIndex The current value index.\n\t * @internal\n\t */\n\tprivate buildQueryParameters(\n\t\tobjectPath: string,\n\t\tcondition: EntityCondition<T> | undefined,\n\t\twhereClauses: string[],\n\t\tvalues: unknown[],\n\t\tvalueIndex: number\n\t): void {\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\tconst joinConditions: string[] = condition.conditions.map(c => {\n\t\t\t\tconst subWhereClauses: string[] = [];\n\t\t\t\tconst subValues: unknown[] = [];\n\t\t\t\tthis.buildQueryParameters(objectPath, c, subWhereClauses, subValues, valueIndex);\n\t\t\t\tvalues.push(...subValues);\n\t\t\t\tvalueIndex += subValues.length;\n\t\t\t\treturn subWhereClauses.join(\" AND \");\n\t\t\t});\n\n\t\t\tconst logicalOperator = this.mapConditionalOperator(condition.logicalOperator);\n\t\t\tconst queryClause = joinConditions.filter(j => j.length > 0).join(` ${logicalOperator} `);\n\n\t\t\tif (queryClause.length > 0) {\n\t\t\t\twhereClauses.push(`(${queryClause})`);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tconst schemaProp = this._entitySchema.properties?.find(p => p.property === condition.property);\n\t\tconst comparison = this.mapComparisonOperator(\n\t\t\tobjectPath,\n\t\t\tcondition,\n\t\t\tschemaProp?.type,\n\t\t\tvalues,\n\t\t\tvalueIndex\n\t\t);\n\t\twhereClauses.push(comparison);\n\t}\n\n\t/**\n\t * Map the framework comparison operators to those in MySQL.\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 values The values to use in the query.\n\t * @param valueIndex The current value index.\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\tvalues: unknown[],\n\t\tvalueIndex: number\n\t): string {\n\t\tlet prop = objectPath;\n\t\tif (prop.length > 0) {\n\t\t\tprop += \".\";\n\t\t}\n\n\t\tprop += comparator.property;\n\n\t\tif (comparator.comparison === ComparisonOperator.In) {\n\t\t\tconst inValues = Is.array(comparator.value) ? comparator.value : [comparator.value];\n\t\t\tvalues.push(...inValues.map(val => this.propertyToDbValue(val, type)));\n\t\t\tconst placeholders = inValues.map((_, index) => `$${valueIndex + index}`).join(\", \");\n\t\t\treturn `\"${prop}\" IN (${placeholders})`;\n\t\t}\n\n\t\t// null/undefined must use IS NULL / IS NOT NULL — never a parameterised placeholder.\n\t\t// Passing undefined through propertyToDbValue() coerces it to NaN for number fields\n\t\t// (Number(undefined) === NaN), and null coerces to 0 (Number(null) === 0), both of\n\t\t// which produce semantically wrong or invalid SQL.\n\t\tif (comparator.value === null || comparator.value === undefined) {\n\t\t\tif (\n\t\t\t\tcomparator.comparison === ComparisonOperator.Equals ||\n\t\t\t\tcomparator.comparison === ComparisonOperator.NotEquals\n\t\t\t) {\n\t\t\t\tconst nullCheck =\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.Equals ? \"IS NULL\" : \"IS NOT NULL\";\n\n\t\t\t\tif (comparator.property.split(\".\").length > 1) {\n\t\t\t\t\tconst rootProp = comparator.property.split(\".\")[0];\n\t\t\t\t\tconst nestedParts = comparator.property.split(\".\").slice(1);\n\t\t\t\t\tconst jsonPath = nestedParts\n\t\t\t\t\t\t.map((p, i, arr) => (i === arr.length - 1 ? `->> '${p}'` : `-> '${p}'`))\n\t\t\t\t\t\t.join(\"\");\n\t\t\t\t\tconst jsonTextExpr = `(\"${rootProp}\"::jsonb ${jsonPath})`;\n\t\t\t\t\treturn `${jsonTextExpr} ${nullCheck}`;\n\t\t\t\t}\n\t\t\t\treturn `\"${prop}\" ${nullCheck}`;\n\t\t\t}\n\t\t}\n\n\t\tconst dbValue = this.propertyToDbValue(comparator.value, type);\n\t\tvalues.push(dbValue);\n\n\t\tif (comparator.property.split(\".\").length > 1) {\n\t\t\tconst rootProp = comparator.property.split(\".\")[0];\n\t\t\tconst nestedParts = comparator.property.split(\".\").slice(1);\n\t\t\tconst rootSchema = this._entitySchema.properties?.find(p => p.property === rootProp);\n\t\t\tconst isArray = rootSchema?.type === EntitySchemaPropertyType.Array;\n\t\t\tconst jsonPath = nestedParts\n\t\t\t\t.map((p, i, arr) => (i === arr.length - 1 ? `->> '${p}'` : `-> '${p}'`))\n\t\t\t\t.join(\"\");\n\t\t\tconst jsonTextExpr = `(\"${rootProp}\"::jsonb ${jsonPath})`;\n\n\t\t\tswitch (comparator.comparison) {\n\t\t\t\tcase ComparisonOperator.Includes: {\n\t\t\t\t\tvalues.pop();\n\t\t\t\t\tvalues.push(`%${String(comparator.value).toLowerCase()}%`);\n\t\t\t\t\tif (isArray) {\n\t\t\t\t\t\tconst elemPath = nestedParts\n\t\t\t\t\t\t\t.map((p, i, arr) => (i === arr.length - 1 ? `->>'${p}'` : `->'${p}'`))\n\t\t\t\t\t\t\t.join(\"\");\n\t\t\t\t\t\treturn `EXISTS (SELECT 1 FROM jsonb_array_elements(\"${rootProp}\") elem WHERE LOWER(elem${elemPath}) ILIKE $${valueIndex})`;\n\t\t\t\t\t}\n\t\t\t\t\treturn `LOWER(${jsonTextExpr}) ILIKE $${valueIndex}`;\n\t\t\t\t}\n\t\t\t\tcase ComparisonOperator.NotEquals:\n\t\t\t\t\treturn `${jsonTextExpr} <> $${valueIndex}`;\n\t\t\t\tcase ComparisonOperator.GreaterThan:\n\t\t\t\t\treturn `${jsonTextExpr} > $${valueIndex}`;\n\t\t\t\tcase ComparisonOperator.LessThan:\n\t\t\t\t\treturn `${jsonTextExpr} < $${valueIndex}`;\n\t\t\t\tcase ComparisonOperator.GreaterThanOrEqual:\n\t\t\t\t\treturn `${jsonTextExpr} >= $${valueIndex}`;\n\t\t\t\tcase ComparisonOperator.LessThanOrEqual:\n\t\t\t\t\treturn `${jsonTextExpr} <= $${valueIndex}`;\n\t\t\t\tdefault:\n\t\t\t\t\treturn `${jsonTextExpr} = $${valueIndex}`;\n\t\t\t}\n\t\t}\n\n\t\tswitch (comparator.comparison) {\n\t\t\tcase ComparisonOperator.Equals:\n\t\t\t\tif (Is.object(comparator.value) || Is.array(comparator.value)) {\n\t\t\t\t\treturn `\"${prop}\" = $${valueIndex}::jsonb`;\n\t\t\t\t}\n\t\t\t\treturn `\"${prop}\" = $${valueIndex}`;\n\t\t\tcase ComparisonOperator.NotEquals:\n\t\t\t\tif (Is.object(comparator.value) || Is.array(comparator.value)) {\n\t\t\t\t\treturn `\"${prop}\" != $${valueIndex}::jsonb`;\n\t\t\t\t}\n\t\t\t\treturn `\"${prop}\" <> $${valueIndex}`;\n\t\t\tcase ComparisonOperator.GreaterThan:\n\t\t\t\treturn `\"${prop}\" > $${valueIndex}`;\n\t\t\tcase ComparisonOperator.LessThan:\n\t\t\t\treturn `\"${prop}\" < $${valueIndex}`;\n\t\t\tcase ComparisonOperator.GreaterThanOrEqual:\n\t\t\t\treturn `\"${prop}\" >= $${valueIndex}`;\n\t\t\tcase ComparisonOperator.LessThanOrEqual:\n\t\t\t\treturn `\"${prop}\" <= $${valueIndex}`;\n\t\t\tcase ComparisonOperator.Includes: {\n\t\t\t\tif (type === EntitySchemaPropertyType.String) {\n\t\t\t\t\treturn `\"${prop}\" ILIKE '%' || $${valueIndex} || '%'`;\n\t\t\t\t}\n\t\t\t\tif (type === EntitySchemaPropertyType.Array || type === EntitySchemaPropertyType.Object) {\n\t\t\t\t\treturn `EXISTS (SELECT 1 FROM jsonb_array_elements(\"${prop}\") elem WHERE elem @> $${valueIndex}::jsonb)`;\n\t\t\t\t}\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\"comparisonNotSupported\",\n\t\t\t\t\t{\n\t\t\t\t\t\tcomparison: comparator.comparison,\n\t\t\t\t\t\ttype\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\t\t\tdefault:\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\"comparisonNotSupported\",\n\t\t\t\t\t{\n\t\t\t\t\t\tcomparison: comparator.comparison\n\t\t\t\t\t}\n\t\t\t\t);\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 (type === EntitySchemaPropertyType.String) {\n\t\t\treturn String(value);\n\t\t} else if (type === EntitySchemaPropertyType.Number) {\n\t\t\treturn Number(value);\n\t\t} else if (type === EntitySchemaPropertyType.Boolean) {\n\t\t\treturn Boolean(value);\n\t\t} else if (\n\t\t\ttype === EntitySchemaPropertyType.Object ||\n\t\t\ttype === EntitySchemaPropertyType.Array\n\t\t) {\n\t\t\treturn value;\n\t\t}\n\t\treturn value;\n\t}\n\n\t/**\n\t * Map the framework conditional operators to those in MySQL.\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(PostgreSqlEntityStorageConnector.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 * @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 * Map entity schema properties to SQL properties.\n\t * @param entitySchema The schema of the entity.\n\t * @returns The SQL properties as a string.\n\t * @throws GeneralError if the entity properties do not exist.\n\t */\n\tprivate mapPostgreSqlProperties(entitySchema: IEntitySchema<T>): string {\n\t\tconst sqlTypeMap: { [key in EntitySchemaPropertyType]: string } = {\n\t\t\t[EntitySchemaPropertyType.String]: \"TEXT\",\n\t\t\t[EntitySchemaPropertyType.Number]: \"REAL\",\n\t\t\t[EntitySchemaPropertyType.Integer]: \"INTEGER\",\n\t\t\t[EntitySchemaPropertyType.Object]: \"JSONB\",\n\t\t\t[EntitySchemaPropertyType.Array]: \"JSONB\",\n\t\t\t[EntitySchemaPropertyType.Boolean]: \"BOOLEAN\"\n\t\t};\n\n\t\tif (!entitySchema.properties) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"entitySchemaPropertiesUndefined\"\n\t\t\t);\n\t\t}\n\n\t\tconst primaryKeys: string[] = [];\n\n\t\tconst props: IEntitySchemaProperty<T>[] = [...entitySchema.properties];\n\n\t\tprops.unshift({\n\t\t\tproperty: PostgreSqlEntityStorageConnector._PARTITION_KEY as keyof T,\n\t\t\ttype: EntitySchemaPropertyType.String,\n\t\t\toptional: false,\n\t\t\tisPrimary: true\n\t\t});\n\n\t\tconst columnDefinitions = props\n\t\t\t.map(prop => {\n\t\t\t\tlet sqlType = sqlTypeMap[prop.type] || \"TEXT\";\n\t\t\t\tif (prop.format) {\n\t\t\t\t\tswitch (prop.type) {\n\t\t\t\t\t\tcase EntitySchemaPropertyType.String:\n\t\t\t\t\t\t\tswitch (prop.format) {\n\t\t\t\t\t\t\t\tcase \"uuid\":\n\t\t\t\t\t\t\t\t\tsqlType = \"UUID\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase EntitySchemaPropertyType.Number:\n\t\t\t\t\t\t\tswitch (prop.format) {\n\t\t\t\t\t\t\t\tcase \"float\":\n\t\t\t\t\t\t\t\t\tsqlType = \"REAL\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\tcase \"double\":\n\t\t\t\t\t\t\t\t\tsqlType = \"DOUBLE PRECISION\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase EntitySchemaPropertyType.Integer:\n\t\t\t\t\t\t\tswitch (prop.format) {\n\t\t\t\t\t\t\t\tcase \"int8\":\n\t\t\t\t\t\t\t\tcase \"uint8\":\n\t\t\t\t\t\t\t\t\tsqlType = \"SMALLINT\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\tcase \"int16\":\n\t\t\t\t\t\t\t\t\tsqlType = \"SMALLINT\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\tcase \"uint16\":\n\t\t\t\t\t\t\t\tcase \"int32\":\n\t\t\t\t\t\t\t\t\tsqlType = \"INTEGER\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\tcase \"uint32\":\n\t\t\t\t\t\t\t\tcase \"int64\":\n\t\t\t\t\t\t\t\tcase \"uint64\":\n\t\t\t\t\t\t\t\t\tsqlType = \"BIGINT\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst columnName = String(prop.property);\n\t\t\t\tconst nullable = prop.optional ? \" NULL\" : \" NOT NULL\";\n\n\t\t\t\tif (prop.isPrimary) {\n\t\t\t\t\tprimaryKeys.push(columnName);\n\t\t\t\t}\n\n\t\t\t\treturn `\"${columnName}\" ${sqlType}${nullable}`;\n\t\t\t})\n\t\t\t.join(\", \");\n\n\t\tconst primaryKeyDefinition =\n\t\t\tprimaryKeys.length > 0 ? `, PRIMARY KEY (\"${primaryKeys.join('\", \"')}\")` : \"\";\n\t\treturn columnDefinitions + primaryKeyDefinition;\n\t}\n}\n"]}
1
+ {"version":3,"file":"postgreSqlEntityStorageConnector.js","sourceRoot":"","sources":["../../src/postgreSqlEntityStorageConnector.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,eAAe,EAAE,cAAc,EAAoB,MAAM,mBAAmB,CAAC;AACtF,OAAO,EACN,SAAS,EACT,MAAM,EACN,gBAAgB,EAChB,YAAY,EACZ,MAAM,EACN,YAAY,EAEZ,EAAE,EACF,YAAY,EACZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,kBAAkB,EAElB,mBAAmB,EACnB,kBAAkB,EAClB,wBAAwB,EAIxB,eAAe,EACf,aA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,KAAK,OAAO;gCACX,OAAO,GAAG,UAAU,CAAC;gCACrB,MAAM;4BACP,KAAK,OAAO;gCACX,OAAO,GAAG,UAAU,CAAC;gCACrB,MAAM;4BACP,KAAK,QAAQ,CAAC;4BACd,KAAK,OAAO;gCACX,OAAO,GAAG,SAAS,CAAC;gCACpB,MAAM;4BACP,KAAK,QAAQ,CAAC;4BACd,KAAK,OAAO,CAAC;4BACb,KAAK,QAAQ;gCACZ,OAAO,GAAG,QAAQ,CAAC;gCACnB,MAAM;wBACR,CAAC;wBACD,MAAM;gBACR,CAAC;YACF,CAAC;YACD,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACzC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC;YAEvD,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;gBACpB,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC9B,CAAC;YAED,OAAO,IAAI,UAAU,KAAK,OAAO,GAAG,QAAQ,EAAE,CAAC;QAChD,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAC;QAEb,MAAM,oBAAoB,GACzB,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,mBAAmB,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/E,OAAO,iBAAiB,GAAG,oBAAoB,CAAC;IACjD,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ContextIdHelper, ContextIdStore, type IContextIds } from \"@twin.org/context\";\nimport {\n\tBaseError,\n\tCoerce,\n\tComponentFactory,\n\tGeneralError,\n\tGuards,\n\tHealthStatus,\n\ttype IHealth,\n\tIs,\n\tObjectHelper\n} from \"@twin.org/core\";\nimport {\n\tComparisonOperator,\n\ttype EntityCondition,\n\tEntitySchemaFactory,\n\tEntitySchemaHelper,\n\tEntitySchemaPropertyType,\n\ttype IComparator,\n\ttype IEntitySchema,\n\ttype IEntitySchemaProperty,\n\tLogicalOperator,\n\tSortDirection\n} from \"@twin.org/entity\";\nimport {\n\tEntityHelper,\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 postgres, { type ParameterOrJSON } from \"postgres\";\nimport type { IPostgreSqlEntityStorageConnectorConfig } from \"./models/IPostgreSqlEntityStorageConnectorConfig.js\";\nimport type { IPostgreSqlEntityStorageConnectorConstructorOptions } from \"./models/IPostgreSqlEntityStorageConnectorConstructorOptions.js\";\n\n/**\n * Class for performing entity storage operations using ql.\n */\nexport class PostgreSqlEntityStorageConnector<\n\tT = unknown\n> implements IEntityStorageMigrationConnector<T> {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<PostgreSqlEntityStorageConnector>();\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 property.\n\t * @internal\n\t */\n\tprivate readonly _primaryKeyProperty: IEntitySchemaProperty<T>;\n\n\t/**\n\t * The configuration for the connector.\n\t * @internal\n\t */\n\tprivate readonly _config: IPostgreSqlEntityStorageConnectorConfig;\n\n\t/**\n\t * The configuration for the connector.\n\t * @internal\n\t */\n\tprivate _connection?: postgres.Sql;\n\n\t/**\n\t * Create a new instance of PostgreSqlEntityStorageConnector.\n\t * @param options The options for the connector.\n\t */\n\tconstructor(options: IPostgreSqlEntityStorageConnectorConstructorOptions) {\n\t\tGuards.object(PostgreSqlEntityStorageConnector.CLASS_NAME, nameof(options), options);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.entitySchema),\n\t\t\toptions.entitySchema\n\t\t);\n\t\tGuards.object<IPostgreSqlEntityStorageConnectorConfig>(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config),\n\t\t\toptions.config\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.host),\n\t\t\toptions.config.host\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.user),\n\t\t\toptions.config.user\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.password),\n\t\t\toptions.config.password\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.database),\n\t\t\toptions.config.database\n\t\t);\n\t\tGuards.stringValue(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tnameof(options.config.tableName),\n\t\t\toptions.config.tableName\n\t\t);\n\n\t\tthis._entitySchemaName = options.entitySchema;\n\t\tthis._entitySchema = EntitySchemaFactory.get(options.entitySchema);\n\t\tthis._partitionContextIds = options.partitionContextIds;\n\t\tthis._primaryKeyProperty = EntitySchemaHelper.getPrimaryKey(this._entitySchema);\n\n\t\tthis._config = options.config;\n\t}\n\n\t/**\n\t * Initialize the PostgreSql 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\ttry {\n\t\t\tconst dbConnection = await this.createConnection();\n\n\t\t\tconst databaseExists = await this.databaseExists();\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: PostgreSqlEntityStorageConnector.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\tdatabaseName: this._config.database\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tawait dbConnection.unsafe(`CREATE DATABASE \"${this._config.database}\";`);\n\t\t\t\tawait this.waitForDatabaseExists();\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: PostgreSqlEntityStorageConnector.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\tdatabaseName: this._config.database\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst tableExists = await this.tableExists();\n\n\t\t\tif (!tableExists) {\n\t\t\t\tawait nodeLogging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tsource: PostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"tableCreating\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\ttableName: this._config.tableName\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tconst createTableQuery = `CREATE TABLE \"${this._config.tableName}\" (${this.mapPostgreSqlProperties(this._entitySchema)})`;\n\t\t\t\tawait dbConnection.unsafe(createTableQuery);\n\t\t\t\tawait this.waitForTableExists();\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: PostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tmessage: \"tableExists\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\ttableName: this._config.tableName\n\t\t\t\t\t}\n\t\t\t\t});\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: PostgreSqlEntityStorageConnector.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\tdatabaseName: this._config.database\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 PostgreSqlEntityStorageConnector.CLASS_NAME;\n\t}\n\n\t/**\n\t * Get the health of the component.\n\t * @returns The health of the component.\n\t */\n\tpublic async health(): Promise<IHealth[]> {\n\t\ttry {\n\t\t\tconst sql = await this.createConnection();\n\t\t\tawait sql`SELECT 1 FROM ${sql(this._config.tableName)} LIMIT 0`;\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tsource: PostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\t\tdescription: \"healthDescription\",\n\t\t\t\t\tdata: { tableName: this._config.tableName }\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: PostgreSqlEntityStorageConnector.CLASS_NAME,\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: { tableName: this._config.tableName }\n\t\t\t\t}\n\t\t\t];\n\t\t}\n\t}\n\n\t/**\n\t * The component needs to be stopped when the node is closed.\n\t * @returns Nothing.\n\t */\n\tpublic async stop(): Promise<void> {\n\t\tif (this._connection) {\n\t\t\tawait this._connection.end();\n\t\t\tthis._connection = undefined;\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 PostgreSql.\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(PostgreSqlEntityStorageConnector.CLASS_NAME, nameof(id), id);\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 dbConnection = await this.createConnection();\n\n\t\t\tconst whereClauses: string[] = [];\n\t\t\tconst values: unknown[] = [];\n\n\t\t\twhereClauses.push(`\"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\" = $1`);\n\t\t\tvalues.push(partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE);\n\n\t\t\tif (secondaryIndex) {\n\t\t\t\twhereClauses.push(`\"${String(secondaryIndex)}\" = $2`);\n\t\t\t\tvalues.push(id);\n\t\t\t} else {\n\t\t\t\twhereClauses.push(`\"${this._primaryKeyProperty.property as string}\" = $2`);\n\t\t\t\tvalues.push(id);\n\t\t\t}\n\n\t\t\tif (Is.arrayValue(conditions)) {\n\t\t\t\tfor (const condition of conditions) {\n\t\t\t\t\twhereClauses.push(`\"${String(condition.property)}\" = $${values.length + 1}`);\n\t\t\t\t\tvalues.push(condition.value);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst query = `SELECT * FROM \"${this._config.tableName}\" WHERE ${whereClauses.join(\" AND \")} LIMIT 1`;\n\n\t\t\tconst rows = await dbConnection.unsafe(query, values as postgres.ParameterOrJSON<never>[]);\n\n\t\t\tif (Is.array(rows) && rows.length === 1) {\n\t\t\t\tif (this._entitySchema.properties) {\n\t\t\t\t\tfor (const prop of this._entitySchema.properties) {\n\t\t\t\t\t\tconst row = rows[0] as unknown as { [key: string]: unknown };\n\t\t\t\t\t\tlet propColumn = prop.property as string;\n\t\t\t\t\t\tpropColumn = propColumn.toLowerCase();\n\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t(prop.type === EntitySchemaPropertyType.Object ||\n\t\t\t\t\t\t\t\tprop.type === EntitySchemaPropertyType.Array) &&\n\t\t\t\t\t\t\ttypeof row[propColumn] === \"string\"\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tlet value: unknown;\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tvalue = JSON.parse((rows[0] as { [key: string]: unknown })[propColumn] as string);\n\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t// If JSON.parse fails, keep the value as string\n\t\t\t\t\t\t\t\t// This handles cases where plain text was stored in Object/Array fields\n\t\t\t\t\t\t\t\tvalue = (rows[0] as { [key: string]: unknown })[propColumn];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tdelete (rows[0] as { [key: string]: unknown })[propColumn];\n\t\t\t\t\t\t\t(rows[0] as { [key: string]: unknown })[prop.property as string] = value;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (row[propColumn] === null) {\n\t\t\t\t\t\t\t(rows[0] as { [key: string]: unknown })[prop.property as string] = undefined;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn EntityHelper.unPrepareEntity<T>(rows[0] as T, [\n\t\t\t\t\tPostgreSqlEntityStorageConnector._PARTITION_KEY\n\t\t\t\t]);\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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>(PostgreSqlEntityStorageConnector.CLASS_NAME, nameof(entity), entity);\n\n\t\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tconst prepared = EntityHelper.prepareEntity(entity, this._entitySchema, [\n\t\t\t{\n\t\t\t\tproperty: PostgreSqlEntityStorageConnector._PARTITION_KEY,\n\t\t\t\tvalue: partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t}\n\t\t]);\n\n\t\tconst id = prepared[this._primaryKeyProperty.property] as unknown as string;\n\n\t\ttry {\n\t\t\tif (Is.arrayValue(conditions)) {\n\t\t\t\tconst itemData = await this.get(id);\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\tconst props = [...(this._entitySchema.properties ?? [])];\n\t\t\tprops.unshift({\n\t\t\t\tproperty: PostgreSqlEntityStorageConnector._PARTITION_KEY as keyof T,\n\t\t\t\ttype: EntitySchemaPropertyType.String\n\t\t\t});\n\n\t\t\tconst keys: string[] = [];\n\t\t\tconst values: unknown[] = [];\n\n\t\t\tfor (const prop of props) {\n\t\t\t\tkeys.push(prop.property as string);\n\t\t\t\tconst val = prepared[prop.property];\n\t\t\t\tvalues.push(val ?? null);\n\t\t\t}\n\n\t\t\tlet sql = `INSERT INTO \"${this._config.tableName}\"`;\n\t\t\tsql += ` (${keys.map(key => `\"${key}\"`).join(\", \")})`;\n\t\t\tsql += ` VALUES (${values.map((_, i) => `$${i + 1}`).join(\", \")})`;\n\t\t\tsql += ` ON CONFLICT (\"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\", \"${this._primaryKeyProperty.property as string}\")`;\n\t\t\tsql += ` DO UPDATE SET ${keys.map(key => `\"${key}\" = EXCLUDED.\"${key}\"`).join(\", \")};`;\n\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tawait dbConnection.unsafe(sql, values as ParameterOrJSON<never>[]);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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(PostgreSqlEntityStorageConnector.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\tEntityHelper.prepareEntity(entity, this._entitySchema, [\n\t\t\t\t{\n\t\t\t\t\tproperty: PostgreSqlEntityStorageConnector._PARTITION_KEY,\n\t\t\t\t\tvalue: partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t\t}\n\t\t\t])\n\t\t);\n\n\t\ttry {\n\t\t\tconst props = [...(this._entitySchema.properties ?? [])];\n\t\t\tprops.unshift({\n\t\t\t\tproperty: PostgreSqlEntityStorageConnector._PARTITION_KEY as keyof T,\n\t\t\t\ttype: EntitySchemaPropertyType.String\n\t\t\t});\n\t\t\tconst keys = props.map(p => p.property as string);\n\n\t\t\tconst allValues: unknown[] = [];\n\t\t\tconst rowPlaceholders: string[] = [];\n\n\t\t\tfor (const prepared of preparedEntities) {\n\t\t\t\tconst rowValues: string[] = [];\n\t\t\t\tfor (const prop of props) {\n\t\t\t\t\tconst val = prepared[prop.property];\n\t\t\t\t\tallValues.push(Is.empty(val) ? null : val);\n\t\t\t\t\trowValues.push(`$${allValues.length}`);\n\t\t\t\t}\n\t\t\t\trowPlaceholders.push(`(${rowValues.join(\", \")})`);\n\t\t\t}\n\n\t\t\tlet sql = `INSERT INTO \"${this._config.tableName}\"`;\n\t\t\tsql += ` (${keys.map(key => `\"${key}\"`).join(\", \")})`;\n\t\t\tsql += ` VALUES ${rowPlaceholders.join(\", \")}`;\n\t\t\tsql += ` ON CONFLICT (\"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\", \"${this._primaryKeyProperty.property as string}\")`;\n\t\t\tsql += ` DO UPDATE SET ${keys.map(key => `\"${key}\" = EXCLUDED.\"${key}\"`).join(\", \")};`;\n\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tawait dbConnection.unsafe(sql, allValues as ParameterOrJSON<never>[]);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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 the entities.\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\n\t\ttry {\n\t\t\tconst sql = `DELETE FROM \"${this._config.tableName}\" WHERE \"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\" = $1`;\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tawait dbConnection.unsafe(sql, [\n\t\t\t\tpartitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t\t]);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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(PostgreSqlEntityStorageConnector.CLASS_NAME, nameof(id), id);\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 dbConnection = await this.createConnection();\n\n\t\t\tconst itemData = await this.get(id);\n\t\t\tif (Is.notEmpty(itemData)) {\n\t\t\t\tconst values: unknown[] = [];\n\t\t\t\tconst whereClauses: string[] = [];\n\n\t\t\t\twhereClauses.push(\n\t\t\t\t\t`\"${this._primaryKeyProperty.property as string}\" = $${values.length + 1}`\n\t\t\t\t);\n\t\t\t\tvalues.push(id);\n\n\t\t\t\twhereClauses.push(\n\t\t\t\t\t`\"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\" = $${values.length + 1}`\n\t\t\t\t);\n\t\t\t\tvalues.push(partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE);\n\n\t\t\t\tif (Is.arrayValue(conditions)) {\n\t\t\t\t\twhereClauses.push(\n\t\t\t\t\t\t...conditions.map(condition => {\n\t\t\t\t\t\t\tvalues.push(condition.value);\n\t\t\t\t\t\t\treturn `\"${String(condition.property)}\" = $${values.length}`;\n\t\t\t\t\t\t})\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tconst query = `DELETE FROM \"${this._config.tableName}\" WHERE ${whereClauses.join(\" AND \")}`;\n\t\t\t\tawait dbConnection.unsafe(query, values as postgres.ParameterOrJSON<never>[]);\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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 their primary key IDs.\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(PostgreSqlEntityStorageConnector.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 sql = `DELETE FROM \"${this._config.tableName}\" WHERE \"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\" = $1 AND \"${this._primaryKeyProperty.property as string}\" = ANY($2)`;\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tawait dbConnection.unsafe(sql, [\n\t\t\t\tpartitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE,\n\t\t\t\tids\n\t\t\t] as ParameterOrJSON<never>[]);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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 entity storage by dropping the table.\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: PostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\tts: Date.now(),\n\t\t\tmessage: \"tableDropping\",\n\t\t\tdata: { tableName: this._config.tableName }\n\t\t});\n\n\t\ttry {\n\t\t\tconst tableExists = await this.tableExists();\n\t\t\tif (tableExists) {\n\t\t\t\tconst dbConnection = await this.createConnection();\n\t\t\t\tawait dbConnection.unsafe(`DROP TABLE \"${this._config.tableName}\";`);\n\t\t\t\tawait this.waitForTableNotExists();\n\t\t\t}\n\n\t\t\tawait nodeLogging?.log({\n\t\t\t\tlevel: \"info\",\n\t\t\t\tsource: PostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\tts: Date.now(),\n\t\t\t\tmessage: \"tableDropped\",\n\t\t\t\tdata: { tableName: this._config.tableName }\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: PostgreSqlEntityStorageConnector.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 * Get all the distinct partition context ids from the storage.\n\t * @returns An array of context id objects, one per unique partition.\n\t */\n\tpublic async getPartitionContextIds(): Promise<IContextIds[]> {\n\t\tif (!Is.arrayValue(this._partitionContextIds)) {\n\t\t\treturn [];\n\t\t}\n\t\ttry {\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tconst rows = await dbConnection.unsafe(\n\t\t\t\t`SELECT DISTINCT \"${PostgreSqlEntityStorageConnector._PARTITION_KEY}\" FROM \"${this._config.tableName}\"`\n\t\t\t);\n\t\t\treturn (rows as { [key: string]: string }[])\n\t\t\t\t.map(row => row[PostgreSqlEntityStorageConnector._PARTITION_KEY])\n\t\t\t\t.filter((id): id is string => Is.stringValue(id))\n\t\t\t\t.map(id => ContextIdHelper.shortSplit(this._partitionContextIds ?? [], id));\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.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 a new target connector for the migration.\n\t * @param entitySchemaName The entity schema name to use for the target connector.\n\t * @returns A new connector configured with a migration table name.\n\t */\n\tpublic async createTargetConnector<U>(\n\t\tentitySchemaName: string\n\t): Promise<PostgreSqlEntityStorageConnector<U>> {\n\t\treturn new PostgreSqlEntityStorageConnector<U>({\n\t\t\tentitySchema: entitySchemaName,\n\t\t\tconfig: {\n\t\t\t\t...this._config,\n\t\t\t\ttableName: `${this._config.tableName}Migration${Date.now()}`\n\t\t\t},\n\t\t\tpartitionContextIds: this._partitionContextIds\n\t\t});\n\t}\n\n\t/**\n\t * Finalize the migration by renaming the migration table to the original table name.\n\t * @param targetConnector The connector pointing to the migration table.\n\t * @param options The optional migration options.\n\t * @param loggingComponentType The node logging component type.\n\t * @returns A connector pointing to the final (renamed) table.\n\t */\n\tpublic async finalizeMigration<U>(\n\t\ttargetConnector: PostgreSqlEntityStorageConnector<U>,\n\t\toptions?: IMigrationOptions<T, U>,\n\t\tloggingComponentType?: string\n\t): Promise<PostgreSqlEntityStorageConnector<U>> {\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\tconst dbConnection = await targetConnector.createConnection();\n\t\tawait dbConnection.unsafe(\n\t\t\t`ALTER TABLE \"${targetConnector._config.tableName}\" RENAME TO \"${this._config.tableName}\"`\n\t\t);\n\t\tconst finalConnector = new PostgreSqlEntityStorageConnector<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\tawait targetConnector.stop();\n\t\t\treturn finalConnector;\n\t\t}\n\t\tthrow new GeneralError(\n\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\"finalizeMigrationFailedBootstrap\",\n\t\t\tundefined\n\t\t);\n\t}\n\n\t/**\n\t * Clean up the migration by tearing down the migration table.\n\t * @param targetConnector The connector pointing to the migration table.\n\t * @param options The optional migration options.\n\t * @param loggingComponentType The node logging component type.\n\t */\n\tpublic async cleanupMigration<U>(\n\t\ttargetConnector?: PostgreSqlEntityStorageConnector<U>,\n\t\toptions?: IMigrationOptions<T, U>,\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 * 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\tconst contextIds = await ContextIdStore.getContextIds();\n\t\tconst partitionKey = ContextIdHelper.combinedContextKey(contextIds, this._partitionContextIds);\n\n\t\tlet sql = \"\";\n\t\ttry {\n\t\t\tconst returnSize = limit ?? PostgreSqlEntityStorageConnector._DEFAULT_LIMIT;\n\n\t\t\tlet orderByClause: string = \"\";\n\t\t\tif (Is.arrayValue(sortProperties)) {\n\t\t\t\tconst orderClauses: string[] = [];\n\t\t\t\tfor (const sortProperty of sortProperties) {\n\t\t\t\t\tconst direction = sortProperty.sortDirection === SortDirection.Ascending ? \"ASC\" : \"DESC\";\n\t\t\t\t\torderClauses.push(`\"${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 { whereClauses, values } = this.buildWhereClause(conditions, partitionKey);\n\n\t\t\tconst startIndex = Coerce.number(cursor) ?? 0;\n\n\t\t\tsql = `SELECT ${properties ? properties.map(p => `\"${String(p)}\"`).join(\", \") : \"*\"} FROM \"${this._config.tableName}\"`;\n\t\t\tif (whereClauses.length > 0) {\n\t\t\t\tsql += ` WHERE ${whereClauses.join(\" AND \")}`;\n\t\t\t}\n\t\t\tsql += ` ${orderByClause} LIMIT ${returnSize + 1} OFFSET ${startIndex}`;\n\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tconst rows = await dbConnection.unsafe(sql, values);\n\n\t\t\tif (this._entitySchema.properties) {\n\t\t\t\tfor (const row of rows) {\n\t\t\t\t\tfor (const prop of this._entitySchema.properties) {\n\t\t\t\t\t\tlet propColumn = prop.property as string;\n\t\t\t\t\t\tpropColumn = propColumn.toLowerCase();\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t(prop.type === EntitySchemaPropertyType.Object ||\n\t\t\t\t\t\t\t\tprop.type === EntitySchemaPropertyType.Array) &&\n\t\t\t\t\t\t\tIs.string(row[propColumn])\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tlet value: unknown;\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tvalue = JSON.parse(row[propColumn] as string);\n\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t// If JSON.parse fails, keep the value as string\n\t\t\t\t\t\t\t\t// This handles cases where plain text was stored in Object/Array fields\n\t\t\t\t\t\t\t\tvalue = row[propColumn];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tdelete row[propColumn];\n\t\t\t\t\t\t\trow[prop.property as string] = value;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (row[propColumn] === null) {\n\t\t\t\t\t\t\trow[prop.property as string] = undefined;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst hasMore = Is.array(rows) && rows.length > returnSize;\n\t\t\tconst resultRows = hasMore ? rows.slice(0, returnSize) : rows;\n\t\t\tconst entities = resultRows as unknown as Partial<T>[];\n\t\t\tfor (let i = 0; i < entities.length; i++) {\n\t\t\t\tentities[i] = EntityHelper.unPrepareEntity(entities[i], [\n\t\t\t\t\tPostgreSqlEntityStorageConnector._PARTITION_KEY\n\t\t\t\t]);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tentities,\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\tPostgreSqlEntityStorageConnector.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\tlet queryStr: string | undefined;\n\t\ttry {\n\t\t\tconst dbConnection = await this.createConnection();\n\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 { whereClauses, values } = this.buildWhereClause(conditions, partitionKey);\n\n\t\t\tqueryStr = `SELECT COUNT(*) AS count FROM \"${this._config.tableName}\"`;\n\t\t\tif (whereClauses.length > 0) {\n\t\t\t\tqueryStr += ` WHERE ${whereClauses.join(\" AND \")}`;\n\t\t\t}\n\n\t\t\tconst result = await dbConnection.unsafe(queryStr, values);\n\t\t\treturn Number(result[0].count);\n\t\t} catch (err) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"countFailed\",\n\t\t\t\t{ sql: queryStr },\n\t\t\t\terr\n\t\t\t);\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 dbConnection = await this.createConnection();\n\t\t\tconst res = await dbConnection.unsafe(\n\t\t\t\t`SELECT datname FROM pg_catalog.pg_database WHERE datname = '${this._config.database}'`\n\t\t\t);\n\t\t\treturn res.length > 0;\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 * Check if the table exists.\n\t * @returns True if the table exists, false otherwise.\n\t * @internal\n\t */\n\tprivate async tableExists(): Promise<boolean> {\n\t\ttry {\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tconst res = await dbConnection.unsafe(\n\t\t\t\t\"SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = $1 LIMIT 1\",\n\t\t\t\t[this._config.tableName] as postgres.ParameterOrJSON<never>[]\n\t\t\t);\n\t\t\treturn res.length > 0;\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/**\n\t * Wait for a table to exist.\n\t * @returns Nothing.\n\t * @internal\n\t */\n\tprivate async waitForTableExists(): Promise<void> {\n\t\tfor (let attempt = 0; attempt < 20; attempt++) {\n\t\t\tconst tableExists = await this.tableExists();\n\t\t\tif (tableExists) {\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 table to not exist.\n\t * @returns Nothing.\n\t * @internal\n\t */\n\tprivate async waitForTableNotExists(): Promise<void> {\n\t\tfor (let attempt = 0; attempt < 20; attempt++) {\n\t\t\tconst tableExists = await this.tableExists();\n\t\t\tif (!tableExists) {\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 * Create a new DB connection.\n\t * @returns The PostgreSql connection.\n\t * @internal\n\t */\n\tprivate async createConnection(): Promise<postgres.Sql> {\n\t\tif (Is.empty(this._connection)) {\n\t\t\tthis._connection = postgres(this.createConnectionConfig());\n\t\t}\n\t\treturn this._connection;\n\t}\n\n\t/**\n\t * Create a new DB connection configuration.\n\t * @returns The PostgreSql connection configuration.\n\t * @internal\n\t */\n\tprivate createConnectionConfig(): postgres.Options<{ [key: string]: postgres.PostgresType }> {\n\t\treturn {\n\t\t\thost: this._config.host,\n\t\t\tport: this._config.port ?? 5432,\n\t\t\tuser: this._config.user,\n\t\t\tpassword: this._config.password\n\t\t};\n\t}\n\n\t/**\n\t * Build where clause arrays for a query, combining partition key and optional conditions.\n\t * @param conditions The optional entity conditions to include.\n\t * @param partitionKey The partition key value.\n\t * @returns The where clauses and bound values.\n\t * @internal\n\t */\n\tprivate buildWhereClause(\n\t\tconditions: EntityCondition<T> | undefined,\n\t\tpartitionKey: string | undefined\n\t): { whereClauses: string[]; values: ParameterOrJSON<never>[] } {\n\t\tconst whereClauses: string[] = [];\n\t\tconst values: ParameterOrJSON<never>[] = [];\n\n\t\tconst finalConditions: EntityCondition<T> = {\n\t\t\tconditions: [],\n\t\t\tlogicalOperator: LogicalOperator.And\n\t\t};\n\n\t\tfinalConditions.conditions.push({\n\t\t\tproperty: PostgreSqlEntityStorageConnector._PARTITION_KEY,\n\t\t\tcomparison: ComparisonOperator.Equals,\n\t\t\tvalue: partitionKey ?? PostgreSqlEntityStorageConnector._PARTITION_KEY_VALUE\n\t\t});\n\n\t\tif (!Is.empty(conditions)) {\n\t\t\tfinalConditions.conditions.push(conditions);\n\t\t}\n\n\t\tthis.buildQueryParameters(\"\", finalConditions, whereClauses, values, 1);\n\n\t\treturn { whereClauses, values };\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 whereClauses The where clauses to use in the query.\n\t * @param values The values to use in the query.\n\t * @param valueIndex The current value index.\n\t * @internal\n\t */\n\tprivate buildQueryParameters(\n\t\tobjectPath: string,\n\t\tcondition: EntityCondition<T> | undefined,\n\t\twhereClauses: string[],\n\t\tvalues: unknown[],\n\t\tvalueIndex: number\n\t): void {\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\tconst joinConditions: string[] = condition.conditions.map(c => {\n\t\t\t\tconst subWhereClauses: string[] = [];\n\t\t\t\tconst subValues: unknown[] = [];\n\t\t\t\tthis.buildQueryParameters(objectPath, c, subWhereClauses, subValues, valueIndex);\n\t\t\t\tvalues.push(...subValues);\n\t\t\t\tvalueIndex += subValues.length;\n\t\t\t\treturn subWhereClauses.join(\" AND \");\n\t\t\t});\n\n\t\t\tconst logicalOperator = this.mapConditionalOperator(condition.logicalOperator);\n\t\t\tconst queryClause = joinConditions.filter(j => j.length > 0).join(` ${logicalOperator} `);\n\n\t\t\tif (queryClause.length > 0) {\n\t\t\t\twhereClauses.push(`(${queryClause})`);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tconst schemaProp = this._entitySchema.properties?.find(p => p.property === condition.property);\n\t\tconst comparison = this.mapComparisonOperator(\n\t\t\tobjectPath,\n\t\t\tcondition,\n\t\t\tschemaProp?.type,\n\t\t\tvalues,\n\t\t\tvalueIndex\n\t\t);\n\t\twhereClauses.push(comparison);\n\t}\n\n\t/**\n\t * Map the framework comparison operators to those in MySQL.\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 values The values to use in the query.\n\t * @param valueIndex The current value index.\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\tvalues: unknown[],\n\t\tvalueIndex: number\n\t): string {\n\t\tlet prop = objectPath;\n\t\tif (prop.length > 0) {\n\t\t\tprop += \".\";\n\t\t}\n\n\t\tprop += comparator.property;\n\n\t\tif (comparator.comparison === ComparisonOperator.In) {\n\t\t\tconst inValues = Is.array(comparator.value) ? comparator.value : [comparator.value];\n\t\t\tvalues.push(...inValues.map(val => this.propertyToDbValue(val, type)));\n\t\t\tconst placeholders = inValues.map((_, index) => `$${valueIndex + index}`).join(\", \");\n\t\t\treturn `\"${prop}\" IN (${placeholders})`;\n\t\t}\n\n\t\t// null/undefined must use IS NULL / IS NOT NULL — never a parameterised placeholder.\n\t\t// Passing undefined through propertyToDbValue() coerces it to NaN for number fields\n\t\t// (Number(undefined) === NaN), and null coerces to 0 (Number(null) === 0), both of\n\t\t// which produce semantically wrong or invalid SQL.\n\t\tif (comparator.value === null || comparator.value === undefined) {\n\t\t\tif (\n\t\t\t\tcomparator.comparison === ComparisonOperator.Equals ||\n\t\t\t\tcomparator.comparison === ComparisonOperator.NotEquals\n\t\t\t) {\n\t\t\t\tconst nullCheck =\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.Equals ? \"IS NULL\" : \"IS NOT NULL\";\n\n\t\t\t\tif (comparator.property.split(\".\").length > 1) {\n\t\t\t\t\tconst rootProp = comparator.property.split(\".\")[0];\n\t\t\t\t\tconst nestedParts = comparator.property.split(\".\").slice(1);\n\t\t\t\t\tconst jsonPath = nestedParts\n\t\t\t\t\t\t.map((p, i, arr) => (i === arr.length - 1 ? `->> '${p}'` : `-> '${p}'`))\n\t\t\t\t\t\t.join(\"\");\n\t\t\t\t\tconst jsonTextExpr = `(\"${rootProp}\"::jsonb ${jsonPath})`;\n\t\t\t\t\treturn `${jsonTextExpr} ${nullCheck}`;\n\t\t\t\t}\n\t\t\t\treturn `\"${prop}\" ${nullCheck}`;\n\t\t\t}\n\t\t}\n\n\t\tconst dbValue = this.propertyToDbValue(comparator.value, type);\n\t\tvalues.push(dbValue);\n\n\t\tif (comparator.property.split(\".\").length > 1) {\n\t\t\tconst rootProp = comparator.property.split(\".\")[0];\n\t\t\tconst nestedParts = comparator.property.split(\".\").slice(1);\n\t\t\tconst rootSchema = this._entitySchema.properties?.find(p => p.property === rootProp);\n\t\t\tconst isArray = rootSchema?.type === EntitySchemaPropertyType.Array;\n\t\t\tconst jsonPath = nestedParts\n\t\t\t\t.map((p, i, arr) => (i === arr.length - 1 ? `->> '${p}'` : `-> '${p}'`))\n\t\t\t\t.join(\"\");\n\t\t\tconst jsonTextExpr = `(\"${rootProp}\"::jsonb ${jsonPath})`;\n\n\t\t\tswitch (comparator.comparison) {\n\t\t\t\tcase ComparisonOperator.Includes: {\n\t\t\t\t\tvalues.pop();\n\t\t\t\t\tvalues.push(`%${String(comparator.value).toLowerCase()}%`);\n\t\t\t\t\tif (isArray) {\n\t\t\t\t\t\tconst elemPath = nestedParts\n\t\t\t\t\t\t\t.map((p, i, arr) => (i === arr.length - 1 ? `->>'${p}'` : `->'${p}'`))\n\t\t\t\t\t\t\t.join(\"\");\n\t\t\t\t\t\treturn `EXISTS (SELECT 1 FROM jsonb_array_elements(\"${rootProp}\") elem WHERE LOWER(elem${elemPath}) ILIKE $${valueIndex})`;\n\t\t\t\t\t}\n\t\t\t\t\treturn `LOWER(${jsonTextExpr}) ILIKE $${valueIndex}`;\n\t\t\t\t}\n\t\t\t\tcase ComparisonOperator.NotIncludes: {\n\t\t\t\t\tvalues.pop();\n\t\t\t\t\tvalues.push(`%${String(comparator.value).toLowerCase()}%`);\n\t\t\t\t\tif (isArray) {\n\t\t\t\t\t\tconst elemPath = nestedParts\n\t\t\t\t\t\t\t.map((p, i, arr) => (i === arr.length - 1 ? `->>'${p}'` : `->'${p}'`))\n\t\t\t\t\t\t\t.join(\"\");\n\t\t\t\t\t\treturn `NOT EXISTS (SELECT 1 FROM jsonb_array_elements(\"${rootProp}\") elem WHERE LOWER(elem${elemPath}) ILIKE $${valueIndex})`;\n\t\t\t\t\t}\n\t\t\t\t\treturn `LOWER(${jsonTextExpr}) NOT ILIKE $${valueIndex}`;\n\t\t\t\t}\n\t\t\t\tcase ComparisonOperator.NotEquals:\n\t\t\t\t\treturn `${jsonTextExpr} <> $${valueIndex}`;\n\t\t\t\tcase ComparisonOperator.GreaterThan:\n\t\t\t\t\treturn `${jsonTextExpr} > $${valueIndex}`;\n\t\t\t\tcase ComparisonOperator.LessThan:\n\t\t\t\t\treturn `${jsonTextExpr} < $${valueIndex}`;\n\t\t\t\tcase ComparisonOperator.GreaterThanOrEqual:\n\t\t\t\t\treturn `${jsonTextExpr} >= $${valueIndex}`;\n\t\t\t\tcase ComparisonOperator.LessThanOrEqual:\n\t\t\t\t\treturn `${jsonTextExpr} <= $${valueIndex}`;\n\t\t\t\tdefault:\n\t\t\t\t\treturn `${jsonTextExpr} = $${valueIndex}`;\n\t\t\t}\n\t\t}\n\n\t\tswitch (comparator.comparison) {\n\t\t\tcase ComparisonOperator.Equals:\n\t\t\t\tif (Is.object(comparator.value) || Is.array(comparator.value)) {\n\t\t\t\t\treturn `\"${prop}\" = $${valueIndex}::jsonb`;\n\t\t\t\t}\n\t\t\t\treturn `\"${prop}\" = $${valueIndex}`;\n\t\t\tcase ComparisonOperator.NotEquals:\n\t\t\t\tif (Is.object(comparator.value) || Is.array(comparator.value)) {\n\t\t\t\t\treturn `\"${prop}\" != $${valueIndex}::jsonb`;\n\t\t\t\t}\n\t\t\t\treturn `\"${prop}\" <> $${valueIndex}`;\n\t\t\tcase ComparisonOperator.GreaterThan:\n\t\t\t\treturn `\"${prop}\" > $${valueIndex}`;\n\t\t\tcase ComparisonOperator.LessThan:\n\t\t\t\treturn `\"${prop}\" < $${valueIndex}`;\n\t\t\tcase ComparisonOperator.GreaterThanOrEqual:\n\t\t\t\treturn `\"${prop}\" >= $${valueIndex}`;\n\t\t\tcase ComparisonOperator.LessThanOrEqual:\n\t\t\t\treturn `\"${prop}\" <= $${valueIndex}`;\n\t\t\tcase ComparisonOperator.Includes: {\n\t\t\t\tif (type === EntitySchemaPropertyType.String) {\n\t\t\t\t\treturn `\"${prop}\" ILIKE '%' || $${valueIndex} || '%'`;\n\t\t\t\t}\n\t\t\t\tif (type === EntitySchemaPropertyType.Array || type === EntitySchemaPropertyType.Object) {\n\t\t\t\t\treturn `EXISTS (SELECT 1 FROM jsonb_array_elements(\"${prop}\") elem WHERE elem @> $${valueIndex}::jsonb)`;\n\t\t\t\t}\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\"comparisonNotSupported\",\n\t\t\t\t\t{\n\t\t\t\t\t\tcomparison: comparator.comparison,\n\t\t\t\t\t\ttype\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\t\t\tcase ComparisonOperator.NotIncludes: {\n\t\t\t\tif (type === EntitySchemaPropertyType.String) {\n\t\t\t\t\treturn `\"${prop}\" NOT ILIKE '%' || $${valueIndex} || '%'`;\n\t\t\t\t}\n\t\t\t\tif (type === EntitySchemaPropertyType.Array || type === EntitySchemaPropertyType.Object) {\n\t\t\t\t\treturn `NOT EXISTS (SELECT 1 FROM jsonb_array_elements(\"${prop}\") elem WHERE elem @> $${valueIndex}::jsonb)`;\n\t\t\t\t}\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\"comparisonNotSupported\",\n\t\t\t\t\t{\n\t\t\t\t\t\tcomparison: comparator.comparison,\n\t\t\t\t\t\ttype\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\t\t\tdefault:\n\t\t\t\tthrow new GeneralError(\n\t\t\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\t\"comparisonNotSupported\",\n\t\t\t\t\t{\n\t\t\t\t\t\tcomparison: comparator.comparison\n\t\t\t\t\t}\n\t\t\t\t);\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 (type === EntitySchemaPropertyType.String) {\n\t\t\treturn String(value);\n\t\t} else if (type === EntitySchemaPropertyType.Number) {\n\t\t\treturn Number(value);\n\t\t} else if (type === EntitySchemaPropertyType.Boolean) {\n\t\t\treturn Boolean(value);\n\t\t} else if (\n\t\t\ttype === EntitySchemaPropertyType.Object ||\n\t\t\ttype === EntitySchemaPropertyType.Array\n\t\t) {\n\t\t\treturn value;\n\t\t}\n\t\treturn value;\n\t}\n\n\t/**\n\t * Map the framework conditional operators to those in MySQL.\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(PostgreSqlEntityStorageConnector.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 * @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 * Map entity schema properties to SQL properties.\n\t * @param entitySchema The schema of the entity.\n\t * @returns The SQL properties as a string.\n\t * @throws GeneralError if the entity properties do not exist.\n\t */\n\tprivate mapPostgreSqlProperties(entitySchema: IEntitySchema<T>): string {\n\t\tconst sqlTypeMap: { [key in EntitySchemaPropertyType]: string } = {\n\t\t\t[EntitySchemaPropertyType.String]: \"TEXT\",\n\t\t\t[EntitySchemaPropertyType.Number]: \"REAL\",\n\t\t\t[EntitySchemaPropertyType.Integer]: \"INTEGER\",\n\t\t\t[EntitySchemaPropertyType.Object]: \"JSONB\",\n\t\t\t[EntitySchemaPropertyType.Array]: \"JSONB\",\n\t\t\t[EntitySchemaPropertyType.Boolean]: \"BOOLEAN\"\n\t\t};\n\n\t\tif (!entitySchema.properties) {\n\t\t\tthrow new GeneralError(\n\t\t\t\tPostgreSqlEntityStorageConnector.CLASS_NAME,\n\t\t\t\t\"entitySchemaPropertiesUndefined\"\n\t\t\t);\n\t\t}\n\n\t\tconst primaryKeys: string[] = [];\n\n\t\tconst props: IEntitySchemaProperty<T>[] = [...entitySchema.properties];\n\n\t\tprops.unshift({\n\t\t\tproperty: PostgreSqlEntityStorageConnector._PARTITION_KEY as keyof T,\n\t\t\ttype: EntitySchemaPropertyType.String,\n\t\t\toptional: false,\n\t\t\tisPrimary: true\n\t\t});\n\n\t\tconst columnDefinitions = props\n\t\t\t.map(prop => {\n\t\t\t\tlet sqlType = sqlTypeMap[prop.type] || \"TEXT\";\n\t\t\t\tif (prop.format) {\n\t\t\t\t\tswitch (prop.type) {\n\t\t\t\t\t\tcase EntitySchemaPropertyType.String:\n\t\t\t\t\t\t\tswitch (prop.format) {\n\t\t\t\t\t\t\t\tcase \"uuid\":\n\t\t\t\t\t\t\t\t\tsqlType = \"UUID\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase EntitySchemaPropertyType.Number:\n\t\t\t\t\t\t\tswitch (prop.format) {\n\t\t\t\t\t\t\t\tcase \"float\":\n\t\t\t\t\t\t\t\t\tsqlType = \"REAL\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\tcase \"double\":\n\t\t\t\t\t\t\t\t\tsqlType = \"DOUBLE PRECISION\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tcase EntitySchemaPropertyType.Integer:\n\t\t\t\t\t\t\tswitch (prop.format) {\n\t\t\t\t\t\t\t\tcase \"int8\":\n\t\t\t\t\t\t\t\tcase \"uint8\":\n\t\t\t\t\t\t\t\t\tsqlType = \"SMALLINT\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\tcase \"int16\":\n\t\t\t\t\t\t\t\t\tsqlType = \"SMALLINT\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\tcase \"uint16\":\n\t\t\t\t\t\t\t\tcase \"int32\":\n\t\t\t\t\t\t\t\t\tsqlType = \"INTEGER\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\tcase \"uint32\":\n\t\t\t\t\t\t\t\tcase \"int64\":\n\t\t\t\t\t\t\t\tcase \"uint64\":\n\t\t\t\t\t\t\t\t\tsqlType = \"BIGINT\";\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst columnName = String(prop.property);\n\t\t\t\tconst nullable = prop.optional ? \" NULL\" : \" NOT NULL\";\n\n\t\t\t\tif (prop.isPrimary) {\n\t\t\t\t\tprimaryKeys.push(columnName);\n\t\t\t\t}\n\n\t\t\t\treturn `\"${columnName}\" ${sqlType}${nullable}`;\n\t\t\t})\n\t\t\t.join(\", \");\n\n\t\tconst primaryKeyDefinition =\n\t\t\tprimaryKeys.length > 0 ? `, PRIMARY KEY (\"${primaryKeys.join('\", \"')}\")` : \"\";\n\t\treturn columnDefinitions + primaryKeyDefinition;\n\t}\n}\n"]}
@@ -1,11 +1,12 @@
1
+ import { type IContextIds } from "@twin.org/context";
1
2
  import { type IHealth } from "@twin.org/core";
2
3
  import { type EntityCondition, type IEntitySchema, SortDirection } from "@twin.org/entity";
3
- import type { IEntityStorageConnector } from "@twin.org/entity-storage-models";
4
+ import { type IEntityStorageMigrationConnector, type IMigrationOptions } from "@twin.org/entity-storage-models";
4
5
  import type { IPostgreSqlEntityStorageConnectorConstructorOptions } from "./models/IPostgreSqlEntityStorageConnectorConstructorOptions.js";
5
6
  /**
6
7
  * Class for performing entity storage operations using ql.
7
8
  */
8
- export declare class PostgreSqlEntityStorageConnector<T = unknown> implements IEntityStorageConnector<T> {
9
+ export declare class PostgreSqlEntityStorageConnector<T = unknown> implements IEntityStorageMigrationConnector<T> {
9
10
  /**
10
11
  * Runtime name for the class.
11
12
  */
@@ -95,6 +96,32 @@ export declare class PostgreSqlEntityStorageConnector<T = unknown> implements IE
95
96
  * @returns True if the teardown process was successful.
96
97
  */
97
98
  teardown(nodeLoggingComponentType?: string): Promise<boolean>;
99
+ /**
100
+ * Get all the distinct partition context ids from the storage.
101
+ * @returns An array of context id objects, one per unique partition.
102
+ */
103
+ getPartitionContextIds(): Promise<IContextIds[]>;
104
+ /**
105
+ * Create a new target connector for the migration.
106
+ * @param entitySchemaName The entity schema name to use for the target connector.
107
+ * @returns A new connector configured with a migration table name.
108
+ */
109
+ createTargetConnector<U>(entitySchemaName: string): Promise<PostgreSqlEntityStorageConnector<U>>;
110
+ /**
111
+ * Finalize the migration by renaming the migration table to the original table name.
112
+ * @param targetConnector The connector pointing to the migration table.
113
+ * @param options The optional migration options.
114
+ * @param loggingComponentType The node logging component type.
115
+ * @returns A connector pointing to the final (renamed) table.
116
+ */
117
+ finalizeMigration<U>(targetConnector: PostgreSqlEntityStorageConnector<U>, options?: IMigrationOptions<T, U>, loggingComponentType?: string): Promise<PostgreSqlEntityStorageConnector<U>>;
118
+ /**
119
+ * Clean up the migration by tearing down the migration table.
120
+ * @param targetConnector The connector pointing to the migration table.
121
+ * @param options The optional migration options.
122
+ * @param loggingComponentType The node logging component type.
123
+ */
124
+ cleanupMigration<U>(targetConnector?: PostgreSqlEntityStorageConnector<U>, options?: IMigrationOptions<T, U>, loggingComponentType?: string): Promise<void>;
98
125
  /**
99
126
  * Find all the entities which match the conditions.
100
127
  * @param conditions The conditions to match for the entities.
@@ -114,9 +141,10 @@ export declare class PostgreSqlEntityStorageConnector<T = unknown> implements IE
114
141
  }>;
115
142
  /**
116
143
  * Count all the entities which match the conditions.
144
+ * @param conditions The optional conditions to match for the entities.
117
145
  * @returns The total count of entities in the storage.
118
146
  */
119
- count(): Promise<number>;
147
+ count(conditions?: EntityCondition<T>): Promise<number>;
120
148
  /**
121
149
  * Map entity schema properties to SQL properties.
122
150
  * @param entitySchema The schema of the entity.
package/docs/changelog.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.0.3-next.14](https://github.com/iotaledger/twin-entity-storage/compare/entity-storage-connector-postgresql-v0.0.3-next.13...entity-storage-connector-postgresql-v0.0.3-next.14) (2026-05-19)
4
+
5
+
6
+ ### Features
7
+
8
+ * adding schema migration functionality to all the connectors ([#85](https://github.com/iotaledger/twin-entity-storage/issues/85)) ([fd1555a](https://github.com/iotaledger/twin-entity-storage/commit/fd1555a34380158214a577586dafae821e72a578))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/entity-storage-models bumped from 0.0.3-next.13 to 0.0.3-next.14
16
+ * devDependencies
17
+ * @twin.org/entity-storage-connector-memory bumped from 0.0.3-next.13 to 0.0.3-next.14
18
+
3
19
  ## [0.0.3-next.13](https://github.com/iotaledger/twin-entity-storage/compare/entity-storage-connector-postgresql-v0.0.3-next.12...entity-storage-connector-postgresql-v0.0.3-next.13) (2026-05-13)
4
20
 
5
21
 
@@ -10,7 +10,7 @@ Class for performing entity storage operations using ql.
10
10
 
11
11
  ## Implements
12
12
 
13
- - `IEntityStorageConnector`\<`T`\>
13
+ - `IEntityStorageMigrationConnector`\<`T`\>
14
14
 
15
15
  ## Constructors
16
16
 
@@ -64,7 +64,7 @@ A promise that resolves to a boolean indicating success.
64
64
 
65
65
  #### Implementation of
66
66
 
67
- `IEntityStorageConnector.bootstrap`
67
+ `IEntityStorageMigrationConnector.bootstrap`
68
68
 
69
69
  ***
70
70
 
@@ -82,7 +82,7 @@ The class name of the component.
82
82
 
83
83
  #### Implementation of
84
84
 
85
- `IEntityStorageConnector.className`
85
+ `IEntityStorageMigrationConnector.className`
86
86
 
87
87
  ***
88
88
 
@@ -100,7 +100,7 @@ The health of the component.
100
100
 
101
101
  #### Implementation of
102
102
 
103
- `IEntityStorageConnector.health`
103
+ `IEntityStorageMigrationConnector.health`
104
104
 
105
105
  ***
106
106
 
@@ -118,7 +118,7 @@ Nothing.
118
118
 
119
119
  #### Implementation of
120
120
 
121
- `IEntityStorageConnector.stop`
121
+ `IEntityStorageMigrationConnector.stop`
122
122
 
123
123
  ***
124
124
 
@@ -136,7 +136,7 @@ The schema for the entities.
136
136
 
137
137
  #### Implementation of
138
138
 
139
- `IEntityStorageConnector.getSchema`
139
+ `IEntityStorageMigrationConnector.getSchema`
140
140
 
141
141
  ***
142
142
 
@@ -174,7 +174,7 @@ The object if it can be found or undefined.
174
174
 
175
175
  #### Implementation of
176
176
 
177
- `IEntityStorageConnector.get`
177
+ `IEntityStorageMigrationConnector.get`
178
178
 
179
179
  ***
180
180
 
@@ -206,7 +206,7 @@ The id of the entity.
206
206
 
207
207
  #### Implementation of
208
208
 
209
- `IEntityStorageConnector.set`
209
+ `IEntityStorageMigrationConnector.set`
210
210
 
211
211
  ***
212
212
 
@@ -232,7 +232,7 @@ Nothing.
232
232
 
233
233
  #### Implementation of
234
234
 
235
- `IEntityStorageConnector.setBatch`
235
+ `IEntityStorageMigrationConnector.setBatch`
236
236
 
237
237
  ***
238
238
 
@@ -250,7 +250,7 @@ Nothing.
250
250
 
251
251
  #### Implementation of
252
252
 
253
- `IEntityStorageConnector.empty`
253
+ `IEntityStorageMigrationConnector.empty`
254
254
 
255
255
  ***
256
256
 
@@ -282,7 +282,7 @@ Nothing.
282
282
 
283
283
  #### Implementation of
284
284
 
285
- `IEntityStorageConnector.remove`
285
+ `IEntityStorageMigrationConnector.remove`
286
286
 
287
287
  ***
288
288
 
@@ -308,7 +308,7 @@ Nothing.
308
308
 
309
309
  #### Implementation of
310
310
 
311
- `IEntityStorageConnector.removeBatch`
311
+ `IEntityStorageMigrationConnector.removeBatch`
312
312
 
313
313
  ***
314
314
 
@@ -334,7 +334,143 @@ True if the teardown process was successful.
334
334
 
335
335
  #### Implementation of
336
336
 
337
- `IEntityStorageConnector.teardown`
337
+ `IEntityStorageMigrationConnector.teardown`
338
+
339
+ ***
340
+
341
+ ### getPartitionContextIds() {#getpartitioncontextids}
342
+
343
+ > **getPartitionContextIds**(): `Promise`\<`IContextIds`[]\>
344
+
345
+ Get all the distinct partition context ids from the storage.
346
+
347
+ #### Returns
348
+
349
+ `Promise`\<`IContextIds`[]\>
350
+
351
+ An array of context id objects, one per unique partition.
352
+
353
+ #### Implementation of
354
+
355
+ `IEntityStorageMigrationConnector.getPartitionContextIds`
356
+
357
+ ***
358
+
359
+ ### createTargetConnector() {#createtargetconnector}
360
+
361
+ > **createTargetConnector**\<`U`\>(`entitySchemaName`): `Promise`\<`PostgreSqlEntityStorageConnector`\<`U`\>\>
362
+
363
+ Create a new target connector for the migration.
364
+
365
+ #### Type Parameters
366
+
367
+ ##### U
368
+
369
+ `U`
370
+
371
+ #### Parameters
372
+
373
+ ##### entitySchemaName
374
+
375
+ `string`
376
+
377
+ The entity schema name to use for the target connector.
378
+
379
+ #### Returns
380
+
381
+ `Promise`\<`PostgreSqlEntityStorageConnector`\<`U`\>\>
382
+
383
+ A new connector configured with a migration table name.
384
+
385
+ #### Implementation of
386
+
387
+ `IEntityStorageMigrationConnector.createTargetConnector`
388
+
389
+ ***
390
+
391
+ ### finalizeMigration() {#finalizemigration}
392
+
393
+ > **finalizeMigration**\<`U`\>(`targetConnector`, `options?`, `loggingComponentType?`): `Promise`\<`PostgreSqlEntityStorageConnector`\<`U`\>\>
394
+
395
+ Finalize the migration by renaming the migration table to the original table name.
396
+
397
+ #### Type Parameters
398
+
399
+ ##### U
400
+
401
+ `U`
402
+
403
+ #### Parameters
404
+
405
+ ##### targetConnector
406
+
407
+ `PostgreSqlEntityStorageConnector`\<`U`\>
408
+
409
+ The connector pointing to the migration table.
410
+
411
+ ##### options?
412
+
413
+ `IMigrationOptions`\<`T`, `U`\>
414
+
415
+ The optional migration options.
416
+
417
+ ##### loggingComponentType?
418
+
419
+ `string`
420
+
421
+ The node logging component type.
422
+
423
+ #### Returns
424
+
425
+ `Promise`\<`PostgreSqlEntityStorageConnector`\<`U`\>\>
426
+
427
+ A connector pointing to the final (renamed) table.
428
+
429
+ #### Implementation of
430
+
431
+ `IEntityStorageMigrationConnector.finalizeMigration`
432
+
433
+ ***
434
+
435
+ ### cleanupMigration() {#cleanupmigration}
436
+
437
+ > **cleanupMigration**\<`U`\>(`targetConnector?`, `options?`, `loggingComponentType?`): `Promise`\<`void`\>
438
+
439
+ Clean up the migration by tearing down the migration table.
440
+
441
+ #### Type Parameters
442
+
443
+ ##### U
444
+
445
+ `U`
446
+
447
+ #### Parameters
448
+
449
+ ##### targetConnector?
450
+
451
+ `PostgreSqlEntityStorageConnector`\<`U`\>
452
+
453
+ The connector pointing to the migration table.
454
+
455
+ ##### options?
456
+
457
+ `IMigrationOptions`\<`T`, `U`\>
458
+
459
+ The optional migration options.
460
+
461
+ ##### loggingComponentType?
462
+
463
+ `string`
464
+
465
+ The node logging component type.
466
+
467
+ #### Returns
468
+
469
+ `Promise`\<`void`\>
470
+
471
+ #### Implementation of
472
+
473
+ `IEntityStorageMigrationConnector.cleanupMigration`
338
474
 
339
475
  ***
340
476
 
@@ -385,16 +521,24 @@ and a cursor which can be used to request more entities.
385
521
 
386
522
  #### Implementation of
387
523
 
388
- `IEntityStorageConnector.query`
524
+ `IEntityStorageMigrationConnector.query`
389
525
 
390
526
  ***
391
527
 
392
528
  ### count() {#count}
393
529
 
394
- > **count**(): `Promise`\<`number`\>
530
+ > **count**(`conditions?`): `Promise`\<`number`\>
395
531
 
396
532
  Count all the entities which match the conditions.
397
533
 
534
+ #### Parameters
535
+
536
+ ##### conditions?
537
+
538
+ `EntityCondition`\<`T`\>
539
+
540
+ The optional conditions to match for the entities.
541
+
398
542
  #### Returns
399
543
 
400
544
  `Promise`\<`number`\>
@@ -403,4 +547,4 @@ The total count of entities in the storage.
403
547
 
404
548
  #### Implementation of
405
549
 
406
- `IEntityStorageConnector.count`
550
+ `IEntityStorageMigrationConnector.count`
package/locales/en.json CHANGED
@@ -23,7 +23,9 @@
23
23
  "countFailed": "Unable to count entities",
24
24
  "emptyFailed": "Unable to empty entity storage",
25
25
  "removeBatchFailed": "Unable to remove batch of entities",
26
- "teardownFailed": "Unable to teardown entity storage"
26
+ "teardownFailed": "Unable to teardown entity storage",
27
+ "getPartitionContextIdsFailed": "Unable to get partition context ids",
28
+ "finalizeMigrationFailedBootstrap": "Finalizing migration failed during bootstrap phase"
27
29
  }
28
30
  },
29
31
  "health": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/entity-storage-connector-postgresql",
3
- "version": "0.0.3-next.13",
3
+ "version": "0.0.3-next.14",
4
4
  "description": "PostgreSQL connector for relational persistence and advanced SQL features.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,7 +17,7 @@
17
17
  "@twin.org/context": "next",
18
18
  "@twin.org/core": "next",
19
19
  "@twin.org/entity": "next",
20
- "@twin.org/entity-storage-models": "0.0.3-next.13",
20
+ "@twin.org/entity-storage-models": "0.0.3-next.14",
21
21
  "@twin.org/logging-models": "next",
22
22
  "@twin.org/nameof": "next",
23
23
  "postgres": "3.4.9"