UNPKG

lemon-core

Version:
94 lines 2.55 kB
"use strict"; /** * file: `dynamodb-value.ts` * - originally inspired via https://github.com/ironSource/node-dynamodb-value * - refactoring for typescript error. * * * @author Steve Jung <steve@lemoncloud.io> * @date 2019-11-20 initial version via backbone * * @copyright (C) 2019 LemonCloud Co Ltd. - All Rights Reserved. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.toDDB = exports.toJavascript = void 0; /** * converts a ddb object into a js object * */ function toJavascript(data, mergeInto = null) { const result = mergeInto || {}; const keys = Object.keys(data); for (let i = 0; i < keys.length; i++) { const p = keys[i]; result[p] = toJsValue(data[p]); } return result; } exports.toJavascript = toJavascript; /** * converts a js object into a ddb object * */ function toDDB(data, mergeInto = null) { const result = mergeInto || {}; const keys = Object.keys(data); for (let i = 0; i < keys.length; i++) { const p = keys[i]; result[p] = toDDBValue(data[p]); } return result; } exports.toDDB = toDDB; function toJsValue(entry) { const types = Object.keys(entry); // TODO maybe it would be better to create a property with undefined value for this ? if (types.length === 0) throw new Error('missing type for ' + entry); const type = types[0]; const val = entry[type]; if (type === 'NULL' && val === true) { return null; } if (type === 'N') { return Number(val); } if (type === 'M') { return toJavascript(val); } if (type === 'L') { return toJsArray(val); } return val; } function toJsArray(arr) { const val = new Array(arr.length); for (let x = 0; x < arr.length; x++) { val[x] = toJsValue(arr[x]); } return val; } function toDDBValue(val) { if (typeof val === 'string') { return { S: val }; } if (typeof val === 'number') { return { N: val.toString() }; } if (typeof val === 'boolean') { return { BOOL: val }; } if (typeof val === 'object' && Array.isArray(val)) { const result = new Array(val.length); for (let i = 0; i < result.length; i++) { result[i] = toDDBValue(val[i]); } return { L: result }; } // TODO add checks for regexp, date and others // then throw if needed if (typeof val === 'object') { return { M: toDDB(val) }; } } //# sourceMappingURL=dynamodb-value.js.map