UNPKG

single-source-of-truth

Version:

<div align='center'> <h1><strong>single-source-of-truth</strong></h1> <i>Use ArkType schemas to generate your Prisma schema</i><br> <code>pnpm add truth@npm:single-source-of-truth arktype</code> </div>

277 lines (269 loc) 6.64 kB
import { type } from "arktype"; //#region src/arktype/resolvers/enum.ts function enumToStandard(name, schema) { return { name, values: schema.truth.values.map((v) => ({ name: v })) }; } //#endregion //#region src/arktype/schemas/enum.ts function enum_(values) { const obj = Object.fromEntries(values.map((v) => [v, v])); const parse = type.valueOf(obj).configure({ " key": Math.random() }); return Object.assign(parse, { truth: { kind: "enum", name: null, values }, toTruth() { return enumToStandard(this.truth.name ?? this.toString(), this); } }); } (function(_enum_) { function is(thing) { if (typeof thing !== "function") return false; return Reflect.get(thing, "truth")?.kind === "enum"; } _enum_.is = is; })(enum_ || (enum_ = {})); //#endregion //#region src/arktype/schemas/field.ts function field(type$1) { const parse = type.raw(type$1).configure({ " key": Math.random() }); return Object.assign(parse, { truth: { kind: "field", name: null }, id_() { this.truth.id = true; return this; }, unique() { this.truth.unique = true; return this; }, updatedAt() { this.truth.updatedAt = true; return this; } }); } (function(_field) { function is(thing) { if (typeof thing !== "function") return false; return Reflect.get(thing, "truth")?.kind === "field"; } _field.is = is; })(field || (field = {})); //#endregion //#region src/arktype/schemas/relation.ts function relation(getter) { const out = Object.assign(getter, { truth: { kind: "relation" }, reference(ownKey, foreignKey) { this.truth.references ??= []; this.truth.references.push([ownKey, foreignKey]); return this; } }); return new Proxy(out, { get(target, key, receiver) { if (key === "name") { function name(name$1) { this.truth.name = name$1; return this; } return name.bind(out); } else return Reflect.get(target, key, receiver); } }); } (function(_relation) { function is(thing) { if (typeof thing !== "function") return false; return Reflect.get(thing, "truth")?.kind === "relation"; } _relation.is = is; })(relation || (relation = {})); //#endregion //#region src/arktype/resolvers/field.ts function fieldToStandard(name, field$1) { const [kind, type$1, attributes] = typeToStandard(field$1); if (field$1.truth.id) attributes.id = true; if (field$1.truth.unique) attributes.unique = true; if (field$1.truth.updatedAt) attributes.updatedAt = true; return { name, kind, type: type$1, attributes }; } function typeToStandard(type$1) { if (type$1.json && "branches" in type$1.json && Array.isArray(type$1.json.branches)) { if (type$1.json.branches.some((b) => b.unit === null)) { const branches = type$1.branches.filter((b) => b.domain !== "null"); if (branches.length === 1) { const [k, t, a] = typeToStandard(branches[0]); return [ k, t, { ...a, nullable: true } ]; } else { const [k, t, a] = typeToStandard(type.raw(type$1.$.node("union", branches).expression).configure({ key: Math.random() })); return [ k, t, { ...a, nullable: true } ]; } } else if (type$1.json.branches.every((b) => typeof b.unit === "boolean")) return [ "scalar", "boolean", {} ]; else if (type$1.json.branches.every((b, _, a) => b.domain?.domain === a[0].domain?.domain)) { const entry = type$1.branches[0]; const [k, t, a] = typeToStandard(entry); return [ k, t, a ]; } } switch (type$1.json?.domain?.domain ?? type$1.json?.domain) { case "string": return [ "scalar", "string", {} ]; case "number": return [ "scalar", (type$1.json?.divisor?.rule ?? type$1.json.divisor) === 1 ? "integer" : "float", {} ]; case "bigint": return [ "scalar", "bigint", {} ]; case "object": return [ "scalar", "object", {} ]; } switch (type$1.json?.proto?.proto ?? type$1.json?.proto) { case "Date": return [ "scalar", "date", {} ]; case "Array": { const [k, t, a] = typeToStandard(type$1.structure.sequence.variadic); return [ k, t, { ...a, list: true } ]; } } if (relation.is(type$1)) { const relative = type$1(); let model$1; const attributes = {}; if (relative.json?.proto === "Array") { model$1 = relative.structure.sequence.variadic; attributes.list = true; } else if (relative.json?.at?.(1)?.unit === null) { model$1 = relative.branches[0]; attributes.nullable = true; } else model$1 = relative; if (type$1.truth.name) attributes.name = type$1.truth.name; if (type$1.truth.references) attributes.references = type$1.truth.references; return [ "object", model$1.truth.name, attributes ]; } throw new Error(`Unable to resolve type: "${type$1}"`); } //#endregion //#region src/arktype/resolvers/model.ts function modelToStandard(name, model$1) { const fields = Object.entries(model$1.truth.shape).map(([name$1, value]) => { if (field.is(value)) { const standard = fieldToStandard(name$1, value); if (value.truth.id) standard.attributes.id = true; if (value.truth.unique) standard.attributes.unique = true; if (value.truth.updatedAt) standard.attributes.updatedAt = true; return standard; } else if (enum_.is(value)) return { name: name$1, kind: "enum", type: value.truth.name, attributes: {} }; else return fieldToStandard(name$1, value()); }); const attributes = {}; if (model$1.truth.id) attributes.id = model$1.truth.id; if (model$1.truth.unique) attributes.unique = model$1.truth.unique; return { name, attributes, fields }; } //#endregion //#region src/arktype/schemas/model.ts function model(shape) { const fields = Object.fromEntries(fieldKeys(shape).map((k) => [k, shape[k]])); const parse = type.raw(fields).configure({ " key": Math.random() }); return Object.assign(parse, { truth: { kind: "model", name: null, shape }, id_(fields$1) { this.truth.id = fields$1; return this; }, unique(fields$1) { this.truth.unique ??= []; this.truth.unique.push(fields$1); return this; }, toTruth() { return modelToStandard(this.truth.name ?? this.toString(), this); } }); } (function(_model) { function is(thing) { if (typeof thing !== "function") return false; return Reflect.get(thing, "truth")?.kind === "model"; } _model.is = is; })(model || (model = {})); function fieldKeys(shape) { return Object.keys(shape).filter((k) => field.is(shape[k])); } //#endregion export { enum_ as i, relation as n, field as r, model as t }; //# sourceMappingURL=model-10gfsxlW.js.map