CellSchema
The CellSchema type describes what values are allowed for each Cell in a Table.
{
type: "string";
default?: string | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
type: "number";
default?: number | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
type: "boolean";
default?: boolean | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
type: "object";
default?: AnyObject | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
type: "array";
default?: AnyArray | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
type: SchemaTypeArray;
default?: string | number | boolean | AnyObject | AnyArray | null;
allowNull?: boolean;
required?: boolean;
enum?: never;
} | {
enum: readonly [string | number | boolean, ...(string | number | boolean)[]];
type?: never;
default?: string | number | boolean | null;
allowNull?: boolean;
required?: boolean;
}A CellSchema specifies either the type of the Cell (string, boolean, number, object, or array), a non-empty array of those types, or a non-empty enum of exact primitive values that are allowed. Multiple type names form a union, such as type: ['string', 'number'], and repeated names have no additional effect. The type and enum properties are mutually exclusive, and enum members can be strings, finite numbers, or booleans, including a mixture of those types.
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.
Set allowNull to true to also allow null, whether the schema uses type or enum. A default value is used only when it has the correct type, matches any member of a type union, is an enum member when applicable, or is null when allowed. Literal schemas passed to Store schema setters are checked against these default rules by TypeScript. A valid default means that the 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: {},
};
When applied to a Store, this CellSchema allows either a string or numeric Cell.
import type {CellSchema} from 'tinybase';
export const referenceCell: CellSchema = {
type: ['string', 'number'],
};
When applied to a Store, this CellSchema allows one of three exact primitive values, or null.
import type {CellSchema} from 'tinybase';
export const ratingCell: CellSchema = {
enum: ['good', 5, true],
allowNull: true,
default: 'good',
};
Since
v1.0.0