@autotelic/fastify-slonik

0.5.1 • Public • Published

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.

Package Sidebar

Install

npm i @autotelic/fastify-slonik

Weekly Downloads

1

Version

0.5.1

License

MIT

Unpacked Size

4.13 kB

Total Files

4

Last publish

Collaborators

  • autotelic