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 install slonik
yarn add slonik
pnpm add slonik
Import
import { createPool, sql } from 'slonik';Quick Example
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
| Package | slonik |
| Category | Database |
| Weekly Downloads | 50K+ |
| License | BSD-3-Clause |
| Install | npm install slonik |
Related Packages
pg (node-postgres) is the standard PostgreSQL client for Node.js, providing low-level access to Post…
Postgres.js is the fastest full-featured PostgreSQL client for Node.js and Deno, using tagged templa…
Knex.js is a batteries-included SQL query builder for Node.js that supports PostgreSQL, MySQL, Maria…
Prisma is a next-generation Node.js and TypeScript ORM that provides a declarative data modeling lan…
Drizzle ORM is a TypeScript ORM that provides a SQL-like query builder with full type safety, zero o…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.