fireball-db
Version:
a lightweight model wrapper for DynamoDB
63 lines (54 loc) • 1.52 kB
JavaScript
var KeySchema, isArray, isObject, keygen, ref;
ref = require('lodash'), isArray = ref.isArray, isObject = ref.isObject;
keygen = require('keygen');
module.exports = KeySchema = (function() {
function KeySchema(options) {
if (options == null) {
options = {};
}
this.hash = options.hash_key || 'identifier';
this.range = options.range_key;
this.size = options.key_size || keygen.large;
this.auto = options.generate_hash_key != null ? options.generate_hash_key : true;
}
KeySchema.prototype.keyed_params = function(hash_value, range_value, params) {
var key;
if (isObject(hash_value)) {
key = this.key_for(hash_value, this.hash, this.range);
params = range_value;
} else {
key = {};
key[this.hash] = hash_value;
if (this.range != null) {
key[this.range] = range_value;
} else {
params = range_value;
}
}
if (params == null) {
params = {};
}
params.Key = key;
return params;
};
KeySchema.prototype.key_for = function(item) {
var key;
key = {};
if (isObject(item)) {
key[this.hash] = item[this.hash];
if (this.range != null) {
key[this.range] = item[this.range];
}
} else {
key[this.hash] = item;
}
return key;
};
KeySchema.prototype.generate_for = function(item) {
if (this.auto && (item[this.hash] == null)) {
item[this.hash] = keygen.url(this.size);
}
return item;
};
return KeySchema;
})();