UNPKG

@constl/bohr-db

Version:

Type-safe databases for orbit-db.

32 lines 1.19 kB
import { generateListValidator, removeUndefinedProperties } from "./utils.js"; export const typedFeed = ({ db, schema, }) => { const { validate } = generateListValidator(schema); return new Proxy(db, { get(target, prop) { if (prop === "all") { return async () => { const all = await target[prop](); const valid = all.filter((x) => validate(x.value)); return valid; }; } else if (prop === "add") { return 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)); }; } else { return target[prop]; } }, }); }; //# sourceMappingURL=feed.js.map