TinyBase logoTinyBase β

CellSchema

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

{
  type: "string";
  default?: string | null;
  allowNull?: boolean;
  required?: boolean;
} | {
  type: "number";
  default?: number | null;
  allowNull?: boolean;
  required?: boolean;
} | {
  type: "boolean";
  default?: boolean | null;
  allowNull?: boolean;
  required?: boolean;
} | {
  type: "object";
  default?: AnyObject;
  allowNull?: boolean;
  required?: boolean;
} | {
  type: "array";
  default?: AnyArray;
  allowNull?: boolean;
  required?: 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. Their contents should recursively be strings, finite numbers, booleans, null, plain objects, or arrays to ensure they are preserved.

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. You can also set required to true to indicate to schema-based typing that the Cell should be present even if it does not have a default.

If neither a default value nor required: true is provided, 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 expects a string Cell to be present without providing a default value.

import type {CellSchema} from 'tinybase';

export const requiredString: CellSchema = {
  type: 'string',
  required: true,
};

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

import type {CellSchema} from 'tinybase';

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

Since

v1.0.0