@twin.org/entity-storage-connector-mongodb 0.0.3-next.2 → 0.0.3-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/docs/examples.md CHANGED
@@ -1 +1,94 @@
1
- # @twin.org/entity-storage-connector-mongodb - Examples
1
+ # Entity Storage Connector MongoDB Examples
2
+
3
+ These snippets show how to initialise a connector, run common data operations, and drop a collection when resetting environments.
4
+
5
+ ## MongoDbEntityStorageConnector
6
+
7
+ ```typescript
8
+ import {
9
+ MongoDbEntityStorageConnector,
10
+ type IMongoDbEntityStorageConnectorConstructorOptions
11
+ } from '@twin.org/entity-storage-connector-mongodb';
12
+ import {
13
+ ComparisonOperator,
14
+ LogicalOperator,
15
+ SortDirection,
16
+ type EntityCondition
17
+ } from '@twin.org/entity';
18
+
19
+ interface Profile {
20
+ id: string;
21
+ email: string;
22
+ status: 'active' | 'inactive';
23
+ createdAt: string;
24
+ }
25
+
26
+ const options: IMongoDbEntityStorageConnectorConstructorOptions = {
27
+ entitySchema: 'Profile',
28
+ config: {
29
+ host: 'localhost',
30
+ port: 27017,
31
+ database: 'entityStorage',
32
+ collection: 'profiles'
33
+ }
34
+ };
35
+
36
+ const connector = new MongoDbEntityStorageConnector<Profile>(options);
37
+ await connector.bootstrap();
38
+
39
+ const className = connector.className();
40
+ const schema = connector.getSchema();
41
+
42
+ await connector.set({
43
+ id: 'profile-1',
44
+ email: 'ada@example.com',
45
+ status: 'active',
46
+ createdAt: '2026-03-09T10:30:00.000Z'
47
+ });
48
+
49
+ const byPrimaryKey = await connector.get('profile-1');
50
+ const bySecondaryIndex = await connector.get('ada@example.com', 'email');
51
+
52
+ const activeCondition: EntityCondition<Profile> = {
53
+ logicalOperator: LogicalOperator.And,
54
+ conditions: [
55
+ {
56
+ property: 'status',
57
+ comparison: ComparisonOperator.Equals,
58
+ value: 'active'
59
+ }
60
+ ]
61
+ };
62
+
63
+ const result = await connector.query(
64
+ activeCondition,
65
+ [{ property: 'createdAt', sortDirection: SortDirection.Descending }],
66
+ ['id', 'email', 'status'],
67
+ undefined,
68
+ 25
69
+ );
70
+
71
+ await connector.remove('profile-1');
72
+ ```
73
+
74
+ ```typescript
75
+ import { MongoDbEntityStorageConnector } from '@twin.org/entity-storage-connector-mongodb';
76
+
77
+ interface Profile {
78
+ id: string;
79
+ email: string;
80
+ status: 'active' | 'inactive';
81
+ createdAt: string;
82
+ }
83
+
84
+ const connector = new MongoDbEntityStorageConnector<Profile>({
85
+ entitySchema: 'Profile',
86
+ config: {
87
+ host: 'localhost',
88
+ database: 'entityStorage',
89
+ collection: 'profiles'
90
+ }
91
+ });
92
+
93
+ await connector.collectionDrop();
94
+ ```
@@ -10,7 +10,7 @@ Class for performing entity storage operations using MongoDb.
10
10
 
11
11
  ## Implements
12
12
 
13
- - `IEntityStorageConnector`\<`T`\>
13
+ - `IEntityStorageMigrationConnector`\<`T`\>
14
14
 
15
15
  ## Constructors
16
16
 
@@ -34,7 +34,7 @@ The options for the connector.
34
34
 
35
35
  ## Properties
36
36
 
37
- ### CLASS\_NAME
37
+ ### CLASS\_NAME {#class_name}
38
38
 
39
39
  > `readonly` `static` **CLASS\_NAME**: `string`
40
40
 
@@ -42,7 +42,7 @@ Runtime name for the class.
42
42
 
43
43
  ## Methods
44
44
 
45
- ### bootstrap()
45
+ ### bootstrap() {#bootstrap}
46
46
 
47
47
  > **bootstrap**(`nodeLoggingComponentType?`): `Promise`\<`boolean`\>
48
48
 
@@ -64,11 +64,37 @@ A promise that resolves to a boolean indicating success.
64
64
 
65
65
  #### Implementation of
