npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dzangolab/fastify-slonik

v0.75.3

Published

Fastify slonik plugin

Downloads

2,394

Readme

@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

Installation

Install with npm:

npm install @dzangolab/fastify-config @dzangolab/fastify-slonik slonik

Install with pnpm:

pnpm add --filter "@scope/project" @dzangolab/fastify-config @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";

const start = async () => {
  // Create fastify instance
  const fastify = Fastify({
    logger: config.logger,
  });
  
  // Register fastify-config plugin
  await fastify.register(configPlugin, { config });
  
  // Register fastify-slonik plugin
  await fastify.register(slonikPlugin, config.slonik);
  
  // Run database migrations
  await fastify.register(migrationPlugin, config.slonik);
  
  await fastify.listen({
    port: config.port,
    host: "0.0.0.0",
  });
};

start();

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().

Enabling query logging

To enable query logging, set queryLogging.enabled to true in the slonik config and set ROARR_LOG=true environment variable to ensure logs are printed to the console.

const config: ApiConfig = {
  ...
  slonik: {
    queryLogging: {
      enabled: true,
    },
    ...
  },
};

This setup activates the slonik-interceptor-query-logging interceptor, which uses roarr to log SQL queries directly to the console.

Limitation: The roarr logger used here is independent of the fastify logger (like pino) and logs directly to the console. Unlike pino, roarr does not natively support logging to files or prettifying the console output.

With this setup, all SQL queries will be logged to the console, making it easier to debug and monitor database interactions.