@twin.org/entity-storage-connector-memory 0.0.2-next.9 → 0.0.3-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.
@@ -4,8 +4,16 @@ Options for the Memory Entity Storage Connector constructor.
4
4
 
5
5
  ## Properties
6
6
 
7
- ### entitySchema
7
+ ### entitySchema {#entityschema}
8
8
 
9
9
  > **entitySchema**: `string`
10
10
 
11
11
  The schema for the entity.
12
+
13
+ ***
14
+
15
+ ### partitionContextIds? {#partitioncontextids}
16
+
17
+ > `optional` **partitionContextIds?**: `string`[]
18
+
19
+ The keys to use from the context ids to create partitions.
package/locales/en.json CHANGED
@@ -1 +1,13 @@
1
- {}
1
+ {
2
+ "info": {
3
+ "memoryEntityStorageConnector": {
4
+ "storeTearingDown": "Tearing down entity storage",
5
+ "storeTornDown": "Entity storage torn down"
6
+ }
7
+ },
8
+ "health": {
9
+ "memoryEntityStorageConnector": {
10
+ "healthDescription": "Memory entity storage is always available"
11
+ }
12
+ }
13
+ }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@twin.org/entity-storage-connector-memory",
3
- "version": "0.0.2-next.9",
4
- "description": "Entity Storage connector implementation using in-memory storage",
3
+ "version": "0.0.3-next.10",
4
+ "description": "In-memory connector for local development, testing and short-lived workloads.",
5
5
  "repository": {
6
6
  "type": "git",
7
- "url": "git+https://github.com/twinfoundation/entity-storage.git",
7
+ "url": "git+https://github.com/iotaledger/entity-storage.git",
8
8
  "directory": "packages/entity-storage-connector-memory"
9
9
  },
10
10
  "author": "martyn.janes@iota.org",
@@ -14,25 +14,25 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
+ "@twin.org/context": "next",
17
18
  "@twin.org/core": "next",
18
19
  "@twin.org/entity": "next",
19
- "@twin.org/entity-storage-models": "0.0.2-next.9",
20
+ "@twin.org/entity-storage-models": "0.0.3-next.10",
21
+ "@twin.org/logging-models": "next",
20
22
  "@twin.org/nameof": "next"
21
23
  },
22
- "main": "./dist/cjs/index.cjs",
23
- "module": "./dist/esm/index.mjs",
24
+ "main": "./dist/es/index.js",
24
25
  "types": "./dist/types/index.d.ts",
25
26
  "exports": {
26
27
  ".": {
27
28
  "types": "./dist/types/index.d.ts",
28
- "require": "./dist/cjs/index.cjs",
29
- "import": "./dist/esm/index.mjs"
29
+ "import": "./dist/es/index.js",
30
+ "default": "./dist/es/index.js"
30
31
  },
31
32
  "./locales/*.json": "./locales/*.json"
32
33
  },
33
34
  "files": [
34
- "dist/cjs",
35
- "dist/esm",
35
+ "dist/es",
36
36
  "dist/types",
37
37
  "locales",
38
38
  "docs"
@@ -54,5 +54,9 @@
54
54
  "memory",
55
55
  "in-memory",
56
56
  "testing"
57
- ]
57
+ ],
58
+ "bugs": {
59
+ "url": "git+https://github.com/iotaledger/entity-storage/issues"
60
+ },
61
+ "homepage": "https://twindev.org"
58
62
  }