66
66
 
67
- `IEntityStorageConnector.bootstrap`
67
+ `IEntityStorageMigrationConnector.bootstrap`
68
68
 
69
69
  ***
70
70
 
71
- ### className()
71
+ ### stop()? {#stop}
72
+
73
+ > `optional` **stop**(`nodeLoggingComponentType?`): `Promise`\<`void`\>
74
+
75
+ The component needs to be stopped when the node is closed.
76
+
77
+ #### Parameters
78
+
79
+ ##### nodeLoggingComponentType?
80
+
81
+ `string`
82
+
83
+ The node logging component type.
84
+
85
+ #### Returns
86
+
87
+ `Promise`\<`void`\>
88
+
89
+ Nothing.
90
+
91
+ #### Implementation of
92
+
93
+ `IEntityStorageMigrationConnector.stop`
94
+
95
+ ***
96
+
97
+ ### className() {#classname}
72
98
 
73
99
  > **className**(): `string`
74
100
 
@@ -82,11 +108,29 @@ The class name of the component.
82
108
 
83
109
  #### Implementation of
84
110
 
85
- `IEntityStorageConnector.className`
111
+ `IEntityStorageMigrationConnector.className`
86
112
 
87
113
  ***
88
114
 
89
- ### getSchema()
115
+ ### health() {#health}
116
+
117
+ > **health**(): `Promise`\<`IHealth`[]\>
118
+
119
+ Returns the health status of the component.
120
+
121
+ #### Returns
122
+
123
+ `Promise`\<`IHealth`[]\>
124
+
125
+ The health status of the component.
126
+
127
+ #### Implementation of
128
+
129
+ `IEntityStorageMigrationConnector.health`
130
+
131
+ ***
132
+
133
+ ### getSchema() {#getschema}
90
134
 
91
135
  > **getSchema**(): `IEntitySchema`
92
136
 
@@ -100,11 +144,11 @@ The schema for the entities.
100
144
 
101
145
  #### Implementation of
102
146
 
103
- `IEntityStorageConnector.getSchema`
147
+ `IEntityStorageMigrationConnector.getSchema`
104
148
 
105
149
  ***
106
150
 
107
- ### get()
151
+ ### get() {#get}
108
152
 
109
153
  > **get**(`id`, `secondaryIndex?`, `conditions?`): `Promise`\<`T` \| `undefined`\>
110
154
 
@@ -138,11 +182,11 @@ The object if it can be found or undefined.
138
182
 
139
183
  #### Implementation of
140
184
 
141
- `IEntityStorageConnector.get`
185
+ `IEntityStorageMigrationConnector.get`
142
186
 
143
187
  ***
144
188
 
145
- ### set()
189
+ ### set() {#set}
146
190
 
147
191
  > **set**(`entity`, `conditions?`): `Promise`\<`void`\>
148
192
 
@@ -170,11 +214,55 @@ The id of the entity.
170
214
 
171
215
  #### Implementation of
172
216
 
173
- `IEntityStorageConnector.set`
217
+ `IEntityStorageMigrationConnector.set`
218
+
219
+ ***
220
+
221
+ ### setBatch() {#setbatch}
222
+
223
+ > **setBatch**(`entities`): `Promise`\<`void`\>
224
+
225
+ Set multiple entities in a batch.
226
+
227
+ #### Parameters
228
+
229
+ ##### entities
230
+
231
+ `T`[]
232
+
233
+ The entities to set.
234
+
235
+ #### Returns
236
+
237
+ `Promise`\<`void`\>
238
+
239
+ Nothing.
240
+
241
+ #### Implementation of
242
+
243
+ `IEntityStorageMigrationConnector.setBatch`
244
+
245
+ ***
246
+
247
+ ### empty() {#empty}
248
+
249
+ > **empty**(): `Promise`\<`void`\>
250
+
251
+ Empty the entity storage.
252
+
253
+ #### Returns
254
+
255
+ `Promise`\<`void`\>
256
+
257
+ Nothing.
258
+
259
+ #### Implementation of
260
+
261
+ `IEntityStorageMigrationConnector.empty`
174
262
 
175
263
  ***
176
264
 
177
- ### remove()
265
+ ### remove() {#remove}
178
266
 
179
267
  > **remove**(`id`, `conditions?`): `Promise`\<`void`\>
180
268
 
@@ -202,11 +290,63 @@ Nothing.
202
290
 
203
291
  #### Implementation of
204
292
 
