Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/notrab/libsql-middleware
Middleware for @libsql/client
https://github.com/notrab/libsql-middleware
libsql sqlite turso
Last synced: 2 months ago
JSON representation
Middleware for @libsql/client
- Host: GitHub
- URL: https://github.com/notrab/libsql-middleware
- Owner: notrab
- Created: 2024-02-07T18:02:34.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-09-27T18:20:20.000Z (4 months ago)
- Last Synced: 2024-09-28T14:05:30.190Z (4 months ago)
- Topics: libsql, sqlite, turso
- Language: TypeScript
- Homepage:
- Size: 439 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# libsql-middleware
The middleware wrapper for `@libsql/client`.
![NPM](https://img.shields.io/npm/v/libsql-middleware)
## Install
```bash
npm install libsql-middleware
```Make sure to install `@libsql/client` if you don't already have it.
## Quickstart
```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");
```## API Reference
### `beforeExecute`
```ts
import { beforeExecute } from "libsql-middleware";const logBeforeExecute = beforeExecute((query) => {
// Do something
return query;
});
```### `afterExecute`
```ts
import { afterExecute } from "libsql-middleware";const logAfterExecute = afterExecute((result, query) => {
// Do something
return result;
});
```### `beforeBatch`
```ts
import { beforeBatch } from "libsql-middleware";const logBeforeBatch = beforeBatch((stmts) => {
// Do something
return stmts;
});
```### `afterBatch`
```ts
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.
```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();
```