@healthzkit/sqlite 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +174 -0
- package/dist/better-sqlite3.d.mts +19 -0
- package/dist/better-sqlite3.mjs +1 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.mjs +1 -0
- package/dist/libsql.d.mts +18 -0
- package/dist/libsql.mjs +1 -0
- package/dist/shared-BIWNv8eR.d.mts +18 -0
- package/dist/shared-BpZLjEOH.mjs +1 -0
- package/dist/sqlite3.d.mts +18 -0
- package/dist/sqlite3.mjs +1 -0
- package/package.json +59 -0
package/README.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# @healthzkit/sqlite
|
|
2
|
+
|
|
3
|
+
SQLite **healthzkit** `HealthAdapter` helpers for **[`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3)** (synchronous), **[`sqlite3`](https://github.com/TryGhost/node-sqlite3)** (callback driver), and **[`@libsql/client`](https://github.com/tursodatabase/libsql-client-ts)** (libSQL / Turso). Successful checks run a lightweight SQL probe (default **`SELECT 1`**) and return `ok` with `metadata.latencyMs` plus any fields from an optional `metadata` hook; failures return `fail` with the caught error.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @healthzkit/sqlite healthzkit
|
|
9
|
+
# plus one of:
|
|
10
|
+
npm install better-sqlite3
|
|
11
|
+
npm install sqlite3
|
|
12
|
+
npm install @libsql/client
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
All drivers are optional peers—install only the one you use.
|
|
16
|
+
|
|
17
|
+
## Package entrypoints
|
|
18
|
+
|
|
19
|
+
| Import | Exports |
|
|
20
|
+
| ----------------------------------- | -------------------------------------------------------------------- |
|
|
21
|
+
| `@healthzkit/sqlite` | `betterSqlite3Adapter`, `sqlite3Adapter`, `libsqlAdapter`, and types |
|
|
22
|
+
| `@healthzkit/sqlite/better-sqlite3` | `betterSqlite3Adapter` only |
|
|
23
|
+
| `@healthzkit/sqlite/sqlite3` | `sqlite3Adapter` only |
|
|
24
|
+
| `@healthzkit/sqlite/libsql` | `libsqlAdapter` only |
|
|
25
|
+
|
|
26
|
+
Use subpath imports when you want to avoid pulling unused drivers into your bundle analysis path.
|
|
27
|
+
|
|
28
|
+
## Shared options
|
|
29
|
+
|
|
30
|
+
Every factory accepts `BaseSqliteOptions`:
|
|
31
|
+
|
|
32
|
+
| Option | Description |
|
|
33
|
+
| ---------- | ----------------------------------------------------------------------------------------------------- |
|
|
34
|
+
| `query` | SQL executed for the probe. Default `SELECT 1`. |
|
|
35
|
+
| `metadata` | Optional `(client) => Record<string, unknown>` (sync or async) merged into metadata with `latencyMs`. |
|
|
36
|
+
|
|
37
|
+
Pass either connection credentials or an existing `client` (not both). The shape depends on the adapter (see below).
|
|
38
|
+
|
|
39
|
+
## `betterSqlite3Adapter` (`better-sqlite3`)
|
|
40
|
+
|
|
41
|
+
**Peer:** `better-sqlite3` ≥ 12.
|
|
42
|
+
|
|
43
|
+
Runs `client.prepare(query).get()` and records round-trip latency. Best for embedded SQLite in the same process.
|
|
44
|
+
|
|
45
|
+
### Connection string
|
|
46
|
+
|
|
47
|
+
The adapter lazily imports `better-sqlite3`, opens the database once, and reuses it across checks.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { createHealthKit } from "healthzkit";
|
|
51
|
+
import { betterSqlite3Adapter } from "@healthzkit/sqlite";
|
|
52
|
+
|
|
53
|
+
const kit = createHealthKit({
|
|
54
|
+
checks: [
|
|
55
|
+
{
|
|
56
|
+
name: "sqlite",
|
|
57
|
+
type: ["readiness"],
|
|
58
|
+
adapter: betterSqlite3Adapter({
|
|
59
|
+
connectionString: process.env.SQLITE_PATH ?? ":memory:",
|
|
60
|
+
options: { readonly: true },
|
|
61
|
+
}),
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`connectionString` is a file path (e.g. `./data/app.db`) or `:memory:`.
|
|
68
|
+
|
|
69
|
+
### Existing client
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import Database from "better-sqlite3";
|
|
73
|
+
import { betterSqlite3Adapter } from "@healthzkit/sqlite/better-sqlite3";
|
|
74
|
+
|
|
75
|
+
const db = new Database("app.db");
|
|
76
|
+
|
|
77
|
+
const adapter = betterSqlite3Adapter({
|
|
78
|
+
client: db,
|
|
79
|
+
query: "SELECT sqlite_version()",
|
|
80
|
+
metadata: (client) => ({ path: "app.db" }),
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## `sqlite3Adapter` (`sqlite3`)
|
|
85
|
+
|
|
86
|
+
**Peer:** `sqlite3` ≥ 5 or ≥ 6.
|
|
87
|
+
|
|
88
|
+
Uses the classic callback API: `client.get(query, callback)`. Suitable when you already depend on `node-sqlite3`.
|
|
89
|
+
|
|
90
|
+
### Connection string
|
|
91
|
+
|
|
92
|
+
Opens the database via `new sqlite3.Database(connectionString, mode, callback)` and caches the handle. Default open mode is `OPEN_READWRITE | OPEN_CREATE`.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { sqlite3Adapter } from "@healthzkit/sqlite/sqlite3";
|
|
96
|
+
|
|
97
|
+
const adapter = sqlite3Adapter({
|
|
98
|
+
connectionString: "./data/app.db",
|
|
99
|
+
mode: undefined, // optional; defaults to read/write + create
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Existing client
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import sqlite3 from "sqlite3";
|
|
107
|
+
import { sqlite3Adapter } from "@healthzkit/sqlite";
|
|
108
|
+
|
|
109
|
+
const db = new sqlite3.Database(":memory:");
|
|
110
|
+
|
|
111
|
+
const adapter = sqlite3Adapter({ client: db });
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## `libsqlAdapter` (`@libsql/client`)
|
|
115
|
+
|
|
116
|
+
**Peer:** `@libsql/client` ≥ 0.14.
|
|
117
|
+
|
|
118
|
+
Runs `await client.execute(query)`. Use for [libSQL](https://github.com/tursodatabase/libsql) and [Turso](https://turso.tech/) (`libsql://`, `https://`, file URLs, etc.).
|
|
119
|
+
|
|
120
|
+
### URL and token
|
|
121
|
+
|
|
122
|
+
The adapter lazily imports `@libsql/client`, calls `createClient` once, and reuses the client across checks.
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
import { libsqlAdapter } from "@healthzkit/sqlite/libsql";
|
|
126
|
+
|
|
127
|
+
const adapter = libsqlAdapter({
|
|
128
|
+
url: process.env.TURSO_DATABASE_URL!,
|
|
129
|
+
authToken: process.env.TURSO_AUTH_TOKEN,
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Existing client
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import { createClient } from "@libsql/client";
|
|
137
|
+
import { libsqlAdapter } from "@healthzkit/sqlite";
|
|
138
|
+
|
|
139
|
+
const client = createClient({ url: "file:local.db" });
|
|
140
|
+
|
|
141
|
+
const adapter = libsqlAdapter({
|
|
142
|
+
client,
|
|
143
|
+
metadata: async () => ({ backend: "libsql" }),
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Check result
|
|
148
|
+
|
|
149
|
+
On success:
|
|
150
|
+
|
|
151
|
+
```json
|
|
152
|
+
{
|
|
153
|
+
"status": "ok",
|
|
154
|
+
"metadata": { "latencyMs": 2 }
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
On failure, `status` is `"fail"` and `error` is set (see [healthzkit](https://github.com/alasti-company/healthzkit) for how that rolls up into probe responses).
|
|
159
|
+
|
|
160
|
+
## Scheduling
|
|
161
|
+
|
|
162
|
+
For databases that should not be queried on every probe, pair these adapters with a **`schedule`** on the check so readiness reads cached results. See the **Scheduling** section in the `healthzkit` README.
|
|
163
|
+
|
|
164
|
+
## Development
|
|
165
|
+
|
|
166
|
+
From the monorepo root:
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
vp install
|
|
170
|
+
vp test --filter @healthzkit/sqlite
|
|
171
|
+
vp pack --filter @healthzkit/sqlite
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
See the repo root `AGENTS.md` for Vite+ / `vp` conventions.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { n as HealthAdapter, t as BaseSqliteOptions } from "./shared-BIWNv8eR.mjs";
|
|
2
|
+
import BetterSqlite3Constructor, { Database } from "better-sqlite3";
|
|
3
|
+
|
|
4
|
+
//#region src/better-sqlite3.d.ts
|
|
5
|
+
type BetterSqlite3OpenOptions = NonNullable<ConstructorParameters<typeof BetterSqlite3Constructor>[1]>;
|
|
6
|
+
interface BetterSqlite3AdapterOptionsWithClient extends BaseSqliteOptions<Database> {
|
|
7
|
+
client: Database;
|
|
8
|
+
connectionString?: never;
|
|
9
|
+
options?: never;
|
|
10
|
+
}
|
|
11
|
+
interface BetterSqlite3AdapterOptionsWithConnectionString extends BaseSqliteOptions<Database> {
|
|
12
|
+
connectionString: string;
|
|
13
|
+
options?: BetterSqlite3OpenOptions;
|
|
14
|
+
client?: never;
|
|
15
|
+
}
|
|
16
|
+
type BetterSqlite3AdapterOptions = BetterSqlite3AdapterOptionsWithClient | BetterSqlite3AdapterOptionsWithConnectionString;
|
|
17
|
+
declare function betterSqlite3Adapter(options: BetterSqlite3AdapterOptions): HealthAdapter;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { BetterSqlite3AdapterOptions, BetterSqlite3AdapterOptionsWithClient, BetterSqlite3AdapterOptionsWithConnectionString, betterSqlite3Adapter };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{n as e,r as t}from"./shared-BpZLjEOH.mjs";function n(n){let r=null,i=null;async function a(){if(`connectionString`in n&&n.connectionString)return r||(i||=(async()=>{try{let{default:e}=await import(`better-sqlite3`),t=new e(n.connectionString,n.options);return r=t,t}finally{i=null}})(),i);if(`client`in n&&n.client!==void 0)return n.client;throw Error(`betterSqlite3Adapter: provide a non-empty connectionString or a client`)}return{async check(){try{let e=await a(),r=Date.now();e.prepare(n.query??`SELECT 1`).get();let i=Date.now()-r,o=n.metadata?n.metadata(e):void 0;return t(i,o instanceof Promise?await o:o)}catch(t){return e(t)}}}}export{n as betterSqlite3Adapter};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { BetterSqlite3AdapterOptions, betterSqlite3Adapter } from "./better-sqlite3.mjs";
|
|
2
|
+
import { Sqlite3AdapterOptions, sqlite3Adapter } from "./sqlite3.mjs";
|
|
3
|
+
import { LibsqlAdapterOptions, libsqlAdapter } from "./libsql.mjs";
|
|
4
|
+
export { type BetterSqlite3AdapterOptions, type LibsqlAdapterOptions, type Sqlite3AdapterOptions, betterSqlite3Adapter, libsqlAdapter, sqlite3Adapter };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{betterSqlite3Adapter as e}from"./better-sqlite3.mjs";import{sqlite3Adapter as t}from"./sqlite3.mjs";import{libsqlAdapter as n}from"./libsql.mjs";export{e as betterSqlite3Adapter,n as libsqlAdapter,t as sqlite3Adapter};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { n as HealthAdapter, t as BaseSqliteOptions } from "./shared-BIWNv8eR.mjs";
|
|
2
|
+
import { Client } from "@libsql/client";
|
|
3
|
+
|
|
4
|
+
//#region src/libsql.d.ts
|
|
5
|
+
interface LibsqlAdapterOptionsWithClient extends BaseSqliteOptions<Client> {
|
|
6
|
+
client: Client;
|
|
7
|
+
url?: never;
|
|
8
|
+
authToken?: never;
|
|
9
|
+
}
|
|
10
|
+
interface LibsqlAdapterOptionsWithConnectionString extends BaseSqliteOptions<Client> {
|
|
11
|
+
url: string;
|
|
12
|
+
authToken?: string;
|
|
13
|
+
client?: never;
|
|
14
|
+
}
|
|
15
|
+
type LibsqlAdapterOptions = LibsqlAdapterOptionsWithClient | LibsqlAdapterOptionsWithConnectionString;
|
|
16
|
+
declare function libsqlAdapter(options: LibsqlAdapterOptions): HealthAdapter;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { LibsqlAdapterOptions, LibsqlAdapterOptionsWithClient, LibsqlAdapterOptionsWithConnectionString, libsqlAdapter };
|
package/dist/libsql.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{n as e,r as t}from"./shared-BpZLjEOH.mjs";function n(n){let r=null,i=null;async function a(){if(`url`in n&&n.url)return r||(i||=(async()=>{try{let{createClient:e}=await import(`@libsql/client`),t=e({url:n.url,authToken:n.authToken});return r=t,t}finally{i=null}})(),i);if(`client`in n&&n.client!==void 0)return n.client;throw Error(`libsqlAdapter: provide a non-empty url or a client`)}return{async check(){try{let e=await a(),r=Date.now();await e.execute(n.query??`SELECT 1`);let i=Date.now()-r,o=n.metadata?n.metadata(e):void 0;return t(i,o instanceof Promise?await o:o)}catch(t){return e(t)}}}}export{n as libsqlAdapter};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { HealthAdapter } from "healthzkit";
|
|
2
|
+
|
|
3
|
+
//#region src/shared.d.ts
|
|
4
|
+
type MetadataFn<TClient> = (client: TClient) => Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
5
|
+
interface BaseSqliteOptions<TClient> {
|
|
6
|
+
/**
|
|
7
|
+
* Query to run as the healthcheck.
|
|
8
|
+
* @default "SELECT 1"
|
|
9
|
+
*/
|
|
10
|
+
query?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Optional function to populate metadata in the check result.
|
|
13
|
+
* Receives the resolved client so you can run additional queries.
|
|
14
|
+
*/
|
|
15
|
+
metadata?: MetadataFn<TClient>;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { HealthAdapter as n, BaseSqliteOptions as t };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=`SELECT 1`;function t(e,t){return{status:`ok`,metadata:{...t,latencyMs:e}}}function n(e){return{status:`fail`,error:e instanceof Error?e:Error(String(e))}}export{n,t as r,e as t};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { n as HealthAdapter, t as BaseSqliteOptions } from "./shared-BIWNv8eR.mjs";
|
|
2
|
+
import { Database } from "sqlite3";
|
|
3
|
+
|
|
4
|
+
//#region src/sqlite3.d.ts
|
|
5
|
+
interface Sqlite3AdapterOptionsWithClient extends BaseSqliteOptions<Database> {
|
|
6
|
+
client: Database;
|
|
7
|
+
connectionString?: never;
|
|
8
|
+
mode?: never;
|
|
9
|
+
}
|
|
10
|
+
interface Sqlite3AdapterOptionsWithConnectionString extends BaseSqliteOptions<Database> {
|
|
11
|
+
connectionString: string;
|
|
12
|
+
mode?: number;
|
|
13
|
+
client?: never;
|
|
14
|
+
}
|
|
15
|
+
type Sqlite3AdapterOptions = Sqlite3AdapterOptionsWithClient | Sqlite3AdapterOptionsWithConnectionString;
|
|
16
|
+
declare function sqlite3Adapter(options: Sqlite3AdapterOptions): HealthAdapter;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { Sqlite3AdapterOptions, Sqlite3AdapterOptionsWithClient, Sqlite3AdapterOptionsWithConnectionString, sqlite3Adapter };
|
package/dist/sqlite3.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{n as e,r as t}from"./shared-BpZLjEOH.mjs";function n(n){let r=null,i=null;async function a(){if(`connectionString`in n&&n.connectionString)return r||(i||=(async()=>{try{let{default:e}=await import(`sqlite3`),t=await new Promise((t,r)=>{let i=n.mode??e.OPEN_READWRITE|e.OPEN_CREATE,a=new e.Database(n.connectionString,i,e=>{if(e){r(e);return}queueMicrotask(()=>t(a))})});return r=t,t}finally{i=null}})(),i);if(`client`in n&&n.client!==void 0)return n.client;throw Error(`sqlite3Adapter: provide a non-empty connectionString or a client`)}return{async check(){try{let e=await a(),r=Date.now();await new Promise((t,r)=>{e.get(n.query??`SELECT 1`,e=>{e?r(e):t()})});let i=Date.now()-r,o=n.metadata?n.metadata(e):void 0;return t(i,o instanceof Promise?await o:o)}catch(t){return e(t)}}}}export{n as sqlite3Adapter};
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@healthzkit/sqlite",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "AGPL-3.0-only",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/alasti-company/healthzkit.dev",
|
|
8
|
+
"directory": "packages/sqlite"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": "./dist/index.mjs",
|
|
16
|
+
"./better-sqlite3": "./dist/better-sqlite3.mjs",
|
|
17
|
+
"./libsql": "./dist/libsql.mjs",
|
|
18
|
+
"./sqlite3": "./dist/sqlite3.mjs",
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "vp pack",
|
|
26
|
+
"dev": "vp pack --watch",
|
|
27
|
+
"test": "vp test",
|
|
28
|
+
"check": "vp check",
|
|
29
|
+
"prepublishOnly": "vp run build"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@libsql/client": "^0.17.3",
|
|
33
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
34
|
+
"@types/node": "^25.6.2",
|
|
35
|
+
"@typescript/native-preview": "7.0.0-dev.20260509.2",
|
|
36
|
+
"better-sqlite3": "^12.10.0",
|
|
37
|
+
"bumpp": "^11.1.0",
|
|
38
|
+
"healthzkit": "workspace:*",
|
|
39
|
+
"sqlite3": "^6.0.1",
|
|
40
|
+
"typescript": "^6.0.3",
|
|
41
|
+
"vite-plus": "^0.1.20"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@libsql/client": ">=0.14.0",
|
|
45
|
+
"better-sqlite3": ">=12.0.0",
|
|
46
|
+
"sqlite3": ">=5.0.0 || >=6.0.0"
|
|
47
|
+
},
|
|
48
|
+
"peerDependenciesMeta": {
|
|
49
|
+
"@libsql/client": {
|
|
50
|
+
"optional": true
|
|
51
|
+
},
|
|
52
|
+
"better-sqlite3": {
|
|
53
|
+
"optional": true
|
|
54
|
+
},
|
|
55
|
+
"sqlite3": {
|
|
56
|
+
"optional": true
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|