@tslock/kysely
v2.0.1
Published
TSLock SQL provider using Kysely query builder
Readme
@tslock/kysely
TSLock SQL provider via the Kysely type-safe query builder.
A TSLock provider that runs the standard ShedLock insert-or-update SQL through Kysely's db.executeQuery() API, sharing configuration and statement generation with @tslock/sql-support. Supports PostgreSQL, MySQL, and SQLite.
Installation
pnpm add @tslock/core @tslock/kysely @tslock/sql-support kysely pg
# or mysql2 / better-sqlite3 instead of pgSetup
Create a shedlock table (same schema as the raw SQL provider — see @tslock/sql).
Usage
import { createLockConfig, DefaultLockingTaskExecutor } from '@tslock/core';
import { KyselyLockProvider } from '@tslock/kysely';
import { SqlConfiguration, DatabaseProduct } from '@tslock/sql-support';
import { Kysely, PostgresDialect } from 'kysely';
import { Pool } from 'pg';
const db = new Kysely<any>({
dialect: new PostgresDialect({ pool: new Pool({ connectionString: process.env.DATABASE_URL }) }),
});
const provider = new KyselyLockProvider(
db,
'postgresql',
new SqlConfiguration({ databaseProduct: DatabaseProduct.POSTGRES }),
);
const executor = new DefaultLockingTaskExecutor(provider);
await executor.executeWithLock(
() => runBatchJob(),
createLockConfig({ name: 'batch-job', lockAtMostFor: '30m' }),
);The second argument is the dialect name: 'postgresql' | 'mysql' | 'sqlite'. The third is a standard SqlConfiguration — see @tslock/sql-support for all options (table name, column overrides, useDbTime, etc.).
Integration tests
The shared storage and fuzz contracts run against an ephemeral PostgreSQL container:
pnpm --filter @tslock/kysely test:integrationDocker is required for this suite.
Requirements
- Node.js >= 22
- Peer:
kysely+ a driver (pg,mysql2, orbetter-sqlite3)
License
Apache 2.0 — see LICENSE for details.
