-
-
Notifications
You must be signed in to change notification settings - Fork 36
Add TypeScript typings #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a1a1c69
0da50e2
f2a868e
a2ed13f
9e1ca07
4851fbd
10eb8d5
f13afb8
9bbd477
b3f2c37
97deecf
4c9b694
db10fe8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import fastify from 'fastify'; | ||
|
|
||
| import fastifyPostgres from '../../../index'; | ||
|
|
||
| const app = fastify(); | ||
|
|
||
| app.register(fastifyPostgres, { | ||
| name: 'sum', | ||
| connectionString: 'postgres://user:password@host:port/sub-db', | ||
| }); | ||
|
|
||
| app.register(fastifyPostgres, { | ||
| name: 'sub', | ||
| connectionString: 'postgres://user:password@host:port/sub-db', | ||
| }); | ||
|
|
||
| app.get('/calc', async () => { | ||
| const sumClient = await app.pg.sum.connect(); | ||
| const subClient = await app.pg.sub.connect(); | ||
|
|
||
| const sumResult = await sumClient.query<{ sum: number }>( | ||
| 'SELECT 2 + 2 as sum' | ||
| ); | ||
| const subResult = await subClient.query<{ sub: number }>( | ||
| 'SELECT 6 - 3 as sub' | ||
| ); | ||
|
|
||
| sumClient.release(); | ||
| subClient.release(); | ||
|
|
||
| return { | ||
| sum: sumResult.rows, | ||
| sub: subResult.rows, | ||
| }; | ||
| }); | ||
|
|
||
| export { app }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { PostgresDb } from '../../../index'; | ||
|
|
||
| declare module 'fastify' { | ||
| export interface FastifyInstance { | ||
| pg: { | ||
| sum: PostgresDb; | ||
| sub: PostgresDb; | ||
| }; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "include": ["."], | ||
| "compilerOptions": { | ||
| /* Basic Options */ | ||
| "noEmit": true, | ||
| "lib": ["ES2020"], | ||
|
||
|
|
||
| /* Module Resolution Options */ | ||
| "esModuleInterop": true, | ||
| "resolveJsonModule": true, | ||
| "moduleResolution": "Node", | ||
fox1t marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /* Strict Type-Checking Options */ | ||
| "strict": true, | ||
|
|
||
| /* Additional Checks */ | ||
| "noUnusedLocals": true, | ||
| "noUnusedParameters": true, | ||
| "noImplicitReturns": true, | ||
| "noFallthroughCasesInSwitch": true | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import fastify from 'fastify'; | ||
|
|
||
| import fastifyPostgres from '../../../index'; | ||
|
|
||
| const app = fastify(); | ||
|
|
||
| app.register(fastifyPostgres, { | ||
| connectionString: 'postgres://user:password@host:port/db', | ||
| }); | ||
|
|
||
| app.get('/calc', async () => { | ||
| const client = await app.pg.connect(); | ||
|
|
||
| const sumResult = await client.query<{ sum: number }>('SELECT 2 + 2 as sum'); | ||
|
|
||
| client.release(); | ||
|
|
||
| return { | ||
| sum: sumResult.rows, | ||
| }; | ||
| }); | ||
|
|
||
| export { app }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type { PostgresDb } from '../../../index'; | ||
|
|
||
| declare module 'fastify' { | ||
| export interface FastifyInstance { | ||
| pg: PostgresDb; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "include": ["."], | ||
| "compilerOptions": { | ||
| /* Basic Options */ | ||
| "noEmit": true, | ||
| "lib": ["ES2020"], | ||
|
|
||
| /* Module Resolution Options */ | ||
| "esModuleInterop": true, | ||
| "resolveJsonModule": true, | ||
| "moduleResolution": "Node", | ||
fox1t marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /* Strict Type-Checking Options */ | ||
| "strict": true, | ||
|
|
||
| /* Additional Checks */ | ||
| "noUnusedLocals": true, | ||
| "noUnusedParameters": true, | ||
| "noImplicitReturns": true, | ||
| "noFallthroughCasesInSwitch": true | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import fastify from 'fastify'; | ||
|
|
||
| import fastifyPostgres from '../../../index'; | ||
|
|
||
| const app = fastify(); | ||
|
|
||
| app.register(fastifyPostgres, { | ||
| connectionString: 'postgres://user:password@host:port/db', | ||
| }); | ||
|
|
||
| app.post('/init-async', async () => { | ||
| const createTableQuery = ` | ||
| CREATE TABLE routes ( | ||
| id bigserial primary key, | ||
| name varchar(80) NOT NULL, | ||
| created_at timestamp default NULL | ||
| ); | ||
| `; | ||
|
|
||
| return app.pg.transact(async (client) => { | ||
| const result = await client.query(createTableQuery); | ||
|
|
||
| return result; | ||
| }); | ||
| }); | ||
|
|
||
| app.post('/init-cb', (_req, reply) => { | ||
| const createTableQuery = ` | ||
| CREATE TABLE routes ( | ||
| id bigserial primary key, | ||
| name varchar(80) NOT NULL, | ||
| created_at timestamp default NULL | ||
| ); | ||
| `; | ||
|
|
||
| app.pg.transact( | ||
| (client) => { | ||
| return client.query(createTableQuery); | ||
| }, | ||
| (error, result) => { | ||
| if (error) { | ||
| reply.status(500).send(error); | ||
| return; | ||
| } | ||
|
|
||
| reply.status(200).send(result); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| export { app }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type { PostgresDb } from '../../../index'; | ||
|
|
||
| declare module 'fastify' { | ||
| export interface FastifyInstance { | ||
| pg: PostgresDb; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "include": ["."], | ||
| "compilerOptions": { | ||
| /* Basic Options */ | ||
| "noEmit": true, | ||
| "lib": ["ES2020"], | ||
|
|
||
| /* Module Resolution Options */ | ||
| "esModuleInterop": true, | ||
| "resolveJsonModule": true, | ||
| "moduleResolution": "Node", | ||
fox1t marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /* Strict Type-Checking Options */ | ||
| "strict": true, | ||
|
|
||
| /* Additional Checks */ | ||
| "noUnusedLocals": true, | ||
| "noUnusedParameters": true, | ||
| "noImplicitReturns": true, | ||
| "noFallthroughCasesInSwitch": true | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import type { FastifyPluginCallback } from 'fastify'; | ||
| import type PgAdapter from 'pg'; | ||
| import type { | ||
| Client as PgClient, | ||
| Pool as PgPool, | ||
| PoolClient as PgPoolClient, | ||
| PoolConfig as PgPoolConfig, | ||
| } from 'pg'; | ||
|
|
||
| type CallbackArgs<TResult> = | ||
| | [error: null, result: TResult] | ||
| | [error: unknown, result: undefined]; | ||
|
|
||
| declare function transact<TResult>( | ||
| fn: (client: PgPoolClient) => Promise<TResult>, | ||
| ): Promise<TResult>; | ||
|
|
||
| declare function transact<TResult>( | ||
| fn: (client: PgPoolClient) => Promise<TResult>, | ||
| cb: (...args: CallbackArgs<TResult>) => void, | ||
| ): void; | ||
|
|
||
| type PostgresDb = { | ||
| pool: PgPool; | ||
| Client: PgClient; | ||
| query: PgPool['query']; | ||
| connect: PgPool['connect']; | ||
| transact: typeof transact; | ||
| }; | ||
|
|
||
| type Options = { | ||
|
||
| /** | ||
| * Custom pg adapter | ||
| */ | ||
| pg?: typeof PgAdapter; | ||
|
|
||
| /** | ||
| * Use pg-native | ||
| */ | ||
| native?: boolean; | ||
|
|
||
| /** | ||
| * Instance name of fastify-postgres | ||
| */ | ||
| name?: string; | ||
| }; | ||
|
|
||
| declare const PostgresPlugin: FastifyPluginCallback<Options & PgPoolConfig>; | ||
|
|
||
| export type { PostgresDb }; | ||
| export default PostgresPlugin; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #!/usr/bin/env sh | ||
|
|
||
| set -ex | ||
|
|
||
| npx tsc --project ./examples/typescript/single-db/tsconfig.json | ||
|
||
| npx tsc --project ./examples/typescript/multiple-db/tsconfig.json | ||
| npx tsc --project ./examples/typescript/transactions/tsconfig.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "include": ["./index.d.ts"], | ||
|
|
||
|
||
| "compilerOptions": { | ||
| "noEmit": true, | ||
| "esModuleInterop": true, | ||
|
|
||
| "strict": true, | ||
| "noUnusedLocals": true, | ||
| "noUnusedParameters": true, | ||
| "noImplicitReturns": true, | ||
| "noFallthroughCasesInSwitch": true, | ||
| }, | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.