TinyBase logoTinyBase β

toTablesSchema

The toTablesSchema method converts a mapping of Zod object schemas into a TinyBase TablesSchema.

toTablesSchema(schemas: {[tableId: string]: any}): TablesSchema
TypeDescription
schemas{[tableId: string]: any}

A mapping of table IDs to Zod object schemas.

returnsTablesSchema

A TinyBase TablesSchema.

This method extracts basic type information (string, number, boolean), basic type unions, primitive enums and literals, default values, nullable flags, and required flags from Zod schemas. Complex validation rules like min/max, regex patterns, refinements, and transforms are ignored.

Example

This example converts Zod schemas to TinyBase format.

import {createStore} from 'tinybase';
import {createZodSchematizer} from 'tinybase/schematizers/schematizer-zod';
import {z} from 'zod';

const schematizer = createZodSchematizer();

const tablesSchema = schematizer.toTablesSchema({
  pets: z.object({
    species: z.enum(['dog', 'cat']),
    age: z.number(),
    sold: z.boolean().default(false),
  }),
});

const store = createStore().setTablesSchema(tablesSchema);
store.setRow('pets', 'fido', {species: 'dog', age: 3});
console.log(store.getRow('pets', 'fido'));
// -> {species: 'dog', age: 3, sold: false}

Since

v7.1.0