UNPKG

@constl/bohr-db

Version:

Type-safe databases for OrbitDB.

35 lines 1.33 kB
import {} from "ajv"; import { generateListValidator, removeUndefinedProperties } from "./utils.js"; export const typedSet = ({ db, schema, }) => { const { validate } = generateListValidator(schema); return new Proxy(db, { get(target, prop) { if (prop === "all") { const wrappedAll = async () => { const allValues = await target.all(); const valids = [...allValues].filter((x) => validate(x)); return new Set(valids); }; return wrappedAll; } else if (prop === "add") { const wrappedAdd = async (data) => { if (typeof data === "object" && !Array.isArray(data)) { data = removeUndefinedProperties(data); } const valid = validate(data); if (valid) { return await target.add(data); } throw new Error(JSON.stringify(data) + JSON.stringify(validate.errors, undefined, 2)); }; return wrappedAdd; } else { return target[prop]; } }, }); }; //# sourceMappingURL=set.js.map