UNPKG

rotorise

Version:

Supercharge your DynamoDB with Rotorise!

129 lines (128 loc) 3.98 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key2 of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key2) && key2 !== except) __defProp(to, key2, { get: () => from[key2], enumerable: !(desc = __getOwnPropDesc(from, key2)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/Rotorise.ts var Rotorise_exports = {}; __export(Rotorise_exports, { tableEntry: () => tableEntry }); module.exports = __toCommonJS(Rotorise_exports); var chainableNoOpProxy = new Proxy(() => chainableNoOpProxy, { get: () => chainableNoOpProxy }); var createPathProxy = (path = "") => { return new Proxy(() => { }, { get: (target, prop) => { if (typeof prop === "string") { if (prop === "toString") { return () => path; } return createPathProxy( path === "" ? prop : !Number.isNaN(Number.parseInt(prop)) ? `${path}[${prop}]` : `${path}.${prop}` ); } } }); }; var key = () => (schema, separator = "#") => (key2, attributes, config) => { const case_ = schema[key2]; if (case_ === void 0) { throw new Error(`Key ${key2.toString()} not found in schema`); } let structure; if (Array.isArray(case_)) { structure = case_; } else if (typeof case_ === "object") { const discriminator = attributes[case_.discriminator]; if (discriminator === void 0) { throw new Error( `Discriminator ${case_.discriminator.toString()} not found in ${JSON.stringify(attributes)}` ); } const val = case_.spec[discriminator]; if (val === void 0) { throw new Error( `Discriminator value ${discriminator?.toString()} not found in ${JSON.stringify(attributes)}` ); } if (val === null) { return void 0; } if (!Array.isArray(val)) { return attributes[val]; } structure = val; } else { const value = attributes[case_]; if (value == null) return void 0; return value; } if (config?.depth !== void 0) { structure = structure.slice(0, config.depth); } const composite = []; for (const keySpec of structure) { const [key3, transform] = Array.isArray(keySpec) ? keySpec : [keySpec]; const value = attributes[key3]; if (value !== void 0 && value !== null && value !== "" || transform) { composite.push(key3.toString().toUpperCase()); composite.push( `${transform ? transform(value) : value}` ); } else if (config?.allowPartial) { break; } else { throw new Error( `buildCompositeKey: Attribute ${key3.toString()} not found in ${JSON.stringify(attributes)}` ); } } return composite.join(separator); }; var toEntry = () => (schema, separator = "#") => (item) => { const entry = { ...item }; for (const key_ in schema) { const val = key()(schema, separator)(key_, item); if (val !== void 0) { entry[key_] = val; } } return entry; }; var fromEntry = () => (schema) => (entry) => { const item = { ...entry }; for (const key_ in schema) { delete item[key_]; } return item; }; var tableEntry = () => (schema, separator = "#") => { return { toEntry: toEntry()(schema, separator), fromEntry: fromEntry()(schema), key: key()(schema, separator), infer: chainableNoOpProxy, path: () => createPathProxy() }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { tableEntry }); //# sourceMappingURL=Rotorise.cjs.map