UNPKG

rotorise

Version:

Supercharge your DynamoDB with Rotorise!

127 lines (126 loc) 3.82 kB
// src/Rotorise.ts var RotoriseError = class extends Error { constructor(message) { super(message); this.name = "RotoriseError"; } }; 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 RotoriseError(`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 RotoriseError( `Discriminator ${case_.discriminator.toString()} not found in ${JSON.stringify(attributes)}` ); } const val = case_.spec[discriminator]; if (val === void 0) { throw new RotoriseError( `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; } const fullLength = structure.length; if (config?.depth !== void 0) { structure = structure.slice(0, config.depth); } const composite = []; for (const keySpec of structure) { const [key3, transform, Default] = Array.isArray(keySpec) ? keySpec : [keySpec]; const value = attributes[key3] ?? Default; if (transform && value !== void 0) { const transformed = transform(value); if (typeof transformed === "object" && transformed !== null) { if (transformed.tag !== void 0) composite.push(transformed.tag); composite.push(transformed.value); } else { composite.push(key3.toString().toUpperCase()); composite.push(transformed); } } else if (value !== void 0 && value !== null && value !== "") { composite.push(key3.toString().toUpperCase()); composite.push(value); } else if (config?.allowPartial) { break; } else { throw new RotoriseError( `buildCompositeKey: Attribute ${key3.toString()} not found in ${JSON.stringify(attributes)}` ); } } if (config?.enforceBoundary && fullLength * 2 > composite.length) { composite.push(""); } return composite.join(separator); }; var toEntry = () => (schema, separator = "#") => (item) => { const entry = { ...item }; const buildKey = key()(schema, separator); for (const key_ in schema) { const val = buildKey(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]) => { const sep = separator ?? "#"; if (sep === "" || typeof sep !== "string") { throw new RotoriseError("Separator must not be an empty string"); } return { toEntry: toEntry()(schema, sep), fromEntry: fromEntry()(schema), key: key()(schema, sep), infer: chainableNoOpProxy, path: () => createPathProxy() }; }; export { RotoriseError, tableEntry }; //# sourceMappingURL=Rotorise.js.map //# sourceMappingURL=Rotorise.js.map