@twin.org/entity-storage-connector-postgresql 0.0.3-next.2 → 0.0.3-next.3
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.
|
@@ -594,9 +594,15 @@ export class PostgreSqlEntityStorageConnector {
|
|
|
594
594
|
return `("${comparator.property.split(".")[0]}"::jsonb ${jsonPath}) = $${valueIndex}`;
|
|
595
595
|
}
|
|
596
596
|
else if (comparator.comparison === ComparisonOperator.Equals) {
|
|
597
|
+
if (Is.object(comparator.value) || Is.array(comparator.value)) {
|
|
598
|
+
return `"${prop}" = $${valueIndex}::jsonb`;
|
|
599
|
+
}
|
|
597
600
|
return `"${prop}" = $${valueIndex}`;
|
|
598
601
|
}
|
|
599
602
|
else if (comparator.comparison === ComparisonOperator.NotEquals) {
|
|
603
|
+
if (Is.object(comparator.value) || Is.array(comparator.value)) {
|
|
604
|
+
return `"${prop}" != $${valueIndex}::jsonb`;
|
|
605
|
+
}
|
|
600
606
|
return `"${prop}" <> $${valueIndex}`;
|
|
601
607
|
}
|
|
602
608
|
else if (comparator.comparison === ComparisonOperator.GreaterThan) {
|
|
@@ -626,21 +632,19 @@ export class PostgreSqlEntityStorageConnector {
|
|
|
626
632
|
* @internal
|
|
627
633
|
*/
|
|
628
634
|
propertyToDbValue(value, type) {
|
|
629
|
-
if (type ===
|
|
635
|
+
if (type === EntitySchemaPropertyType.String) {
|
|
630
636
|
return String(value);
|
|
631
637
|
}
|
|
632
|
-
else if (type ===
|
|
638
|
+
else if (type === EntitySchemaPropertyType.Number) {
|
|
633
639
|
return Number(value);
|
|
634
640
|
}
|
|
635
|
-
else if (type ===
|
|
641
|
+
else if (type === EntitySchemaPropertyType.Boolean) {
|
|
636
642
|
return Boolean(value);
|
|
637
643
|
}
|
|
638
|
-
else if (type ===
|
|
644
|
+
else if (type === EntitySchemaPropertyType.Object ||
|
|
645
|
+
type === EntitySchemaPropertyType.Array) {
|
|
639
646
|
return value;
|
|
640
647
|
}
|
|
641
|
-
if (Is.object(value)) {
|
|
642
|
-
return JSON.stringify(value);
|
|
643
|
-
}
|
|
644
648
|
return value;
|
|
645
649
|
}
|
|
646
650
|
/**
|
|
@@ -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,EAAE,EACF,YAAY,EACZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,kBAAkB,EAElB,mBAAmB,EACnB,kBAAkB,EAClB,wBAAwB,EAIxB,eAAe,EACf,aAAa,EACb,MAAM,kBAAkB,CAAC;AAI1B,OAAO,QAAkC,MAAM,UAAU,CAAC;AAI1D;;GAEG;AACH,MAAM,OAAO,gCAAgC;IAC5C;;OAEG;IACI,MAAM,CAAU,UAAU,sCAAsD;IAEvF;;;OAGG;IACK,MAAM,CAAU,cAAc,GAAW,EAAE,CAAC;IAEpD;;;OAGG;IACK,MAAM,CAAU,cAAc,GAAW,aAAa,CAAC;IAE/D;;;OAGG;IACK,MAAM,CAAU,oBAAoB,GAAW,MAAM,CAAC;IAE9D;;;OAGG;IACc,aAAa,CAAmB;IAEjD;;;OAGG;IACc,oBAAoB,CAAY;IAEjD;;;OAGG;IACc,mBAAmB,CAA2B;IAE/D;;;OAGG;IACc,OAAO,CAA0C;IAElE;;;OAGG;IACK,WAAW,CAAgB;IAEnC;;;OAGG;IACH,YAAY,OAA4D;QACvE,MAAM,CAAC,MAAM,CAAC,gCAAgC,CAAC,UAAU,aAAmB,OAAO,CAAC,CAAC;QACrF,MAAM,CAAC,WAAW,CACjB,gCAAgC,CAAC,UAAU,0BAE3C,OAAO,CAAC,YAAY,CACpB,CAAC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Content":["// Copyright 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\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 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 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\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 * 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 * 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 * Drop the table.\n\t * @returns Nothing.\n\t */\n\tpublic async tableDrop(): Promise<void> {\n\t\ttry {\n\t\t\tconst tableExists = await this.tableExists();\n\t\t\tif (!tableExists) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tawait dbConnection.unsafe(`DROP TABLE \"${this._config.tableName}\";`);\n\n\t\t\tawait this.waitForTableNotExists();\n\t\t} catch {\n\t\t\t// Ignore errors\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\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 jsonPath = comparator.property\n\t\t\t\t.split(\".\")\n\t\t\t\t.slice(1)\n\t\t\t\t.map((p, i, arr) => (i === arr.length - 1 ? `->> '${p}'` : `-> '${p}'`))\n\t\t\t\t.join(\"\");\n\t\t\treturn `(\"${comparator.property.split(\".\")[0]}\"::jsonb ${jsonPath}) = $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.Equals) {\n\t\t\treturn `\"${prop}\" = $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.NotEquals) {\n\t\t\treturn `\"${prop}\" <> $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.GreaterThan) {\n\t\t\treturn `\"${prop}\" > $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.LessThan) {\n\t\t\treturn `\"${prop}\" < $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.GreaterThanOrEqual) {\n\t\t\treturn `\"${prop}\" >= $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.LessThanOrEqual) {\n\t\t\treturn `\"${prop}\" <= $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.Includes) {\n\t\t\treturn `EXISTS (SELECT 1 FROM jsonb_array_elements(\"${prop}\") elem WHERE elem @> $${valueIndex}::jsonb)`;\n\t\t}\n\n\t\tthrow new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, \"comparisonNotSupported\", {\n\t\t\tcomparison: comparator.comparison\n\t\t});\n\t}\n\n\t/**\n\t * Format a value to insert into DB.\n\t * @param value The value to format.\n\t * @param type The type for the property.\n\t * @returns The value after conversion.\n\t * @internal\n\t */\n\tprivate propertyToDbValue(value: unknown, type?: EntitySchemaPropertyType): unknown {\n\t\tif (type === \"string\") {\n\t\t\treturn String(value);\n\t\t} else if (type === \"number\") {\n\t\t\treturn Number(value);\n\t\t} else if (type === \"boolean\") {\n\t\t\treturn Boolean(value);\n\t\t} else if (type === \"array\") {\n\t\t\treturn value;\n\t\t}\n\n\t\tif (Is.object(value)) {\n\t\t\treturn JSON.stringify(value);\n\t\t}\n\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\tconst sqlType = sqlTypeMap[prop.type] || \"TEXT\";\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,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EACN,SAAS,EACT,MAAM,EACN,gBAAgB,EAChB,YAAY,EACZ,MAAM,EACN,EAAE,EACF,YAAY,EACZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,kBAAkB,EAElB,mBAAmB,EACnB,kBAAkB,EAClB,wBAAwB,EAIxB,eA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ACjC,CAAC;QACH,CAAC;QAED,MAAM,WAAW,GAAa,EAAE,CAAC;QAEjC,MAAM,KAAK,GAA+B,CAAC,GAAG,YAAY,CAAC,UAAU,CAAC,CAAC;QAEvE,KAAK,CAAC,OAAO,CAAC;YACb,QAAQ,EAAE,gCAAgC,CAAC,cAAyB;YACpE,IAAI,EAAE,wBAAwB,CAAC,MAAM;YACrC,QAAQ,EAAE,KAAK;YACf,SAAS,EAAE,IAAI;SACf,CAAC,CAAC;QAEH,MAAM,iBAAiB,GAAG,KAAK;aAC7B,GAAG,CAAC,IAAI,CAAC,EAAE;YACX,MAAM,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC;YAChD,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 } from \"@twin.org/context\";\nimport {\n\tBaseError,\n\tCoerce,\n\tComponentFactory,\n\tGeneralError,\n\tGuards,\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 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 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\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 * 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 * 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 * Drop the table.\n\t * @returns Nothing.\n\t */\n\tpublic async tableDrop(): Promise<void> {\n\t\ttry {\n\t\t\tconst tableExists = await this.tableExists();\n\t\t\tif (!tableExists) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst dbConnection = await this.createConnection();\n\t\t\tawait dbConnection.unsafe(`DROP TABLE \"${this._config.tableName}\";`);\n\n\t\t\tawait this.waitForTableNotExists();\n\t\t} catch {\n\t\t\t// Ignore errors\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\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 jsonPath = comparator.property\n\t\t\t\t.split(\".\")\n\t\t\t\t.slice(1)\n\t\t\t\t.map((p, i, arr) => (i === arr.length - 1 ? `->> '${p}'` : `-> '${p}'`))\n\t\t\t\t.join(\"\");\n\t\t\treturn `(\"${comparator.property.split(\".\")[0]}\"::jsonb ${jsonPath}) = $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.Equals) {\n\t\t\tif (Is.object(comparator.value) || Is.array(comparator.value)) {\n\t\t\t\treturn `\"${prop}\" = $${valueIndex}::jsonb`;\n\t\t\t}\n\t\t\treturn `\"${prop}\" = $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.NotEquals) {\n\t\t\tif (Is.object(comparator.value) || Is.array(comparator.value)) {\n\t\t\t\treturn `\"${prop}\" != $${valueIndex}::jsonb`;\n\t\t\t}\n\t\t\treturn `\"${prop}\" <> $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.GreaterThan) {\n\t\t\treturn `\"${prop}\" > $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.LessThan) {\n\t\t\treturn `\"${prop}\" < $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.GreaterThanOrEqual) {\n\t\t\treturn `\"${prop}\" >= $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.LessThanOrEqual) {\n\t\t\treturn `\"${prop}\" <= $${valueIndex}`;\n\t\t} else if (comparator.comparison === ComparisonOperator.Includes) {\n\t\t\treturn `EXISTS (SELECT 1 FROM jsonb_array_elements(\"${prop}\") elem WHERE elem @> $${valueIndex}::jsonb)`;\n\t\t}\n\n\t\tthrow new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, \"comparisonNotSupported\", {\n\t\t\tcomparison: comparator.comparison\n\t\t});\n\t}\n\n\t/**\n\t * Format a value to insert into DB.\n\t * @param value The value to format.\n\t * @param type The type for the property.\n\t * @returns The value after conversion.\n\t * @internal\n\t */\n\tprivate propertyToDbValue(value: unknown, type?: EntitySchemaPropertyType): unknown {\n\t\tif (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\tconst sqlType = sqlTypeMap[prop.type] || \"TEXT\";\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"]}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @twin.org/entity-storage-connector-postgresql - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.3-next.3](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.3-next.2...entity-storage-connector-postgresql-v0.0.3-next.3) (2025-11-26)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add support for object comparison conditions ([eb505a1](https://github.com/twinfoundation/entity-storage/commit/eb505a17a3642e95c4e3cf137a77a0a8fb388c97))
|
|
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.2 to 0.0.3-next.3
|
|
16
|
+
* devDependencies
|
|
17
|
+
* @twin.org/entity-storage-connector-memory bumped from 0.0.3-next.2 to 0.0.3-next.3
|
|
18
|
+
|
|
3
19
|
## [0.0.3-next.2](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.3-next.1...entity-storage-connector-postgresql-v0.0.3-next.2) (2025-11-13)
|
|
4
20
|
|
|
5
21
|
|
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.
|
|
3
|
+
"version": "0.0.3-next.3",
|
|
4
4
|
"description": "Entity Storage connector implementation using PostgreSql storage",
|
|
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.
|
|
20
|
+
"@twin.org/entity-storage-models": "0.0.3-next.3",
|
|
21
21
|
"@twin.org/logging-models": "next",
|
|
22
22
|
"@twin.org/nameof": "next",
|
|
23
23
|
"postgres": "3.4.7"
|