TinyBase logoTinyBase

CellSchema

The CellSchema type describes what values are allowed for each Cell in a Table.

{
  type: "string";
  default?: string | null;
  allowNull?: boolean;
} | {
  type: "number";
  default?: number | null;
  allowNull?: boolean;
} | {
  type: "boolean";
  default?: boolean | null;
  allowNull?: boolean;
} | {
  type: "object";
  default?: AnyObject;
  allowNull?: boolean;
} | {
  type: "array";
  default?: AnyArray;
  allowNull?: boolean;
}

A CellSchema specifies the type of the Cell (string, boolean, number, null since v7.0, or object or array since v8.0), and what the default value can be when an explicit value is not specified.

For object and array types, TinyBase automatically serializes values to and from JSON when storing and retrieving them. This means you can set an object or array Cell directly, and get back the same structure.

If a default value is provided (and its type is correct), you can be certain that that Cell will always be present in a Row.

If the default value is not provided (or its type is incorrect), the Cell may be missing from the Row, but when present you can be guaranteed it is of the correct type.

Examples

When applied to a Store, this CellSchema ensures a boolean Cell is always present, and defaults it to false.

import type {CellSchema} from 'tinybase';

export const requiredBoolean: CellSchema = {
  type: 'boolean',
  default: false,
};

When applied to a Store, this CellSchema allows an object Cell containing arbitrary data, defaulting to an empty object.

import type {CellSchema} from 'tinybase';

export const tagsCell: CellSchema = {
  type: 'object',
  default: {},
};

Since

v1.0.0