dynamodb-data-types
Version:
A utility to help represent Amazon DynamoDB Data Types.
69 lines (57 loc) • 1.46 kB
JavaScript
const { generatorAlphaLower, generatorAlphaUpper } = require('unique-sequence');
const attr = require('../AttributeValue');
function valueRefs() {
const list = [];
const generator = generatorAlphaLower();
const existing = {};
function isPrimitive(val) {
const t = typeof val;
return val === null ||
t === 'string' ||
t === 'number' ||
t === 'boolean';
}
function isExistingValue(item) {
return isPrimitive(item) &&
typeof existing[item] !== 'undefined';
}
function addToExistingValues(item, ref) {
existing[item] = ref;
}
function getExistingValue(item) {
return existing[item];
}
return {
get items() { return list; },
get length() { return list.length; },
makeRef(item) {
if (isExistingValue(item)) {
return getExistingValue(item);
}
const ref = `:${generator.next().value}`;
list.push([ref, attr.wrap1(item)]);
addToExistingValues(item, ref);
return ref;
},
};
}
function nameRefs() {
const list = [];
const generator = generatorAlphaUpper();
return {
get items() { return list; },
get length() { return list.length; },
makeRef(item) {
const ref = `#${generator.next().value}`;
list.push([ref, item]);
return ref;
},
};
}
function refsForUpdateExpr() {
return {
valueRefs: valueRefs(),
nameRefs: nameRefs()
};
}
module.exports.refsForUpdateExpr = refsForUpdateExpr;