import type { PrimaryKey, AlternateKey, ForeignKey, Table, SQLiteColumnTypes, } from "../types.ts"; import { type BuildTransition, buildTransition, withForeignKeyColumnTransition, withOptionalColumnTransition, type WithRequiredColumnTransition, withRequiredColumnTransition, type WithOptionalColumnTransition, type WithForeignKeyColumnTransition, } from "../transitions/index.ts"; export interface WithPrimaryKeyColumn< TPrimaryKey extends Record, TAlternateKeys extends Record, TForeignKeys extends Record, > { table: Table; build: BuildTransition; withRequiredColumn: WithRequiredColumnTransition< TPrimaryKey, TAlternateKeys, TForeignKeys >; withOptionalColumn: WithOptionalColumnTransition< TPrimaryKey, TAlternateKeys, TForeignKeys >; withForeignKeyColumn: WithForeignKeyColumnTransition< TPrimaryKey, TAlternateKeys, TForeignKeys >; } export function withPrimaryKeyColumn< TPrimaryKey extends Record, TAlternateKeys extends Record, TForeignKeys extends Record, const TName extends string, const TType extends SQLiteColumnTypes, >( table: Table, name: TName, type: TType, ): WithPrimaryKeyColumn< TPrimaryKey & Record, TAlternateKeys, TForeignKeys > { const new_table = { ...table, primaryKey: { [name]: { type, isPrimaryKey: true, }, }, } as Table< TPrimaryKey & Record, TAlternateKeys, TForeignKeys >; return { table: new_table, build: buildTransition(new_table), withRequiredColumn: withRequiredColumnTransition(new_table), withOptionalColumn: withOptionalColumnTransition(new_table), withForeignKeyColumn: withForeignKeyColumnTransition(new_table), }; }