This file contains 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 { Users } from '../../pg-to-ts-db'; | |
const response = await client.query<Users[]>('SELECT * FROM users'); |
This file contains 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
// ./pg-to-ts-db.d.ts | |
// Table users | |
export interface Users { | |
id: number; | |
first_name: string; | |
last_name: string; | |
email: string; | |
country: string | null; | |
} |
This file contains 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
DATABASE_URL=postgresql://... npm run pg-to-ts-generate |
This file contains 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
// package.json | |
"scripts": { | |
... | |
"pg-to-ts-generate": "pg-to-ts generate -c $DATABASE_URL -o ./pg-to-ts-db.d.ts" | |
}, |
This file contains 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
npm install --save-dev pg-to-ts |
This file contains 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 { Users } from '../../kysely-db'; | |
const response = await client.query<Users[]>('SELECT * FROM users'); |
This file contains 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
// ./kysely-db.d.ts | |
import type { ColumnType } from 'kysely'; | |
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U> | |
? ColumnType<S, I | undefined, U> | |
: ColumnType<T, T | undefined, T>; | |
export interface Users { | |
country: string | null; |
This file contains 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
npm run kysely-generate |
This file contains 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
// .env | |
DATABASE_URL="postgresql://..." |
This file contains 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
// package.json | |
"scripts": { | |
... | |
"kysely-generate": "kysely-codegen --out-file ./kysely-db.d.ts", | |
}, |
NewerOlder