Skip to content

Instantly share code, notes, and snippets.

View PaulieScanlon's full-sized avatar

Paul Scanlon PaulieScanlon

View GitHub Profile
@PaulieScanlon
PaulieScanlon / query.ts
Created April 25, 2024 19:43
Example of typed query
import type { Users } from '../../pg-to-ts-db';
const response = await client.query<Users[]>('SELECT * FROM users');
@PaulieScanlon
PaulieScanlon / pg-to-ts-db.d.t
Created April 25, 2024 19:42
pg-to-ts generated types
// ./pg-to-ts-db.d.ts
// Table users
export interface Users {
id: number;
first_name: string;
last_name: string;
email: string;
country: string | null;
}
@PaulieScanlon
PaulieScanlon / .sh
Created April 25, 2024 19:41
pg-to-ts-generate run comand
DATABASE_URL=postgresql://... npm run pg-to-ts-generate
@PaulieScanlon
PaulieScanlon / package.json
Created April 25, 2024 19:40
pg-to-ts-generate
// package.json
"scripts": {
...
"pg-to-ts-generate": "pg-to-ts generate -c $DATABASE_URL -o ./pg-to-ts-db.d.ts"
},
@PaulieScanlon
PaulieScanlon / .sh
Created April 25, 2024 19:38
npm install script pg-to-ts
npm install --save-dev pg-to-ts
@PaulieScanlon
PaulieScanlon / query.ts
Created April 25, 2024 19:37
Example of typed query
import type { Users } from '../../kysely-db';
const response = await client.query<Users[]>('SELECT * FROM users');
@PaulieScanlon
PaulieScanlon / kysely-db.d.ts
Created April 25, 2024 19:36
Kysely generated types
// ./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;
@PaulieScanlon
PaulieScanlon / .sh
Created April 25, 2024 19:35
kysely-generate run comand
npm run kysely-generate
@PaulieScanlon
PaulieScanlon / .env
Created April 25, 2024 19:34
DATABASE_URL in .env
// .env
DATABASE_URL="postgresql://..."
@PaulieScanlon
PaulieScanlon / package.json
Created April 25, 2024 19:33
kysely-generate script
// package.json
"scripts": {
...
"kysely-generate": "kysely-codegen --out-file ./kysely-db.d.ts",
},