-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a1a1c69
feat: add typescript typings
0da50e2
fix: remove unnecessary named tuple
f2a868e
fix: export plugin options
a2ed13f
fix: sync typescript configuration with fastify
9e1ca07
fix: make type checking faster and more portable
4851fbd
fix: downgrade typescript target to node 10 in examples
10eb8d5
test: add tests for typings
f13afb8
chore: sync tsconfig placement with fastify
9bbd477
fix: use base tsconfig files for examples
b3f2c37
fix: add named export for PostgresPlugin
97deecf
fix: use types from pg namespace
4c9b694
fix: remove pg typings from peerDeps
db10fe8
fix: move examples typechecking to separated script
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| 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 }; |
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
| 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; | ||
| }; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "extends": "@tsconfig/node10/tsconfig.json", | ||
| "compilerOptions": { | ||
| "noEmit": true, | ||
| } | ||
| } | ||
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
| 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 }; |
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
| 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; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "extends": "@tsconfig/node10/tsconfig.json", | ||
| "compilerOptions": { | ||
| "noEmit": true, | ||
| } | ||
| } |
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
| 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 }; |
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
| 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; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "extends": "@tsconfig/node10/tsconfig.json", | ||
| "compilerOptions": { | ||
| "noEmit": true, | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { FastifyPluginCallback } from 'fastify'; | ||
| import * as Pg from 'pg'; | ||
|
|
||
| declare function transact<TResult>( | ||
| fn: (client: Pg.PoolClient) => Promise<TResult> | ||
| ): Promise<TResult>; | ||
|
|
||
| declare function transact<TResult>( | ||
| fn: (client: Pg.PoolClient) => Promise<TResult>, | ||
| cb: (error: Error | null, result?: TResult) => void | ||
| ): void; | ||
|
|
||
| type PostgresDb = { | ||
| pool: Pg.Pool; | ||
| Client: Pg.Client; | ||
| query: Pg.Pool['query']; | ||
| connect: Pg.Pool['connect']; | ||
| transact: typeof transact; | ||
| }; | ||
|
|
||
| type PostgresPluginOptions = { | ||
| /** | ||
| * Custom pg | ||
| */ | ||
| pg?: typeof Pg; | ||
|
|
||
| /** | ||
| * Use pg-native | ||
| */ | ||
| native?: boolean; | ||
|
|
||
| /** | ||
| * Instance name of fastify-postgres | ||
| */ | ||
| name?: string; | ||
| } & Pg.PoolConfig; | ||
|
|
||
| declare const fastifyPostgres: FastifyPluginCallback<PostgresPluginOptions>; | ||
|
|
||
| export { fastifyPostgres, PostgresDb, PostgresPluginOptions }; | ||
| export default fastifyPostgres; |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import defaultPluginImport from '../../index'; | ||
| import { | ||
| fastifyPostgres as namedPluginImport, | ||
| PostgresDb, | ||
| PostgresPluginOptions, | ||
| } from '../../index'; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import fastify from 'fastify'; | ||
| import * as pg from 'pg'; | ||
|
|
||
| import fastifyPostgres from '../../index'; | ||
|
|
||
| const app = fastify(); | ||
|
|
||
| // Without parameters | ||
| app.register(fastifyPostgres); | ||
| app.register(fastifyPostgres, {}); | ||
|
|
||
| // Own pg adapter | ||
| app.register(fastifyPostgres, { pg }); | ||
|
|
||
| // Native libpq wrapper | ||
| app.register(fastifyPostgres, { native: true }); | ||
|
|
||
| // Multiple databases | ||
| app.register(fastifyPostgres, { name: 'users' }); | ||
| app.register(fastifyPostgres, { name: 'posts' }); | ||
|
|
||
| // Pool options | ||
| app.register(fastifyPostgres, { | ||
| user: 'dbuser', | ||
| host: 'database.server.com', | ||
| database: 'mydb', | ||
| password: 'secretpassword', | ||
| port: 3211, | ||
| }); | ||
| app.register(fastifyPostgres, { | ||
| connectionString: 'postgres://user:password@host:port/db', | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import fastify from 'fastify'; | ||
| import { Client, Pool, PoolClient, QueryResult } from 'pg'; | ||
| import { expectType } from 'tsd'; | ||
|
|
||
| import fastifyPostgres, { PostgresDb } from '../../index'; | ||
|
|
||
| const app = fastify(); | ||
|
|
||
| app.register(fastifyPostgres, { | ||
| connectionString: 'postgres://user:password@host:port/db', | ||
| }); | ||
|
|
||
| declare module 'fastify' { | ||
| export interface FastifyInstance { | ||
| pg: PostgresDb; | ||
| } | ||
| } | ||
|
|
||
| app.get('/calc', async () => { | ||
| expectType<PostgresDb>(app.pg); | ||
|
|
||
| expectType<Pool>(app.pg.pool); | ||
| expectType<Client>(app.pg.Client); | ||
|
|
||
| const client = await app.pg.connect(); | ||
| expectType<PoolClient>(client); | ||
|
|
||
| const sumResult = await client.query<{ sum: number }>('SELECT 2 + 2 as sum'); | ||
| expectType<QueryResult<{ sum: number }>>(sumResult); | ||
|
|
||
| client.release(); | ||
|
|
||
| return { | ||
| sum: sumResult.rows, | ||
| }; | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.