notrab / libsql-client-hooks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

libsql-client-hooks

The middleware wrapper for @libsql/client.

NPM

Install

npm install libsql-client-hooks

Make sure to install @libsql/client if you don't already have it.

Quickstart

import { createClient } from "@libsql/client";
import { beforeExecute, withLibsqlHooks } from "libsql-client-hooks";

const client = createClient({ url: "file:dev.db" });

const logBeforeExecute = beforeExecute((query) => {
  console.log("Before executing");
  return query;
});

const clientWithHooks = withLibsqlHooks(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-client-hooks";

const logBeforeExecute = beforeExecute((query) => {
  // Do something
  return query;
});

afterExecute

import { afterExecute } from "libsql-client-hooks";

const logAfterExecute = afterExecute((result, query) => {
  // Do something
  return result;
});

beforeBatch

import { beforeBatch } from "libsql-client-hooks";

const logBeforeBatch = beforeBatch((stmts) => {
  // Do something
  return stmts;
});

afterBatch

import { afterBatch } from "libsql-client-hooks";

const logAfterBatch = afterBatch((results, stmts) => {
  // Do something
  return results;
});

withLibsqlHooks

The withLibsqlHooks method binds the original @libsql/client methods so you can use them as you normally would, but now with middleware.

import { withLibsqlHooks } from "libsql-client-hooks";

const clientWithHooks = withLibsqlHooks(client, [
  // Your plugins
]);

// Use the `@libsql/client` as you normally would
// But now with middleware!
await clientWithHooks.execute();
await clientWithHooks.batch();
await clientWithHooks.transaction();

About


Languages

Language:TypeScript 99.1%Language:JavaScript 0.9%