🗄️
Database50K+/wkBSD-3-Clause

slonik

Slonik is a Node.js PostgreSQL client with strict types, detailed logging, and assertions that encourage writing safe SQL. Unlike ORMs that generate SQL from me

Installation

npm
npm install slonik
yarn
yarn add slonik
pnpm
pnpm add slonik

Import

ESM
import { createPool, sql } from 'slonik';

Quick Example

usage
import { createPool, sql } from 'slonik';

const pool = await createPool(process.env.DATABASE_URL);

const users = await pool.many(
  sql.unsafe`SELECT * FROM users WHERE active = true`
);

await pool.query(
  sql.unsafe`INSERT INTO users (name) VALUES (${name})`
);

About slonik

Slonik is a Node.js PostgreSQL client with strict types, detailed logging, and assertions that encourage writing safe SQL. Unlike ORMs that generate SQL from method chains or schema definitions, Slonik uses tagged template literals (sql`SELECT * FROM users WHERE id = ${id}`) that provide automatic parameter binding, preventing SQL injection while keeping queries readable and close to actual SQL. The library enforces safe practices through its type system — it requires explicit type casting for interpolated values and prevents common mistakes like accidentally interpolating raw strings into queries. Slonik includes connection pooling, automatic reconnection, interceptors for query lifecycle hooks (logging, benchmarking, normalization), transaction management, stream queries for large result sets, and built-in support for PostgreSQL types including arrays, JSON, and timestamps. The library integrates with Zod for runtime result validation, ensuring query results match expected shapes. Slonik is designed for developers who prefer writing SQL directly rather than using an ORM abstraction, but want safety guarantees, connection management, and observability that raw pg queries do not provide.

Quick Facts

Packageslonik
CategoryDatabase
Weekly Downloads50K+
LicenseBSD-3-Clause
Installnpm install slonik

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.