Before and after execute hooks for libSQL.
npm install libsql-middlewareThe middleware wrapper for @libsql/client.
!NPM
``bash`
npm install libsql-middleware
Make sure to install @libsql/client if you don't already have it.
`ts
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");
`
`ts
import { beforeExecute } from "libsql-middleware";
const logBeforeExecute = beforeExecute((query) => {
// Do something
return query;
});
`
`ts
import { afterExecute } from "libsql-middleware";
const logAfterExecute = afterExecute((result, query) => {
// Do something
return result;
});
`
`ts
import { beforeBatch } from "libsql-middleware";
const logBeforeBatch = beforeBatch((stmts) => {
// Do something
return stmts;
});
`
`ts
import { afterBatch } from "libsql-middleware";
const logAfterBatch = afterBatch((results, stmts) => {
// Do something
return results;
});
`
The withMiddleware method binds the original @libsql/client methods so you can use them as you normally would, but now with middleware.
`ts
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();