MCPcopy Index your code
hub / github.com/gajus/slonik

github.com/gajus/slonik @49.10.7 sqlite

repository ↗ · DeepWiki ↗ · release 49.10.7 ↗
341 symbols 1,281 edges 199 files 0 documented · 0% 2 cross-repo links
README

Slonik

NPM version Canonical Code Style Twitter Follow

A battle-tested Node.js PostgreSQL client with strict types, detailed logging and assertions.

Tailing Slonik logs

Principles

  • Promotes writing raw SQL.
  • Discourages ad-hoc dynamic generation of SQL.

Read: Stop using Knex.js

Features

Contents

About Slonik

Battle-Tested

Slonik began as a collection of utilities designed for working with node-postgres. It continues to use node-postgres driver as it provides a robust foundation for interacting with PostgreSQL. However, what once was a collection of utilities has since grown into a framework that abstracts repeating code patterns, protects against unsafe connection handling and value interpolation, and provides a rich debugging experience.

Slonik has been battle-tested with large data volumes and queries ranging from simple CRUD operations to data-warehousing needs.

Origin of the name

Slonik

"Słonik" is a Polish diminutive of "słoń," meaning “little elephant” or “baby elephant.” The word "słoń" itself comes from Proto-Slavic *slonъ, which was likely borrowed from a Germanic language and may ultimately trace back to Latin.

Repeating code patterns and type safety

Among the primary reasons for developing Slonik, was the motivation to reduce the repeating code patterns and add a level of type safety. This is primarily achieved through the methods such as one, many, etc. But what is the issue? It is best illustrated with an example.

Suppose the requirement is to write a method that retrieves a resource ID given values defining (what we assume to be) a unique constraint. If we did not have the aforementioned helper methods available, then it would need to be written as:

import { sql, type DatabaseConnection } from "slonik";

type DatabaseRecordIdType = number;

const getFooIdByBar = async (
  connection: DatabaseConnection,
  bar: string,
): Promise<DatabaseRecordIdType> => {
  const fooResult = await connection.query(sql.typeAlias("id")`
    SELECT id
    FROM foo
    WHERE bar = ${bar}
  `);

  if (fooResult.rowCount === 0) {
    throw new Error("Resource not found.");
  }

  if (fooResult.rowCount > 1) {
    throw new Error("Data integrity constraint violation.");
  }

  return fooResult[0].id;
};

oneFirst method abstracts all of the above logic into:

const getFooIdByBar = (
  connection: DatabaseConnection,
  bar: string,
): Promise<DatabaseRecordIdType> => {
  return connection.oneFirst(sql.typeAlias("id")`
    SELECT id
    FROM foo
    WHERE bar = ${bar}
  `);
};

oneFirst throws:

  • NotFoundError if query returns no rows
  • DataIntegrityError if query returns multiple rows
  • DataIntegrityError if query returns multiple columns

In the absence of helper methods, the overhead of repeating code becomes particularly visible when writing routines where multiple queries depend on the proceeding query results. Using methods with inbuilt assertions ensures that in case of an error, the error points to the source of the problem. In contrast, unless assertions for all possible outcomes are typed out as in the previous example, the unexpected result of the query will be fed to the next operation. If you are lucky, the next operation will simply break; if you are unlucky, you are risking data corruption and hard-to-locate bugs.

Furthermore, using methods that guarantee the shape of the results allows us to leverage static type checking and catch some of the errors even before executing the code, e.g.

const fooId = await connection.many(sql.typeAlias("id")`
  SELECT id
  FROM foo
  WHERE bar = ${bar}
`);

await connection.query(sql.typeAlias("void")`
  DELETE FROM baz
  WHERE foo_id = ${fooId}
`);

Static type check of the above example will produce a warning as the fooId is guaranteed to be an array and binding of the last query is expecting a primitive value.

Protecting against unsafe connection handling

Slonik only allows to check out a connection for the duration of the promise routine supplied to the pool#connect() method.

The primary reason for implementing only this connection pooling method is because the alternative is inherently unsafe, e.g.

// This is not valid Slonik API

const main = async () => {
  const connection = await pool.connect();

  await connection.query(sql.typeAlias("foo")`SELECT foo()`);

  await connection.release();
};

In this example, if SELECT foo() produces an error, then connection is never released, i.e. the connection hangs indefinitely.

A fix to the above is to ensure that connection#release() is always called, i.e.

// This is not valid Slonik API

