UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

35 lines (34 loc) 1.1 kB
import { DataSchema, ITEM } from "../../schema/DataSchema.js"; import { NumberSchema } from "../../schema/NumberSchema.js"; /** Default identifier schema (integer). */ export const ID = new NumberSchema({ step: 1, min: Number.MIN_SAFE_INTEGER, max: Number.MAX_SAFE_INTEGER, value: 0, one: "ID", title: "ID", }); /** Declarative definition of a database collection/table. */ export class Collection extends DataSchema { /** Collection name (used as the table/collection key). */ name; /** Schema for the identifier type. */ id; /** Schema for a complete item (id + data). */ item; constructor(name, id, data) { const dataSchema = data instanceof DataSchema ? data : new DataSchema({ props: data }); super({ ...dataSchema, props: dataSchema.props }); this.name = name; this.id = id; this.item = ITEM(id, dataSchema); } toString() { return this.name; } } /** Shortcut factory for creating a Collection. */ export function COLLECTION(name, id, data) { return new Collection(name, id, data); }