UNPKG

loro-mirror

Version:

Type-safe state management synchronized with Loro CRDT via a declarative schema and bidirectional mirroring.

165 lines 3.78 kB
export * from "./types"; export * from "./validators"; /** * Create a schema definition */ export function schema(definition, options) { return { type: "schema", definition, options: options || {}, getContainerType() { return "Map"; }, }; } /** * Define a string field */ schema.String = function (options) { return { type: "string", options: (options || {}), getContainerType: () => { return null; }, }; }; /** * Define a number field */ schema.Number = function (options) { return { type: "number", options: options || {}, getContainerType: () => { return null; // Primitive type, no container }, }; }; /** * Define a boolean field */ schema.Boolean = function (options) { return { type: "boolean", options: options || {}, getContainerType: () => { return null; // Primitive type, no container }, }; }; /** * Define a field to be ignored (not synced with Loro) */ schema.Ignore = function (options) { return { type: "ignore", options: options || {}, getContainerType: () => { return null; }, }; }; /** * Define a Loro map */ schema.LoroMap = function (definition, options) { const baseSchema = { type: "loro-map", definition, options: options || {}, getContainerType: () => { return "Map"; }, }; // Add catchall method like zod const schemaWithCatchall = { ...baseSchema, catchall: (catchallSchema) => { return { ...baseSchema, catchallType: catchallSchema, catchall: (newCatchallSchema) => { return { ...baseSchema, catchallType: newCatchallSchema, catchall: schemaWithCatchall.catchall, }; }, }; }, }; return schemaWithCatchall; }; /** * Create a dynamic record schema (like zod's z.record) */ schema.LoroMapRecord = function (valueSchema, options) { return { type: "loro-map", definition: {}, catchallType: valueSchema, options: options || {}, getContainerType: () => { return "Map"; }, catchall: (newCatchallSchema) => { return schema.LoroMapRecord(newCatchallSchema, options); }, }; }; /** * Define a Loro list */ schema.LoroList = function (itemSchema, idSelector, options) { return { type: "loro-list", itemSchema, idSelector: idSelector, options: options || {}, getContainerType: () => { return "List"; }, }; }; schema.LoroMovableList = function (itemSchema, idSelector, options) { return { type: "loro-movable-list", itemSchema, idSelector: idSelector, options: options || {}, getContainerType: () => { return "MovableList"; }, }; }; /** * Define a Loro text field */ schema.LoroText = function (options) { return { type: "loro-text", options: options || {}, getContainerType: () => { return "Text"; }, }; }; /** * Define a Loro tree * * Each tree node has a `data` map described by `nodeSchema`. */ // oxlint-disable-next-line no-explicit-any schema.LoroTree = function (nodeSchema, options) { return { type: "loro-tree", nodeSchema, options: options || {}, getContainerType() { return "Tree"; }, }; }; //# sourceMappingURL=index.js.map