UNPKG

@nerdware/ddb-single-table

Version:

A schema-based DynamoDB modeling tool, high-level API, and type-generator built to supercharge single-table designs!⚡

31 lines (30 loc) 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertJsDates = void 0; const ts_type_safety_utils_1 = require("@nerdware/ts-type-safety-utils"); const index_js_1 = require("../utils/index.js"); const DATE_CONVERSION_FNS = { /** Recursively converts JS `Date` objects and datetime-formatted strings into ISO-8601 strings. */ toDB: (0, index_js_1.getRecursiveValueConverter)((value) => (0, ts_type_safety_utils_1.isDate)(value) ? value.toISOString() : (0, index_js_1.isValidIso8601DatetimeString)(value) ? new Date(value).toISOString() : undefined), /** Recursively converts datetime-formatted strings into JS `Date` objects. */ fromDB: (0, index_js_1.getRecursiveValueConverter)((value) => (0, index_js_1.isValidIso8601DatetimeString)(value) ? new Date(value) : undefined), }; /** * Converts all JS `Date` objects contained within `item` into ISO-8601 strings, and vice versa. */ const convertJsDates = (ioDirection, item) => { // Get the appropriate conversion function based on the I/O direction const typeConverterFn = DATE_CONVERSION_FNS[ioDirection]; // To avoid mutating the original item, create a new object to return const itemToReturn = { ...item }; // Iterate over the item's keys for (const itemKey of Object.keys(item)) { itemToReturn[itemKey] = typeConverterFn(item[itemKey]); } return itemToReturn; }; exports.convertJsDates = convertJsDates;