@@ -1,185 +0,0 @@
1
- 'use strict';
2
-
3
- var core = require('@twin.org/core');
4
- var entity = require('@twin.org/entity');
5
-
6
- // Copyright 2024 IOTA Stiftung.
7
- // SPDX-License-Identifier: Apache-2.0.
8
- /**
9
- * Class for performing entity storage operations in-memory.
10
- */
11
- class MemoryEntityStorageConnector {
12
- /**
13
- * Default Page Size for cursor.
14
- * @internal
15
- */
16
- static _DEFAULT_PAGE_SIZE = 40;
17
- /**
18
- * Runtime name for the class.
19
- */
20
- CLASS_NAME = "MemoryEntityStorageConnector";
21
- /**
22
- * The schema for the entity.
23
- * @internal
24
- */
25
- _entitySchema;
26
- /**
27
- * The primary key.
28
- * @internal
29
- */
30
- _primaryKey;
31
- /**
32
- * The storage for the in-memory items.
33
- * @internal
34
- */
35
- _store;
36
- /**
37
- * Create a new instance of MemoryEntityStorageConnector.
38
- * @param options The options for the connector.
39
- */
40
- constructor(options) {
41
- core.Guards.object(this.CLASS_NAME, "options", options);
42
- core.Guards.stringValue(this.CLASS_NAME, "options.entitySchema", options.entitySchema);
43
- this._entitySchema = entity.EntitySchemaFactory.get(options.entitySchema);
44
- this._primaryKey = entity.EntitySchemaHelper.getPrimaryKey(this._entitySchema);
45
- this._store = [];
46
- }
47
- /**
48
- * Get the schema for the entities.
49
- * @returns The schema for the entities.
50
- */
51
- getSchema() {
52
- return this._entitySchema;
53
- }
54
- /**
55
- * Get an entity.
56
- * @param id The id of the entity to get, or the index value if secondaryIndex is set.
57
- * @param secondaryIndex Get the item using a secondary index.
58
- * @param conditions The optional conditions to match for the entities.
59
- * @returns The object if it can be found or undefined.
60
- */
61
- async get(id, secondaryIndex, conditions) {
62
- core.Guards.stringValue(this.CLASS_NAME, "id", id);
63
- const index = this.findItem(id, secondaryIndex, conditions);
64
- return index >= 0 ? this._store[index] : undefined;
65
- }
66
- /**
67
- * Set an entity.
68
- * @param entity The entity to set.
69
- * @param conditions The optional conditions to match for the entities.
70
- * @returns The id of the entity.
71
- */
72
- async set(entity$1, conditions) {
73
- core.Guards.object(this.CLASS_NAME, "entity", entity$1);
74
- entity.EntitySchemaHelper.validateEntity(entity$1, this.getSchema());
75
- const existingIndex = this.findItem(entity$1[this._primaryKey.property], undefined, conditions);
76
- if (existingIndex >= 0) {
77
- this._store[existingIndex] = entity$1;
78
- }
79
- else {
80
- this._store.push(entity$1);
81
- }
82
- }
83
- /**
84
- * Remove the entity.
85
- * @param id The id of the entity to remove.
86
- * @param conditions The optional conditions to match for the entities.
87
- * @returns Nothing.
88
- */
89
- async remove(id, conditions) {
90
- core.Guards.stringValue(this.CLASS_NAME, "id", id);
91
- const index = this.findItem(id, undefined, conditions);
92
- if (index >= 0) {
93
- this._store.splice(index, 1);
94
- }
95
- }
96
- /**
97
- * Find all the entities which match the conditions.
98
- * @param conditions The conditions to match for the entities.
99
- * @param sortProperties The optional sort order.
100
- * @param properties The optional properties to return, defaults to all.
101
- * @param cursor The cursor to request the next page of entities.
102
- * @param pageSize The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
103
- * @returns All the entities for the storage matching the conditions,
104
- * and a cursor which can be used to request more entities.
105
- */
106
- async query(conditions, sortProperties, properties, cursor, pageSize) {
107
- let allEntities = this._store.slice();
108
- const entities = [];
109
- const finalPageSize = pageSize ?? MemoryEntityStorageConnector._DEFAULT_PAGE_SIZE;
110
- let nextCursor;
111
- if (allEntities.length > 0) {
112
- const finalSortKeys = entity.EntitySchemaHelper.buildSortProperties(this._entitySchema, sortProperties);
113
- allEntities = entity.EntitySorter.sort(allEntities, finalSortKeys);
114
- const startIndex = core.Coerce.number(cursor) ?? 0;
115
- for (let i = startIndex; i < allEntities.length; i++) {
116
- if (entity.EntityConditions.check(allEntities[i], conditions) && entities.length < finalPageSize) {
117
- entities.push(core.ObjectHelper.pick(allEntities[i], properties));
118
- if (entities.length >= finalPageSize) {
119
- if (i < allEntities.length - 1) {
120
- nextCursor = (i + 1).toString();
121
- }
122
- break;
123
- }
124
- }
125
- }
126
- }
127
- return {
128
- entities,
129
- cursor: nextCursor
130
- };
131
- }
132
- /**
133
- * Get the memory store.
134
- * @returns The store.
135
- */
136
- getStore() {
137
- return this._store;
138
- }
139
- /**
140
- * Find the item in the store.
141
- * @param id The id to search for.
142
- * @param secondaryIndex The secondary index to search for.
143
- * @param conditions The optional conditions to match for the entities.
144
- * @returns The index of the item if found or -1.
145
- * @internal
146
- */
147
- findItem(id, secondaryIndex, conditions) {
148
- const finalConditions = [];
149
- if (!core.Is.empty(secondaryIndex)) {
150
- finalConditions.push({
151
- property: secondaryIndex,
152
- comparison: entity.ComparisonOperator.Equals,
153
- value: id
154
- });
155
- }
156
- if (core.Is.arrayValue(conditions)) {
157
- // If we haven't added a secondary index condition we need to add the primary key condition.
158
- if (finalConditions.length === 0) {
159
- finalConditions.push({
160
- property: this._primaryKey.property,
161
- comparison: entity.ComparisonOperator.Equals,
162
- value: id
163
- });
164
- }
165
- finalConditions.push(...conditions.map(c => ({
166
- property: c.property,
167
- comparison: entity.ComparisonOperator.Equals,
168
- value: c.value
169
- })));
170
- }
171
- if (finalConditions.length > 0) {
172
- for (let i = 0; i < this._store.length; i++) {
173
- if (entity.EntityConditions.check(this._store[i], { conditions: finalConditions })) {
174
- return i;
175
- }
176
- }
177
- }
178
- else {
179
- return this._store.findIndex(e => e[this._primaryKey.property] === id);
180
- }
181
- return -1;
182
- }
183
- }
184
-
185
- exports.MemoryEntityStorageConnector = MemoryEntityStorageConnector;
@@ -1,183 +0,0 @@
1
- import { Guards, Coerce, ObjectHelper, Is } from '@twin.org/core';
2
- import { EntitySchemaFactory, EntitySchemaHelper, EntitySorter, EntityConditions, ComparisonOperator } from '@twin.org/entity';
3
-
4
- // Copyright 2024 IOTA Stiftung.
5
- // SPDX-License-Identifier: Apache-2.0.
6
- /**
7
- * Class for performing entity storage operations in-memory.
8
- */
9
- class MemoryEntityStorageConnector {
10
- /**
11
- * Default Page Size for cursor.
12
- * @internal
13
- */
14
- static _DEFAULT_PAGE_SIZE = 40;
15
- /**
16
- * Runtime name for the class.
17
- */
18
- CLASS_NAME = "MemoryEntityStorageConnector";
19
- /**
20
- * The schema for the entity.
21
- * @internal
22
- */
23
- _entitySchema;
24
- /**
25
- * The primary key.
26
- * @internal
27
- */
28
- _primaryKey;
29
- /**
30
- * The storage for the in-memory items.
31
- * @internal
32
- */
33
- _store;
34
- /**
35
- * Create a new instance of MemoryEntityStorageConnector.
36
- * @param options The options for the connector.
37
- */
38
- constructor(options) {
39
- Guards.object(this.CLASS_NAME, "options", options);
40
- Guards.stringValue(this.CLASS_NAME, "options.entitySchema", options.entitySchema);
41
- this._entitySchema = EntitySchemaFactory.get(options.entitySchema);
42
- this._primaryKey = EntitySchemaHelper.getPrimaryKey(this._entitySchema);
43
- this._store = [];
44
- }
45
- /**
46
- * Get the schema for the entities.
47
- * @returns The schema for the entities.
48
- */
49
- getSchema() {
50
- return this._entitySchema;
51
- }
52
- /**
53
- * Get an entity.
54
- * @param id The id of the entity to get, or the index value if secondaryIndex is set.
55
- * @param secondaryIndex Get the item using a secondary index.
56
- * @param conditions The optional conditions to match for the entities.
57
- * @returns The object if it can be found or undefined.
58
- */
59
- async get(id, secondaryIndex, conditions) {
60
- Guards.stringValue(this.CLASS_NAME, "id", id);
61
- const index = this.findItem(id, secondaryIndex, conditions);
62
- return index >= 0 ? this._store[index] : undefined;
63
- }
64
- /**
65
- * Set an entity.
66
- * @param entity The entity to set.
67
- * @param conditions The optional conditions to match for the entities.
68
- * @returns The id of the entity.
69
- */
70
- async set(entity, conditions) {
71
- Guards.object(this.CLASS_NAME, "entity", entity);
72
- EntitySchemaHelper.validateEntity(entity, this.getSchema());
73
- const existingIndex = this.findItem(entity[this._primaryKey.property], undefined, conditions);
74
- if (existingIndex >= 0) {
75
- this._store[existingIndex] = entity;
76
- }
77
- else {
78
- this._store.push(entity);
79
- }
80
- }
81
- /**
82
- * Remove the entity.
83
- * @param id The id of the entity to remove.
84
- * @param conditions The optional conditions to match for the entities.
85
- * @returns Nothing.
86
- */
87
- async remove(id, conditions) {
88
- Guards.stringValue(this.CLASS_NAME, "id", id);
89
- const index = this.findItem(id, undefined, conditions);
90
- if (index >= 0) {
91
- this._store.splice(index, 1);
92
- }
93
- }
94
- /**
95
- * Find all the entities which match the conditions.
96
- * @param conditions The conditions to match for the entities.
97
- * @param sortProperties The optional sort order.
98
- * @param properties The optional properties to return, defaults to all.
99
- * @param cursor The cursor to request the next page of entities.
100
- * @param pageSize The suggested number of entities to return in each chunk, in some scenarios can return a different amount.
101
- * @returns All the entities for the storage matching the conditions,
102
- * and a cursor which can be used to request more entities.
103
- */
104
- async query(conditions, sortProperties, properties, cursor, pageSize) {
105
- let allEntities = this._store.slice();
106
- const entities = [];
107
- const finalPageSize = pageSize ?? MemoryEntityStorageConnector._DEFAULT_PAGE_SIZE;
108
- let nextCursor;
109
- if (allEntities.length > 0) {
110
- const finalSortKeys = EntitySchemaHelper.buildSortProperties(this._entitySchema, sortProperties);
111
- allEntities = EntitySorter.sort(allEntities, finalSortKeys);
112
- const startIndex = Coerce.number(cursor) ?? 0;
113
- for (let i = startIndex; i < allEntities.length; i++) {
114
- if (EntityConditions.check(allEntities[i], conditions) && entities.length < finalPageSize) {
115
- entities.push(ObjectHelper.pick(allEntities[i], properties));
116
- if (entities.length >= finalPageSize) {
117
- if (i < allEntities.length - 1) {
118
- nextCursor = (i + 1).toString();
119
- }
120
- break;
121
- }
122
- }
123
- }
124
- }
125
- return {
126
- entities,
127
- cursor: nextCursor
128
- };
129
- }
130
- /**
131
- * Get the memory store.
132
- * @returns The store.
133
- */
134
- getStore() {
135
- return this._store;
136
- }
137
- /**
138
- * Find the item in the store.
139
- * @param id The id to search for.
140
- * @param secondaryIndex The secondary index to search for.
141
- * @param conditions The optional conditions to match for the entities.
142
- * @returns The index of the item if found or -1.
143
- * @internal
144
- */
145
- findItem(id, secondaryIndex, conditions) {
146
- const finalConditions = [];
147
- if (!Is.empty(secondaryIndex)) {
148
- finalConditions.push({
149
- property: secondaryIndex,
150
- comparison: ComparisonOperator.Equals,
151
- value: id
152
- });
153
- }
154
- if (Is.arrayValue(conditions)) {
155
- // If we haven't added a secondary index condition we need to add the primary key condition.
156
- if (finalConditions.length === 0) {
157
- finalConditions.push({
158
- property: this._primaryKey.property,
159
- comparison: ComparisonOperator.Equals,
160
- value: id
161
- });
162
- }
163
- finalConditions.push(...conditions.map(c => ({
164
- property: c.property,
165
- comparison: ComparisonOperator.Equals,
166
- value: c.value
167
- })));
168
- }
169
- if (finalConditions.length > 0) {
170
- for (let i = 0; i < this._store.length; i++) {
171
- if (EntityConditions.check(this._store[i], { conditions: finalConditions })) {
172
- return i;
173
- }
174
- }
175
- }
176
- else {
177
- return this._store.findIndex(e => e[this._primaryKey.property] === id);
178
- }
179
- return -1;
180
- }
181
- }
182
-
183
- export { MemoryEntityStorageConnector };