Slonik Utilities provides a set of helper functions designed to simplify common data manipulation operations within a PostgreSQL database when using the Slonik client library. It abstracts away the direct construction of parameterized SQL queries for tasks such as `UPDATE`, `UPDATE ... DISTINCT`, and `UPSERT`, allowing developers to express these operations using JavaScript objects. The library is currently at stable version 2.0.2 and receives updates primarily to maintain compatibility with new major versions of Slonik (e.g., supporting Slonik v29). Its key differentiators include enhancing developer productivity by reducing boilerplate SQL, improving type safety through its TypeScript definitions, and mitigating SQL injection risks by leveraging Slonik's tagged template literals internally. It does not replace Slonik but complements it by offering a higher-level, opinionated API for frequent DML patterns.
npm install slonik-utilitiesVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use the `slonik-utilities` `update` function to modify records in a PostgreSQL database using a Slonik connection, including basic setup and verification.
Ensure your `package.json` specifies `slonik: "^28.0.0"` or `"^29.0.0"` (or newer compatible versions) and run `npm install` or `yarn install`.
Use `import { ... } from 'slonik-utilities';` in an ES module context. For Node.js, ensure your `package.json` has `"type": "module"` or use a `.mjs` file extension.Ensure your JavaScript property names in `namedValueBindings` and `booleanExpressionValues` match the snake_case representation of your PostgreSQL column names. For example, `givenName` in JS becomes `given_name` in SQL.
Update your TypeScript code to remove the `Type` suffix from imported types from `slonik-utilities`.
Ensure you are passing a valid `Slonik` `Connection` or `Pool` instance. When using a `pool.connect()` callback, ensure you pass the `connection` argument from the callback, e.g., `const result = await pool.connect(async (connection) => update(connection, ...));`
Verify your Slonik pool creation. Ensure `createPool` is called with a valid `connectionString` (e.g., `process.env.DATABASE_URL`) and that your database is accessible.
Install the package (`npm install slonik-utilities`). If the error persists and you are in a Node.js project, switch to `import` statements and ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json` or `.mjs` file extension).