1.4.1 • Published 1 year ago
libsql-middleware v1.4.1
libsql-middleware
The middleware wrapper for @libsql/client.
Install
npm install libsql-middlewareMake sure to install @libsql/client if you don't already have it.
Quickstart
import { createClient } from "@libsql/client";
import { beforeExecute, withMiddleware } from "libsql-middleware";
const client = createClient({ url: "file:dev.db" });
const logBeforeExecute = beforeExecute((query) => {
console.log("Before executing");
return query;
});
const clientWithHooks = withMiddleware(client, [logBeforeExecute]);
await clientWithHooks.execute(
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)"
);
await clientWithHooks.execute("INSERT INTO users (name) VALUES ('Test User')");
await clientWithHooks.execute("SELECT * FROM users");API Reference
beforeExecute
import { beforeExecute } from "libsql-middleware";
const logBeforeExecute = beforeExecute((query) => {
// Do something
return query;
});afterExecute
import { afterExecute } from "libsql-middleware";
const logAfterExecute = afterExecute((result, query) => {
// Do something
return result;
});beforeBatch
import { beforeBatch } from "libsql-middleware";
const logBeforeBatch = beforeBatch((stmts) => {
// Do something
return stmts;
});afterBatch
import { afterBatch } from "libsql-middleware";
const logAfterBatch = afterBatch((results, stmts) => {
// Do something
return results;
});withMiddleware
The withMiddleware method binds the original @libsql/client methods so you can use them as you normally would, but now with middleware.
import { withMiddleware } from "libsql-middleware";
const clientWithHooks = withMiddleware(client, [
// Your plugins
]);
// Use the `@libsql/client` as you normally would
// But now with middleware!
await clientWithHooks.execute();
await clientWithHooks.batch();
await clientWithHooks.transaction();