@twin.org/entity-storage-connector-postgresql 0.0.1-next.21
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/LICENSE +201 -0
- package/README.md +35 -0
- package/dist/cjs/index.cjs +556 -0
- package/dist/esm/index.mjs +554 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/models/IPostgreSqlEntityStorageConnectorConfig.d.ts +29 -0
- package/dist/types/models/IPostgreSqlEntityStorageConnectorConstructorOptions.d.ts +19 -0
- package/dist/types/postgreSqlEntityStorageConnector.d.ts +94 -0
- package/docs/changelog.md +5 -0
- package/docs/examples.md +1 -0
- package/docs/reference/classes/PostgreSqlEntityStorageConnector.md +254 -0
- package/docs/reference/index.md +10 -0
- package/docs/reference/interfaces/IPostgreSqlEntityStorageConnectorConfig.md +51 -0
- package/docs/reference/interfaces/IPostgreSqlEntityStorageConnectorConstructorOptions.md +33 -0
- package/locales/en.json +21 -0
- package/package.json +42 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { type EntityCondition, type IEntitySchema, SortDirection } from "@twin.org/entity";
|
|
2
|
+
import type { IEntityStorageConnector } from "@twin.org/entity-storage-models";
|
|
3
|
+
import type { IPostgreSqlEntityStorageConnectorConstructorOptions } from "./models/IPostgreSqlEntityStorageConnectorConstructorOptions";
|
|
4
|
+
/**
|
|
5
|
+
* Class for performing entity storage operations using ql.
|
|
6
|
+
*/
|
|
7
|
+
export declare class PostgreSqlEntityStorageConnector<T = unknown> implements IEntityStorageConnector<T> {
|
|
8
|
+
/**
|
|
9
|
+
* Runtime name for the class.
|
|
10
|
+
*/
|
|
11
|
+
readonly CLASS_NAME: string;
|
|
12
|
+
/**
|
|
13
|
+
* Create a new instance of PostgreSqlEntityStorageConnector.
|
|
14
|
+
* @param options The options for the connector.
|
|
15
|
+
*/
|
|
16
|
+
constructor(options: IPostgreSqlEntityStorageConnectorConstructorOptions);
|
|
17
|
+
/**
|
|
18
|
+
* Initialize the PostgreSql environment.
|
|
19
|
+
* @param nodeLoggingConnectorType Optional type of the logging connector.
|
|
20
|
+
* @returns A promise that resolves to a boolean indicating success.
|
|
21
|
+
*/
|
|
22
|
+
bootstrap(nodeLoggingConnectorType?: string): Promise<boolean>;
|
|
23
|
+
/**
|
|
24
|
+
* Get the schema for the entities.
|
|
25
|
+
* @returns The schema for the entities.
|
|
26
|
+
*/
|
|
27
|
+
getSchema(): IEntitySchema;
|
|
28
|
+
/**
|
|
29
|
+
* Get an entity from PostgreSql.
|
|
30
|
+
* @param id The id of the entity to get, or the index value if secondaryIndex is set.
|
|
31
|
+
* @param secondaryIndex Get the item using a secondary index.
|
|
32
|
+
* @param conditions The optional conditions to match for the entities.
|
|
33
|
+
* @returns The object if it can be found or undefined.
|
|
34
|
+
*/
|
|
35
|
+
get(id: string, secondaryIndex?: keyof T, conditions?: {
|
|
36
|
+
property: keyof T;
|
|
37
|
+
value: unknown;
|
|
38
|
+
}[]): Promise<T | undefined>;
|
|
39
|
+
/**
|
|
40
|
+
* Set an entity.
|
|
41
|
+
* @param entity The entity to set.
|
|
42
|
+
* @param conditions The optional conditions to match for the entities.
|
|
43
|
+
* @returns The id of the entity.
|
|
44
|
+
*/
|
|
45
|
+
set(entity: T, conditions?: {
|
|
46
|
+
property: keyof T;
|
|
47
|
+
value: unknown;
|
|
48
|
+
}[]): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Remove the entity.
|
|
51
|
+
* @param id The id of the entity to remove.
|
|
52
|
+
* @param conditions The optional conditions to match for the entities.
|
|
53
|
+
* @returns Nothing.
|
|
54
|
+
*/
|
|
55
|
+
remove(id: string, conditions?: {
|
|
56
|
+
property: keyof T;
|
|
57
|
+
value: unknown;
|
|
58
|
+
}[]): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Find all the entities which match the conditions.
|
|
61
|
+
* @param conditions The conditions to match for the entities.
|
|
62
|
+
* @param sortProperties The optional sort order.
|
|
63
|
+
* @param properties The optional properties to return, defaults to all.
|
|
64
|
+
* @param cursor The cursor to request the next page of entities.
|
|
65
|
+
* @param pageSize The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
|
|
66
|
+
* @returns All the entities for the storage matching the conditions,
|
|
67
|
+
* and a cursor which can be used to request more entities.
|
|
68
|
+
*/
|
|
69
|
+
query(conditions?: EntityCondition<T>, sortProperties?: {
|
|
70
|
+
property: keyof T;
|
|
71
|
+
sortDirection: SortDirection;
|
|
72
|
+
}[], properties?: (keyof T)[], cursor?: string, pageSize?: number): Promise<{
|
|
73
|
+
entities: Partial<T>[];
|
|
74
|
+
cursor?: string;
|
|
75
|
+
}>;
|
|
76
|
+
/**
|
|
77
|
+
* Drop the table.
|
|
78
|
+
* @returns Nothing.
|
|
79
|
+
*/
|
|
80
|
+
tableDrop(): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Map entity schema properties to SQL properties.
|
|
83
|
+
* @param entitySchema The schema of the entity.
|
|
84
|
+
* @returns The SQL properties as a string.
|
|
85
|
+
* @throws GeneralError if the entity properties do not exist.
|
|
86
|
+
*/
|
|
87
|
+
private mapPostgreSqlProperties;
|
|
88
|
+
/**
|
|
89
|
+
* Validate that the entity matches the schema.
|
|
90
|
+
* @param entity The entity to validate.
|
|
91
|
+
* @throws GeneralError if the entity schema properties are undefined or if the entity does not match the schema.
|
|
92
|
+
*/
|
|
93
|
+
private entitySqlVerification;
|
|
94
|
+
}
|
package/docs/examples.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @twin.org/entity-storage-connector-postgresql - Examples
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# Class: PostgreSqlEntityStorageConnector\<T\>
|
|
2
|
+
|
|
3
|
+
Class for performing entity storage operations using ql.
|
|
4
|
+
|
|
5
|
+
## Type Parameters
|
|
6
|
+
|
|
7
|
+
• **T** = `unknown`
|
|
8
|
+
|
|
9
|
+
## Implements
|
|
10
|
+
|
|
11
|
+
- `IEntityStorageConnector`\<`T`\>
|
|
12
|
+
|
|
13
|
+
## Constructors
|
|
14
|
+
|
|
15
|
+
### new PostgreSqlEntityStorageConnector()
|
|
16
|
+
|
|
17
|
+
> **new PostgreSqlEntityStorageConnector**\<`T`\>(`options`): [`PostgreSqlEntityStorageConnector`](PostgreSqlEntityStorageConnector.md)\<`T`\>
|
|
18
|
+
|
|
19
|
+
Create a new instance of PostgreSqlEntityStorageConnector.
|
|
20
|
+
|
|
21
|
+
#### Parameters
|
|
22
|
+
|
|
23
|
+
##### options
|
|
24
|
+
|
|
25
|
+
[`IPostgreSqlEntityStorageConnectorConstructorOptions`](../interfaces/IPostgreSqlEntityStorageConnectorConstructorOptions.md)
|
|
26
|
+
|
|
27
|
+
The options for the connector.
|
|
28
|
+
|
|
29
|
+
#### Returns
|
|
30
|
+
|
|
31
|
+
[`PostgreSqlEntityStorageConnector`](PostgreSqlEntityStorageConnector.md)\<`T`\>
|
|
32
|
+
|
|
33
|
+
## Properties
|
|
34
|
+
|
|
35
|
+
### CLASS\_NAME
|
|
36
|
+
|
|
37
|
+
> `readonly` **CLASS\_NAME**: `string`
|
|
38
|
+
|
|
39
|
+
Runtime name for the class.
|
|
40
|
+
|
|
41
|
+
#### Implementation of
|
|
42
|
+
|
|
43
|
+
`IEntityStorageConnector.CLASS_NAME`
|
|
44
|
+
|
|
45
|
+
## Methods
|
|
46
|
+
|
|
47
|
+
### bootstrap()
|
|
48
|
+
|
|
49
|
+
> **bootstrap**(`nodeLoggingConnectorType`?): `Promise`\<`boolean`\>
|
|
50
|
+
|
|
51
|
+
Initialize the PostgreSql environment.
|
|
52
|
+
|
|
53
|
+
#### Parameters
|
|
54
|
+
|
|
55
|
+
##### nodeLoggingConnectorType?
|
|
56
|
+
|
|
57
|
+
`string`
|
|
58
|
+
|
|
59
|
+
Optional type of the logging connector.
|
|
60
|
+
|
|
61
|
+
#### Returns
|
|
62
|
+
|
|
63
|
+
`Promise`\<`boolean`\>
|
|
64
|
+
|
|
65
|
+
A promise that resolves to a boolean indicating success.
|
|
66
|
+
|
|
67
|
+
#### Implementation of
|
|
68
|
+
|
|
69
|
+
`IEntityStorageConnector.bootstrap`
|
|
70
|
+
|
|
71
|
+
***
|
|
72
|
+
|
|
73
|
+
### getSchema()
|
|
74
|
+
|
|
75
|
+
> **getSchema**(): `IEntitySchema`
|
|
76
|
+
|
|
77
|
+
Get the schema for the entities.
|
|
78
|
+
|
|
79
|
+
#### Returns
|
|
80
|
+
|
|
81
|
+
`IEntitySchema`
|
|
82
|
+
|
|
83
|
+
The schema for the entities.
|
|
84
|
+
|
|
85
|
+
#### Implementation of
|
|
86
|
+
|
|
87
|
+
`IEntityStorageConnector.getSchema`
|
|
88
|
+
|
|
89
|
+
***
|
|
90
|
+
|
|
91
|
+
### get()
|
|
92
|
+
|
|
93
|
+
> **get**(`id`, `secondaryIndex`?, `conditions`?): `Promise`\<`undefined` \| `T`\>
|
|
94
|
+
|
|
95
|
+
Get an entity from PostgreSql.
|
|
96
|
+
|
|
97
|
+
#### Parameters
|
|
98
|
+
|
|
99
|
+
##### id
|
|
100
|
+
|
|
101
|
+
`string`
|
|
102
|
+
|
|
103
|
+
The id of the entity to get, or the index value if secondaryIndex is set.
|
|
104
|
+
|
|
105
|
+
##### secondaryIndex?
|
|
106
|
+
|
|
107
|
+
keyof `T`
|
|
108
|
+
|
|
109
|
+
Get the item using a secondary index.
|
|
110
|
+
|
|
111
|
+
##### conditions?
|
|
112
|
+
|
|
113
|
+
`object`[]
|
|
114
|
+
|
|
115
|
+
The optional conditions to match for the entities.
|
|
116
|
+
|
|
117
|
+
#### Returns
|
|
118
|
+
|
|
119
|
+
`Promise`\<`undefined` \| `T`\>
|
|
120
|
+
|
|
121
|
+
The object if it can be found or undefined.
|
|
122
|
+
|
|
123
|
+
#### Implementation of
|
|
124
|
+
|
|
125
|
+
`IEntityStorageConnector.get`
|
|
126
|
+
|
|
127
|
+
***
|
|
128
|
+
|
|
129
|
+
### set()
|
|
130
|
+
|
|
131
|
+
> **set**(`entity`, `conditions`?): `Promise`\<`void`\>
|
|
132
|
+
|
|
133
|
+
Set an entity.
|
|
134
|
+
|
|
135
|
+
#### Parameters
|
|
136
|
+
|
|
137
|
+
##### entity
|
|
138
|
+
|
|
139
|
+
`T`
|
|
140
|
+
|
|
141
|
+
The entity to set.
|
|
142
|
+
|
|
143
|
+
##### conditions?
|
|
144
|
+
|
|
145
|
+
`object`[]
|
|
146
|
+
|
|
147
|
+
The optional conditions to match for the entities.
|
|
148
|
+
|
|
149
|
+
#### Returns
|
|
150
|
+
|
|
151
|
+
`Promise`\<`void`\>
|
|
152
|
+
|
|
153
|
+
The id of the entity.
|
|
154
|
+
|
|
155
|
+
#### Implementation of
|
|
156
|
+
|
|
157
|
+
`IEntityStorageConnector.set`
|
|
158
|
+
|
|
159
|
+
***
|
|
160
|
+
|
|
161
|
+
### remove()
|
|
162
|
+
|
|
163
|
+
> **remove**(`id`, `conditions`?): `Promise`\<`void`\>
|
|
164
|
+
|
|
165
|
+
Remove the entity.
|
|
166
|
+
|
|
167
|
+
#### Parameters
|
|
168
|
+
|
|
169
|
+
##### id
|
|
170
|
+
|
|
171
|
+
`string`
|
|
172
|
+
|
|
173
|
+
The id of the entity to remove.
|
|
174
|
+
|
|
175
|
+
##### conditions?
|
|
176
|
+
|
|
177
|
+
`object`[]
|
|
178
|
+
|
|
179
|
+
The optional conditions to match for the entities.
|
|
180
|
+
|
|
181
|
+
#### Returns
|
|
182
|
+
|
|
183
|
+
`Promise`\<`void`\>
|
|
184
|
+
|
|
185
|
+
Nothing.
|
|
186
|
+
|
|
187
|
+
#### Implementation of
|
|
188
|
+
|
|
189
|
+
`IEntityStorageConnector.remove`
|
|
190
|
+
|
|
191
|
+
***
|
|
192
|
+
|
|
193
|
+
### query()
|
|
194
|
+
|
|
195
|
+
> **query**(`conditions`?, `sortProperties`?, `properties`?, `cursor`?, `pageSize`?): `Promise`\<\{ `entities`: `Partial`\<`T`\>[]; `cursor`: `string`; \}\>
|
|
196
|
+
|
|
197
|
+
Find all the entities which match the conditions.
|
|
198
|
+
|
|
199
|
+
#### Parameters
|
|
200
|
+
|
|
201
|
+
##### conditions?
|
|
202
|
+
|
|
203
|
+
`EntityCondition`\<`T`\>
|
|
204
|
+
|
|
205
|
+
The conditions to match for the entities.
|
|
206
|
+
|
|
207
|
+
##### sortProperties?
|
|
208
|
+
|
|
209
|
+
`object`[]
|
|
210
|
+
|
|
211
|
+
The optional sort order.
|
|
212
|
+
|
|
213
|
+
##### properties?
|
|
214
|
+
|
|
215
|
+
keyof `T`[]
|
|
216
|
+
|
|
217
|
+
The optional properties to return, defaults to all.
|
|
218
|
+
|
|
219
|
+
##### cursor?
|
|
220
|
+
|
|
221
|
+
`string`
|
|
222
|
+
|
|
223
|
+
The cursor to request the next page of entities.
|
|
224
|
+
|
|
225
|
+
##### pageSize?
|
|
226
|
+
|
|
227
|
+
`number`
|
|
228
|
+
|
|
229
|
+
The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
|
|
230
|
+
|
|
231
|
+
#### Returns
|
|
232
|
+
|
|
233
|
+
`Promise`\<\{ `entities`: `Partial`\<`T`\>[]; `cursor`: `string`; \}\>
|
|
234
|
+
|
|
235
|
+
All the entities for the storage matching the conditions,
|
|
236
|
+
and a cursor which can be used to request more entities.
|
|
237
|
+
|
|
238
|
+
#### Implementation of
|
|
239
|
+
|
|
240
|
+
`IEntityStorageConnector.query`
|
|
241
|
+
|
|
242
|
+
***
|
|
243
|
+
|
|
244
|
+
### tableDrop()
|
|
245
|
+
|
|
246
|
+
> **tableDrop**(): `Promise`\<`void`\>
|
|
247
|
+
|
|
248
|
+
Drop the table.
|
|
249
|
+
|
|
250
|
+
#### Returns
|
|
251
|
+
|
|
252
|
+
`Promise`\<`void`\>
|
|
253
|
+
|
|
254
|
+
Nothing.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# @twin.org/entity-storage-connector-postgresql
|
|
2
|
+
|
|
3
|
+
## Classes
|
|
4
|
+
|
|
5
|
+
- [PostgreSqlEntityStorageConnector](classes/PostgreSqlEntityStorageConnector.md)
|
|
6
|
+
|
|
7
|
+
## Interfaces
|
|
8
|
+
|
|
9
|
+
- [IPostgreSqlEntityStorageConnectorConfig](interfaces/IPostgreSqlEntityStorageConnectorConfig.md)
|
|
10
|
+
- [IPostgreSqlEntityStorageConnectorConstructorOptions](interfaces/IPostgreSqlEntityStorageConnectorConstructorOptions.md)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Interface: IPostgreSqlEntityStorageConnectorConfig
|
|
2
|
+
|
|
3
|
+
Configuration for the PostgreSql Entity Storage Connector.
|
|
4
|
+
|
|
5
|
+
## Properties
|
|
6
|
+
|
|
7
|
+
### host
|
|
8
|
+
|
|
9
|
+
> **host**: `string`
|
|
10
|
+
|
|
11
|
+
The host for the PostgreSql instance.
|
|
12
|
+
|
|
13
|
+
***
|
|
14
|
+
|
|
15
|
+
### port?
|
|
16
|
+
|
|
17
|
+
> `optional` **port**: `number`
|
|
18
|
+
|
|
19
|
+
The port for the PostgreSql instance.
|
|
20
|
+
|
|
21
|
+
***
|
|
22
|
+
|
|
23
|
+
### user
|
|
24
|
+
|
|
25
|
+
> **user**: `string`
|
|
26
|
+
|
|
27
|
+
The user for the PostgreSql instance.
|
|
28
|
+
|
|
29
|
+
***
|
|
30
|
+
|
|
31
|
+
### password
|
|
32
|
+
|
|
33
|
+
> **password**: `string`
|
|
34
|
+
|
|
35
|
+
The password for the PostgreSql instance.
|
|
36
|
+
|
|
37
|
+
***
|
|
38
|
+
|
|
39
|
+
### database
|
|
40
|
+
|
|
41
|
+
> **database**: `string`
|
|
42
|
+
|
|
43
|
+
The name of the database to be used.
|
|
44
|
+
|
|
45
|
+
***
|
|
46
|
+
|
|
47
|
+
### tableName
|
|
48
|
+
|
|
49
|
+
> **tableName**: `string`
|
|
50
|
+
|
|
51
|
+
The name of the table to be used.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Interface: IPostgreSqlEntityStorageConnectorConstructorOptions
|
|
2
|
+
|
|
3
|
+
The options for the PostgreSql entity storage connector constructor.
|
|
4
|
+
|
|
5
|
+
## Properties
|
|
6
|
+
|
|
7
|
+
### entitySchema
|
|
8
|
+
|
|
9
|
+
> **entitySchema**: `string`
|
|
10
|
+
|
|
11
|
+
The schema for the entity.
|
|
12
|
+
|
|
13
|
+
***
|
|
14
|
+
|
|
15
|
+
### loggingConnectorType?
|
|
16
|
+
|
|
17
|
+
> `optional` **loggingConnectorType**: `string`
|
|
18
|
+
|
|
19
|
+
The type of logging connector to use.
|
|
20
|
+
|
|
21
|
+
#### Default
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
logging
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
***
|
|
28
|
+
|
|
29
|
+
### config
|
|
30
|
+
|
|
31
|
+
> **config**: [`IPostgreSqlEntityStorageConnectorConfig`](IPostgreSqlEntityStorageConnectorConfig.md)
|
|
32
|
+
|
|
33
|
+
The configuration for the connector.
|
package/locales/en.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"info": {
|
|
3
|
+
"postgreSqlEntityStorageConnector": {
|
|
4
|
+
"databaseCreating": "Database \"{databaseName}\" creating",
|
|
5
|
+
"databaseExists": "Database \"{databaseName}\" created or it already exists",
|
|
6
|
+
"tableExists": "Database \"{tableName}\" created or it already exists"
|
|
7
|
+
}
|
|
8
|
+
},
|
|
9
|
+
"error": {
|
|
10
|
+
"postgreSqlEntityStorageConnector": {
|
|
11
|
+
"setFailed": "Unable to set entity \"{id}\"",
|
|
12
|
+
"getFailed": "Unable to get entity \"{id}\"",
|
|
13
|
+
"removeFailed": "Unable to remove entity \"{id}\"",
|
|
14
|
+
"queryFailed": "The query failed",
|
|
15
|
+
"comparisonNotSupported": "Comparison operator \"{comparison}\" is not supported",
|
|
16
|
+
"conditionalNotSupported": "Conditional operator \"{operator}\" is not supported",
|
|
17
|
+
"sortSingle": "You can only sort by a single property",
|
|
18
|
+
"sortNotIndexed": "The property \"{property}\" is not indexed and cannot be used for sorting"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@twin.org/entity-storage-connector-postgresql",
|
|
3
|
+
"version": "0.0.1-next.21",
|
|
4
|
+
"description": "Entity Storage connector implementation using PostgreSql storage",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/twinfoundation/entity-storage.git",
|
|
8
|
+
"directory": "packages/entity-storage-connector-postgresql"
|
|
9
|
+
},
|
|
10
|
+
"author": "adrian.sanchez.sequeira@iota.org",
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=20.0.0"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@twin.org/core": "next",
|
|
18
|
+
"@twin.org/entity": "next",
|
|
19
|
+
"@twin.org/entity-storage-models": "0.0.1-next.21",
|
|
20
|
+
"@twin.org/logging-models": "next",
|
|
21
|
+
"@twin.org/nameof": "next",
|
|
22
|
+
"postgres": "^3.4.5"
|
|
23
|
+
},
|
|
24
|
+
"main": "./dist/cjs/index.cjs",
|
|
25
|
+
"module": "./dist/esm/index.mjs",
|
|
26
|
+
"types": "./dist/types/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/types/index.d.ts",
|
|
30
|
+
"require": "./dist/cjs/index.cjs",
|
|
31
|
+
"import": "./dist/esm/index.mjs"
|
|
32
|
+
},
|
|
33
|
+
"./locales/*.json": "./locales/*.json"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist/cjs",
|
|
37
|
+
"dist/esm",
|
|
38
|
+
"dist/types",
|
|
39
|
+
"locales",
|
|
40
|
+
"docs"
|
|
41
|
+
]
|
|
42
|
+
}
|