A battle-tested Node.js PostgreSQL client with strict runtime and build-time type safety, composable SQL, and detailed logging.
Slonik is a Node.js PostgreSQL client that provides runtime and build-time type safety, composable SQL, and extensive safety features. It solves the problem of unsafe SQL generation and connection handling by enforcing strict patterns, preventing SQL injection, and ensuring predictable query results. Built on `node-postgres`, it adds layers of protection and developer-friendly utilities.
Node.js developers building applications with PostgreSQL who need robust safety against SQL injection, reliable transaction handling, and strong typing for query results. It's especially valuable for teams managing large-scale data or requiring strict data integrity.
Developers choose Slonik for its uncompromising focus on safety—eliminating SQL injection risks through mandatory tagged templates—and its rich feature set like runtime validation with Zod, detailed logging, and helper methods that reduce boilerplate. It’s battle-tested for high-volume workloads and offers a middleware system for extensibility.
A Node.js PostgreSQL client with runtime and build time type safety, and composable SQL.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Enforces safe value interpolation through mandatory tagged template literals, blocking raw string queries to eliminate injection risks entirely, as highlighted in the README's security philosophy.
Integrates Zod to validate query results at runtime, catching schema drift and ensuring data integrity, with detailed error handling for validation failures.
Helper methods like `one` and `many` enforce expected row counts and reduce repetitive error-checking code, providing static type inference in TypeScript.
Transactions are scoped to promise routines, guaranteeing automatic commit or rollback without manual management, preventing connection leaks.
Uses Roarr for logs with stack traces and query values, aiding debugging and performance analysis, as demonstrated in the README's GIF example.
Runtime validation with Zod adds processing time; the README admits it can take ~25ms to validate 100,000 rows, which may impact high-volume applications.
Requires understanding of interceptors, type parsers, and configuration options, making initial setup more involved compared to simpler clients like `node-postgres`.
Compared to established alternatives, Slonik has fewer community plugins, and its strict API restricts dynamic SQL, which can be cumbersome for complex query building.
The README notes that `pg-native` is not officially supported, limiting options for teams seeking native performance optimizations.