diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d7038c0..7a44caa 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@v2 + 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/.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 diff --git a/README.md b/README.md index 39ed5cb..1ddc7d6 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. @@ -11,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! @@ -48,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}`) }) @@ -76,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}`) }) @@ -98,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}`) }) @@ -146,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}`) }) @@ -175,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}`) }) @@ -203,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}`) }) @@ -231,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}`) }) @@ -269,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. 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/package.json b/package.json index 6ee88f7..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", @@ -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", @@ -44,7 +44,7 @@ "pg-native": "^3.0.0", "standard": "^17.0.0", "tap": "^16.0.0", - "tsd": "^0.20.0", + "tsd": "^0.22.0", "typescript": "^4.4.3" }, "peerDependencies": { 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); + } +);