Comparing version 1.9.17 to 1.9.18
@@ -65,2 +65,3 @@ /** | ||
* @param {Object} tablesNames | ||
* @param {Boolean} forcePartitionField | ||
*/ | ||
@@ -70,2 +71,3 @@ function DB() { | ||
let tablesNames = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
let forcePartitionField = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2]; | ||
@@ -77,3 +79,4 @@ _classCallCheck(this, DB); | ||
_this._tablesNames = tablesNames; | ||
_this._validation = new _deepValidation2.default(models); | ||
_this._validation = new _deepValidation2.default(models, forcePartitionField); | ||
_this._forcePartitionField = forcePartitionField; | ||
_this._models = _this._rawModelsToVogels(models); | ||
@@ -345,2 +348,23 @@ return _this; | ||
/** | ||
* @param {String} partitionKey | ||
* @returns {DB} | ||
*/ | ||
}, { | ||
key: 'setDynamoDBPartitionKey', | ||
value: function setDynamoDBPartitionKey(partitionKey) { | ||
for (let modelName in this._models) { | ||
if (!this._models.hasOwnProperty(modelName) || modelName === DB.PARTITION_TABLE) { | ||
continue; | ||
} | ||
let modelInstance = this._models[modelName]; | ||
modelInstance[_ExtendModel.ExtendModel.PARTITION_KEY] = partitionKey; | ||
} | ||
return this; | ||
} | ||
/** | ||
* @param {String} name | ||
@@ -354,4 +378,3 @@ * @returns {Object} | ||
value: function _wrapModelSchema(name) { | ||
return { | ||
hashKey: 'Id', | ||
let schema = { | ||
timestamps: true, | ||
@@ -361,7 +384,15 @@ tableName: this._tablesNames[name], | ||
}; | ||
if (this._usePartitionField && name !== DB.PARTITION_TABLE) { | ||
schema.hashKey = _ExtendModel.ExtendModel.PARTITION_FIELD; | ||
schema.rangeKey = 'Id'; | ||
} else { | ||
schema.hashKey = 'Id'; | ||
} | ||
return schema; | ||
} | ||
/** | ||
* @param {String} str | ||
* @returns {String} | ||
* @returns {Boolean} | ||
* @private | ||
@@ -372,2 +403,9 @@ */ | ||
key: '_lispCase', | ||
/** | ||
* @param {String} str | ||
* @returns {String} | ||
* @private | ||
*/ | ||
value: function _lispCase(str) { | ||
@@ -378,3 +416,4 @@ return str.replace(/([a-z])([A-Z])/g, '$1-$2').split(/[^a-z0-9\-]+/i).join('-').toLowerCase(); | ||
/** | ||
* @returns {Number} | ||
* @returns {*} | ||
* @private | ||
*/ | ||
@@ -426,2 +465,17 @@ | ||
} | ||
}, { | ||
key: '_usePartitionField', | ||
get: function get() { | ||
return this._forcePartitionField || this.kernel && this.kernel.accountMicroservice; | ||
} | ||
}, { | ||
key: '_security', | ||
get: function get() { | ||
return this.container.get('security'); | ||
} | ||
/** | ||
* @returns {String} | ||
*/ | ||
}], [{ | ||
@@ -450,2 +504,12 @@ key: 'startLocalDynamoDBServer', | ||
}, { | ||
key: 'PARTITION_TABLE', | ||
get: function get() { | ||
return 'Account'; | ||
} | ||
/** | ||
* @returns {Number} | ||
*/ | ||
}, { | ||
key: 'LOCAL_DB_PORT', | ||
@@ -452,0 +516,0 @@ get: function get() { |
@@ -35,6 +35,9 @@ /** | ||
this._model = model; | ||
this._registerQueryWrappers(); | ||
} | ||
/** | ||
* @returns {Object} | ||
* @returns {ExtendModel} | ||
* @private | ||
*/ | ||
@@ -44,2 +47,20 @@ | ||
_createClass(ExtendModel, [{ | ||
key: '_registerQueryWrappers', | ||
value: function _registerQueryWrappers() { | ||
this._model.deepQuery = function () { | ||
return this.hasOwnProperty(ExtendModel.PARTITION_KEY) ? this.query(this[ExtendModel.PARTITION_KEY]) : this.scan(); | ||
}; | ||
this._model.anonymousQuery = function () { | ||
return this.query(ExtendModel.ANONYMOUS_PARTITION); | ||
}; | ||
return this; | ||
} | ||
/** | ||
* @returns {Object} | ||
*/ | ||
}, { | ||
key: 'inject', | ||
@@ -185,15 +206,15 @@ | ||
findAll: function findAll(cb) { | ||
return _this.model.scan().loadAll().exec(cb); | ||
return _this.model.deepQuery().loadAll().exec(cb); | ||
}, | ||
findAllPaginated: function findAllPaginated(startKey, limit, cb) { | ||
return _this.model.scan().startKey(startKey).limit(limit).exec(cb); | ||
return _this.model.deepQuery().startKey(startKey).limit(limit).exec(cb); | ||
}, | ||
findOneById: function findOneById(id, cb) { | ||
return _this.model.get(id, cb); | ||
return _this.model.hasOwnProperty(ExtendModel.PARTITION_KEY) ? _this.model.get(_this.model[ExtendModel.PARTITION_KEY], id, cb) : _this.model.get(id, cb); | ||
}, | ||
findOneBy: function findOneBy(fieldName, value, cb) { | ||
return _this.model.scan().where(fieldName).equals(value).exec(cb); | ||
return _this.model.deepQuery().where(fieldName).equals(value).exec(cb); | ||
}, | ||
@@ -204,11 +225,11 @@ | ||
return _this.model.scan().where(fieldName).equals(value).limit(limit).exec(cb); | ||
return _this.model.deepQuery().where(fieldName).equals(value).limit(limit).exec(cb); | ||
}, | ||
findAllBy: function findAllBy(fieldName, value, cb) { | ||
return _this.model.scan().where(fieldName).equals(value).loadAll().exec(cb); | ||
return _this.model.deepQuery().where(fieldName).equals(value).loadAll().exec(cb); | ||
}, | ||
findAllByPaginated: function findAllByPaginated(fieldName, value, startKey, limit, cb) { | ||
return _this.model.scan().where(fieldName).equals(value).startKey(startKey).limit(limit).exec(cb); | ||
return _this.model.deepQuery().where(fieldName).equals(value).startKey(startKey).limit(limit).exec(cb); | ||
}, | ||
@@ -221,3 +242,3 @@ | ||
return _this.model.scan().filterExpression(scanParams.filterExpression).expressionAttributeValues(scanParams.filterExpressionValues).expressionAttributeNames(scanParams.filterExpressionNames).limit(limit).exec(cb); | ||
return _this.model.deepQuery().filterExpression(scanParams.filterExpression).expressionAttributeValues(scanParams.filterExpressionValues).expressionAttributeNames(scanParams.filterExpressionNames).limit(limit).exec(cb); | ||
}, | ||
@@ -228,3 +249,3 @@ | ||
return _this.model.scan().filterExpression(scanParams.filterExpression).expressionAttributeValues(scanParams.filterExpressionValues).expressionAttributeNames(scanParams.filterExpressionNames).limit(1).exec(cb); | ||
return _this.model.deepQuery().filterExpression(scanParams.filterExpression).expressionAttributeValues(scanParams.filterExpressionValues).expressionAttributeNames(scanParams.filterExpressionNames).limit(1).exec(cb); | ||
}, | ||
@@ -235,3 +256,3 @@ | ||
return _this.model.scan().filterExpression(scanParams.filterExpression).expressionAttributeValues(scanParams.filterExpressionValues).expressionAttributeNames(scanParams.filterExpressionNames).loadAll().exec(cb); | ||
return _this.model.deepQuery().filterExpression(scanParams.filterExpression).expressionAttributeValues(scanParams.filterExpressionValues).expressionAttributeNames(scanParams.filterExpressionNames).loadAll().exec(cb); | ||
}, | ||
@@ -242,3 +263,3 @@ | ||
return _this.model.scan().filterExpression(scanParams.filterExpression).expressionAttributeValues(scanParams.filterExpressionValues).expressionAttributeNames(scanParams.filterExpressionNames).startKey(startKey).limit(limit).exec(cb); | ||
return _this.model.deepQuery().filterExpression(scanParams.filterExpression).expressionAttributeValues(scanParams.filterExpressionValues).expressionAttributeNames(scanParams.filterExpressionNames).startKey(startKey).limit(limit).exec(cb); | ||
}, | ||
@@ -249,7 +270,35 @@ | ||
return (_this$model = _this.model).getItems.apply(_this$model, arguments); | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
if (_this.model.hasOwnProperty(ExtendModel.PARTITION_KEY)) { | ||
let ids = args.shift(); | ||
let composedIds = ids.map(id => { | ||
let idObj = {}; | ||
idObj[ExtendModel.PARTITION_FIELD] = _this.model[ExtendModel.PARTITION_KEY]; | ||
idObj.Id = id; | ||
return idObj; | ||
}); | ||
// concat with anonymous partition search | ||
composedIds = composedIds.concat(ids.map(id => { | ||
let idObj = {}; | ||
idObj[ExtendModel.PARTITION_FIELD] = ExtendModel.ANONYMOUS_PARTITION; | ||
idObj.Id = id; | ||
return idObj; | ||
})); | ||
args.unshift(composedIds); | ||
} | ||
return (_this$model = _this.model).getItems.apply(_this$model, args); | ||
}, | ||
deleteById: function deleteById(id, cb) { | ||
return _this.model.destroy(id, cb); | ||
return _this.model[ExtendModel.PARTITION_KEY] ? _this.model.destroy(_this.model[ExtendModel.PARTITION_KEY], id, cb) : _this.model.destroy(id, cb); | ||
}, | ||
@@ -262,2 +311,6 @@ | ||
createItem: function createItem(data, cb) { | ||
if (_this.model.hasOwnProperty(ExtendModel.PARTITION_KEY)) { | ||
data[ExtendModel.PARTITION_FIELD] = _this.model[ExtendModel.PARTITION_KEY]; | ||
} | ||
return _this.model.create(data, cb); | ||
@@ -292,3 +345,3 @@ }, | ||
return _this.model.scan().filterExpression(scanParams.filterExpression).expressionAttributeValues(scanParams.filterExpressionValues).expressionAttributeNames(scanParams.filterExpressionNames).limit(1).exec(scanCb); | ||
return _this.model.deepQuery().filterExpression(scanParams.filterExpression).expressionAttributeValues(scanParams.filterExpressionValues).expressionAttributeNames(scanParams.filterExpressionNames).limit(1).exec(scanCb); | ||
}, | ||
@@ -299,2 +352,6 @@ | ||
if (_this.model.hasOwnProperty(ExtendModel.PARTITION_KEY)) { | ||
data[ExtendModel.PARTITION_FIELD] = _this.model[ExtendModel.PARTITION_KEY]; | ||
} | ||
return _this.model.update(data, cb); | ||
@@ -306,2 +363,6 @@ }, | ||
if (_this.model.hasOwnProperty(ExtendModel.PARTITION_KEY)) { | ||
data[ExtendModel.PARTITION_FIELD] = _this.model[ExtendModel.PARTITION_KEY]; | ||
} | ||
return _this.model.update(data, condition, cb); | ||
@@ -367,2 +428,7 @@ } | ||
} | ||
/** | ||
* @returns {String} | ||
*/ | ||
}, { | ||
@@ -383,2 +449,27 @@ key: 'DEFAULT_LIMIT', | ||
} | ||
}, { | ||
key: 'PARTITION_FIELD', | ||
get: function get() { | ||
return 'AccountId'; | ||
} | ||
/** | ||
* @returns {String} | ||
*/ | ||
}, { | ||
key: 'PARTITION_KEY', | ||
get: function get() { | ||
return 'deepPartitionKey'; | ||
} | ||
/** | ||
* @returns {String} | ||
*/ | ||
}, { | ||
key: 'ANONYMOUS_PARTITION', | ||
get: function get() { | ||
return 'anonymous'; | ||
} | ||
}]); | ||
@@ -385,0 +476,0 @@ |
{ | ||
"name": "deep-db", | ||
"version": "1.9.17", | ||
"version": "1.9.18", | ||
"description": "DEEP Database Library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
97835
2167