const main = async () => {
  const connection = await pool.connect();

  let lastExecutionResult;

  try {
    lastExecutionResult = await connection.query(sql.typeAlias("foo")`SELECT foo()`);
  } finally {
    await connection.release();
  }

  return lastExecutionResult;
};

Slonik abstracts the latter pattern into pool#connect() method.

const main = () => {
  return pool.connect((connection) => {
    return connection.query(sql.typeAlias("foo")`SELECT foo()`);
  });
};

Using this pattern, we guarantee that connection is always released as soon as the connect() routine resolves or is rejected.

Resetting connection state

After the connection is released, Slonik resets the connection state. This is to prevent connection state from leaking between queries.

The default behaviour is to execute DISCARD ALL command. This behaviour can be adjusted by configuring resetConnection routine, e.g.

import { createPool, sql } from "slonik";
import { createPgDriverFactory } from "@slonik/pg-driver";

const pool = createPool("postgres://", {
  driverFactory: createPgDriverFactory(),
  resetConnection: async (connection) => {
    await connection.query("DISCARD ALL");
  },
});

[!NOTE] Resetting a connection is a heavy operation. Depending on the application requirements, it may make sense to disable connection reset, e.g.

```ts import { createPool } from "slonik"; import { createPgDriverFactory } from "@slonik/pg-driver";

const pool = createPool("postgres://", { driverFactory: createPgDriverFactory(), resetConnection: async () => {}, }); ```

Protecting against unsafe transaction handling

Just like in the unsafe connection handling example, Slonik only allows to create a transaction for the duration of the promise routine supplied to the connection#transaction() method.

connection.transaction(async (transactionConnection) => {
  await transactionConnection.query(sql.typeAlias("void")`INSERT INTO foo (bar) VALUES ('baz')`);
  await transactionConnection.query(sql.typeAlias("void")`INSERT INTO qux (quux) VALUES ('quuz')`);
});

This pattern ensures that the transaction is either

Extension points exported contracts — how you extend this code

DriverStream (Interface)
(no doc)
packages/driver/src/factories/createDriverFactory.ts

Core symbols most depended-on inside this repo

query
called by 204
packages/slonik/src/binders/bindPoolConnection.ts
createPool
called by 173
packages/slonik/src/factories/createPool.ts
transaction
called by 64
packages/slonik/src/binders/bindPoolConnection.ts
oneFirst
called by 57
packages/slonik/src/binders/bindPoolConnection.ts
on
called by 43
packages/slonik/src/binders/bindTransactionConnection.ts
createSqlTag
called by 37
packages/sql-tag/src/factories/createSqlTag.ts
createPoolWithSpy
called by 32
packages/slonik/src/helpers.test/createPoolWithSpy.ts
delay
called by 28
packages/driver/src/factories/createDriverFactory.ts

Shape

Function 192
Method 82
Class 66
Interface 1

Languages

TypeScript100%

Modules by API surface

packages/errors/src/errors.ts62 symbols
packages/slonik/src/factories/createConnectionPool.test.ts39 symbols
packages/slonik/src/binders/bindTransactionConnection.ts34 symbols
packages/slonik/src/binders/bindPoolConnection.ts18 symbols
packages/slonik/src/factories/createConnectionPool.ts13 symbols
packages/pg-driver/src/factories/createPgDriverFactory.ts10 symbols
packages/driver/src/factories/createDriverFactory.ts9 symbols
packages/slonik/src/factories/createConnection.ts6 symbols
packages/slonik/src/routines/executeQuery.ts5 symbols
packages/slonik/src/helpers.test/createIntegrationTests.ts4 symbols
packages/slonik/src/connectionMethods/stream.ts4 symbols
packages/slonik-interceptor-query-cache/src/utilities/extractCacheAttributes.ts4 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

Dependencies from manifests, versioned

@changesets/changelog-github0.5.2 · 1×
@changesets/cli2.29.8 · 1×
@node-rs/xxhash1.7.6 · 1×
@opentelemetry/api1.9.0 · 1×
@slonik/driver49.10.7 · 1×
@slonik/errors49.10.7 · 1×
@slonik/pg-driver49.10.7 · 1×
@slonik/sql-tag49.10.7 · 1×
@slonik/test-sslsworkspace:^ · 1×
@slonik/types49.10.7 · 1×
@slonik/utilities49.10.7 · 1×

Datastores touched

fooDatabase · 1 repos
barDatabase · 1 repos
database-nameDatabase · 1 repos
dbDatabase · 1 repos
dbnameDatabase · 1 repos
mydbDatabase · 1 repos
postgresDatabase · 1 repos

For agents

$ claude mcp add slonik \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact