import {createSchema} from '../../../zero-schema/src/builder/schema-builder.ts'; import { boolean, enumeration, json, number, string, table, } from '../../../zero-schema/src/builder/table-builder.ts'; type DocReviewKind = 'edge_case' | 'normal'; type DocReviewState = 'active' | 'archived'; const jsonCols = { str: json(), num: json(), bool: json(), nil: json(), obj: json<{foo: string}>(), arr: json(), } as const; export const schema = createSchema({ tables: [ table('basic') .columns({ id: string(), a: number(), b: string(), c: boolean().optional(), }) .primaryKey('id'), table('names') .from('divergent_names') .columns({ id: string().from('divergent_id'), a: number().from('divergent_a'), b: string().from('divergent_b'), c: boolean().from('divergent_c').optional(), }) .primaryKey('id'), table('compoundPk') .columns({ a: string(), b: number(), c: string().optional(), }) .primaryKey('a', 'b'), table('dateTypes') .columns({ ts: number(), tstz: number(), tswtz: number(), tswotz: number(), d: number(), }) .primaryKey('ts'), table('arrayCases') .columns({ id: string(), tags: json().optional(), }) .primaryKey('id'), table('jsonCases') .columns({ ...jsonCols, str: string(), }) .primaryKey('str'), table('jsonbCases').columns(jsonCols).primaryKey('str'), table('typesWithParams') .from('types_with_params') .columns({ id: string(), char: string(), bpchar: string(), varchar: string(), numeric: number(), decimal: number(), }) .primaryKey('id'), table('uuidAndEnum') .columns({ id: string(), reference_id: string(), status: enumeration<'active' | 'inactive' | 'pending'>(), type: enumeration<'user' | 'system' | 'admin'>(), }) .primaryKey('id'), table('alternate_basic') .from('alternate_schema.basic') .columns({ id: string(), a: number(), b: string(), c: boolean().optional(), }) .primaryKey('id'), table('textRepresentedScalars') .columns({ id: string(), ip: string(), mac: string(), title: string(), }) .primaryKey('id'), // `style` uses an enum in `public` named `line`, like the built-in // geometric type. See `builtinNamedEnum` in `schemaSql`. table('builtinNamedEnum') .columns({ id: string(), style: enumeration<'solid' | 'dashed'>(), }) .primaryKey('id'), // A table in a non-public Postgres schema whose enum columns (and enum // array column) use enum types that also live in that non-public schema. // The `priority` column is backed by a domain in the non-public schema to // exercise domain-backed columns. See `docReviewSchemaSql`. table('docReviewItem') .from('app.doc_review_item') .columns({ id: string(), kind: enumeration(), state: enumeration(), kinds: json().optional(), priority: number().optional(), }) .primaryKey('id'), ], relationships: [], enableLegacyMutators: true, enableLegacyQueries: true, }); export const schemaSql = `CREATE EXTENSION IF NOT EXISTS isn; CREATE TABLE basic ( id TEXT PRIMARY KEY, a INTEGER, b TEXT, C BOOLEAN ); CREATE TABLE divergent_names ( divergent_id TEXT PRIMARY KEY, divergent_a INTEGER, divergent_b TEXT, divergent_c BOOLEAN ); CREATE TABLE "compoundPk" ( a TEXT, b INTEGER, c TEXT, PRIMARY KEY (a, b) ); CREATE TABLE "dateTypes" ( "ts" TIMESTAMP, "tstz" TIMESTAMPTZ, "tswtz" TIMESTAMP WITH TIME ZONE, "tswotz" TIMESTAMP WITHOUT TIME ZONE, "d" DATE, PRIMARY KEY ("ts") ); CREATE TABLE "arrayCases" ( "id" TEXT PRIMARY KEY, "tags" TEXT[] ); CREATE TABLE "jsonbCases" ( "str" JSONB, "num" JSONB, "bool" JSONB, "nil" JSONB, "obj" JSONB, "arr" JSONB, PRIMARY KEY ("str") ); CREATE TABLE "jsonCases" ( "str" TEXT, "num" JSON, "bool" JSON, "nil" JSON, "obj" JSON, "arr" JSON, PRIMARY KEY ("str") ); CREATE TABLE types_with_params ( id TEXT PRIMARY KEY, char CHAR(10), bpchar BPCHAR, varchar VARCHAR(20), numeric NUMERIC(8, 3), decimal DECIMAL(10, 5) ); CREATE TYPE "statusEnum" AS ENUM ('active', 'inactive', 'pending'); CREATE TYPE type_enum AS ENUM ('user', 'system', 'admin'); CREATE TABLE "uuidAndEnum" ( "id" UUID PRIMARY KEY, "reference_id" UUID NOT NULL, "status" "statusEnum" NOT NULL, "type" type_enum NOT NULL ); CREATE SCHEMA alternate_schema; CREATE TABLE alternate_schema.basic ( id TEXT PRIMARY KEY, a INTEGER, b TEXT, C BOOLEAN ); CREATE TABLE "textRepresentedScalars" ( "id" ISBN13 PRIMARY KEY, "ip" INET NOT NULL, "mac" MACADDR NOT NULL, "title" TEXT NOT NULL ); -- An enum in public with the name of a built-in type. Postgres searches -- pg_catalog before search_path, so an unqualified "line" cast resolves to the -- built-in geometric type rather than this enum. CREATE TYPE public.line AS ENUM ('solid', 'dashed'); CREATE TABLE "builtinNamedEnum" ( "id" TEXT PRIMARY KEY, "style" public.line NOT NULL ); -- A table living in a non-public schema, whose enum (and enum array) columns -- reference enum types declared in that same non-public schema, plus a column -- backed by a domain declared in that schema. Generated CRUD SQL must -- schema-qualify the enum casts (e.g. "app"."doc_review_item_kind") so that -- they resolve under the default search_path ("$user", public). CREATE SCHEMA app; CREATE TYPE app.doc_review_item_kind AS ENUM ('edge_case', 'normal'); CREATE TYPE app.doc_review_item_state AS ENUM ('active', 'archived'); -- A domain in the non-public schema. Postgres' information_schema reports -- domain columns as their underlying base type, so Zero treats them as the -- base type and never needs to reference the domain name in a cast. CREATE DOMAIN app.priority AS INTEGER CHECK (VALUE >= 0); CREATE TABLE app.doc_review_item ( id TEXT PRIMARY KEY, kind app.doc_review_item_kind NOT NULL, state app.doc_review_item_state NOT NULL, kinds app.doc_review_item_kind[], priority app.priority ); `; export const seedDataSql = ` INSERT INTO basic (id, a, b, c) VALUES ('1', 2, 'foo', true); INSERT INTO divergent_names (divergent_id, divergent_a, divergent_b, divergent_c) VALUES ('2', 3, 'bar', false); INSERT INTO "compoundPk" (a, b, c) VALUES ('a', 1, 'c'); INSERT INTO "dateTypes" (ts, tstz, tswtz, tswotz) VALUES ( '2021-01-01 00:00:01', '2022-02-02 00:00:02', '2023-03-03 00:00:03', '2024-04-04 00:00:04' ); `;