205
- `IEntityStorageConnector.remove`
293
+ `IEntityStorageMigrationConnector.remove`
294
+
295
+ ***
296
+
297
+ ### removeBatch() {#removebatch}
298
+
299
+ > **removeBatch**(`ids`): `Promise`\<`void`\>
300
+
301
+ Remove multiple entities by id.
302
+
303
+ #### Parameters
304
+
305
+ ##### ids
306
+
307
+ `string`[]
308
+
309
+ The ids of the entities to remove.
310
+
311
+ #### Returns
312
+
313
+ `Promise`\<`void`\>
314
+
315
+ Nothing.
316
+
317
+ #### Implementation of
318
+
319
+ `IEntityStorageMigrationConnector.removeBatch`
320
+
321
+ ***
322
+
323
+ ### teardown() {#teardown}
324
+
325
+ > **teardown**(`nodeLoggingComponentType?`): `Promise`\<`boolean`\>
326
+
327
+ Teardown the entity storage by dropping the collection.
328
+
329
+ #### Parameters
330
+
331
+ ##### nodeLoggingComponentType?
332
+
333
+ `string`
334
+
335
+ The node logging component type.
336
+
337
+ #### Returns
338
+
339
+ `Promise`\<`boolean`\>
340
+
341
+ True if the teardown process was successful.
342
+
343
+ #### Implementation of
344
+
345
+ `IEntityStorageMigrationConnector.teardown`
206
346
 
207
347
  ***
208
348
 
209
- ### query()
349
+ ### query() {#query}
210
350
 
211
351
  > **query**(`conditions?`, `sortProperties?`, `properties?`, `cursor?`, `limit?`): `Promise`\<\{ `entities`: `Partial`\<`T`\>[]; `cursor?`: `string`; \}\>
212
352
 
@@ -253,18 +393,168 @@ and a cursor which can be used to request more entities.
253
393
 
254
394
  #### Implementation of
255
395
 
256
- `IEntityStorageConnector.query`
396
+ `IEntityStorageMigrationConnector.query`
397
+
398
+ ***
399
+
400
+ ### count() {#count}
401
+
402
+ > **count**(`conditions?`): `Promise`\<`number`\>
403
+
404
+ Count all the entities which match the conditions.
405
+
406
+ #### Parameters
407
+
408
+ ##### conditions?
409
+
410
+ `EntityCondition`\<`T`\>
411
+
412
+ The optional conditions to match for the entities.
413
+
414
+ #### Returns
415
+
416
+ `Promise`\<`number`\>
417
+
418
+ The total count of entities in the storage.
419
+
420
+ #### Implementation of
421
+
422
+ `IEntityStorageMigrationConnector.count`
423
+
424
+ ***
425
+
426
+ ### getPartitionContextIds() {#getpartitioncontextids}
427
+
428
+ > **getPartitionContextIds**(): `Promise`\<`IContextIds`[]\>
429
+
430
+ Get all unique partition context ids present in the collection.
431
+
432
+ #### Returns
433
+
434
+ `Promise`\<`IContextIds`[]\>
435
+
436
+ An array of context id objects, one per unique partition.
437
+
438
+ #### Implementation of
439
+
440
+ `IEntityStorageMigrationConnector.getPartitionContextIds`
441
+
442
+ ***
443
+
444
+ ### createTargetConnector() {#createtargetconnector}
445
+
446
+ > **createTargetConnector**\<`U`\>(`newEntitySchema`): `Promise`\<`IEntityStorageConnector`\<`U`\>\>
447
+
448
+ Create the target connector for performing the migration using a temporary collection.
449
+
450
+ #### Type Parameters
451
+
452
+ ##### U
453
+
454
+ `U`
455
+
456
+ #### Parameters
457
+
458
+ ##### newEntitySchema
459
+
460
+ `string`
461
+
462
+ The name of the new entity schema to create the connector for.
463
+
464
+ #### Returns
465
+
466
+ `Promise`\<`IEntityStorageConnector`\<`U`\>\>
467
+
468
+ Connector for performing the migration.
469
+
470
+ #### Implementation of
471
+
472
+ `IEntityStorageMigrationConnector.createTargetConnector`
257
473
 
258
474
  ***
259
475
 
