Created
December 5, 2024 20:02
-
-
Save JPBM135/2077fe900c7f7c103fc3eda3d5581951 to your computer and use it in GitHub Desktop.
Knex and Pg-to-Ts integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { Knex } from 'knex'; | |
import type { TableTypes } from '../../generated/database.types.js'; | |
type SelectTableType<K extends keyof TableTypes> = TableTypes[K]['select'] & { | |
[P in Exclude<keyof TableTypes[K]['select'], symbol> as `${K}.${P}`]: TableTypes[K]['select'][P]; | |
}; | |
type InsertTableType<K extends keyof TableTypes> = TableTypes[K]['input']; | |
type UpdateTableType<K extends keyof TableTypes> = Partial<Exclude<TableTypes[K]['input'], 'id'>>; | |
type PgToTsTablesTypesToKnexTypes = { | |
[K in keyof TableTypes]: Knex.CompositeTableType< | |
SelectTableType<K>, | |
InsertTableType<K>, | |
UpdateTableType<K> | |
>; | |
}; | |
declare module 'knex/types/tables.d.ts' { | |
interface Tables extends PgToTsTablesTypesToKnexTypes {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment