@dzangolab/fastify-slonik
TypeScript icon, indicating that this package has built-in type declarations

0.65.0 • Public • Published

@dzangolab/fastify-slonik

A Fastify plugin that provides an easy integration of slonik in a fastify API.

The plugin is a thin wrapper around the fastify-slonik plugin.

The plugin also includes logic to run migrations via @dzangolab/postgres-migrations which is forked from postgres-migrations.

Requirements

  • @dzangolab/fastify-config
  • slonik

Installation

In a simple repo:

npm install @dzangolab/fastify-slonik slonik

If using in a monorepo with pnpm:

pnpm add --filter "myrepo" @dzangolab/fastify-slonik slonik

Usage

Add a slonik block to your config:

import { parse } from "@dzangolab/fastify-config";
import dotenv from "dotenv";

import type { ApiConfig } from "@dzangolab/fastify-config";

dotenv.config();

const config: ApiConfig = {
  ...
  slonik: {
    db: {
      databaseName: process.env.DB_NAME as string,
      host: process.env.DB_HOST as string,
      password: process.env.DB_PASSWORD as string,
      port: parse(process.env.DB_PORT, 5432) as number,
      username: process.env.DB_USER as string,
    },
    migrations: {
      development: parse(
        process.env.MIGRATIONS_DEVELOPMENT_FOLDER,
        "migrations"
      ) as string,
      production: parse(
        process.env.MIGRATIONS_PRODUCTION_FOLDER,
        "apps/api/build/migrations"
      ) as string,
    },
  },
  ...
};

export default config;

Register the plugin with your Fastify instance:

import configPlugin from "@dzangolab/fastify-config";
import slonikPlugin, { migrationPlugin } from "@dzangolab/fastify-slonik";
import fastify from "fastify";

import config from "./config";

import type { ApiConfig } from "@dzangolab/fastify-config";
import type { FastifyInstance } from "fastify";

// Create fastify instance
const fastify = Fastify({
  logger: config.logger,
});

// Register fastify-config plugin
fastify.register(configPlugin, { config });

// Register fastify-slonik plugin
fastify.register(slonikPlugin);

// Run database migrations
await api.register(migrationPlugin);

await fastify.listen({
  port: config.port,
  host: "0.0.0.0",
 });

Note: migrationPlugin should be registered after all the plugins.

Configuration

db

Attribute Type Description
database string The name of the database to connect to.
host string The database's host.
password string The password for connecting to the database.
port number The database's port.
username string The username for connecting to the database.

migrations

Paths to the migrations files. You can specify 1 path per environment. Currently the only environments supported are: development andproduction.

The path must be relative to node.js process.cwd().

Readme

Keywords

none

Package Sidebar

Install

npm i @dzangolab/fastify-slonik

Weekly Downloads

420

Version

0.65.0

License

MIT

Unpacked Size

117 kB

Total Files

50

Last publish

Collaborators

  • opichon
  • uddhab
  • rameshlohala
  • cpxpratik