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
SELECT name, country, email FROM users |
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
CREATE TABLE analytics ( | |
id SERIAL PRIMARY KEY, | |
date TIMESTAMP WITH TIME ZONE NOT NULL, | |
slug VARCHAR NOT NULL, | |
referrer VARCHAR, | |
flag VARCHAR, | |
country VARCHAR, | |
city VARCHAR, | |
latitude DECIMAL, | |
longitude DECIMAL |
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 { Users } from '../../pg-to-ts-db'; | |
const response = await client.query<Users[]>('SELECT * FROM users'); |
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
// ./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 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
DATABASE_URL=postgresql://... npm run pg-to-ts-generate |
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
// package.json | |
"scripts": { | |
... | |
"pg-to-ts-generate": "pg-to-ts generate -c $DATABASE_URL -o ./pg-to-ts-db.d.ts" | |
}, |
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
npm install --save-dev pg-to-ts |
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 { Users } from '../../kysely-db'; | |
const response = await client.query<Users[]>('SELECT * FROM users'); |
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
// ./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 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
npm run kysely-generate |