import { ColumnDefinition , InternalColumn } from './column' ; import { DbConfig , GetResultType } from './config' ; export type TableRow < Config extends DbConfig , T > = T extends TableDefinition < infer Columns > ? { [ K in keyof Columns ] : Columns [ K ] extends ColumnDefinition < infer DataType , infer IsNotNull , boolean > ? IsNotNull extends true ? GetResultType < Config , DataType > : GetResultType < Config , DataType > | GetResultType < Config , 'Null' > : never ; } : never ; export class TableDefinition < Columns > { private _tableDefinitionBrand : any ; } export const makeTable = < TableName extends string , TableDefinition extends { [ column ] : ColumnDefinition < any , any , any > ; } > ( tableName , originalTableName , tableDefinition ) => { const columnNames = Object . keys ( tableDefinition as unknown as object ) as ( keyof TableDefinition ) [ ] ; const columns = columnNames . reduce ( ( map , columnName ) => { const column = new InternalColumn ( columnName as string , tableName , undefined ) as any ; map [ columnName ] = column ; return map ; } , { } as any ) ; const table = { ... columns , as < T extends string > ( alias ) { return makeTable ( alias , tableName , tableDefinition ) as any ; } , getName ( ) { return tableName ; } , getOriginalName ( ) { return originalTableName ; } , } ; return table ; } ; export const defineTable = < Columns extends { [ column ] : ColumnDefinition < any , any , any > ; } > ( tableDefinition ) : TableDefinition < Columns > => { return tableDefinition as any ; } ;