@karmaniverous/entity-manager
Advanced tools
Comparing version 0.1.2-1 to 1.0.0
@@ -13,2 +13,3 @@ "use strict"; | ||
var _isNil2 = _interopRequireDefault(require("lodash/isNil")); | ||
var _cloneDeep2 = _interopRequireDefault(require("lodash/cloneDeep")); | ||
var _PrivateEntityManager = require("./PrivateEntityManager.js"); | ||
@@ -56,3 +57,3 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* Decorate an entity item with keys. | ||
* Add sharded keys to an entity item. Does not mutate original item. | ||
* | ||
@@ -70,2 +71,8 @@ * @param {string} entityToken - Entity token. | ||
// Validate item. | ||
_classPrivateFieldGet(this, _entityManager).validateItem(item); | ||
// Clone item. | ||
const newItem = (0, _cloneDeep2.default)(item); | ||
// Get entity config. | ||
@@ -81,7 +88,6 @@ const { | ||
// Add shardKey. | ||
_classPrivateFieldGet(this, _entityManager).validateItem(item); | ||
if (overwrite || (0, _isNil2.default)(item[shardKeyToken])) { | ||
const entityKey = sharding.entityKey(item); | ||
const timestamp = sharding.timestamp(item); | ||
item[shardKeyToken] = _classPrivateFieldGet(this, _entityManager).getShardKey(entityToken, entityKey, timestamp); | ||
if (overwrite || (0, _isNil2.default)(newItem[shardKeyToken])) { | ||
const entityKey = sharding.entityKey(newItem); | ||
const timestamp = sharding.timestamp(newItem); | ||
newItem[shardKeyToken] = _classPrivateFieldGet(this, _entityManager).getShardKey(entityToken, entityKey, timestamp); | ||
} | ||
@@ -91,9 +97,9 @@ | ||
(0, _forEach2.default)(keys, (getValue, key) => { | ||
if (overwrite || (0, _isNil2.default)(item[key])) item[key] = getValue(item); | ||
if (overwrite || (0, _isNil2.default)(newItem[key])) newItem[key] = getValue(newItem); | ||
}); | ||
_classPrivateFieldGet(this, _entityManager).logger.debug('done', item); | ||
_classPrivateFieldGet(this, _entityManager).logger.debug('done', newItem); | ||
// Remove undefined shardKey. | ||
if ((0, _isNil2.default)(item[shardKeyToken])) delete item[shardKeyToken]; | ||
return item; | ||
if ((0, _isNil2.default)(newItem[shardKeyToken])) delete newItem[shardKeyToken]; | ||
return newItem; | ||
} | ||
@@ -202,3 +208,40 @@ | ||
} | ||
/** | ||
* Remove sharded keys from an entity item. Does not mutate original item. | ||
* | ||
* @param {string} entityToken - Entity token. | ||
* @param {object} item - Entity item. | ||
* @returns {object} Stripped entity item. | ||
* @throws {Error} If entityToken is invalid. | ||
* @throws {Error} If item is invalid. | ||
*/ | ||
removeKeys(entityToken, item) { | ||
_classPrivateFieldGet(this, _entityManager).logger.debug(`removing sharded index keys from ${entityToken}...`, item); | ||
// Validate item. | ||
_classPrivateFieldGet(this, _entityManager).validateItem(item); | ||
// Clone item. | ||
const newItem = (0, _cloneDeep2.default)(item); | ||
// Get entity config. | ||
const { | ||
shardKeyToken | ||
} = _classPrivateFieldGet(this, _entityManager); | ||
const { | ||
keys | ||
} = _classPrivateFieldGet(this, _entityManager).getEntityConfig(entityToken); | ||
// Remove shardKey. | ||
delete newItem[shardKeyToken]; | ||
// Remove keys. | ||
(0, _forEach2.default)(keys, (getValue, key) => { | ||
delete newItem[key]; | ||
}); | ||
_classPrivateFieldGet(this, _entityManager).logger.debug('done', newItem); | ||
return newItem; | ||
} | ||
} | ||
exports.EntityManager = EntityManager; |
@@ -32,3 +32,3 @@ /** | ||
/** | ||
* Decorate an entity item with keys. | ||
* Add sharded keys to an entity item. Does not mutate original item. | ||
* | ||
@@ -50,2 +50,8 @@ * @param {string} entityToken - Entity token. | ||
// Validate item. | ||
this.#entityManager.validateItem(item); | ||
// Clone item. | ||
const newItem = _.cloneDeep(item); | ||
// Get entity config. | ||
@@ -56,7 +62,6 @@ const { shardKeyToken } = this.#entityManager; | ||
// Add shardKey. | ||
this.#entityManager.validateItem(item); | ||
if (overwrite || _.isNil(item[shardKeyToken])) { | ||
const entityKey = sharding.entityKey(item); | ||
const timestamp = sharding.timestamp(item); | ||
item[shardKeyToken] = this.#entityManager.getShardKey( | ||
if (overwrite || _.isNil(newItem[shardKeyToken])) { | ||
const entityKey = sharding.entityKey(newItem); | ||
const timestamp = sharding.timestamp(newItem); | ||
newItem[shardKeyToken] = this.#entityManager.getShardKey( | ||
entityToken, | ||
@@ -70,10 +75,10 @@ entityKey, | ||
_.forEach(keys, (getValue, key) => { | ||
if (overwrite || _.isNil(item[key])) item[key] = getValue(item); | ||
if (overwrite || _.isNil(newItem[key])) newItem[key] = getValue(newItem); | ||
}); | ||
this.#entityManager.logger.debug('done', item); | ||
this.#entityManager.logger.debug('done', newItem); | ||
// Remove undefined shardKey. | ||
if (_.isNil(item[shardKeyToken])) delete item[shardKeyToken]; | ||
if (_.isNil(newItem[shardKeyToken])) delete newItem[shardKeyToken]; | ||
return item; | ||
return newItem; | ||
} | ||
@@ -191,2 +196,39 @@ | ||
} | ||
/** | ||
* Remove sharded keys from an entity item. Does not mutate original item. | ||
* | ||
* @param {string} entityToken - Entity token. | ||
* @param {object} item - Entity item. | ||
* @returns {object} Stripped entity item. | ||
* @throws {Error} If entityToken is invalid. | ||
* @throws {Error} If item is invalid. | ||
*/ | ||
removeKeys(entityToken, item) { | ||
this.#entityManager.logger.debug( | ||
`removing sharded index keys from ${entityToken}...`, | ||
item | ||
); | ||
// Validate item. | ||
this.#entityManager.validateItem(item); | ||
// Clone item. | ||
const newItem = _.cloneDeep(item); | ||
// Get entity config. | ||
const { shardKeyToken } = this.#entityManager; | ||
const { keys } = this.#entityManager.getEntityConfig(entityToken); | ||
// Remove shardKey. | ||
delete newItem[shardKeyToken]; | ||
// Remove keys. | ||
_.forEach(keys, (getValue, key) => { | ||
delete newItem[key]; | ||
}); | ||
this.#entityManager.logger.debug('done', newItem); | ||
return newItem; | ||
} | ||
} |
{ | ||
"name": "@karmaniverous/entity-manager", | ||
"version": "0.1.2-1", | ||
"version": "1.0.0", | ||
"publishConfig": { | ||
@@ -5,0 +5,0 @@ "access": "public" |
@@ -225,2 +225,3 @@ # entity-manager | ||
* [.query(entityToken, keyToken, item, shardQuery, [pageKeys])](#module_entity-manager.EntityManager+query) ⇒ <code>Promise.<ShardedQueryResult></code> | ||
* [.removeKeys(entityToken, item)](#module_entity-manager.EntityManager+removeKeys) ⇒ <code>object</code> | ||
* _inner_ | ||
@@ -243,2 +244,3 @@ * [~ShardQueryResult](#module_entity-manager..ShardQueryResult) : <code>object</code> | ||
* [.query(entityToken, keyToken, item, shardQuery, [pageKeys])](#module_entity-manager.EntityManager+query) ⇒ <code>Promise.<ShardedQueryResult></code> | ||
* [.removeKeys(entityToken, item)](#module_entity-manager.EntityManager+removeKeys) ⇒ <code>object</code> | ||
@@ -266,3 +268,3 @@ <a name="new_module_entity-manager.EntityManager_new"></a> | ||
#### entityManager.addKeys(entityToken, item, [overwrite]) ⇒ <code>object</code> | ||
Decorate an entity item with keys. | ||
Add sharded keys to an entity item. Does not mutate original item. | ||
@@ -321,2 +323,20 @@ **Kind**: instance method of [<code>EntityManager</code>](#module_entity-manager.EntityManager) | ||
<a name="module_entity-manager.EntityManager+removeKeys"></a> | ||
#### entityManager.removeKeys(entityToken, item) ⇒ <code>object</code> | ||
Remove sharded keys from an entity item. Does not mutate original item. | ||
**Kind**: instance method of [<code>EntityManager</code>](#module_entity-manager.EntityManager) | ||
**Returns**: <code>object</code> - Stripped entity item. | ||
**Throws**: | ||
- <code>Error</code> If entityToken is invalid. | ||
- <code>Error</code> If item is invalid. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| entityToken | <code>string</code> | Entity token. | | ||
| item | <code>object</code> | Entity item. | | ||
<a name="module_entity-manager..ShardQueryResult"></a> | ||
@@ -323,0 +343,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
63156
1159
0
378