rotorise
Version:
Supercharge your DynamoDB with Rotorise!
104 lines • 2.95 kB
JavaScript
// src/Rotorise.ts
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()
};
};
export {
tableEntry
};
//# sourceMappingURL=Rotorise.js.map