$ show gajus/slonik
Slonik is a production-grade, battle-tested Node.js PostgreSQL client library that emphasizes strict typing, detailed logging, and runtime assertions. It provides a type-safe SQL query interface via tagged template literals (the `sql` tag), connection pooling with configurable lifecycle management, transaction support, and an interceptor (middleware) system for extending query execution, connection handling, and result transformation. The library is structured as a monorepo with multiple packages: core `slonik`, `@slonik/sql-tag` for SQL template construction, `@slonik/driver` and `@slonik/pg-driver` for driver abstraction, `@slonik/errors` for a comprehensive error hierarchy, `@slonik/utilities` for shared utilities, and community interceptor packages for query caching, logging, field name transformation, and DataLoader integration. It integrates with Zod for optional runtime query result validation and uses Roarr for logging. The architecture is modular, with clean separation of concerns, making it suitable for both small projects and large-scale production systems.
The LLM identified 5 files worth highlighting for reuse:
Factory function that creates the `sql` tagged template literal. It parses template strings and interpolated values into typed SQL tokens (e.g., QuerySqlToken, FragmentSqlToken, ArraySqlToken). This component is self-contained, reusable as a standalone SQL builder, and demonstrates advanced TypeScript pattern matching and tokenization.
Defines the abstract `Driver` interface and factory for creating driver instances. This allows swapping the underlying PostgreSQL client (e.g., `pg` vs. a mock). The component is isolated, with no internal dependencies beyond types, and can be extracted to implement custom drivers for other databases or testing.
Defines a comprehensive error hierarchy extending `SlonikError`, including `NotFoundError`, `DataIntegrityError`, `IntegrityConstraintViolationError`, and others. Each error class carries structured metadata (e.g., column names, constraint details). This pattern is reusable for any library that needs rich, typed error handling.
Custom type parser for PostgreSQL `int8` (bigint) that converts raw string values to JavaScript `BigInt`. It is a standalone function implementing the `TypeParser` interface, demonstrating how to extend Slonik's type system. This pattern is reusable for any custom PostgreSQL type parsing.
Parses a PostgreSQL DSN (connection string) into its components (host, port, database, user, password). It is a pure function with no internal dependencies, fully reusable in any Node.js project for DSN parsing.