UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

93 lines (92 loc) 3.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromPrimitiveSchemaDTO = void 0; const index_js_1 = require("../../../../schema/binary/index.js"); const index_js_2 = require("../../../../schema/boolean/index.js"); const index_js_3 = require("../../../../schema/null/index.js"); const index_js_4 = require("../../../../schema/number/index.js"); const index_js_5 = require("../../../../schema/string/index.js"); const pipe_js_1 = require("../../../../transformers/pipe.js"); const prefix_js_1 = require("../../../../transformers/prefix.js"); const suffix_js_1 = require("../../../../transformers/suffix.js"); const isString_js_1 = require("../../../../utils/validation/isString.js"); const transformer_js_1 = require("./transformer.js"); const charCodeAt0 = (str) => str.charCodeAt(0); /** * @debt feature "handle defaults, links & validators" */ const fromPrimitiveSchemaDTO = (dto) => { const { keyDefault, putDefault, updateDefault, keyLink, putLink, updateLink, ...props } = dto; keyDefault; putDefault; updateDefault; keyLink; putLink; updateLink; switch (props.type) { case 'null': { return (0, index_js_3.nul)(props); } case 'boolean': { return (0, index_js_2.boolean)(props); } case 'number': { const { enum: _enum, ...rest } = props; const schema = (0, index_js_4.number)(rest); return _enum ? schema.enum(..._enum.map(value => ((0, isString_js_1.isString)(value) ? BigInt(value) : value))) : schema; } case 'string': { const { enum: _enum, transform, ...rest } = props; let schema = (0, index_js_5.string)(rest); if (transform !== undefined) { const transformer = fromStringSchemaTransformerDTO(transform); if (transformer !== null) { schema = schema.transform(transformer); } } return _enum ? schema.enum(..._enum) : schema; } case 'binary': { const { enum: _enum, ...rest } = props; const schema = (0, index_js_1.binary)(rest); if (!_enum) { return schema; } return schema.enum(..._enum.map(value => new Uint8Array(atob(value).split('').map(charCodeAt0)))); } } }; exports.fromPrimitiveSchemaDTO = fromPrimitiveSchemaDTO; const fromStringSchemaTransformerDTO = (transformerDTO) => { try { switch (transformerDTO.transformerId) { case 'custom': return null; case 'prefix': { const { prefix: _prefix, delimiter } = transformerDTO; return (0, prefix_js_1.prefix)(_prefix, { delimiter }); } case 'suffix': { const { suffix: _suffix, delimiter } = transformerDTO; return (0, suffix_js_1.suffix)(_suffix, { delimiter }); } case 'pipe': { const { transformers: transformerDTOs } = transformerDTO; const transformers = []; for (const transformerDTO of transformerDTOs) { const transformer = (0, transformer_js_1.fromTransformerDTO)(transformerDTO); if (transformer === null) { return null; } transformers.push(transformer); } return (0, pipe_js_1.pipe)(...transformers); } } } catch { return null; } };