@autotelic/fastify-slonik
v0.5.1
Published
A Slonik plugin for Fastify
Downloads
15
Maintainers
Readme
Fastify Slonik
A Fastify plugin that uses the PostgreSQL client, Slonik. Slonik abstracts repeating code patterns, protects against unsafe connection handling and value interpolation, and provides a rich debugging experience.
Usage
npm i @autotelic/fastify-slonik
Example:
// index.js
const fastifySlonik = require('fastify-slonik')
// register fastify-slonik
fastify.register(fastifySlonik, {
connectionString: process.env.DATABASE_URL
})
// setup test route
fastify.get('/users', async function (request, reply) {
const { params: { id: userId } } = request
const queryText = this.slonik.sql`
SELECT * FROM users
WHERE user_id = ${userId}
`
const user = await this.slonik.query(queryText)
reply.send(user)
}
API
Decorator
This plugin decorates fastify with slonik
exposing all query methods.
View Slonik API for details.
Development and Testing
Tap is used for testing
Fixtures
$ docker-compose up
To run the tests:
- Create .envrc
cp .envrc.example .envrc
- If not using direnv, update .envrc to .env
$ npm test
License
Licensed under MIT.