From fe3f5be7538a091435c2edee120044ffdb2de867 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Tue, 24 May 2022 16:55:54 +0100 Subject: [PATCH 01/10] ci: bring into line with reusable workflow (#133) --- .github/workflows/ci.yml | 72 +++++++++++++++++++++++----------------- README.md | 2 -- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d7038c0..b5e27fd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,22 +1,38 @@ name: CI -'on': + +on: push: paths-ignore: - - docs/** + - 'docs/**' - '*.md' pull_request: paths-ignore: - - docs/** + - 'docs/**' - '*.md' + jobs: + dependency-review: + name: Dependency Review + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Check out repo + uses: actions/checkout@v3 + with: + persist-credentials: false + + - name: Dependency review + uses: actions/dependency-review-action@v1 + test: runs-on: ubuntu-latest + permissions: + contents: read strategy: matrix: - node-version: - - 14 - - 16 - - 18 + node-version: [14, 16, 18] services: postgres: image: postgres:11-alpine @@ -30,14 +46,19 @@ jobs: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 steps: - - uses: actions/checkout@v3 - - name: Use Node.js + - name: Check out repo + uses: actions/checkout@v3 + with: + persist-credentials: false + + - name: Setup Node ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - - name: Install Dependencies - run: | - npm install + + - name: Install dependencies + run: npm i + - name: CI environment setup run: > npm i node-gyp @@ -50,34 +71,23 @@ jobs: job.services.postgres.ports[5432] }} -d postgres -c 'CREATE TABLE users(id serial PRIMARY KEY, username VARCHAR (50) NOT NULL);' -U postgres - - name: Check licenses - run: | - npm run license-checker --if-present + - name: Run tests run: | npm run test:ci - - name: Coveralls Parallel - uses: coverallsapp/github-action@1.1.3 - with: - github-token: ${{ secrets.github_token }} - parallel: true - flag-name: run-${{ matrix.node-version }}-${{ matrix.os }} - coverage: - needs: test - runs-on: ubuntu-latest - steps: - - name: Coveralls Finished - uses: coverallsapp/github-action@1.1.3 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - parallel-finished: true + automerge: + name: Automerge Dependabot PRs + if: > + github.event_name == 'pull_request' && + github.event.pull_request.user.login == 'dependabot[bot]' needs: test - runs-on: ubuntu-latest permissions: pull-requests: write contents: write + runs-on: ubuntu-latest steps: - uses: fastify/github-action-merge-dependabot@v3 with: github-token: ${{ secrets.GITHUB_TOKEN }} + target: major diff --git a/README.md b/README.md index 39ed5cb..db36718 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,6 @@ ![CI](https://github.com/fastify/fastify-postgres/workflows/CI/badge.svg) [![NPM version](https://img.shields.io/npm/v/@fastify/postgres.svg?style=flat)](https://www.npmjs.com/package/@fastify/postgres) -[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-postgres/badge.svg)](https://snyk.io/test/github/fastify/fastify-postgres) -[![Coverage Status](https://coveralls.io/repos/github/fastify/fastify-postgres/badge.svg?branch=master)](https://coveralls.io/github/fastify/fastify-postgres?branch=master) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) Fastify PostgreSQL connection plugin; with this, you can share the same PostgreSQL connection pool in every part of your server. From a72fbada21b990c06b882bba38d356596b34b04a Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Fri, 10 Jun 2022 18:26:38 +0100 Subject: [PATCH 02/10] chore(.gitignore): use updated skeleton template (#134) --- .gitignore | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 1992952..c1b9a5c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ npm-debug.log* yarn-debug.log* yarn-error.log* lerna-debug.log* +.pnpm-debug.log* # Diagnostic reports (https://nodejs.org/api/report.html) report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json @@ -53,6 +54,9 @@ web_modules/ # Optional eslint cache .eslintcache +# Optional stylelint cache +.stylelintcache + # Microbundle cache .rpt2_cache/ .rts2_cache_cjs/ @@ -68,9 +72,12 @@ web_modules/ # Yarn Integrity file .yarn-integrity -# dotenv environment variables file +# dotenv environment variable files .env -.env.test +.env.development.local +.env.test.local +.env.production.local +.env.local # parcel-bundler cache (https://parceljs.org/) .cache @@ -93,6 +100,13 @@ dist # vuepress build output .vuepress/dist +# vuepress v2.x temp and cache directory +.temp +.cache + +# Docusaurus cache and generated files +.docusaurus + # Serverless directories .serverless/ @@ -123,6 +137,7 @@ dist # lock files package-lock.json +pnpm-lock.yaml yarn.lock # editor files From ff27f0713daa1624b55e994b6cd96c18649f7673 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 20 Jun 2022 04:08:01 +0000 Subject: [PATCH 03/10] build(deps-dev): bump tsd from 0.20.0 to 0.21.0 (#136) Bumps [tsd](https://github.com/SamVerschueren/tsd) from 0.20.0 to 0.21.0. - [Release notes](https://github.com/SamVerschueren/tsd/releases) - [Commits](https://github.com/SamVerschueren/tsd/compare/v0.20.0...v0.21.0) --- updated-dependencies: - dependency-name: tsd dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6ee88f7..7f6deeb 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "pg-native": "^3.0.0", "standard": "^17.0.0", "tap": "^16.0.0", - "tsd": "^0.20.0", + "tsd": "^0.21.0", "typescript": "^4.4.3" }, "peerDependencies": { From 6412b917d7a65a738b751f5df4de5b0100cee06c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 1 Jul 2022 04:03:52 +0000 Subject: [PATCH 04/10] build(deps): bump actions/dependency-review-action from 1 to 2 (#137) Bumps [actions/dependency-review-action](https://github.com/actions/dependency-review-action) from 1 to 2. - [Release notes](https://github.com/actions/dependency-review-action/releases) - [Commits](https://github.com/actions/dependency-review-action/compare/v1...v2) --- updated-dependencies: - dependency-name: actions/dependency-review-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b5e27fd..7a44caa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: persist-credentials: false - name: Dependency review - uses: actions/dependency-review-action@v1 + uses: actions/dependency-review-action@v2 test: runs-on: ubuntu-latest From a94257b7fa789f6c1fa4bae067ca134a1e6364db Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 4 Jul 2022 04:09:28 +0000 Subject: [PATCH 05/10] build(deps-dev): bump tsd from 0.21.0 to 0.22.0 (#138) Bumps [tsd](https://github.com/SamVerschueren/tsd) from 0.21.0 to 0.22.0. - [Release notes](https://github.com/SamVerschueren/tsd/releases) - [Commits](https://github.com/SamVerschueren/tsd/compare/v0.21.0...v0.22.0) --- updated-dependencies: - dependency-name: tsd dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7f6deeb..ac40633 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "pg-native": "^3.0.0", "standard": "^17.0.0", "tap": "^16.0.0", - "tsd": "^0.21.0", + "tsd": "^0.22.0", "typescript": "^4.4.3" }, "peerDependencies": { From a329c0009996baf7617609a41b43e0f3d53ab119 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 18 Jul 2022 04:06:29 +0000 Subject: [PATCH 06/10] build(deps): bump fastify-plugin from 3.0.1 to 4.0.0 (#139) Bumps [fastify-plugin](https://github.com/fastify/fastify-plugin) from 3.0.1 to 4.0.0. - [Release notes](https://github.com/fastify/fastify-plugin/releases) - [Commits](https://github.com/fastify/fastify-plugin/compare/v3.0.1...v4.0.0) --- updated-dependencies: - dependency-name: fastify-plugin dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ac40633..aecf764 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ }, "homepage": "https://github.com/fastify/fastify-postgres#readme", "dependencies": { - "fastify-plugin": "^3.0.0" + "fastify-plugin": "^4.0.0" }, "devDependencies": { "@tsconfig/node10": "^1.0.8", From 0b5ef4fc9f7e756b4ef994e6a93eedaf72803249 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Mon, 25 Jul 2022 09:44:48 +0100 Subject: [PATCH 07/10] docs: use `npm install` alias; remove redundant `--save` arg See https://github.com/fastify/fastify/pull/3875 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index db36718..91a100c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Under the hood [node-postgres](https://github.com/brianc/node-postgres) is used, ## Install ``` -npm i pg @fastify/postgres --save +npm i pg @fastify/postgres ``` ## Usage Add it to your project with `register` and you are done! @@ -267,7 +267,7 @@ In the plugin this works by using the `preHandler` hook to open the transaction, Install the compiler and typings for pg module: ```shell script -npm install --save-dev typescript @types/pg +npm i -D typescript @types/pg ``` More examples in the [examples/typescript](./examples/typescript) directory. From e82cf2ff465261f5b7bfada534805e360d5b5a6f Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Fri, 5 Aug 2022 09:44:27 +0000 Subject: [PATCH 08/10] docs(readme): replace use of deprecated variadic `listen()` --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 91a100c..1ddc7d6 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ fastify.get('/user/:id', (req, reply) => { } }) -fastify.listen(3000, err => { +fastify.listen({ port: 3000 }, err => { if (err) throw err console.log(`server listening on ${fastify.server.address().port}`) }) @@ -74,7 +74,7 @@ fastify.get('/user/:id', async (req, reply) => { } }) -fastify.listen(3000, err => { +fastify.listen({ port: 3000 }, err => { if (err) throw err console.log(`server listening on ${fastify.server.address().port}`) }) @@ -96,7 +96,7 @@ fastify.get('/user/:id', (req, reply) => { ) }) -fastify.listen(3000, err => { +fastify.listen({ port: 3000 }, err => { if (err) throw err console.log(`server listening on ${fastify.server.address().port}`) }) @@ -144,7 +144,7 @@ fastify.pg.transact((client, commit) => { */ -fastify.listen(3000, err => { +fastify.listen({ port: 3000 }, err => { if (err) throw err console.log(`server listening on ${fastify.server.address().port}`) }) @@ -173,7 +173,7 @@ fastify.get('/user/:id', (req, reply) => { ) }) -fastify.listen(3000, err => { +fastify.listen({ port: 3000 }, err => { if (err) throw err console.log(`server listening on ${fastify.server.address().port}`) }) @@ -201,7 +201,7 @@ fastify.get('/user/:id', (req, reply) => { ) }) -fastify.listen(3000, err => { +fastify.listen({ port: 3000 }, err => { if (err) throw err console.log(`server listening on ${fastify.server.address().port}`) }) @@ -229,7 +229,7 @@ fastify.get('/user/:id', (req, reply) => { ) }) -fastify.listen(3000, err => { +fastify.listen({ port: 3000 }, err => { if (err) throw err console.log(`server listening on ${fastify.server.address().port}`) }) From 9fbdc746f8cb7d0b999e6b18685e5806062fae14 Mon Sep 17 00:00:00 2001 From: Bryan Dun Date: Tue, 16 Aug 2022 03:38:10 -0400 Subject: [PATCH 09/10] =?UTF-8?q?Adding=20types=20to=20declaration=20merge?= =?UTF-8?q?=20for=20route=20transact=20option=20in=20typesc=E2=80=A6=20(#1?= =?UTF-8?q?42)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adding types to declaration merge for route transact option in typescript * added typescript definition tests, added string as transact option type * adding examples with transact route, minor test fix * fixing quote styles * Update index.d.ts Co-authored-by: Uzlopak Co-authored-by: Matteo Collina Co-authored-by: Uzlopak --- examples/typescript/transactions/app.ts | 28 +++++++++++++++++++++++++ index.d.ts | 12 +++++++++++ test/types/transaction.test-d.ts | 24 +++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/examples/typescript/transactions/app.ts b/examples/typescript/transactions/app.ts index 636ec59..0ffe723 100644 --- a/examples/typescript/transactions/app.ts +++ b/examples/typescript/transactions/app.ts @@ -48,4 +48,32 @@ app.post('/init-cb', (_req, reply) => { ); }); +app.post('/transact-route', { pg: { transact: true } }, async (req, _reply) => { + const createTableQuery = ` + CREATE TABLE routes ( + id bigserial primary key, + name varchar(80) NOT NULL, + created_at timestamp default NULL + ); + `; + + return req.pg?.query(createTableQuery); +}); + +app.post( + '/transact-route-alternate', + { pg: { transact: 'primary' } }, + async (req, _reply) => { + const createTableQuery = ` + CREATE TABLE routes ( + id bigserial primary key, + name varchar(80) NOT NULL, + created_at timestamp default NULL + ); + `; + + return req.pg?.query(createTableQuery); + } +); + export { app }; diff --git a/index.d.ts b/index.d.ts index 8dacfdc..d6ea691 100644 --- a/index.d.ts +++ b/index.d.ts @@ -18,6 +18,10 @@ type PostgresDb = { transact: typeof transact; }; +type FastifyPostgresRouteOptions = { + transact: boolean | string; +}; + type PostgresPluginOptions = { /** * Custom pg @@ -41,6 +45,14 @@ declare module 'fastify' { export interface FastifyInstance { pg: PostgresDb & Record; } + + export interface FastifyRequest { + pg?: Pg.PoolClient; + } + + export interface RouteShorthandOptions { + pg?: FastifyPostgresRouteOptions; + } } export { fastifyPostgres, PostgresDb, PostgresPluginOptions }; diff --git a/test/types/transaction.test-d.ts b/test/types/transaction.test-d.ts index a48841a..f56588b 100644 --- a/test/types/transaction.test-d.ts +++ b/test/types/transaction.test-d.ts @@ -54,3 +54,27 @@ app.post('/insert-cb', (_req, reply) => { } ); }); + +app.post('/transact-route', { pg: { transact: true } }, async (req, _reply) => { + const insertQuery = ` + INSERT INTO routes(name) + VALUES ('ochakovo') + RETURNING 1 + 1 as sum; + `; + + return req.pg?.query(insertQuery); +}); + +app.post( + '/transact-route-alternate', + { pg: { transact: 'primary' } }, + async (req, _reply) => { + const insertQuery = ` + INSERT INTO routes(name) + VALUES ('ochakovo') + RETURNING 1 + 1 as sum; + `; + + return req.pg?.query(insertQuery); + } +); From 7b1f5a18449d2227adb3c2d265f05322ae14f663 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 16 Aug 2022 09:43:33 +0200 Subject: [PATCH 10/10] Bumped v5.1.0 Signed-off-by: Matteo Collina --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index aecf764..5a87f09 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fastify/postgres", - "version": "5.0.0", + "version": "5.1.0", "description": "Fastify PostgreSQL connection plugin", "main": "index.js", "types": "index.d.ts",