260
- ### collectionDrop()
476
+ ### finalizeMigration() {#finalizemigration}
477
+
478
+ > **finalizeMigration**\<`U`\>(`targetConnector`, `options?`, `loggingComponentType?`): `Promise`\<`MongoDbEntityStorageConnector`\<`U`\>\>
479
+
480
+ Finalize the migration by dropping the source collection and renaming the migration collection to the original name.
481
+
482
+ #### Type Parameters
483
+
484
+ ##### U
485
+
486
+ `U`
487
+
488
+ #### Parameters
489
+
490
+ ##### targetConnector
261
491
 
262
- > **collectionDrop**(): `Promise`\<`void`\>
492
+ `MongoDbEntityStorageConnector`\<`U`\>
263
493
 
264
- Drop the collection.
494
+ The connector holding the migrated data in a temporary collection.
495
+
496
+ ##### options?
497
+
498
+ `IMigrationOptions`\<`T`, `U`\>
499
+
500
+ The options to control how the migration is finalized.
501
+
502
+ ##### loggingComponentType?
503
+
504
+ `string`
505
+
506
+ The logging component type to use during finalization.
507
+
508
+ #### Returns
509
+
510
+ `Promise`\<`MongoDbEntityStorageConnector`\<`U`\>\>
511
+
512
+ The final connector using the original collection name with the new schema.
513
+
514
+ #### Implementation of
515
+
516
+ `IEntityStorageMigrationConnector.finalizeMigration`
517
+
518
+ ***
519
+
520
+ ### cleanupMigration() {#cleanupmigration}
521
+
522
+ > **cleanupMigration**\<`U`\>(`targetConnector`, `options?`, `loggingComponentType?`): `Promise`\<`void`\>
523
+
524
+ Cleanup a failed or aborted migration by dropping the temporary migration collection.
525
+
526
+ #### Type Parameters
527
+
528
+ ##### U
529
+
530
+ `U`
531
+
532
+ #### Parameters
533
+
534
+ ##### targetConnector
535
+
536
+ `IEntityStorageConnector`\<`U`\> \| `undefined`
537
+
538
+ The target connector to cleanup.
539
+
540
+ ##### options?
541
+
542
+ `IMigrationOptions`\<`T`, `U`\>
543
+
544
+ The options to control how the migration is cleaned up.
545
+
546
+ ##### loggingComponentType?
547
+
548
+ `string`
549
+
550
+ The optional component type to use for logging.
265
551
 
266
552
  #### Returns
267
553
 
268
554
  `Promise`\<`void`\>
269
555
 
270
- Nothing.
556
+ A promise that resolves when the cleanup is complete.
557
+
558
+ #### Implementation of
559
+
560
+ `IEntityStorageMigrationConnector.cleanupMigration`
@@ -4,7 +4,7 @@ Configuration for the MongoDb Entity Storage Connector.
4
4
 
5
5
  ## Properties
6
6
 
7
- ### host
7
+ ### host {#host}
8
8
 
9
9
  > **host**: `string`
10
10
 
@@ -12,31 +12,31 @@ The host for the MongoDb instance.
12
12
 
13
13
  ***
14
14
 
15
- ### port?
15
+ ### port? {#port}
16
16
 
17
- > `optional` **port**: `number`
17
+ > `optional` **port?**: `number`
18
18
 
19
19
  The port for the MongoDb instance.
20
20
 
21
21
  ***
22
22
 
23
- ### user?
23
+ ### user? {#user}
24
24
 
25
- > `optional` **user**: `string`
25
+ > `optional` **user?**: `string`
26
26
 
27
27
  The user for the MongoDb instance.
28
28
 
29
29
  ***
30
30
 
31
- ### password?
31
+ ### password? {#password}
32
32
 
33
- > `optional` **password**: `string`
33
+ > `optional` **password?**: `string`
34
34
 
35
35
  The password for the MongoDb instance.
36
36
 
37
37
  ***
38
38
 
39
- ### database
39
+ ### database {#database}
40
40
 
41
41
  > **database**: `string`
42
42
 
@@ -44,7 +44,7 @@ The name of the database to be used.
44
44
 
45
45
  ***
46
46
 
47
- ### collection
47
+ ### collection {#collection}
48
48
 
49
49
  > **collection**: `string`
50
50
 
@@ -4,7 +4,7 @@ The options for the MongoDb entity storage connector constructor.
4
4
 
5
5
  ## Properties
6
6
 
7
- ### entitySchema
7
+ ### entitySchema {#entityschema}
8
8
 
9
9
  > **entitySchema**: `string`
10
10
 
@@ -12,17 +12,17 @@ The schema for the entity.
12
12
 
13
13
  ***
14
14
 
15
- ### partitionContextIds?
15
+ ### partitionContextIds? {#partitioncontextids}
16
16
 
17
- > `optional` **partitionContextIds**: `string`[]
17
+ > `optional` **partitionContextIds?**: `string`[]
18
18
 
19
19
  The keys to use from the context ids to create partitions.
20
20
 
21
21
  ***
22
22
 
23
- ### loggingComponentType?
23
+ ### loggingComponentType? {#loggingcomponenttype}
24
24
 
25
- > `optional` **loggingComponentType**: `string`
25
+ > `optional` **loggingComponentType?**: `string`
26
26
 
27
27
  The type of logging component to use.
28
28
 
@@ -34,7 +34,7 @@ logging
34
34
 
35
35
  ***
36
36
 
37
- ### config
37
+ ### config {#config}
38
38
 
39
39
  > **config**: [`IMongoDbEntityStorageConnectorConfig`](IMongoDbEntityStorageConnectorConfig.md)
40
40
 
package/locales/en.json CHANGED
@@ -3,7 +3,9 @@
3
3
  "mongoDbEntityStorageConnector": {
4
4
  "databaseCreating": "Database \"{databaseName}\" creating",
5
5
  "databaseExists": "Database \"{databaseName}\" created or it already exists",
6
- "collectionExists": "Database \"{collectionName}\" created or it already exists"
6
+ "collectionExists": "Database \"{collectionName}\" created or it already exists",
7
+ "collectionDropping": "Dropping collection \"{collection}\"",
8
+ "collectionDropped": "Collection \"{collection}\" dropped"
7
9
  }
8
10
  },
9
11
  "error": {
@@ -12,7 +14,20 @@
12
14
  "getFailed": "Unable to get entity \"{id}\"",
13
15
  "removeFailed": "Unable to remove entity \"{id}\"",
14
16
  "databaseCreateFailed": "The database creation failed for \"{databaseName}\"",
15
- "unsupportedComparisonOperator": "Comparison operator \"{comparison}\" is not supported"
17
+ "unsupportedComparisonOperator": "Comparison operator \"{comparison}\" is not supported",
18
+ "setBatchFailed": "Unable to set batch of entities",
19
+ "removeBatchFailed": "Unable to remove batch of entities",
20
+ "emptyFailed": "Unable to empty entity storage",
21
+ "countFailed": "Unable to count entities",
22
+ "teardownFailed": "Unable to teardown entity storage",
23
+ "getPartitionContextIdsFailed": "Unable to get partition context ids",
24
+ "finalizeMigrationFailedBootstrap": "Finalizing migration failed during bootstrap phase"
25
+ }
26
+ },
27
+ "health": {
28
+ "mongoDbEntityStorageConnector": {
29
+ "healthDescription": "Checks if the MongoDB collection \"{collection}\" in database \"{database}\" is available",
30
+ "connectionFailed": "Failed to connect to MongoDB collection \"{collection}\" in database \"{database}\""
16
31
  }
17
32
  }
18
33
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@twin.org/entity-storage-connector-mongodb",
3
- "version": "0.0.3-next.2",
4
- "description": "Entity Storage connector implementation using MongoDb storage",
3
+ "version": "0.0.3-next.21",
4
+ "description": "MongoDB connector for flexible document-oriented persistence.",
5
5
  "repository": {
6
6
  "type": "git",
7
- "url": "git+https://github.com/twinfoundation/entity-storage.git",
7
+ "url": "git+https://github.com/iotaledger/twin-entity-storage.git",
8
8
  "directory": "packages/entity-storage-connector-mongodb"
9
9
  },
10
10
  "author": "adrian.sanchez.sequeira@iota.org",
@@ -17,10 +17,10 @@
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.2",
20
+ "@twin.org/entity-storage-models": "0.0.3-next.21",
21
21
  "@twin.org/logging-models": "next",
22
22
  "@twin.org/nameof": "next",
23
- "mongodb": "7.0.0"
23
+ "mongodb": "7.2.0"
24
24
  },
25
25
  "main": "./dist/es/index.js",
26
26
  "types": "./dist/types/index.d.ts",
@@ -54,7 +54,7 @@
54
54
  "integration"
55
55
  ],
56
56
  "bugs": {
57
- "url": "git+https://github.com/twinfoundation/entity-storage/issues"
57
+ "url": "git+https://github.com/iotaledger/twin-entity-storage/issues"
58
58
  },
59
59
  "homepage": "https://twindev.org"
60
60
  }