@dzangolab/fastify-slonik
v0.76.1
Published
Fastify slonik plugin
Downloads
2,711
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.