@broberg/db-sdk 0.1.0

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 ADDED
@@ -0,0 +1,61 @@
1
+ # @broberg/db-sdk
2
+
3
+ The WebHouse fleet's shared **database transport** — one thin facade every
4
+ project depends on, instead of scattering driver calls across repos. The same
5
+ play as [`@broberg/ai-sdk`](https://www.npmjs.com/package/@broberg/ai-sdk), for
6
+ the data layer.
7
+
8
+ **Turso/libSQL today; Postgres/Supabase transports planned** behind the same API.
9
+ Pilot consumer: **Buddy Cloud** (F067).
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ bun add @broberg/db-sdk
15
+ # or: npm i @broberg/db-sdk
16
+ ```
17
+
18
+ ## Use
19
+
20
+ ```ts
21
+ import { createClient, createReplica, health } from '@broberg/db-sdk';
22
+
23
+ // Remote (cloud hub). Reads TURSO_DATABASE_URL + TURSO_AUTH_TOKEN from env.
24
+ const db = createClient();
25
+ await health(db); // { ok: true, version: "3.45.1" }
26
+
27
+ // Embedded replica (edge daemon): a local file synced from the remote primary.
28
+ const edge = createReplica({ path: '/data/buddy.db', syncIntervalSeconds: 30 });
29
+ // reads hit the local file at SQLite speed; writes forward to the primary
30
+ await edge.sync(); // or rely on the interval
31
+ ```
32
+
33
+ **Thin by design.** No ORM coupling — layer Drizzle in the consumer:
34
+
35
+ ```ts
36
+ import { drizzle } from 'drizzle-orm/libsql';
37
+ import { createClient } from '@broberg/db-sdk';
38
+ const orm = drizzle(createClient(), { schema });
39
+ ```
40
+
41
+ ## API
42
+
43
+ | Export | Signature | What it does |
44
+ |---|---|---|
45
+ | `createClient` | `(cfg?: DbConfig) => Client` | Remote client. `cfg`: `{ url?, authToken?, backend? }` (env fallback). |
46
+ | `createReplica` | `(cfg: ReplicaConfig) => Client` | Embedded replica. `cfg`: `{ path, syncUrl?, authToken?, syncIntervalSeconds?, backend? }`. |
47
+ | `health` | `(client) => Promise<{ ok, version }>` | Liveness + SQLite-version probe. |
48
+
49
+ Re-exports `Client` / `ResultSet` (from `@libsql/client`) and the config types.
50
+
51
+ ## Multi-backend roadmap
52
+
53
+ `backend` defaults to `'libsql'` (Turso) — the only transport implemented today.
54
+ `postgres` / `supabase` are the planned extension points: a new transport lands
55
+ under `src/backends/` and widens the `Backend` union, **without changing the
56
+ public `createClient` / `createReplica` surface**. The domain layer (ORM, schema,
57
+ tenant-scoping) stays in each consumer.
58
+
59
+ ## License
60
+
61
+ FSL-1.1-Apache-2.0 © Christian Broberg
@@ -0,0 +1,68 @@
1
+ import { Client } from '@libsql/client';
2
+ export { Client, ResultSet } from '@libsql/client';
3
+
4
+ /** Shared types for @broberg/db-sdk. */
5
+
6
+ /**
7
+ * Which database transport backs the client. `libsql` (Turso) is the only
8
+ * backend implemented today; `postgres` / `supabase` are the planned extension
9
+ * points — adding one widens this union WITHOUT changing the public API.
10
+ */
11
+ type Backend = 'libsql';
12
+ /** Connection config for a remote database. Falls back to env. */
13
+ interface DbConfig {
14
+ /** `libsql://…` (remote) or `file:…` (local). Default: `$TURSO_DATABASE_URL`. */
15
+ url?: string;
16
+ /** Auth token. Default: `$TURSO_AUTH_TOKEN`. */
17
+ authToken?: string;
18
+ /** Transport backend. Default `libsql`. */
19
+ backend?: Backend;
20
+ }
21
+ /** Embedded-replica config: a local file kept in sync with a remote primary. */
22
+ interface ReplicaConfig {
23
+ /** Local file path for the replica DB (e.g. `/data/buddy.db`). */
24
+ path: string;
25
+ /** Remote primary to sync from. Default: `$TURSO_DATABASE_URL`. */
26
+ syncUrl?: string;
27
+ /** Auth token for the remote. Default: `$TURSO_AUTH_TOKEN`. */
28
+ authToken?: string;
29
+ /** Auto-sync interval in seconds. Omit to sync manually via `client.sync()`. */
30
+ syncIntervalSeconds?: number;
31
+ /** Transport backend. Default `libsql` (embedded replicas are libSQL-specific). */
32
+ backend?: Backend;
33
+ }
34
+
35
+ /**
36
+ * @broberg/db-sdk — the WebHouse fleet's shared database client.
37
+ *
38
+ * One thin facade, every project: instead of scattering driver calls in each
39
+ * repo, projects depend on this package and gain a single place to evolve
40
+ * transports, limits and observability — the same play as `@broberg/ai-sdk`.
41
+ *
42
+ * Turso/libSQL is the only transport implemented today (Buddy Cloud, F067, is
43
+ * the pilot consumer); `postgres` / `supabase` are planned behind the same
44
+ * `createClient` / `createReplica` surface (see `Backend`). Intentionally THIN:
45
+ * a remote client, an embedded-replica client, and a health probe. ORM stays in
46
+ * the consumer (`drizzle(createClient(), { schema })`).
47
+ */
48
+
49
+ /**
50
+ * Create a client against a REMOTE database. With no args it reads
51
+ * `TURSO_DATABASE_URL` + `TURSO_AUTH_TOKEN` from the env. Backend defaults to
52
+ * `libsql` (Turso).
53
+ */
54
+ declare function createClient(cfg?: DbConfig): Client;
55
+ /**
56
+ * Create an EMBEDDED REPLICA: a local SQLite file kept in sync with the remote
57
+ * Turso primary. Reads hit the local file at SQLite speed; writes forward to the
58
+ * primary; `client.sync()` (or `syncIntervalSeconds`) pulls the latest. This is
59
+ * how F067 edge daemons read fleet state fast and tolerate a cloud blip.
60
+ */
61
+ declare function createReplica(cfg: ReplicaConfig): Client;
62
+ /** Liveness + SQLite version probe — handy for health endpoints. */
63
+ declare function health(client: Client): Promise<{
64
+ ok: boolean;
65
+ version: string;
66
+ }>;
67
+
68
+ export { type Backend, type DbConfig, type ReplicaConfig, createClient, createReplica, health };
package/dist/index.js ADDED
@@ -0,0 +1,53 @@
1
+ // src/backends/libsql.ts
2
+ import { createClient as createLibsqlClient } from "@libsql/client";
3
+ function requireUrl(url, what) {
4
+ if (!url) {
5
+ throw new Error(
6
+ `@broberg/db-sdk: missing ${what} \u2014 set TURSO_DATABASE_URL or pass it explicitly`
7
+ );
8
+ }
9
+ return url;
10
+ }
11
+ function libsqlClient(cfg) {
12
+ const url = requireUrl(cfg.url ?? process.env.TURSO_DATABASE_URL, "database url");
13
+ const authToken = cfg.authToken ?? process.env.TURSO_AUTH_TOKEN;
14
+ return createLibsqlClient({ url, authToken });
15
+ }
16
+ function libsqlReplica(cfg) {
17
+ const syncUrl = requireUrl(cfg.syncUrl ?? process.env.TURSO_DATABASE_URL, "sync url");
18
+ const authToken = cfg.authToken ?? process.env.TURSO_AUTH_TOKEN;
19
+ return createLibsqlClient({
20
+ url: `file:${cfg.path}`,
21
+ syncUrl,
22
+ authToken,
23
+ syncInterval: cfg.syncIntervalSeconds
24
+ });
25
+ }
26
+
27
+ // src/index.ts
28
+ function unsupported(backend) {
29
+ return new Error(
30
+ `@broberg/db-sdk: backend '${backend}' is not implemented yet \u2014 only 'libsql' (Turso) is available. Add it under src/backends/ and widen the Backend type; the public API stays the same.`
31
+ );
32
+ }
33
+ function createClient(cfg = {}) {
34
+ const backend = cfg.backend ?? "libsql";
35
+ if (backend === "libsql") return libsqlClient(cfg);
36
+ throw unsupported(backend);
37
+ }
38
+ function createReplica(cfg) {
39
+ const backend = cfg.backend ?? "libsql";
40
+ if (backend === "libsql") return libsqlReplica(cfg);
41
+ throw unsupported(backend);
42
+ }
43
+ async function health(client) {
44
+ const res = await client.execute("select sqlite_version() as v");
45
+ const version = String(res.rows[0]?.v ?? "unknown");
46
+ return { ok: true, version };
47
+ }
48
+ export {
49
+ createClient,
50
+ createReplica,
51
+ health
52
+ };
53
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/backends/libsql.ts","../src/index.ts"],"sourcesContent":["/** libSQL / Turso backend for @broberg/db-sdk. */\nimport { createClient as createLibsqlClient } from '@libsql/client';\nimport type { Client } from '@libsql/client';\nimport type { DbConfig, ReplicaConfig } from '../types';\n\nfunction requireUrl(url: string | undefined, what: string): string {\n if (!url) {\n throw new Error(\n `@broberg/db-sdk: missing ${what} — set TURSO_DATABASE_URL or pass it explicitly`,\n );\n }\n return url;\n}\n\n/** Remote libSQL/Turso client. */\nexport function libsqlClient(cfg: DbConfig): Client {\n const url = requireUrl(cfg.url ?? process.env.TURSO_DATABASE_URL, 'database url');\n const authToken = cfg.authToken ?? process.env.TURSO_AUTH_TOKEN;\n return createLibsqlClient({ url, authToken });\n}\n\n/** Embedded replica: a local SQLite file kept in sync with the remote primary. */\nexport function libsqlReplica(cfg: ReplicaConfig): Client {\n const syncUrl = requireUrl(cfg.syncUrl ?? process.env.TURSO_DATABASE_URL, 'sync url');\n const authToken = cfg.authToken ?? process.env.TURSO_AUTH_TOKEN;\n return createLibsqlClient({\n url: `file:${cfg.path}`,\n syncUrl,\n authToken,\n syncInterval: cfg.syncIntervalSeconds,\n });\n}\n","/**\n * @broberg/db-sdk — the WebHouse fleet's shared database client.\n *\n * One thin facade, every project: instead of scattering driver calls in each\n * repo, projects depend on this package and gain a single place to evolve\n * transports, limits and observability — the same play as `@broberg/ai-sdk`.\n *\n * Turso/libSQL is the only transport implemented today (Buddy Cloud, F067, is\n * the pilot consumer); `postgres` / `supabase` are planned behind the same\n * `createClient` / `createReplica` surface (see `Backend`). Intentionally THIN:\n * a remote client, an embedded-replica client, and a health probe. ORM stays in\n * the consumer (`drizzle(createClient(), { schema })`).\n */\nimport type { Client, DbConfig, ReplicaConfig } from './types';\nimport { libsqlClient, libsqlReplica } from './backends/libsql';\n\nexport type { Client, ResultSet, DbConfig, ReplicaConfig, Backend } from './types';\n\nfunction unsupported(backend: string): Error {\n return new Error(\n `@broberg/db-sdk: backend '${backend}' is not implemented yet — only 'libsql' (Turso) ` +\n `is available. Add it under src/backends/ and widen the Backend type; the public ` +\n `API stays the same.`,\n );\n}\n\n/**\n * Create a client against a REMOTE database. With no args it reads\n * `TURSO_DATABASE_URL` + `TURSO_AUTH_TOKEN` from the env. Backend defaults to\n * `libsql` (Turso).\n */\nexport function createClient(cfg: DbConfig = {}): Client {\n const backend: string = cfg.backend ?? 'libsql';\n if (backend === 'libsql') return libsqlClient(cfg);\n throw unsupported(backend);\n}\n\n/**\n * Create an EMBEDDED REPLICA: a local SQLite file kept in sync with the remote\n * Turso primary. Reads hit the local file at SQLite speed; writes forward to the\n * primary; `client.sync()` (or `syncIntervalSeconds`) pulls the latest. This is\n * how F067 edge daemons read fleet state fast and tolerate a cloud blip.\n */\nexport function createReplica(cfg: ReplicaConfig): Client {\n const backend: string = cfg.backend ?? 'libsql';\n if (backend === 'libsql') return libsqlReplica(cfg);\n throw unsupported(backend);\n}\n\n/** Liveness + SQLite version probe — handy for health endpoints. */\nexport async function health(client: Client): Promise<{ ok: boolean; version: string }> {\n const res = await client.execute('select sqlite_version() as v');\n const version = String(res.rows[0]?.v ?? 'unknown');\n return { ok: true, version };\n}\n"],"mappings":";AACA,SAAS,gBAAgB,0BAA0B;AAInD,SAAS,WAAW,KAAyB,MAAsB;AACjE,MAAI,CAAC,KAAK;AACR,UAAM,IAAI;AAAA,MACR,4BAA4B,IAAI;AAAA,IAClC;AAAA,EACF;AACA,SAAO;AACT;AAGO,SAAS,aAAa,KAAuB;AAClD,QAAM,MAAM,WAAW,IAAI,OAAO,QAAQ,IAAI,oBAAoB,cAAc;AAChF,QAAM,YAAY,IAAI,aAAa,QAAQ,IAAI;AAC/C,SAAO,mBAAmB,EAAE,KAAK,UAAU,CAAC;AAC9C;AAGO,SAAS,cAAc,KAA4B;AACxD,QAAM,UAAU,WAAW,IAAI,WAAW,QAAQ,IAAI,oBAAoB,UAAU;AACpF,QAAM,YAAY,IAAI,aAAa,QAAQ,IAAI;AAC/C,SAAO,mBAAmB;AAAA,IACxB,KAAK,QAAQ,IAAI,IAAI;AAAA,IACrB;AAAA,IACA;AAAA,IACA,cAAc,IAAI;AAAA,EACpB,CAAC;AACH;;;ACbA,SAAS,YAAY,SAAwB;AAC3C,SAAO,IAAI;AAAA,IACT,6BAA6B,OAAO;AAAA,EAGtC;AACF;AAOO,SAAS,aAAa,MAAgB,CAAC,GAAW;AACvD,QAAM,UAAkB,IAAI,WAAW;AACvC,MAAI,YAAY,SAAU,QAAO,aAAa,GAAG;AACjD,QAAM,YAAY,OAAO;AAC3B;AAQO,SAAS,cAAc,KAA4B;AACxD,QAAM,UAAkB,IAAI,WAAW;AACvC,MAAI,YAAY,SAAU,QAAO,cAAc,GAAG;AAClD,QAAM,YAAY,OAAO;AAC3B;AAGA,eAAsB,OAAO,QAA2D;AACtF,QAAM,MAAM,MAAM,OAAO,QAAQ,8BAA8B;AAC/D,QAAM,UAAU,OAAO,IAAI,KAAK,CAAC,GAAG,KAAK,SAAS;AAClD,SAAO,EAAE,IAAI,MAAM,QAAQ;AAC7B;","names":[]}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@broberg/db-sdk",
3
+ "version": "0.1.0",
4
+ "description": "Thin shared database client for the WebHouse fleet — one DB SDK, every project. Turso/libSQL today; Postgres/Supabase transports later. Pilot: Buddy Cloud (F067).",
5
+ "type": "module",
6
+ "license": "FSL-1.1-Apache-2.0",
7
+ "author": "Christian Broberg <cb@webhouse.dk>",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/broberg-ai/db-sdk.git"
11
+ },
12
+ "publishConfig": {
13
+ "access": "public",
14
+ "provenance": true
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "main": "./dist/index.js",
20
+ "module": "./dist/index.js",
21
+ "types": "./dist/index.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.js"
26
+ }
27
+ },
28
+ "scripts": {
29
+ "build": "tsup",
30
+ "typecheck": "tsc --noEmit",
31
+ "test": "bun test",
32
+ "clean": "rm -rf dist .turbo"
33
+ },
34
+ "dependencies": {
35
+ "@libsql/client": "^0.17.3"
36
+ },
37
+ "devDependencies": {
38
+ "@types/bun": "^1.1.0",
39
+ "tsup": "^8.3.0",
40
+ "typescript": "^5.6.0"
41
+ },
42
+ "engines": {
43
+ "bun": ">=1.3.0"
44
+ }
45
+ }