UNPKG

dynamodb-toolbox

Version:

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

32 lines (31 loc) 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compileAttributeNameDecoder = exports.withAttributeNameDecoding = exports.withDecoding = exports.withOptional = void 0; const zod_1 = require("zod"); const withOptional = (schema, { partial, defined }, zodSchema) => defined === true ? zodSchema : partial === true || schema.props.required === 'never' ? zod_1.z.optional(zodSchema) : zodSchema; exports.withOptional = withOptional; const withDecoding = (schema, { transform }, zodSchema) => transform === false ? zodSchema : schema.props.transform !== undefined ? zod_1.z.preprocess(encoded => schema.props.transform.decode(encoded), zodSchema) : zodSchema; exports.withDecoding = withDecoding; const withAttributeNameDecoding = (schema, { transform }, zodSchema) => transform === false || Object.values(schema.attributes).every(attribute => attribute.props.savedAs === undefined) ? zodSchema : zod_1.z.preprocess((0, exports.compileAttributeNameDecoder)(schema), zodSchema); exports.withAttributeNameDecoding = withAttributeNameDecoding; const compileAttributeNameDecoder = (schema) => (encoded) => { var _a; const decoded = {}; for (const [attrName, attribute] of Object.entries(schema.attributes)) { const savedAs = (_a = attribute.props.savedAs) !== null && _a !== void 0 ? _a : attrName; decoded[attrName] = encoded[savedAs]; } return decoded; }; exports.compileAttributeNameDecoder = compileAttributeNameDecoder;