UNPKG

dynamodb-toolbox

Version:

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

40 lines (39 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compileAttributeNameEncoder = exports.withAttributeNameEncoding = exports.withEncoding = exports.withOptional = exports.withDefault = void 0; const zod_1 = require("zod"); const withDefault = (schema, { fill }, zodSchema) => fill === false ? zodSchema : schema.props.key === true && schema.props.keyDefault !== undefined ? zodSchema.default(schema.props.keyDefault) : schema.props.putDefault !== undefined ? zodSchema.default(schema.props.putDefault) : zodSchema; exports.withDefault = withDefault; const withOptional = (schema, { defined }, zodSchema) => defined === true ? zodSchema : schema.props.required === 'never' ? zod_1.z.optional(zodSchema) : zodSchema; exports.withOptional = withOptional; const withEncoding = (schema, { transform }, zodSchema) => transform === false ? zodSchema : schema.props.transform !== undefined ? zodSchema.transform(decoded => schema.props.transform.encode(decoded)) : zodSchema; exports.withEncoding = withEncoding; const withAttributeNameEncoding = (schema, { transform }, zodSchema) => transform === false || Object.values(schema.attributes).every(attribute => attribute.props.savedAs === undefined) ? zodSchema : zodSchema.transform((0, exports.compileAttributeNameEncoder)(schema)); exports.withAttributeNameEncoding = withAttributeNameEncoding; const compileAttributeNameEncoder = (schema) => (decoded) => { var _a; const encoded = {}; for (const [attrName, attribute] of Object.entries(schema.attributes)) { const savedAs = (_a = attribute.props.savedAs) !== null && _a !== void 0 ? _a : attrName; encoded[savedAs] = decoded[attrName]; } return encoded; }; exports.compileAttributeNameEncoder = compileAttributeNameEncoder;