A small TypeScript library that wraps a TinyBase Store with a Zod-backed typed API.
You describe your data model once as a Zod schema:
- Tables:
tables: { [tableId]: z.object({ ...cells }) } - Values:
values: z.object({ ...valueIds })
…and createTypedStore(store, schema) returns a typedStore whose get* methods decode and whose set* methods encode, while keeping TinyBase’s API shape (tables/rows/cells/values + listeners + transactions).
Public entrypoint: src/index.ts (re-exports codec, store, and type).
TinyBase cells are expected to be scalars, so this wrapper enforces a simple contract:
- Every schema used in a table cell or value must encode to:
string | number | boolean | null | undefined
undefinedmeans “delete / absent”
If a schema encodes to a non-primitive (for example a plain z.object(...), z.array(...), z.date(), z.bigint(), etc.), writes will throw with a message like:
Invalid encoded value for TinyBase storage at ... Expected string|number|boolean|null (or undefined to delete), got object.
To store structured data, you must wrap it in an explicit codec.
import { createStore } from "tinybase";
import z from "zod";
import {
createTypedStore,
createReadonlyTypedStore,
json,
dateAsIso,
dateAsNumberMs,
} from "tinybase-zod";
const userRow = z.object({
name: z.string(),
age: z.number(),
// Structured cells must be wrapped so they encode to strings.
prefs: json(z.object({ theme: z.enum(["light", "dark"]) })),
tags: json(z.array(z.string())),
// Non-JSON types should use explicit codecs too.
createdAt: dateAsIso,
// Or persist as numbers (ms since epoch).
createdAtMs: dateAsNumberMs,
});
const schema = {
tables: {
users: userRow,
},
values: z.object({
selectedUserId: z.string().nullable(),
}),
} as const;
const store = createStore();
const typed = createTypedStore(store, schema);
const readonly = createReadonlyTypedStore(store, schema);
// Or: const readonly = typed.asReadonly();
// Tests typically initialize tables/values explicitly.
store.setTables({ users: {} });
store.setValues({});
typed.setRow("users", "u1", {
name: "Ava",
age: 34,
prefs: { theme: "dark" },
tags: ["admin"],
});
// Reads are decoded.
console.log(typed.getCell("users", "u1", "prefs")); // { theme: "dark" }
// Underlying TinyBase storage contains JSON strings for json(...) fields.
console.log(store.getCell("users", "u1", "prefs")); // '{"theme":"dark"}'A StoreSchema is:
tables: a map of table ids toz.object({...})row schemasvalues: a singlez.object({...})for global values
createTypedStore does not auto-modify your schema. It uses it as-is.
- Tables are encoded/decoded via
z.record(z.string(), rowSchema). getTable,getRow,getCell,getValuereturn decoded values.setTable,setRow,setCell,setValueencode before writing.setContent([tables, values])encodes both parts and replaces the store content (see TinyBase docs:setContent).
On every write, the wrapper validates that encoded cells are storage scalars.
- When writing rows/tables, any encoded
undefinedcells are stripped. - When writing a single cell/value, if the encoded result is
undefined, the wrapper deletes it.
This means you can model “optional deletes” using codecs that sometimes encode to undefined.
getValue(valueId) returns exactly what your Zod schema describes for that value id:
- If the value schema is required (for example
z.string()), callinggetValuewhen the underlying store value is missing will throw a Zod error. - If the value schema is optional (
z.string().optional()), missing values returnundefined. - If the value schema has a default (
z.string().default("x")), missing values return the default.
This removes the extra | undefined that was previously added by the typed wrapper.
Listener callbacks wrap the underlying TinyBase listener and:
- pass
typedStoreinstead of the raw store - decode
new/oldvalues - decode
getCellChange(...)/getValueChange(...)results when those helper functions are available
Mutator listeners (mutator: true) behave like TinyBase: they run before non-mutators and can safely update state.
If you want to expose a typed store to consumers that should not be able to write, use:
createReadonlyTypedStore(store, schema), ortyped.asReadonly()
Readonly typed stores are type-level only (developer ergonomics):
- reads still decode the same way
- write methods are not present on the TypeScript type
- at runtime, the underlying object is still the same store (no security boundary)
src/codec.ts exports a helper:
json(schema)wraps any JSON-serializable schema and stores it as a JSON string.
It is implemented via z.codec(...) with JSON.stringify / JSON.parse.
If you need null/undefined support, apply wrappers outside the codec:
const maybePrefs = json(z.object({ theme: z.string() })).optional();
const nullablePrefs = json(z.object({ theme: z.string() })).nullable();Use it for:
- objects (
z.object(...)) - arrays (
z.array(...)) - tuples / records / unions that decode to JSON data
For non-JSON types, define your own storage representation:
import { z } from "zod";
export const bigintAsString = z.codec(z.string(), z.bigint(), {
encode: (b) => b.toString(),
decode: (s) => BigInt(s),
});This repo also ships date codecs in src/codec.ts:
dateAsIso(ISO string)dateAsNumberMs/dateAsNumberSeconds(numbers)
This wrapper intentionally avoids a common TinyBase behavior: creating/patching rows implicitly via setCell.
-
typed.setCell(...)throws if the row does not already exist.- This prevents accidentally creating a “partial” row that does not satisfy your Zod row schema.
- Create rows with
setRow(...)first.
-
typed.setRow(...)expects a schema-valid row.- If you want “partial row updates”, model that explicitly:
- make fields optional in your Zod schema, or
- update individual cells with
setCell(...)after creating the row.
- If you want “partial row updates”, model that explicitly:
If you access the underlying TinyBase store directly, you’ll see encoded storage values (for example JSON strings), not decoded runtime objects.
If your underlying store is a TinyBase MergeableStore, the typed wrapper exposes:
typed.setDefaultContent([tables, values])(and the function form)
It accepts typed content (decoded shapes) and encodes it according to your schema before forwarding to the underlying TinyBase method. See TinyBase docs: setDefaultContent.
Because writes call .encode(), any schema that can’t encode will fail at runtime.
Common causes:
- Plain structured schemas (
z.object,z.array, etc.) encode to objects/arrays → wrap withjson(...). - Unidirectional transforms (
z.preprocess,.transform) are not encodable in Zod v4 → use a bidirectionalz.codec(...)instead.
bun installbun testbun run checkbun run buildsrc/store.ts: implementation ofcreateTypedStoresrc/codec.ts: storage codecs likejson(...)anddateAsIsosrc/type.ts: the TypeScript type-level surface forTypedStore<Schema>src/example.ts: a small usage sketchsrc/*.test.ts: runtime behavior testssrc/*.type.test.ts: type-level tests