Skip to content

LibSQL driver

LibSQL is a fork of SQLite that is both Open Source, and Open Contributions. It fully compatible with SQLite (either file-based or in-process), but also supports remote LibSQL servers (like Turso) for remote and distributed SQLite access.

The LibSQL driver can be installed with the @cadence-mq/driver-libsql package, and the @libsql/client package is required to connect to the database.

pnpm add @cadence-mq/core @cadence-mq/driver-libsql @libsql/client

In order to use the LibSQL driver, you need to setup the schema for the database. This can be done once at startup, and is done by calling the setupSchema function, this function is exported from the @cadence-mq/driver-libsql package and will create the necessary tables and indexes for the driver to work, it can be executed just once, and is safe to execute multiple times.

You can also use the getSchema function to get the schema as a string, and execute it manually.

index.ts
import { setupSchema } from '@cadence-mq/driver-libsql';
import { createClient } from '@libsql/client';
const client = createClient({ /**/ });
// Setup the schema
await setupSchema({ client });

Here is a simple example of how to use the LibSQL driver to store jobs in a local SQLite file.

index.ts
import { createCadence } from '@cadence-mq/core';
import { createLibSqlDriver, setupSchema } from '@cadence-mq/driver-libsql';
import { createClient } from '@libsql/client';
const fileUrl = 'file:./cadence-mq.db';
const client = createClient({ url: fileUrl });
const driver = createLibSqlDriver({ client });
const cadence = createCadence({ driver });
// Setup the schema for the database, can be done once at startup
await setupSchema({ client });
17 collapsed lines
// Register a task to be executed
cadence.registerTask({
taskName: 'send-welcome-email',
handler: async ({ data }) => {
console.log(`Sending welcome email to ${data.email}`);
},
});
// Create a worker to execute the jobs
const worker = cadence.createWorker({ workerId: '1' });
worker.start();
// Schedule a job to be executed as soon as possible
await cadence.scheduleJob({
taskName: 'send-welcome-email',
data: { email: 'test@test.com' },
});

Here is a simple example of how to use the LibSQL driver to store jobs in a remote LibSQL server.

index.ts
import { createCadence } from '@cadence-mq/core';
import { createLibSqlDriver, setupSchema } from '@cadence-mq/driver-libsql';
import { createClient } from '@libsql/client';
// It is highly recommended to store your DB credentials in env variables
const client = createClient({
url: process.env.DATABASE_URL,
authToken: process.env.AUTH_TOKEN,
});
const driver = createLibSqlDriver({ client });
const cadence = createCadence({ driver });
// Setup the schema for the database, can be done once at startup
await setupSchema({ client });
17 collapsed lines
// Register a task to be executed
cadence.registerTask({
taskName: 'send-welcome-email',
handler: async ({ data }) => {
console.log(`Sending welcome email to ${data.email}`);
},
});
// Create a worker to execute the jobs
const worker = cadence.createWorker({ workerId: '1' });
worker.start();
// Schedule a job to be executed as soon as possible
await cadence.scheduleJob({
taskName: 'send-welcome-email',
data: { email: 'test@test.com' },
});

Here is a simple example of how to use the LibSQL driver to store jobs in-process, this is useful for testing and development. For a lower overhead, you can use the in-memory driver instead.

index.ts
import { createCadence } from '@cadence-mq/core';
import { createLibSqlDriver, setupSchema } from '@cadence-mq/driver-libsql';
import { createClient } from '@libsql/client';
const client = createClient({ url: ':memory:' });
const driver = createLibSqlDriver({ client });
const cadence = createCadence({ driver });
// Setup the schema for the database, can be done once at startup
await setupSchema({ client });
17 collapsed lines
// Register a task to be executed
cadence.registerTask({
taskName: 'send-welcome-email',
handler: async ({ data }) => {
console.log(`Sending welcome email to ${data.email}`);
},
});
// Create a worker to execute the jobs
const worker = cadence.createWorker({ workerId: '1' });
worker.start();
// Schedule a job to be executed as soon as possible
await cadence.scheduleJob({
taskName: 'send-welcome-email',
data: { email: 'test@test.com' },
});