import { type BuilderEnumColumn, type BuilderIndex, type BuilderJSONColumn, type BuilderManyColumn, type BuilderOneColumn, type BuilderScalarColumn, _enum, bigint, boolean, float, hex, index, int, json, many, one, string, } from "./columns.js"; import type { Column, Constraints, EnumColumn, ExtractEnumNames, ExtractNonVirtualColumnNames, ExtractReferenceColumnNames, ExtractTableNames, IdColumn, Index, JSONColumn, ManyColumn, OneColumn, ReferenceColumn, ScalarColumn, Schema, Table, } from "./common.js"; type GetTable< table, tableName extends string = string, schema = {}, /// tableNames extends string = {} extends schema ? string : ExtractTableNames, enumNames extends string = {} extends schema ? string : ExtractEnumNames, > = {} extends table ? {} : table extends { id: IdColumn; } ? { [columnName in keyof table]: table[columnName] extends ScalarColumn ? ScalarColumn : table[columnName] extends ReferenceColumn ? ReferenceColumn< table[columnName][" scalar"], table[columnName][" optional"], `${tableNames}.id` > : table[columnName] extends JSONColumn ? JSONColumn : table[columnName] extends OneColumn ? OneColumn> : table[columnName] extends ManyColumn ? {} extends schema ? ManyColumn : table[columnName] extends ManyColumn ? ManyColumn< table[columnName][" referenceTable"], ExtractReferenceColumnNames< schema[table[columnName][" referenceTable"] & keyof schema], tableName > & string > : ManyColumn> : table[columnName] extends EnumColumn ? EnumColumn : Column; } : { id: IdColumn } & { [columnName: string]: Column; }; type GetConstraints< constraints, table, /// columnName extends string = ExtractNonVirtualColumnNames, > = {} extends constraints ? {} : { [name in keyof constraints]: Index; }; export const createTable = ( t: GetTable
, c?: GetConstraints, ): { table: table; constraints: constraints } => ({ table: t as table, constraints: c as constraints, }); export const createEnum = (e: _enum) => e; const P = { createTable, createEnum, string, bigint, int, float, hex, boolean, json, one, many, enum: _enum, index, }; type P = { /** * Create a database table. * * - Docs: https://ponder.sh/docs/schema#tables * * @example * export default createSchema((p) => ({ * t: p.createTable({ * id: p.string(), * }) * })); */ createTable: ( t: GetTable
, c?: GetConstraints, ) => { table: table; constraints: constraints }; /** * Create an Enum type for the database. * * - Docs: https://ponder.sh/docs/schema#tables * * @example * export default createSchema((p) => ({ * e: p.createEnum(["ONE", "TWO"]) * t: p.createTable({ * id: p.string(), * a: p.enum("e"), * }) * })); */ createEnum: (e: _enum) => _enum; /** * Primitive `string` column type. * * - Docs: https://ponder.sh/docs/schema#primitives * * @example * import { createSchema } from "@ponder/core"; * * export default createSchema((p) => ({ * t: p.createTable({ * id: p.string(), * }) * })); */ string: () => BuilderScalarColumn<"string", false, false>; /** * Primitive `bigint` column type. * * - Docs: https://ponder.sh/docs/schema#primitives * * @example * import { createSchema } from "@ponder/core"; * * export default createSchema((p) => ({ * t: p.createTable({ * id: p.bigint(), * }) * })); */ bigint: () => BuilderScalarColumn<"bigint", false, false>; /** * Primitive `int` column type. * * - Docs: https://ponder.sh/docs/schema#primitives * * @example * import { createSchema } from "@ponder/core"; * * export default createSchema((p) => ({ * t: p.createTable({ * id: p.int(), * }) * })); */ int: () => BuilderScalarColumn<"int", false, false>; /** * Primitive `float` column type. * * - Docs: https://ponder.sh/docs/schema#primitives * * @example * import { createSchema } from "@ponder/core"; * * export default createSchema((p) => ({ * t: p.createTable({ * id: p.string(), * f: p.float(), * }) * })); */ float: () => BuilderScalarColumn<"float", false, false>; /** * Primitive `hex` column type. * * - Docs: https://ponder.sh/docs/schema#primitives * * @example * import { createSchema } from "@ponder/core"; * * export default createSchema((p) => ({ * t: p.createTable({ * id: p.hex(), * }) * })); */ hex: () => BuilderScalarColumn<"hex", false, false>; /** * Primitive `boolean` column type. * * - Docs: https://ponder.sh/docs/schema#primitives * * @example * import { createSchema } from "@ponder/core"; * * export default createSchema((p) => ({ * t: p.createTable({ * id: p.string(), * b: p.boolean(), * }) * })); */ boolean: () => BuilderScalarColumn<"boolean", false, false>; /** * Primitive `JSON` column type. * * - Docs: https://ponder.sh/docs/schema#primitives * * @example * import { createSchema } from "@ponder/core"; * * export default createSchema((p) => ({ * t: p.createTable({ * id: p.string(), * b: p.json(), * }) * })); */ json: () => BuilderJSONColumn; /** * One-to-one column type.`one` columns don't exist in the database. They are only present when querying data from the GraphQL API. * * - Docs: https://ponder.sh/docs/schema#one-to-one * * @param reference Reference column to be resolved. * * @example * import { createSchema } from "@ponder/core"; * * export default createSchema((p) => ({ * a: p.createTable({ * id: p.string(), * b_id: p.string.references("b.id"), * b: p.one("b_id"), * }) * b: p.createTable({ * id: p.string(), * }) * })); */ one: ( ref: reference, ) => BuilderOneColumn; /** * Many-to-one column type. `many` columns don't exist in the database. They are only present when querying data from the GraphQL API. * * - Docs: https://ponder.sh/docs/schema#one-to-many * * @param reference Reference column that references the `id` column of the current table. * * @example * import { createSchema } from "@ponder/core"; * * export default createSchema((p) => ({ * a: p.createTable({ * id: p.string(), * ref: p.string.references("b.id"), * }) * b: p.createTable({ * id: p.string(), * m: p.many("a.ref"), * }) * })); */ many: ( ref: `${referenceTable}.${referenceColumn}`, ) => BuilderManyColumn; /** * Custom defined allowable value column type. * * - Docs: https://ponder.sh/docs/schema#enum * * @param type Enum defined elsewhere in the schema with `p.createEnum()`. * * @example * export default createSchema((p) => ({ * e: p.createEnum(["ONE", "TWO"]) * t: p.createTable({ * id: p.string(), * a: p.enum("e"), * }) * })); */ enum: <_enum extends string>( __enum: _enum, ) => BuilderEnumColumn<_enum, false, false>; /** * Create a table index. * * - Docs: https://ponder.sh/docs/schema#indexes * * @param columns Column or columns to include in the index. * * @example * export default createSchema((p) => ({ * t: p.createTable({ * id: p.string(), * age: p.int(), * }, { * ageIndex: p.index("age"), * }) * })); */ index: ( c: column, ) => BuilderIndex; }; type CreateSchemaParameters = {} extends schema ? {} : { [tableName in keyof schema]: schema[tableName] extends { table: infer table extends Table; constraints: infer constraints extends Constraints; } ? { table: GetTable; constraints: GetConstraints; } : readonly string[]; }; export const createSchema = ( _schema: (p: P) => CreateSchemaParameters, ): unknown extends schema ? Schema : schema => { // @ts-ignore return _schema(P) as schema; };