UNPKG

squid

Version:

Provides SQL tagged template strings and a schema definition function.

69 lines (68 loc) 2.13 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = __importDefault(require("debug")); var ColumnType; (function (ColumnType) { ColumnType["Any"] = "any"; ColumnType["Array"] = "array"; ColumnType["Boolean"] = "boolean"; ColumnType["Date"] = "date"; ColumnType["Enum"] = "enum"; ColumnType["JSON"] = "json"; ColumnType["Number"] = "number"; ColumnType["Object"] = "object"; ColumnType["String"] = "string"; })(ColumnType = exports.ColumnType || (exports.ColumnType = {})); const debugSchema = debug_1.default("squid:schema"); /** * Schema types to declare the data type of table columns. */ exports.Schema = { Any: { type: ColumnType.Any }, Boolean: { type: ColumnType.Boolean }, Date: { type: ColumnType.Date }, Number: { type: ColumnType.Number }, String: { type: ColumnType.String }, Array(elementType) { return { type: ColumnType.Array, subtype: elementType }; }, Enum(values) { return { type: ColumnType.Enum, enum: values }; }, JSON(subtype) { return { type: ColumnType.JSON, subtype }; }, Object(props) { return { type: ColumnType.Object, props }; }, default(column) { return Object.assign(Object.assign({}, column), { hasDefault: true }); }, nullable(column) { return Object.assign(Object.assign({}, column), { hasDefault: true, nullable: true }); } }; const allTableSchemas = []; /** * Declare a table's schema. Registers the schema globally. */ function defineTable(tableName, schema) { debugSchema(`Defining schema for table ${tableName}:`, schema); const table = { name: tableName, columns: schema }; allTableSchemas.push(table); return table; } exports.defineTable = defineTable; /** * Return an array of all defined table schemas. */ function getAllTableSchemas() { return allTableSchemas; } exports.getAllTableSchemas = getAllTableSchemas;