@karmaniverous/entity-manager 0.1.2-1 → 1.0.0
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
CHANGED
|
@@ -222,6 +222,7 @@ This change can be accomplished with no breaking changes to existing implementat
|
|
|
222
222
|
* [.addKeys(entityToken, item, [overwrite])](#module_entity-manager.EntityManager+addKeys) ⇒ <code>object</code>
|
|
223
223
|
* [.getKeySpace(entityToken, keyToken, item, timestamp)](#module_entity-manager.EntityManager+getKeySpace) ⇒ <code>Array.<string></code>
|
|
224
224
|
* [.query(entityToken, keyToken, item, shardQuery, [pageKeys])](#module_entity-manager.EntityManager+query) ⇒ <code>Promise.<ShardedQueryResult></code>
|
|
225
|
+
* [.removeKeys(entityToken, item)](#module_entity-manager.EntityManager+removeKeys) ⇒ <code>object</code>
|
|
225
226
|
* _inner_
|
|
226
227
|
* [~ShardQueryResult](#module_entity-manager..ShardQueryResult) : <code>object</code>
|
|
227
228
|
* [~ShardQueryFunction](#module_entity-manager..ShardQueryFunction) ⇒ <code>Promise.<ShardQueryResult></code>
|
|
@@ -239,6 +240,7 @@ Manage DynamoDb entities.
|
|
|
239
240
|
* [.addKeys(entityToken, item, [overwrite])](#module_entity-manager.EntityManager+addKeys) ⇒ <code>object</code>
|
|
240
241
|
* [.getKeySpace(entityToken, keyToken, item, timestamp)](#module_entity-manager.EntityManager+getKeySpace) ⇒ <code>Array.<string></code>
|
|
241
242
|
* [.query(entityToken, keyToken, item, shardQuery, [pageKeys])](#module_entity-manager.EntityManager+query) ⇒ <code>Promise.<ShardedQueryResult></code>
|
|
243
|
+
* [.removeKeys(entityToken, item)](#module_entity-manager.EntityManager+removeKeys) ⇒ <code>object</code>
|
|
242
244
|
|
|
243
245
|
<a name="new_module_entity-manager.EntityManager_new"></a>
|
|
244
246
|
|
|
@@ -261,7 +263,7 @@ Create an EntityManager instance.
|
|
|
261
263
|
<a name="module_entity-manager.EntityManager+addKeys"></a>
|
|
262
264
|
|
|
263
265
|
#### entityManager.addKeys(entityToken, item, [overwrite]) ⇒ <code>object</code>
|
|
264
|
-
|
|
266
|
+
Add sharded keys to an entity item. Does not mutate original item.
|
|
265
267
|
|
|
266
268
|
**Kind**: instance method of [<code>EntityManager</code>](#module_entity-manager.EntityManager)
|
|
267
269
|
**Returns**: <code>object</code> - Decorated entity item.
|
|
@@ -315,6 +317,24 @@ Query an entity across shards.
|
|
|
315
317
|
| shardQuery | <code>ShardQueryFunction</code> | Sharded query function. |
|
|
316
318
|
| [pageKeys] | <code>object</code> | Map of shard page keys. |
|
|
317
319
|
|
|
320
|
+
<a name="module_entity-manager.EntityManager+removeKeys"></a>
|
|
321
|
+
|
|
322
|
+
#### entityManager.removeKeys(entityToken, item) ⇒ <code>object</code>
|
|
323
|
+
Remove sharded keys from an entity item. Does not mutate original item.
|
|
324
|
+
|
|
325
|
+
**Kind**: instance method of [<code>EntityManager</code>](#module_entity-manager.EntityManager)
|
|
326
|
+
**Returns**: <code>object</code> - Stripped entity item.
|
|
327
|
+
**Throws**:
|
|
328
|
+
|
|
329
|
+
- <code>Error</code> If entityToken is invalid.
|
|
330
|
+
- <code>Error</code> If item is invalid.
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
| Param | Type | Description |
|
|
334
|
+
| --- | --- | --- |
|
|
335
|
+
| entityToken | <code>string</code> | Entity token. |
|
|
336
|
+
| item | <code>object</code> | Entity item. |
|
|
337
|
+
|
|
318
338
|
<a name="module_entity-manager..ShardQueryResult"></a>
|
|
319
339
|
|
|
320
340
|
### entity-manager~ShardQueryResult : <code>object</code>
|
|
@@ -10,6 +10,7 @@ var _fromPairs2 = _interopRequireDefault(require("lodash/fromPairs"));
|
|
|
10
10
|
var _sortedUniq2 = _interopRequireDefault(require("lodash/sortedUniq"));
|
|
11
11
|
var _forEach2 = _interopRequireDefault(require("lodash/forEach"));
|
|
12
12
|
var _isNil2 = _interopRequireDefault(require("lodash/isNil"));
|
|
13
|
+
var _cloneDeep2 = _interopRequireDefault(require("lodash/cloneDeep"));
|
|
13
14
|
var _PrivateEntityManager = require("./PrivateEntityManager.js");
|
|
14
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
16
|
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
@@ -52,7 +53,7 @@ class EntityManager {
|
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
/**
|
|
55
|
-
*
|
|
56
|
+
* Add sharded keys to an entity item. Does not mutate original item.
|
|
56
57
|
*
|
|
57
58
|
* @param {string} entityToken - Entity token.
|
|
58
59
|
* @param {object} item - Entity item.
|
|
@@ -65,6 +66,12 @@ class EntityManager {
|
|
|
65
66
|
let overwrite = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
66
67
|
_classPrivateFieldGet(this, _entityManager).logger.debug(`adding sharded index keys to ${entityToken}${overwrite ? ' with overwrite' : ''}...`, item);
|
|
67
68
|
|
|
69
|
+
// Validate item.
|
|
70
|
+
_classPrivateFieldGet(this, _entityManager).validateItem(item);
|
|
71
|
+
|
|
72
|
+
// Clone item.
|
|
73
|
+
const newItem = (0, _cloneDeep2.default)(item);
|
|
74
|
+
|
|
68
75
|
// Get entity config.
|
|
69
76
|
const {
|
|
70
77
|
shardKeyToken
|
|
@@ -75,22 +82,21 @@ class EntityManager {
|
|
|
75
82
|
} = _classPrivateFieldGet(this, _entityManager).getEntityConfig(entityToken);
|
|
76
83
|
|
|
77
84
|
// Add shardKey.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
item[shardKeyToken] = _classPrivateFieldGet(this, _entityManager).getShardKey(entityToken, entityKey, timestamp);
|
|
85
|
+
if (overwrite || (0, _isNil2.default)(newItem[shardKeyToken])) {
|
|
86
|
+
const entityKey = sharding.entityKey(newItem);
|
|
87
|
+
const timestamp = sharding.timestamp(newItem);
|
|
88
|
+
newItem[shardKeyToken] = _classPrivateFieldGet(this, _entityManager).getShardKey(entityToken, entityKey, timestamp);
|
|
83
89
|
}
|
|
84
90
|
|
|
85
91
|
// Add keys.
|
|
86
92
|
(0, _forEach2.default)(keys, (getValue, key) => {
|
|
87
|
-
if (overwrite || (0, _isNil2.default)(
|
|
93
|
+
if (overwrite || (0, _isNil2.default)(newItem[key])) newItem[key] = getValue(newItem);
|
|
88
94
|
});
|
|
89
|
-
_classPrivateFieldGet(this, _entityManager).logger.debug('done',
|
|
95
|
+
_classPrivateFieldGet(this, _entityManager).logger.debug('done', newItem);
|
|
90
96
|
|
|
91
97
|
// Remove undefined shardKey.
|
|
92
|
-
if ((0, _isNil2.default)(
|
|
93
|
-
return
|
|
98
|
+
if ((0, _isNil2.default)(newItem[shardKeyToken])) delete newItem[shardKeyToken];
|
|
99
|
+
return newItem;
|
|
94
100
|
}
|
|
95
101
|
|
|
96
102
|
/**
|
|
@@ -194,5 +200,42 @@ class EntityManager {
|
|
|
194
200
|
});
|
|
195
201
|
return result;
|
|
196
202
|
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Remove sharded keys from an entity item. Does not mutate original item.
|
|
206
|
+
*
|
|
207
|
+
* @param {string} entityToken - Entity token.
|
|
208
|
+
* @param {object} item - Entity item.
|
|
209
|
+
* @returns {object} Stripped entity item.
|
|
210
|
+
* @throws {Error} If entityToken is invalid.
|
|
211
|
+
* @throws {Error} If item is invalid.
|
|
212
|
+
*/
|
|
213
|
+
removeKeys(entityToken, item) {
|
|
214
|
+
_classPrivateFieldGet(this, _entityManager).logger.debug(`removing sharded index keys from ${entityToken}...`, item);
|
|
215
|
+
|
|
216
|
+
// Validate item.
|
|
217
|
+
_classPrivateFieldGet(this, _entityManager).validateItem(item);
|
|
218
|
+
|
|
219
|
+
// Clone item.
|
|
220
|
+
const newItem = (0, _cloneDeep2.default)(item);
|
|
221
|
+
|
|
222
|
+
// Get entity config.
|
|
223
|
+
const {
|
|
224
|
+
shardKeyToken
|
|
225
|
+
} = _classPrivateFieldGet(this, _entityManager);
|
|
226
|
+
const {
|
|
227
|
+
keys
|
|
228
|
+
} = _classPrivateFieldGet(this, _entityManager).getEntityConfig(entityToken);
|
|
229
|
+
|
|
230
|
+
// Remove shardKey.
|
|
231
|
+
delete newItem[shardKeyToken];
|
|
232
|
+
|
|
233
|
+
// Remove keys.
|
|
234
|
+
(0, _forEach2.default)(keys, (getValue, key) => {
|
|
235
|
+
delete newItem[key];
|
|
236
|
+
});
|
|
237
|
+
_classPrivateFieldGet(this, _entityManager).logger.debug('done', newItem);
|
|
238
|
+
return newItem;
|
|
239
|
+
}
|
|
197
240
|
}
|
|
198
241
|
exports.EntityManager = EntityManager;
|
|
@@ -29,7 +29,7 @@ export class EntityManager {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
32
|
+
* Add sharded keys to an entity item. Does not mutate original item.
|
|
33
33
|
*
|
|
34
34
|
* @param {string} entityToken - Entity token.
|
|
35
35
|
* @param {object} item - Entity item.
|
|
@@ -46,16 +46,21 @@ export class EntityManager {
|
|
|
46
46
|
item
|
|
47
47
|
);
|
|
48
48
|
|
|
49
|
+
// Validate item.
|
|
50
|
+
this.#entityManager.validateItem(item);
|
|
51
|
+
|
|
52
|
+
// Clone item.
|
|
53
|
+
const newItem = _.cloneDeep(item);
|
|
54
|
+
|
|
49
55
|
// Get entity config.
|
|
50
56
|
const { shardKeyToken } = this.#entityManager;
|
|
51
57
|
const { keys, sharding } = this.#entityManager.getEntityConfig(entityToken);
|
|
52
58
|
|
|
53
59
|
// Add shardKey.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
item[shardKeyToken] = this.#entityManager.getShardKey(
|
|
60
|
+
if (overwrite || _.isNil(newItem[shardKeyToken])) {
|
|
61
|
+
const entityKey = sharding.entityKey(newItem);
|
|
62
|
+
const timestamp = sharding.timestamp(newItem);
|
|
63
|
+
newItem[shardKeyToken] = this.#entityManager.getShardKey(
|
|
59
64
|
entityToken,
|
|
60
65
|
entityKey,
|
|
61
66
|
timestamp
|
|
@@ -64,14 +69,14 @@ export class EntityManager {
|
|
|
64
69
|
|
|
65
70
|
// Add keys.
|
|
66
71
|
_.forEach(keys, (getValue, key) => {
|
|
67
|
-
if (overwrite || _.isNil(
|
|
72
|
+
if (overwrite || _.isNil(newItem[key])) newItem[key] = getValue(newItem);
|
|
68
73
|
});
|
|
69
|
-
this.#entityManager.logger.debug('done',
|
|
74
|
+
this.#entityManager.logger.debug('done', newItem);
|
|
70
75
|
|
|
71
76
|
// Remove undefined shardKey.
|
|
72
|
-
if (_.isNil(
|
|
77
|
+
if (_.isNil(newItem[shardKeyToken])) delete newItem[shardKeyToken];
|
|
73
78
|
|
|
74
|
-
return
|
|
79
|
+
return newItem;
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
/**
|
|
@@ -184,4 +189,41 @@ export class EntityManager {
|
|
|
184
189
|
|
|
185
190
|
return result;
|
|
186
191
|
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Remove sharded keys from an entity item. Does not mutate original item.
|
|
195
|
+
*
|
|
196
|
+
* @param {string} entityToken - Entity token.
|
|
197
|
+
* @param {object} item - Entity item.
|
|
198
|
+
* @returns {object} Stripped entity item.
|
|
199
|
+
* @throws {Error} If entityToken is invalid.
|
|
200
|
+
* @throws {Error} If item is invalid.
|
|
201
|
+
*/
|
|
202
|
+
removeKeys(entityToken, item) {
|
|
203
|
+
this.#entityManager.logger.debug(
|
|
204
|
+
`removing sharded index keys from ${entityToken}...`,
|
|
205
|
+
item
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
// Validate item.
|
|
209
|
+
this.#entityManager.validateItem(item);
|
|
210
|
+
|
|
211
|
+
// Clone item.
|
|
212
|
+
const newItem = _.cloneDeep(item);
|
|
213
|
+
|
|
214
|
+
// Get entity config.
|
|
215
|
+
const { shardKeyToken } = this.#entityManager;
|
|
216
|
+
const { keys } = this.#entityManager.getEntityConfig(entityToken);
|
|
217
|
+
|
|
218
|
+
// Remove shardKey.
|
|
219
|
+
delete newItem[shardKeyToken];
|
|
220
|
+
|
|
221
|
+
// Remove keys.
|
|
222
|
+
_.forEach(keys, (getValue, key) => {
|
|
223
|
+
delete newItem[key];
|
|
224
|
+
});
|
|
225
|
+
this.#entityManager.logger.debug('done', newItem);
|
|
226
|
+
|
|
227
|
+
return newItem;
|
|
228
|
+
}
|
|
187
229
|
}
|