@twin.org/entity-storage-connector-postgresql 0.0.2-next.1 → 0.0.2-next.10
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.
- package/README.md +1 -1
- package/dist/cjs/index.cjs +48 -50
- package/dist/esm/index.mjs +49 -51
- package/dist/types/models/IPostgreSqlEntityStorageConnectorConstructorOptions.d.ts +2 -2
- package/dist/types/postgreSqlEntityStorageConnector.d.ts +6 -6
- package/docs/changelog.md +144 -0
- package/docs/reference/classes/PostgreSqlEntityStorageConnector.md +7 -11
- package/docs/reference/interfaces/IPostgreSqlEntityStorageConnectorConstructorOptions.md +3 -3
- package/locales/en.json +2 -2
- package/package.json +22 -3
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm install @twin.org/entity-storage-connector-postgresql
|
|
|
13
13
|
The tests developed are functional tests and need an instance of PostgreSql up and running. To run PostgreSql locally:
|
|
14
14
|
|
|
15
15
|
```sh
|
|
16
|
-
docker run -p
|
|
16
|
+
docker run -p 5444:5432 --name twin-entity-storage-postgresql --hostname postgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -d postgres
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
Afterwards you can run the tests as follows:
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
var core = require('@twin.org/core');
|
|
4
4
|
var entity = require('@twin.org/entity');
|
|
5
|
-
var loggingModels = require('@twin.org/logging-models');
|
|
6
5
|
var postgres = require('postgres');
|
|
7
6
|
|
|
8
7
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -12,14 +11,14 @@ var postgres = require('postgres');
|
|
|
12
11
|
*/
|
|
13
12
|
class PostgreSqlEntityStorageConnector {
|
|
14
13
|
/**
|
|
15
|
-
*
|
|
16
|
-
* @internal
|
|
14
|
+
* Runtime name for the class.
|
|
17
15
|
*/
|
|
18
|
-
static
|
|
16
|
+
static CLASS_NAME = "PostgreSqlEntityStorageConnector";
|
|
19
17
|
/**
|
|
20
|
-
*
|
|
18
|
+
* Limit the number of entities when finding.
|
|
19
|
+
* @internal
|
|
21
20
|
*/
|
|
22
|
-
|
|
21
|
+
static _DEFAULT_LIMIT = 40;
|
|
23
22
|
/**
|
|
24
23
|
* The schema for the entity.
|
|
25
24
|
* @internal
|
|
@@ -40,33 +39,33 @@ class PostgreSqlEntityStorageConnector {
|
|
|
40
39
|
* @param options The options for the connector.
|
|
41
40
|
*/
|
|
42
41
|
constructor(options) {
|
|
43
|
-
core.Guards.object(
|
|
44
|
-
core.Guards.stringValue(
|
|
45
|
-
core.Guards.object(
|
|
46
|
-
core.Guards.stringValue(
|
|
47
|
-
core.Guards.stringValue(
|
|
48
|
-
core.Guards.stringValue(
|
|
49
|
-
core.Guards.stringValue(
|
|
50
|
-
core.Guards.stringValue(
|
|
42
|
+
core.Guards.object(PostgreSqlEntityStorageConnector.CLASS_NAME, "options", options);
|
|
43
|
+
core.Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.entitySchema", options.entitySchema);
|
|
44
|
+
core.Guards.object(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config", options.config);
|
|
45
|
+
core.Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.host", options.config.host);
|
|
46
|
+
core.Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.user", options.config.user);
|
|
47
|
+
core.Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.password", options.config.password);
|
|
48
|
+
core.Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.database", options.config.database);
|
|
49
|
+
core.Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.tableName", options.config.tableName);
|
|
51
50
|
this._entitySchema = entity.EntitySchemaFactory.get(options.entitySchema);
|
|
52
51
|
this._config = options.config;
|
|
53
52
|
}
|
|
54
53
|
/**
|
|
55
54
|
* Initialize the PostgreSql environment.
|
|
56
|
-
* @param
|
|
55
|
+
* @param nodeLoggingComponentType Optional type of the logging component.
|
|
57
56
|
* @returns A promise that resolves to a boolean indicating success.
|
|
58
57
|
*/
|
|
59
|
-
async bootstrap(
|
|
60
|
-
const nodeLogging =
|
|
58
|
+
async bootstrap(nodeLoggingComponentType) {
|
|
59
|
+
const nodeLogging = core.ComponentFactory.getIfExists(nodeLoggingComponentType);
|
|
61
60
|
try {
|
|
62
61
|
const dbConnection = await this.createConnection();
|
|
63
62
|
await nodeLogging?.log({
|
|
64
63
|
level: "info",
|
|
65
|
-
source:
|
|
64
|
+
source: PostgreSqlEntityStorageConnector.CLASS_NAME,
|
|
66
65
|
ts: Date.now(),
|
|
67
66
|
message: "databaseCreating",
|
|
68
67
|
data: {
|
|
69
|
-
|
|
68
|
+
databaseName: this._config.database
|
|
70
69
|
}
|
|
71
70
|
});
|
|
72
71
|
const res = await dbConnection.unsafe(`SELECT datname FROM pg_catalog.pg_database WHERE datname = '${this._config.database}'`);
|
|
@@ -75,11 +74,11 @@ class PostgreSqlEntityStorageConnector {
|
|
|
75
74
|
}
|
|
76
75
|
await nodeLogging?.log({
|
|
77
76
|
level: "info",
|
|
78
|
-
source:
|
|
77
|
+
source: PostgreSqlEntityStorageConnector.CLASS_NAME,
|
|
79
78
|
ts: Date.now(),
|
|
80
79
|
message: "databaseExists",
|
|
81
80
|
data: {
|
|
82
|
-
|
|
81
|
+
databaseName: this._config.database
|
|
83
82
|
}
|
|
84
83
|
});
|
|
85
84
|
const tableExistsQuery = `SELECT to_regclass('${this._config.tableName}')`;
|
|
@@ -90,28 +89,25 @@ class PostgreSqlEntityStorageConnector {
|
|
|
90
89
|
}
|
|
91
90
|
await nodeLogging?.log({
|
|
92
91
|
level: "info",
|
|
93
|
-
source:
|
|
92
|
+
source: PostgreSqlEntityStorageConnector.CLASS_NAME,
|
|
94
93
|
ts: Date.now(),
|
|
95
94
|
message: "tableExists",
|
|
96
95
|
data: {
|
|
97
|
-
|
|
96
|
+
tableName: this._config.tableName
|
|
98
97
|
}
|
|
99
98
|
});
|
|
100
99
|
}
|
|
101
100
|
catch (error) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}
|
|
101
|
+
await nodeLogging?.log({
|
|
102
|
+
level: "error",
|
|
103
|
+
source: PostgreSqlEntityStorageConnector.CLASS_NAME,
|
|
104
|
+
ts: Date.now(),
|
|
105
|
+
message: "databaseCreateFailed",
|
|
106
|
+
error: core.BaseError.fromError(error),
|
|
107
|
+
data: {
|
|
108
|
+
databaseName: this._config.database
|
|
109
|
+
}
|
|
110
|
+
});
|
|
115
111
|
return false;
|
|
116
112
|
}
|
|
117
113
|
return true;
|
|
@@ -131,7 +127,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
131
127
|
* @returns The object if it can be found or undefined.
|
|
132
128
|
*/
|
|
133
129
|
async get(id, secondaryIndex, conditions) {
|
|
134
|
-
core.Guards.stringValue(
|
|
130
|
+
core.Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "id", id);
|
|
135
131
|
try {
|
|
136
132
|
const dbConnection = await this.createConnection();
|
|
137
133
|
const whereClauses = [];
|
|
@@ -175,7 +171,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
175
171
|
}
|
|
176
172
|
}
|
|
177
173
|
catch (err) {
|
|
178
|
-
throw new core.GeneralError(
|
|
174
|
+
throw new core.GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "getFailed", {
|
|
179
175
|
id
|
|
180
176
|
}, err);
|
|
181
177
|
}
|
|
@@ -188,7 +184,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
188
184
|
* @returns The id of the entity.
|
|
189
185
|
*/
|
|
190
186
|
async set(entity$1, conditions) {
|
|
191
|
-
core.Guards.object(
|
|
187
|
+
core.Guards.object(PostgreSqlEntityStorageConnector.CLASS_NAME, "entity", entity$1);
|
|
192
188
|
entity.EntitySchemaHelper.validateEntity(entity$1, this.getSchema());
|
|
193
189
|
const primaryKey = entity.EntitySchemaHelper.getPrimaryKey(this.getSchema());
|
|
194
190
|
const id = entity$1[primaryKey.property];
|
|
@@ -212,7 +208,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
212
208
|
.join(", ")};`, values);
|
|
213
209
|
}
|
|
214
210
|
catch (err) {
|
|
215
|
-
throw new core.GeneralError(
|
|
211
|
+
throw new core.GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "setFailed", {
|
|
216
212
|
id
|
|
217
213
|
}, err);
|
|
218
214
|
}
|
|
@@ -224,7 +220,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
224
220
|
* @returns Nothing.
|
|
225
221
|
*/
|
|
226
222
|
async remove(id, conditions) {
|
|
227
|
-
core.Guards.stringValue(
|
|
223
|
+
core.Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "id", id);
|
|
228
224
|
try {
|
|
229
225
|
const dbConnection = await this.createConnection();
|
|
230
226
|
const itemData = await this.get(id);
|
|
@@ -243,7 +239,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
243
239
|
}
|
|
244
240
|
}
|
|
245
241
|
catch (err) {
|
|
246
|
-
throw new core.GeneralError(
|
|
242
|
+
throw new core.GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "removeFailed", {
|
|
247
243
|
id
|
|
248
244
|
}, err);
|
|
249
245
|
}
|
|
@@ -253,15 +249,15 @@ class PostgreSqlEntityStorageConnector {
|
|
|
253
249
|
* @param conditions The conditions to match for the entities.
|
|
254
250
|
* @param sortProperties The optional sort order.
|
|
255
251
|
* @param properties The optional properties to return, defaults to all.
|
|
256
|
-
* @param cursor The cursor to request the next
|
|
257
|
-
* @param
|
|
252
|
+
* @param cursor The cursor to request the next chunk of entities.
|
|
253
|
+
* @param limit The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
|
|
258
254
|
* @returns All the entities for the storage matching the conditions,
|
|
259
255
|
* and a cursor which can be used to request more entities.
|
|
260
256
|
*/
|
|
261
|
-
async query(conditions, sortProperties, properties, cursor,
|
|
257
|
+
async query(conditions, sortProperties, properties, cursor, limit) {
|
|
262
258
|
const sql = "";
|
|
263
259
|
try {
|
|
264
|
-
const returnSize =
|
|
260
|
+
const returnSize = limit ?? PostgreSqlEntityStorageConnector._DEFAULT_LIMIT;
|
|
265
261
|
let orderByClause = "";
|
|
266
262
|
if (Array.isArray(sortProperties)) {
|
|
267
263
|
const orderClauses = [];
|
|
@@ -305,7 +301,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
305
301
|
};
|
|
306
302
|
}
|
|
307
303
|
catch (err) {
|
|
308
|
-
throw new core.GeneralError(
|
|
304
|
+
throw new core.GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "queryFailed", { sql }, err);
|
|
309
305
|
}
|
|
310
306
|
}
|
|
311
307
|
/**
|
|
@@ -436,7 +432,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
436
432
|
else if (comparator.comparison === entity.ComparisonOperator.Includes) {
|
|
437
433
|
return `EXISTS (SELECT 1 FROM jsonb_array_elements("${prop}") elem WHERE elem @> $${values.length}::jsonb)`;
|
|
438
434
|
}
|
|
439
|
-
throw new core.GeneralError(
|
|
435
|
+
throw new core.GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "comparisonNotSupported", {
|
|
440
436
|
comparison: comparator.comparison
|
|
441
437
|
});
|
|
442
438
|
}
|
|
@@ -479,7 +475,9 @@ class PostgreSqlEntityStorageConnector {
|
|
|
479
475
|
else if (operator === entity.LogicalOperator.Or) {
|
|
480
476
|
return "OR";
|
|
481
477
|
}
|
|
482
|
-
throw new core.GeneralError(
|
|
478
|
+
throw new core.GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "conditionalNotSupported", {
|
|
479
|
+
operator
|
|
480
|
+
});
|
|
483
481
|
}
|
|
484
482
|
/**
|
|
485
483
|
* Verify the conditions for the entity.
|
|
@@ -505,7 +503,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
505
503
|
[entity.EntitySchemaPropertyType.Boolean]: "BOOLEAN"
|
|
506
504
|
};
|
|
507
505
|
if (!entitySchema.properties) {
|
|
508
|
-
throw new core.GeneralError(
|
|
506
|
+
throw new core.GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "entitySchemaPropertiesUndefined");
|
|
509
507
|
}
|
|
510
508
|
const primaryKeys = [];
|
|
511
509
|
const columnDefinitions = entitySchema.properties
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { Guards, BaseError, GeneralError, Is, ObjectHelper } from '@twin.org/core';
|
|
1
|
+
import { Guards, ComponentFactory, BaseError, GeneralError, Is, ObjectHelper } from '@twin.org/core';
|
|
2
2
|
import { EntitySchemaFactory, EntitySchemaHelper, EntitySchemaPropertyType, SortDirection, ComparisonOperator, LogicalOperator } from '@twin.org/entity';
|
|
3
|
-
import { LoggingConnectorFactory } from '@twin.org/logging-models';
|
|
4
3
|
import postgres from 'postgres';
|
|
5
4
|
|
|
6
5
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -10,14 +9,14 @@ import postgres from 'postgres';
|
|
|
10
9
|
*/
|
|
11
10
|
class PostgreSqlEntityStorageConnector {
|
|
12
11
|
/**
|
|
13
|
-
*
|
|
14
|
-
* @internal
|
|
12
|
+
* Runtime name for the class.
|
|
15
13
|
*/
|
|
16
|
-
static
|
|
14
|
+
static CLASS_NAME = "PostgreSqlEntityStorageConnector";
|
|
17
15
|
/**
|
|
18
|
-
*
|
|
16
|
+
* Limit the number of entities when finding.
|
|
17
|
+
* @internal
|
|
19
18
|
*/
|
|
20
|
-
|
|
19
|
+
static _DEFAULT_LIMIT = 40;
|
|
21
20
|
/**
|
|
22
21
|
* The schema for the entity.
|
|
23
22
|
* @internal
|
|
@@ -38,33 +37,33 @@ class PostgreSqlEntityStorageConnector {
|
|
|
38
37
|
* @param options The options for the connector.
|
|
39
38
|
*/
|
|
40
39
|
constructor(options) {
|
|
41
|
-
Guards.object(
|
|
42
|
-
Guards.stringValue(
|
|
43
|
-
Guards.object(
|
|
44
|
-
Guards.stringValue(
|
|
45
|
-
Guards.stringValue(
|
|
46
|
-
Guards.stringValue(
|
|
47
|
-
Guards.stringValue(
|
|
48
|
-
Guards.stringValue(
|
|
40
|
+
Guards.object(PostgreSqlEntityStorageConnector.CLASS_NAME, "options", options);
|
|
41
|
+
Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.entitySchema", options.entitySchema);
|
|
42
|
+
Guards.object(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config", options.config);
|
|
43
|
+
Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.host", options.config.host);
|
|
44
|
+
Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.user", options.config.user);
|
|
45
|
+
Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.password", options.config.password);
|
|
46
|
+
Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.database", options.config.database);
|
|
47
|
+
Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "options.config.tableName", options.config.tableName);
|
|
49
48
|
this._entitySchema = EntitySchemaFactory.get(options.entitySchema);
|
|
50
49
|
this._config = options.config;
|
|
51
50
|
}
|
|
52
51
|
/**
|
|
53
52
|
* Initialize the PostgreSql environment.
|
|
54
|
-
* @param
|
|
53
|
+
* @param nodeLoggingComponentType Optional type of the logging component.
|
|
55
54
|
* @returns A promise that resolves to a boolean indicating success.
|
|
56
55
|
*/
|
|
57
|
-
async bootstrap(
|
|
58
|
-
const nodeLogging =
|
|
56
|
+
async bootstrap(nodeLoggingComponentType) {
|
|
57
|
+
const nodeLogging = ComponentFactory.getIfExists(nodeLoggingComponentType);
|
|
59
58
|
try {
|
|
60
59
|
const dbConnection = await this.createConnection();
|
|
61
60
|
await nodeLogging?.log({
|
|
62
61
|
level: "info",
|
|
63
|
-
source:
|
|
62
|
+
source: PostgreSqlEntityStorageConnector.CLASS_NAME,
|
|
64
63
|
ts: Date.now(),
|
|
65
64
|
message: "databaseCreating",
|
|
66
65
|
data: {
|
|
67
|
-
|
|
66
|
+
databaseName: this._config.database
|
|
68
67
|
}
|
|
69
68
|
});
|
|
70
69
|
const res = await dbConnection.unsafe(`SELECT datname FROM pg_catalog.pg_database WHERE datname = '${this._config.database}'`);
|
|
@@ -73,11 +72,11 @@ class PostgreSqlEntityStorageConnector {
|
|
|
73
72
|
}
|
|
74
73
|
await nodeLogging?.log({
|
|
75
74
|
level: "info",
|
|
76
|
-
source:
|
|
75
|
+
source: PostgreSqlEntityStorageConnector.CLASS_NAME,
|
|
77
76
|
ts: Date.now(),
|
|
78
77
|
message: "databaseExists",
|
|
79
78
|
data: {
|
|
80
|
-
|
|
79
|
+
databaseName: this._config.database
|
|
81
80
|
}
|
|
82
81
|
});
|
|
83
82
|
const tableExistsQuery = `SELECT to_regclass('${this._config.tableName}')`;
|
|
@@ -88,28 +87,25 @@ class PostgreSqlEntityStorageConnector {
|
|
|
88
87
|
}
|
|
89
88
|
await nodeLogging?.log({
|
|
90
89
|
level: "info",
|
|
91
|
-
source:
|
|
90
|
+
source: PostgreSqlEntityStorageConnector.CLASS_NAME,
|
|
92
91
|
ts: Date.now(),
|
|
93
92
|
message: "tableExists",
|
|
94
93
|
data: {
|
|
95
|
-
|
|
94
|
+
tableName: this._config.tableName
|
|
96
95
|
}
|
|
97
96
|
});
|
|
98
97
|
}
|
|
99
98
|
catch (error) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
}
|
|
99
|
+
await nodeLogging?.log({
|
|
100
|
+
level: "error",
|
|
101
|
+
source: PostgreSqlEntityStorageConnector.CLASS_NAME,
|
|
102
|
+
ts: Date.now(),
|
|
103
|
+
message: "databaseCreateFailed",
|
|
104
|
+
error: BaseError.fromError(error),
|
|
105
|
+
data: {
|
|
106
|
+
databaseName: this._config.database
|
|
107
|
+
}
|
|
108
|
+
});
|
|
113
109
|
return false;
|
|
114
110
|
}
|
|
115
111
|
return true;
|
|
@@ -129,7 +125,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
129
125
|
* @returns The object if it can be found or undefined.
|
|
130
126
|
*/
|
|
131
127
|
async get(id, secondaryIndex, conditions) {
|
|
132
|
-
Guards.stringValue(
|
|
128
|
+
Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "id", id);
|
|
133
129
|
try {
|
|
134
130
|
const dbConnection = await this.createConnection();
|
|
135
131
|
const whereClauses = [];
|
|
@@ -173,7 +169,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
173
169
|
}
|
|
174
170
|
}
|
|
175
171
|
catch (err) {
|
|
176
|
-
throw new GeneralError(
|
|
172
|
+
throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "getFailed", {
|
|
177
173
|
id
|
|
178
174
|
}, err);
|
|
179
175
|
}
|
|
@@ -186,7 +182,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
186
182
|
* @returns The id of the entity.
|
|
187
183
|
*/
|
|
188
184
|
async set(entity, conditions) {
|
|
189
|
-
Guards.object(
|
|
185
|
+
Guards.object(PostgreSqlEntityStorageConnector.CLASS_NAME, "entity", entity);
|
|
190
186
|
EntitySchemaHelper.validateEntity(entity, this.getSchema());
|
|
191
187
|
const primaryKey = EntitySchemaHelper.getPrimaryKey(this.getSchema());
|
|
192
188
|
const id = entity[primaryKey.property];
|
|
@@ -210,7 +206,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
210
206
|
.join(", ")};`, values);
|
|
211
207
|
}
|
|
212
208
|
catch (err) {
|
|
213
|
-
throw new GeneralError(
|
|
209
|
+
throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "setFailed", {
|
|
214
210
|
id
|
|
215
211
|
}, err);
|
|
216
212
|
}
|
|
@@ -222,7 +218,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
222
218
|
* @returns Nothing.
|
|
223
219
|
*/
|
|
224
220
|
async remove(id, conditions) {
|
|
225
|
-
Guards.stringValue(
|
|
221
|
+
Guards.stringValue(PostgreSqlEntityStorageConnector.CLASS_NAME, "id", id);
|
|
226
222
|
try {
|
|
227
223
|
const dbConnection = await this.createConnection();
|
|
228
224
|
const itemData = await this.get(id);
|
|
@@ -241,7 +237,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
241
237
|
}
|
|
242
238
|
}
|
|
243
239
|
catch (err) {
|
|
244
|
-
throw new GeneralError(
|
|
240
|
+
throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "removeFailed", {
|
|
245
241
|
id
|
|
246
242
|
}, err);
|
|
247
243
|
}
|
|
@@ -251,15 +247,15 @@ class PostgreSqlEntityStorageConnector {
|
|
|
251
247
|
* @param conditions The conditions to match for the entities.
|
|
252
248
|
* @param sortProperties The optional sort order.
|
|
253
249
|
* @param properties The optional properties to return, defaults to all.
|
|
254
|
-
* @param cursor The cursor to request the next
|
|
255
|
-
* @param
|
|
250
|
+
* @param cursor The cursor to request the next chunk of entities.
|
|
251
|
+
* @param limit The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
|
|
256
252
|
* @returns All the entities for the storage matching the conditions,
|
|
257
253
|
* and a cursor which can be used to request more entities.
|
|
258
254
|
*/
|
|
259
|
-
async query(conditions, sortProperties, properties, cursor,
|
|
255
|
+
async query(conditions, sortProperties, properties, cursor, limit) {
|
|
260
256
|
const sql = "";
|
|
261
257
|
try {
|
|
262
|
-
const returnSize =
|
|
258
|
+
const returnSize = limit ?? PostgreSqlEntityStorageConnector._DEFAULT_LIMIT;
|
|
263
259
|
let orderByClause = "";
|
|
264
260
|
if (Array.isArray(sortProperties)) {
|
|
265
261
|
const orderClauses = [];
|
|
@@ -303,7 +299,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
303
299
|
};
|
|
304
300
|
}
|
|
305
301
|
catch (err) {
|
|
306
|
-
throw new GeneralError(
|
|
302
|
+
throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "queryFailed", { sql }, err);
|
|
307
303
|
}
|
|
308
304
|
}
|
|
309
305
|
/**
|
|
@@ -434,7 +430,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
434
430
|
else if (comparator.comparison === ComparisonOperator.Includes) {
|
|
435
431
|
return `EXISTS (SELECT 1 FROM jsonb_array_elements("${prop}") elem WHERE elem @> $${values.length}::jsonb)`;
|
|
436
432
|
}
|
|
437
|
-
throw new GeneralError(
|
|
433
|
+
throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "comparisonNotSupported", {
|
|
438
434
|
comparison: comparator.comparison
|
|
439
435
|
});
|
|
440
436
|
}
|
|
@@ -477,7 +473,9 @@ class PostgreSqlEntityStorageConnector {
|
|
|
477
473
|
else if (operator === LogicalOperator.Or) {
|
|
478
474
|
return "OR";
|
|
479
475
|
}
|
|
480
|
-
throw new GeneralError(
|
|
476
|
+
throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "conditionalNotSupported", {
|
|
477
|
+
operator
|
|
478
|
+
});
|
|
481
479
|
}
|
|
482
480
|
/**
|
|
483
481
|
* Verify the conditions for the entity.
|
|
@@ -503,7 +501,7 @@ class PostgreSqlEntityStorageConnector {
|
|
|
503
501
|
[EntitySchemaPropertyType.Boolean]: "BOOLEAN"
|
|
504
502
|
};
|
|
505
503
|
if (!entitySchema.properties) {
|
|
506
|
-
throw new GeneralError(
|
|
504
|
+
throw new GeneralError(PostgreSqlEntityStorageConnector.CLASS_NAME, "entitySchemaPropertiesUndefined");
|
|
507
505
|
}
|
|
508
506
|
const primaryKeys = [];
|
|
509
507
|
const columnDefinitions = entitySchema.properties
|
|
@@ -8,10 +8,10 @@ export interface IPostgreSqlEntityStorageConnectorConstructorOptions {
|
|
|
8
8
|
*/
|
|
9
9
|
entitySchema: string;
|
|
10
10
|
/**
|
|
11
|
-
* The type of logging
|
|
11
|
+
* The type of logging component to use.
|
|
12
12
|
* @default logging
|
|
13
13
|
*/
|
|
14
|
-
|
|
14
|
+
loggingComponentType?: string;
|
|
15
15
|
/**
|
|
16
16
|
* The configuration for the connector.
|
|
17
17
|
*/
|
|
@@ -8,7 +8,7 @@ export declare class PostgreSqlEntityStorageConnector<T = unknown> implements IE
|
|
|
8
8
|
/**
|
|
9
9
|
* Runtime name for the class.
|
|
10
10
|
*/
|
|
11
|
-
readonly CLASS_NAME: string;
|
|
11
|
+
static readonly CLASS_NAME: string;
|
|
12
12
|
/**
|
|
13
13
|
* Create a new instance of PostgreSqlEntityStorageConnector.
|
|
14
14
|
* @param options The options for the connector.
|
|
@@ -16,10 +16,10 @@ export declare class PostgreSqlEntityStorageConnector<T = unknown> implements IE
|
|
|
16
16
|
constructor(options: IPostgreSqlEntityStorageConnectorConstructorOptions);
|
|
17
17
|
/**
|
|
18
18
|
* Initialize the PostgreSql environment.
|
|
19
|
-
* @param
|
|
19
|
+
* @param nodeLoggingComponentType Optional type of the logging component.
|
|
20
20
|
* @returns A promise that resolves to a boolean indicating success.
|
|
21
21
|
*/
|
|
22
|
-
bootstrap(
|
|
22
|
+
bootstrap(nodeLoggingComponentType?: string): Promise<boolean>;
|
|
23
23
|
/**
|
|
24
24
|
* Get the schema for the entities.
|
|
25
25
|
* @returns The schema for the entities.
|
|
@@ -61,15 +61,15 @@ export declare class PostgreSqlEntityStorageConnector<T = unknown> implements IE
|
|
|
61
61
|
* @param conditions The conditions to match for the entities.
|
|
62
62
|
* @param sortProperties The optional sort order.
|
|
63
63
|
* @param properties The optional properties to return, defaults to all.
|
|
64
|
-
* @param cursor The cursor to request the next
|
|
65
|
-
* @param
|
|
64
|
+
* @param cursor The cursor to request the next chunk of entities.
|
|
65
|
+
* @param limit The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
|
|
66
66
|
* @returns All the entities for the storage matching the conditions,
|
|
67
67
|
* and a cursor which can be used to request more entities.
|
|
68
68
|
*/
|
|
69
69
|
query(conditions?: EntityCondition<T>, sortProperties?: {
|
|
70
70
|
property: keyof T;
|
|
71
71
|
sortDirection: SortDirection;
|
|
72
|
-
}[], properties?: (keyof T)[], cursor?: string,
|
|
72
|
+
}[], properties?: (keyof T)[], cursor?: string, limit?: number): Promise<{
|
|
73
73
|
entities: Partial<T>[];
|
|
74
74
|
cursor?: string;
|
|
75
75
|
}>;
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,149 @@
|
|
|
1
1
|
# @twin.org/entity-storage-connector-postgresql - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.2-next.10](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.2-next.9...entity-storage-connector-postgresql-v0.0.2-next.10) (2025-10-09)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add validate-locales ([e66ef0d](https://github.com/twinfoundation/entity-storage/commit/e66ef0de26ca2f82b3fe89bb5c7a15a0978a9644))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Dependencies
|
|
12
|
+
|
|
13
|
+
* The following workspace dependencies were updated
|
|
14
|
+
* dependencies
|
|
15
|
+
* @twin.org/entity-storage-models bumped from 0.0.2-next.9 to 0.0.2-next.10
|
|
16
|
+
* devDependencies
|
|
17
|
+
* @twin.org/entity-storage-connector-memory bumped from 0.0.2-next.9 to 0.0.2-next.10
|
|
18
|
+
|
|
19
|
+
## [0.0.2-next.9](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.2-next.8...entity-storage-connector-postgresql-v0.0.2-next.9) (2025-10-02)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Miscellaneous Chores
|
|
23
|
+
|
|
24
|
+
* **entity-storage-connector-postgresql:** Synchronize repo versions
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Dependencies
|
|
28
|
+
|
|
29
|
+
* The following workspace dependencies were updated
|
|
30
|
+
* dependencies
|
|
31
|
+
* @twin.org/entity-storage-models bumped from 0.0.2-next.8 to 0.0.2-next.9
|
|
32
|
+
* devDependencies
|
|
33
|
+
* @twin.org/entity-storage-connector-memory bumped from 0.0.2-next.8 to 0.0.2-next.9
|
|
34
|
+
|
|
35
|
+
## [0.0.2-next.8](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.2-next.7...entity-storage-connector-postgresql-v0.0.2-next.8) (2025-08-29)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
### Features
|
|
39
|
+
|
|
40
|
+
* eslint migration to flat config ([f033b64](https://github.com/twinfoundation/entity-storage/commit/f033b64984c0e6a8129d929c9dd816dcc1b8dab0))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
### Dependencies
|
|
44
|
+
|
|
45
|
+
* The following workspace dependencies were updated
|
|
46
|
+
* dependencies
|
|
47
|
+
* @twin.org/entity-storage-models bumped from 0.0.2-next.7 to 0.0.2-next.8
|
|
48
|
+
* devDependencies
|
|
49
|
+
* @twin.org/entity-storage-connector-memory bumped from 0.0.2-next.7 to 0.0.2-next.8
|
|
50
|
+
|
|
51
|
+
## [0.0.2-next.7](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.2-next.6...entity-storage-connector-postgresql-v0.0.2-next.7) (2025-08-20)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
### Features
|
|
55
|
+
|
|
56
|
+
* logging naming consistency ([f99d12d](https://github.com/twinfoundation/entity-storage/commit/f99d12dea04b6d4f2b5632ff5473e9ec7d5f9055))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
### Dependencies
|
|
60
|
+
|
|
61
|
+
* The following workspace dependencies were updated
|
|
62
|
+
* dependencies
|
|
63
|
+
* @twin.org/entity-storage-models bumped from 0.0.2-next.6 to 0.0.2-next.7
|
|
64
|
+
* devDependencies
|
|
65
|
+
* @twin.org/entity-storage-connector-memory bumped from 0.0.2-next.6 to 0.0.2-next.7
|
|
66
|
+
|
|
67
|
+
## [0.0.2-next.6](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.2-next.5...entity-storage-connector-postgresql-v0.0.2-next.6) (2025-08-19)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
### Features
|
|
71
|
+
|
|
72
|
+
* update framework core ([b59a380](https://github.com/twinfoundation/entity-storage/commit/b59a380bb7fba2b43610f69074dcdee24a4737da))
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
### Dependencies
|
|
76
|
+
|
|
77
|
+
* The following workspace dependencies were updated
|
|
78
|
+
* dependencies
|
|
79
|
+
* @twin.org/entity-storage-models bumped from 0.0.2-next.5 to 0.0.2-next.6
|
|
80
|
+
* devDependencies
|
|
81
|
+
* @twin.org/entity-storage-connector-memory bumped from 0.0.2-next.5 to 0.0.2-next.6
|
|
82
|
+
|
|
83
|
+
## [0.0.2-next.5](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.2-next.4...entity-storage-connector-postgresql-v0.0.2-next.5) (2025-08-11)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
### Miscellaneous Chores
|
|
87
|
+
|
|
88
|
+
* **entity-storage-connector-postgresql:** Synchronize repo versions
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
### Dependencies
|
|
92
|
+
|
|
93
|
+
* The following workspace dependencies were updated
|
|
94
|
+
* dependencies
|
|
95
|
+
* @twin.org/entity-storage-models bumped from 0.0.2-next.4 to 0.0.2-next.5
|
|
96
|
+
* devDependencies
|
|
97
|
+
* @twin.org/entity-storage-connector-memory bumped from 0.0.2-next.4 to 0.0.2-next.5
|
|
98
|
+
|
|
99
|
+
## [0.0.2-next.4](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.2-next.3...entity-storage-connector-postgresql-v0.0.2-next.4) (2025-08-08)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
### Miscellaneous Chores
|
|
103
|
+
|
|
104
|
+
* **entity-storage-connector-postgresql:** Synchronize repo versions
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
### Dependencies
|
|
108
|
+
|
|
109
|
+
* The following workspace dependencies were updated
|
|
110
|
+
* dependencies
|
|
111
|
+
* @twin.org/entity-storage-models bumped from 0.0.2-next.3 to 0.0.2-next.4
|
|
112
|
+
* devDependencies
|
|
113
|
+
* @twin.org/entity-storage-connector-memory bumped from 0.0.2-next.3 to 0.0.2-next.4
|
|
114
|
+
|
|
115
|
+
## [0.0.2-next.3](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.2-next.2...entity-storage-connector-postgresql-v0.0.2-next.3) (2025-07-25)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
### Miscellaneous Chores
|
|
119
|
+
|
|
120
|
+
* **entity-storage-connector-postgresql:** Synchronize repo versions
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
### Dependencies
|
|
124
|
+
|
|
125
|
+
* The following workspace dependencies were updated
|
|
126
|
+
* dependencies
|
|
127
|
+
* @twin.org/entity-storage-models bumped from 0.0.2-next.2 to 0.0.2-next.3
|
|
128
|
+
* devDependencies
|
|
129
|
+
* @twin.org/entity-storage-connector-memory bumped from 0.0.2-next.2 to 0.0.2-next.3
|
|
130
|
+
|
|
131
|
+
## [0.0.2-next.2](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.2-next.1...entity-storage-connector-postgresql-v0.0.2-next.2) (2025-07-24)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
### Features
|
|
135
|
+
|
|
136
|
+
* synchronised storage ([#44](https://github.com/twinfoundation/entity-storage/issues/44)) ([94e10e2](https://github.com/twinfoundation/entity-storage/commit/94e10e26d1feec801449dc04af7a9757ac7495ff))
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
### Dependencies
|
|
140
|
+
|
|
141
|
+
* The following workspace dependencies were updated
|
|
142
|
+
* dependencies
|
|
143
|
+
* @twin.org/entity-storage-models bumped from 0.0.2-next.1 to 0.0.2-next.2
|
|
144
|
+
* devDependencies
|
|
145
|
+
* @twin.org/entity-storage-connector-memory bumped from 0.0.2-next.1 to 0.0.2-next.2
|
|
146
|
+
|
|
3
147
|
## [0.0.2-next.1](https://github.com/twinfoundation/entity-storage/compare/entity-storage-connector-postgresql-v0.0.2-next.0...entity-storage-connector-postgresql-v0.0.2-next.1) (2025-07-17)
|
|
4
148
|
|
|
5
149
|
|
|
@@ -36,29 +36,25 @@ The options for the connector.
|
|
|
36
36
|
|
|
37
37
|
### CLASS\_NAME
|
|
38
38
|
|
|
39
|
-
> `readonly` **CLASS\_NAME**: `string`
|
|
39
|
+
> `readonly` `static` **CLASS\_NAME**: `string`
|
|
40
40
|
|
|
41
41
|
Runtime name for the class.
|
|
42
42
|
|
|
43
|
-
#### Implementation of
|
|
44
|
-
|
|
45
|
-
`IEntityStorageConnector.CLASS_NAME`
|
|
46
|
-
|
|
47
43
|
## Methods
|
|
48
44
|
|
|
49
45
|
### bootstrap()
|
|
50
46
|
|
|
51
|
-
> **bootstrap**(`
|
|
47
|
+
> **bootstrap**(`nodeLoggingComponentType?`): `Promise`\<`boolean`\>
|
|
52
48
|
|
|
53
49
|
Initialize the PostgreSql environment.
|
|
54
50
|
|
|
55
51
|
#### Parameters
|
|
56
52
|
|
|
57
|
-
#####
|
|
53
|
+
##### nodeLoggingComponentType?
|
|
58
54
|
|
|
59
55
|
`string`
|
|
60
56
|
|
|
61
|
-
Optional type of the logging
|
|
57
|
+
Optional type of the logging component.
|
|
62
58
|
|
|
63
59
|
#### Returns
|
|
64
60
|
|
|
@@ -194,7 +190,7 @@ Nothing.
|
|
|
194
190
|
|
|
195
191
|
### query()
|
|
196
192
|
|
|
197
|
-
> **query**(`conditions?`, `sortProperties?`, `properties?`, `cursor?`, `
|
|
193
|
+
> **query**(`conditions?`, `sortProperties?`, `properties?`, `cursor?`, `limit?`): `Promise`\<\{ `entities`: `Partial`\<`T`\>[]; `cursor?`: `string`; \}\>
|
|
198
194
|
|
|
199
195
|
Find all the entities which match the conditions.
|
|
200
196
|
|
|
@@ -222,9 +218,9 @@ The optional properties to return, defaults to all.
|
|
|
222
218
|
|
|
223
219
|
`string`
|
|
224
220
|
|
|
225
|
-
The cursor to request the next
|
|
221
|
+
The cursor to request the next chunk of entities.
|
|
226
222
|
|
|
227
|
-
#####
|
|
223
|
+
##### limit?
|
|
228
224
|
|
|
229
225
|
`number`
|
|
230
226
|
|
|
@@ -12,11 +12,11 @@ The schema for the entity.
|
|
|
12
12
|
|
|
13
13
|
***
|
|
14
14
|
|
|
15
|
-
###
|
|
15
|
+
### loggingComponentType?
|
|
16
16
|
|
|
17
|
-
> `optional` **
|
|
17
|
+
> `optional` **loggingComponentType**: `string`
|
|
18
18
|
|
|
19
|
-
The type of logging
|
|
19
|
+
The type of logging component to use.
|
|
20
20
|
|
|
21
21
|
#### Default
|
|
22
22
|
|
package/locales/en.json
CHANGED
|
@@ -8,14 +8,14 @@
|
|
|
8
8
|
},
|
|
9
9
|
"error": {
|
|
10
10
|
"postgreSqlEntityStorageConnector": {
|
|
11
|
+
"databaseCreateFailed": "Unable to create database \"{databaseName}\"",
|
|
11
12
|
"setFailed": "Unable to set entity \"{id}\"",
|
|
12
13
|
"getFailed": "Unable to get entity \"{id}\"",
|
|
13
14
|
"removeFailed": "Unable to remove entity \"{id}\"",
|
|
14
15
|
"queryFailed": "The query failed",
|
|
15
16
|
"comparisonNotSupported": "Comparison operator \"{comparison}\" is not supported",
|
|
16
17
|
"conditionalNotSupported": "Conditional operator \"{operator}\" is not supported",
|
|
17
|
-
"
|
|
18
|
-
"sortNotIndexed": "The property \"{property}\" is not indexed and cannot be used for sorting"
|
|
18
|
+
"entitySchemaPropertiesUndefined": "The entity schema properties are undefined"
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/entity-storage-connector-postgresql",
|
|
3
|
-
"version": "0.0.2-next.
|
|
3
|
+
"version": "0.0.2-next.10",
|
|
4
4
|
"description": "Entity Storage connector implementation using PostgreSql storage",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@twin.org/core": "next",
|
|
18
18
|
"@twin.org/entity": "next",
|
|
19
|
-
"@twin.org/entity-storage-models": "0.0.2-next.
|
|
19
|
+
"@twin.org/entity-storage-models": "0.0.2-next.10",
|
|
20
20
|
"@twin.org/logging-models": "next",
|
|
21
21
|
"@twin.org/nameof": "next",
|
|
22
22
|
"postgres": "3.4.7"
|
|
@@ -38,5 +38,24 @@
|
|
|
38
38
|
"dist/types",
|
|
39
39
|
"locales",
|
|
40
40
|
"docs"
|
|
41
|
-
]
|
|
41
|
+
],
|
|
42
|
+
"keywords": [
|
|
43
|
+
"twin",
|
|
44
|
+
"trade",
|
|
45
|
+
"iota",
|
|
46
|
+
"framework",
|
|
47
|
+
"blockchain",
|
|
48
|
+
"entity-storage",
|
|
49
|
+
"entity",
|
|
50
|
+
"storage",
|
|
51
|
+
"persistence",
|
|
52
|
+
"database",
|
|
53
|
+
"connector",
|
|
54
|
+
"adapter",
|
|
55
|
+
"integration"
|
|
56
|
+
],
|
|
57
|
+
"bugs": {
|
|
58
|
+
"url": "git+https://github.com/twinfoundation/entity-storage/issues"
|
|
59
|
+
},
|
|
60
|
+
"homepage": "https://twindev.org"
|
|
42
61
|
}
|