dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
51 lines (50 loc) • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPrimitiveSchemaDTO = void 0;
const index_js_1 = require("../../../../transformers/index.js");
const isBigInt_js_1 = require("../../../../utils/validation/isBigInt.js");
const utils_js_1 = require("./utils.js");
/**
* @debt feature "handle defaults, links & validators DTOs"
*/
const getPrimitiveSchemaDTO = (schema) => {
const defaultsDTO = (0, utils_js_1.getDefaultsDTO)(schema);
const { props } = schema;
const { required, hidden, key, savedAs, transform } = props;
const attrDTO = {
type: schema.type,
...(required !== undefined && required !== 'atLeastOnce' ? { required } : {}),
...(hidden !== undefined && hidden !== false ? { hidden } : {}),
...(key !== undefined && key !== false ? { key } : {}),
...(savedAs !== undefined ? { savedAs } : {}),
...(transform !== undefined
? {
transform: (0, index_js_1.isSerializableTransformer)(transform)
? transform.toJSON()
: { transformerId: 'custom' }
}
: {}),
...defaultsDTO
// We need to cast as `.enum` is not coupled to `.type`
};
if (props.enum) {
switch (schema.type) {
case 'binary': {
const textDecoder = new TextDecoder('utf8');
// @ts-ignore type inference can be improved here
attrDTO.enum = props.enum.map(value => btoa(textDecoder.decode(value)));
break;
}
case 'number': {
// @ts-ignore type inference can be improved here
attrDTO.enum = props.enum.map(value => ((0, isBigInt_js_1.isBigInt)(value) ? value.toString() : value));
break;
}
default:
// @ts-ignore type inference can be improved here
attrDTO.enum = props.enum;
}
}
return attrDTO;
};
exports.getPrimitiveSchemaDTO = getPrimitiveSchemaDTO;