@storyshelf/db-sqlite 0.3.2 → 0.5.3
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 +29 -3
- package/dist/better-sqlite3.d.mts +29 -0
- package/dist/better-sqlite3.mjs +116 -0
- package/dist/better-sqlite3.mjs.map +1 -0
- package/dist/bun-sqlite.d.mts +30 -0
- package/dist/bun-sqlite.mjs +120 -0
- package/dist/bun-sqlite.mjs.map +1 -0
- package/dist/d1.d.mts +38 -0
- package/dist/d1.mjs +108 -0
- package/dist/d1.mjs.map +1 -0
- package/dist/ddl.d.mts +14 -0
- package/dist/ddl.mjs +342 -0
- package/dist/ddl.mjs.map +1 -0
- package/dist/drizzle-factory.d.mts +19 -0
- package/dist/drizzle-factory.mjs +121 -0
- package/dist/drizzle-factory.mjs.map +1 -0
- package/dist/index.mjs +102 -36
- package/dist/index.mjs.map +1 -1
- package/dist/schema/baseline.d.mts +204 -0
- package/dist/schema/baseline.mjs +23 -0
- package/dist/schema/baseline.mjs.map +1 -0
- package/dist/schema/build.d.mts +405 -0
- package/dist/schema/build.mjs +33 -0
- package/dist/schema/build.mjs.map +1 -0
- package/dist/schema/capture-attempt.d.mts +482 -0
- package/dist/schema/capture-attempt.mjs +40 -0
- package/dist/schema/capture-attempt.mjs.map +1 -0
- package/dist/schema/comment.d.mts +215 -0
- package/dist/schema/comment.mjs +26 -0
- package/dist/schema/comment.mjs.map +1 -0
- package/dist/schema/content-refs.d.mts +95 -0
- package/dist/schema/content-refs.mjs +16 -0
- package/dist/schema/content-refs.mjs.map +1 -0
- package/dist/schema/label.d.mts +288 -0
- package/dist/schema/label.mjs +30 -0
- package/dist/schema/label.mjs.map +1 -0
- package/dist/schema/member.d.mts +140 -0
- package/dist/schema/member.mjs +20 -0
- package/dist/schema/member.mjs.map +1 -0
- package/dist/schema/notification.d.mts +344 -0
- package/dist/schema/notification.mjs +33 -0
- package/dist/schema/notification.mjs.map +1 -0
- package/dist/schema/project-group-mapping.d.mts +119 -0
- package/dist/schema/project-group-mapping.mjs +18 -0
- package/dist/schema/project-group-mapping.mjs.map +1 -0
- package/dist/schema/project.d.mts +358 -0
- package/dist/schema/project.mjs +29 -0
- package/dist/schema/project.mjs.map +1 -0
- package/dist/schema/snapshot.d.mts +487 -0
- package/dist/schema/snapshot.mjs +38 -0
- package/dist/schema/snapshot.mjs.map +1 -0
- package/dist/schema/status-config.d.mts +157 -0
- package/dist/schema/status-config.mjs +20 -0
- package/dist/schema/status-config.mjs.map +1 -0
- package/dist/schema/token.d.mts +157 -0
- package/dist/schema/token.mjs +21 -0
- package/dist/schema/token.mjs.map +1 -0
- package/dist/schema/user-invite.d.mts +137 -0
- package/dist/schema/user-invite.mjs +19 -0
- package/dist/schema/user-invite.mjs.map +1 -0
- package/dist/schema/user.d.mts +238 -0
- package/dist/schema/user.mjs +23 -0
- package/dist/schema/user.mjs.map +1 -0
- package/dist/schema/webhook.d.mts +157 -0
- package/dist/schema/webhook.mjs +20 -0
- package/dist/schema/webhook.mjs.map +1 -0
- package/dist/schema.d.mts +47 -0
- package/dist/schema.mjs +46 -0
- package/dist/schema.mjs.map +1 -0
- package/dist/turso.d.mts +32 -0
- package/dist/turso.mjs +134 -0
- package/dist/turso.mjs.map +1 -0
- package/package.json +66 -10
package/README.md
CHANGED
|
@@ -18,13 +18,13 @@ npm install @storyshelf/db-sqlite
|
|
|
18
18
|
|
|
19
19
|
```ts
|
|
20
20
|
import { createSqliteDatabase } from "@storyshelf/db-sqlite";
|
|
21
|
-
import {
|
|
21
|
+
import { createShelfApp } from "@storyshelf/app";
|
|
22
22
|
import { createLocalStorage } from "@storyshelf/storage-local";
|
|
23
23
|
|
|
24
24
|
const database = createSqliteDatabase("./data/shelf.db");
|
|
25
25
|
|
|
26
26
|
const storage = createLocalStorage("./data");
|
|
27
|
-
const app =
|
|
27
|
+
const app = createShelfApp({ database, storage });
|
|
28
28
|
await app.lifecycle.init();
|
|
29
29
|
```
|
|
30
30
|
|
|
@@ -36,8 +36,34 @@ Opens (or creates) a SQLite database at `path` and returns a `DatabaseAdapter`.
|
|
|
36
36
|
|
|
37
37
|
The returned adapter implements every method of the `DatabaseAdapter` interface (`insert`, `update`, `get`, `remove`, `list`, `count`, `all`) plus `metadata`/`lifecycle` from the shared `Adapter` base.
|
|
38
38
|
|
|
39
|
+
## Presets (same schema, different driver)
|
|
40
|
+
|
|
41
|
+
| Subpath | Driver | Install | When |
|
|
42
|
+
| -------------------------------------- | --------------------- | ------------------------ | -------------------------------------------------------------------------------------- |
|
|
43
|
+
| `@storyshelf/db-sqlite` (root) | `node:sqlite` builtin | — | Default. Single-node VPS/Docker. Zero config. |
|
|
44
|
+
| `@storyshelf/db-sqlite/turso` | `@libsql/client` | `nub add @libsql/client` | Serverless (Turso cloud, Vercel, Lambda); `file:` URLs and embedded replicas work too. |
|
|
45
|
+
| `@storyshelf/db-sqlite/better-sqlite3` | `better-sqlite3` | `nub add better-sqlite3` | Native sync driver (needs `allowBuilds` for the postinstall). |
|
|
46
|
+
| `@storyshelf/db-sqlite/bun-sqlite` | `bun:sqlite` builtin | — (Bun only) | Bun runtime. |
|
|
47
|
+
| `@storyshelf/db-sqlite/d1` | D1 binding | — (Workers only) | Cloudflare Workers (`env.DB`). |
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { createTursoDatabase } from "@storyshelf/db-sqlite/turso";
|
|
51
|
+
|
|
52
|
+
const database = createTursoDatabase({
|
|
53
|
+
url: process.env["TURSO_DATABASE_URL"],
|
|
54
|
+
authToken: process.env["TURSO_AUTH_TOKEN"],
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
All presets accept an injected `client` instead of connection options (the caller then owns teardown); all except D1 also accept plain connection options and throw on missing/invalid input. Driver peers are optional: consumers only install their backend's client.
|
|
59
|
+
|
|
60
|
+
> Do not add any of drizzle-orm's peer packages as a direct dependency of a
|
|
61
|
+
> single workspace package — drizzle-orm resolves a distinct copy per unique
|
|
62
|
+
> peer set, which splits the shared `Table`/`SQL` types. Preset types that a
|
|
63
|
+
> peer would provide live in colocated `*-ambient.d.ts` files instead.
|
|
64
|
+
|
|
39
65
|
## How it fits in
|
|
40
66
|
|
|
41
|
-
`db-sqlite` is the default `database` option for `
|
|
67
|
+
`db-sqlite` is the default `database` option for `createShelfApp` in single-node self-hosted deployments. It implements the same `DatabaseAdapter` interface across the root entry and every preset subpath, so switching databases only means swapping the import.
|
|
42
68
|
|
|
43
69
|
See `docs/architecture.md` and ADR 0002.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import __tsdown_shims_path from 'node:path';
|
|
2
|
+
import __tsdown_shims_url from 'node:url';
|
|
3
|
+
import Database from "better-sqlite3";
|
|
4
|
+
import { DatabaseAdapter } from "@storyshelf/core/adapter/database";
|
|
5
|
+
//#region src/better-sqlite3.d.ts
|
|
6
|
+
type BetterSqlite3Client = InstanceType<typeof Database>;
|
|
7
|
+
/**
|
|
8
|
+
* Options for creating a better-sqlite3-backed DatabaseAdapter.
|
|
9
|
+
*
|
|
10
|
+
* When `client` is supplied, `path` is ignored and the caller owns the
|
|
11
|
+
* lifecycle of the client (teardown is a no-op). This is the hook for
|
|
12
|
+
* shared connections and hermetic tests.
|
|
13
|
+
*/
|
|
14
|
+
interface BetterSqlite3DatabaseOptions {
|
|
15
|
+
path?: string;
|
|
16
|
+
readonly?: boolean;
|
|
17
|
+
client?: BetterSqlite3Client;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create a better-sqlite3-backed DatabaseAdapter using Drizzle ORM.
|
|
21
|
+
*
|
|
22
|
+
* @param options - Either a filesystem `path` (use `":memory:"` for an
|
|
23
|
+
* ephemeral database) or a preconfigured better-sqlite3 `client`.
|
|
24
|
+
* @returns A DatabaseAdapter backed by the SQLite file (WAL mode).
|
|
25
|
+
*/
|
|
26
|
+
declare function createBetterSqlite3Database(options: BetterSqlite3DatabaseOptions): DatabaseAdapter;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { BetterSqlite3DatabaseOptions, createBetterSqlite3Database };
|
|
29
|
+
//# sourceMappingURL=better-sqlite3.d.mts.map
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import "node:path";
|
|
2
|
+
import "node:url";
|
|
3
|
+
import.meta.url;
|
|
4
|
+
import { DDL, tableColumns } from "./ddl.mjs";
|
|
5
|
+
import { createDrizzleAdapter, withManualTransactions } from "./drizzle-factory.mjs";
|
|
6
|
+
import { schema } from "./schema.mjs";
|
|
7
|
+
import Database from "better-sqlite3";
|
|
8
|
+
import { drizzle } from "drizzle-orm/better-sqlite3";
|
|
9
|
+
//#region src/better-sqlite3.ts
|
|
10
|
+
/**
|
|
11
|
+
* better-sqlite3 preset for the SQLite database adapter.
|
|
12
|
+
*
|
|
13
|
+
* Same schema, DDL, and Drizzle factory as the `node:sqlite` default;
|
|
14
|
+
* only the driver differs (synchronous, native). Requires the optional
|
|
15
|
+
* `better-sqlite3` peer (`nub add better-sqlite3`).
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Create a better-sqlite3-backed DatabaseAdapter using Drizzle ORM.
|
|
19
|
+
*
|
|
20
|
+
* @param options - Either a filesystem `path` (use `":memory:"` for an
|
|
21
|
+
* ephemeral database) or a preconfigured better-sqlite3 `client`.
|
|
22
|
+
* @returns A DatabaseAdapter backed by the SQLite file (WAL mode).
|
|
23
|
+
*/
|
|
24
|
+
function createBetterSqlite3Database(options) {
|
|
25
|
+
if (!options.client && options.path === void 0) throw new Error("createBetterSqlite3Database: provide `path` or a preconfigured `client`");
|
|
26
|
+
const owned = !options.client;
|
|
27
|
+
const client = options.client ?? new Database(options.path, connectionOptions(options));
|
|
28
|
+
if (owned) {
|
|
29
|
+
client.exec("PRAGMA journal_mode = WAL");
|
|
30
|
+
client.exec("PRAGMA busy_timeout = 5000");
|
|
31
|
+
}
|
|
32
|
+
const db = drizzle(client, { schema });
|
|
33
|
+
const adapter = createDrizzleAdapter(db, {
|
|
34
|
+
metadata: {
|
|
35
|
+
name: "better-sqlite3",
|
|
36
|
+
version: globalThis.__PKG_VERSION__ ?? "0.0.0",
|
|
37
|
+
description: "better-sqlite3 database adapter (native + Drizzle)",
|
|
38
|
+
kind: "better-sqlite3",
|
|
39
|
+
category: "database"
|
|
40
|
+
},
|
|
41
|
+
tables: schema,
|
|
42
|
+
migrate: () => {
|
|
43
|
+
runMigrations(client);
|
|
44
|
+
},
|
|
45
|
+
close: () => {
|
|
46
|
+
if (owned) client.close();
|
|
47
|
+
},
|
|
48
|
+
ping: () => {
|
|
49
|
+
client.prepare("SELECT 1").get();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return withManualTransactions(adapter, (sql) => {
|
|
53
|
+
client.exec(sql);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
const WEBHOOK_SECRET_DROP = "ALTER TABLE webhooks DROP COLUMN secret";
|
|
57
|
+
/** better-sqlite3 rejects `undefined` option values — pass only defined keys. */
|
|
58
|
+
function connectionOptions(options) {
|
|
59
|
+
return options.readonly === void 0 ? {} : { readonly: options.readonly };
|
|
60
|
+
}
|
|
61
|
+
function execIgnore(client, sql) {
|
|
62
|
+
try {
|
|
63
|
+
client.exec(sql);
|
|
64
|
+
} catch {}
|
|
65
|
+
}
|
|
66
|
+
function migrateCommentsTable(client) {
|
|
67
|
+
if (!client.prepare("SELECT sql FROM sqlite_master WHERE type='table' AND name='comments'").get()?.sql.includes("user_id TEXT NOT NULL REFERENCES users")) return;
|
|
68
|
+
client.exec("PRAGMA foreign_keys = OFF");
|
|
69
|
+
client.exec(`
|
|
70
|
+
CREATE TABLE comments_new (
|
|
71
|
+
id TEXT PRIMARY KEY,
|
|
72
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
73
|
+
build_id TEXT NOT NULL REFERENCES builds(id) ON DELETE CASCADE,
|
|
74
|
+
snapshot_id TEXT REFERENCES snapshots(id) ON DELETE CASCADE,
|
|
75
|
+
user_id TEXT REFERENCES users(id) ON DELETE CASCADE,
|
|
76
|
+
body TEXT NOT NULL,
|
|
77
|
+
parent_id TEXT,
|
|
78
|
+
resolved INTEGER NOT NULL DEFAULT 0,
|
|
79
|
+
created_at TEXT NOT NULL,
|
|
80
|
+
updated_at TEXT NOT NULL
|
|
81
|
+
)
|
|
82
|
+
`);
|
|
83
|
+
client.exec("INSERT INTO comments_new SELECT * FROM comments");
|
|
84
|
+
client.exec("DROP TABLE comments");
|
|
85
|
+
client.exec("ALTER TABLE comments_new RENAME TO comments");
|
|
86
|
+
client.exec("CREATE INDEX IF NOT EXISTS comments_build_id_idx ON comments (build_id)");
|
|
87
|
+
client.exec("PRAGMA foreign_keys = ON");
|
|
88
|
+
}
|
|
89
|
+
/** Add any DDL column missing from a volume created before it shipped. */
|
|
90
|
+
function ensureColumns(client, ddl) {
|
|
91
|
+
for (const [table, columns] of tableColumns(ddl)) {
|
|
92
|
+
const rows = client.prepare(`PRAGMA table_info(${table})`).all();
|
|
93
|
+
const existing = new Set(rows.map((row) => String(row.name)));
|
|
94
|
+
for (const column of columns) {
|
|
95
|
+
const [name] = column.split(/\s+/u);
|
|
96
|
+
if (name && !existing.has(name)) execIgnore(client, `ALTER TABLE ${table} ADD COLUMN ${column}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function runMigrations(client) {
|
|
101
|
+
client.exec("PRAGMA foreign_keys = ON");
|
|
102
|
+
client.exec(DDL);
|
|
103
|
+
ensureColumns(client, DDL);
|
|
104
|
+
try {
|
|
105
|
+
client.exec(WEBHOOK_SECRET_DROP);
|
|
106
|
+
} catch {}
|
|
107
|
+
try {
|
|
108
|
+
migrateCommentsTable(client);
|
|
109
|
+
} catch {
|
|
110
|
+
execIgnore(client, "PRAGMA foreign_keys = ON");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
//#endregion
|
|
114
|
+
export { createBetterSqlite3Database };
|
|
115
|
+
|
|
116
|
+
//# sourceMappingURL=better-sqlite3.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"better-sqlite3.mjs","names":[],"sources":["../src/better-sqlite3.ts"],"sourcesContent":["import type { DatabaseAdapter } from \"@storyshelf/core/adapter/database\";\n/**\n * better-sqlite3 preset for the SQLite database adapter.\n *\n * Same schema, DDL, and Drizzle factory as the `node:sqlite` default;\n * only the driver differs (synchronous, native). Requires the optional\n * `better-sqlite3` peer (`nub add better-sqlite3`).\n */\nimport Database from \"better-sqlite3\";\nimport { drizzle } from \"drizzle-orm/better-sqlite3\";\nimport { DDL, tableColumns } from \"./ddl.ts\";\nimport { createDrizzleAdapter, withManualTransactions } from \"./drizzle-factory.ts\";\nimport { schema } from \"./schema/index.ts\";\n\ndeclare const __PKG_VERSION__: string | undefined;\n\ntype BetterSqlite3Client = InstanceType<typeof Database>;\n\n/**\n * Options for creating a better-sqlite3-backed DatabaseAdapter.\n *\n * When `client` is supplied, `path` is ignored and the caller owns the\n * lifecycle of the client (teardown is a no-op). This is the hook for\n * shared connections and hermetic tests.\n */\nexport interface BetterSqlite3DatabaseOptions {\n path?: string;\n readonly?: boolean;\n client?: BetterSqlite3Client;\n}\n\n/**\n * Create a better-sqlite3-backed DatabaseAdapter using Drizzle ORM.\n *\n * @param options - Either a filesystem `path` (use `\":memory:\"` for an\n * ephemeral database) or a preconfigured better-sqlite3 `client`.\n * @returns A DatabaseAdapter backed by the SQLite file (WAL mode).\n */\nexport function createBetterSqlite3Database(\n options: BetterSqlite3DatabaseOptions,\n): DatabaseAdapter {\n if (!options.client && options.path === undefined) {\n throw new Error(\"createBetterSqlite3Database: provide `path` or a preconfigured `client`\");\n }\n\n const owned = !options.client;\n const client = options.client ?? new Database(options.path, connectionOptions(options));\n if (owned) {\n client.exec(\"PRAGMA journal_mode = WAL\");\n client.exec(\"PRAGMA busy_timeout = 5000\");\n }\n const db = drizzle(client, { schema });\n\n const adapter = createDrizzleAdapter(db, {\n metadata: {\n name: \"better-sqlite3\",\n version: (globalThis as unknown as { __PKG_VERSION__?: string }).__PKG_VERSION__ ?? \"0.0.0\",\n description: \"better-sqlite3 database adapter (native + Drizzle)\",\n kind: \"better-sqlite3\",\n category: \"database\",\n },\n tables: schema,\n migrate: () => {\n runMigrations(client);\n },\n close: () => {\n if (owned) {\n client.close();\n }\n },\n ping: () => {\n client.prepare(\"SELECT 1\").get();\n },\n });\n\n return withManualTransactions(adapter, (sql) => {\n client.exec(sql);\n });\n}\n\nconst WEBHOOK_SECRET_DROP = \"ALTER TABLE webhooks DROP COLUMN secret\";\n\n/** better-sqlite3 rejects `undefined` option values — pass only defined keys. */\nfunction connectionOptions(options: BetterSqlite3DatabaseOptions): { readonly?: boolean } {\n return options.readonly === undefined ? {} : { readonly: options.readonly };\n}\n\nfunction execIgnore(client: BetterSqlite3Client, sql: string): void {\n try {\n client.exec(sql);\n } catch {\n // idempotent — column already exists or other no-op\n }\n}\n\nfunction migrateCommentsTable(client: BetterSqlite3Client): void {\n const row = client\n .prepare(\"SELECT sql FROM sqlite_master WHERE type='table' AND name='comments'\")\n .get() as { sql: string } | undefined;\n if (!row?.sql.includes(\"user_id TEXT NOT NULL REFERENCES users\")) {\n return;\n }\n client.exec(\"PRAGMA foreign_keys = OFF\");\n client.exec(`\n CREATE TABLE comments_new (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n build_id TEXT NOT NULL REFERENCES builds(id) ON DELETE CASCADE,\n snapshot_id TEXT REFERENCES snapshots(id) ON DELETE CASCADE,\n user_id TEXT REFERENCES users(id) ON DELETE CASCADE,\n body TEXT NOT NULL,\n parent_id TEXT,\n resolved INTEGER NOT NULL DEFAULT 0,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n )\n `);\n client.exec(\"INSERT INTO comments_new SELECT * FROM comments\");\n client.exec(\"DROP TABLE comments\");\n client.exec(\"ALTER TABLE comments_new RENAME TO comments\");\n client.exec(\"CREATE INDEX IF NOT EXISTS comments_build_id_idx ON comments (build_id)\");\n client.exec(\"PRAGMA foreign_keys = ON\");\n}\n\n/** Add any DDL column missing from a volume created before it shipped. */\nfunction ensureColumns(client: BetterSqlite3Client, ddl: string): void {\n for (const [table, columns] of tableColumns(ddl)) {\n const rows = client.prepare(`PRAGMA table_info(${table})`).all() as { name: unknown }[];\n const existing = new Set(rows.map((row) => String(row.name)));\n for (const column of columns) {\n const [name] = column.split(/\\s+/u);\n if (name && !existing.has(name)) {\n execIgnore(client, `ALTER TABLE ${table} ADD COLUMN ${column}`);\n }\n }\n }\n}\n\nfunction runMigrations(client: BetterSqlite3Client): void {\n client.exec(\"PRAGMA foreign_keys = ON\");\n client.exec(DDL);\n ensureColumns(client, DDL);\n try {\n client.exec(WEBHOOK_SECRET_DROP);\n } catch {\n // already migrated — ignore\n }\n try {\n migrateCommentsTable(client);\n } catch {\n execIgnore(client, \"PRAGMA foreign_keys = ON\");\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,4BACd,SACiB;CACjB,IAAI,CAAC,QAAQ,UAAU,QAAQ,SAAS,KAAA,GACtC,MAAM,IAAI,MAAM,yEAAyE;CAG3F,MAAM,QAAQ,CAAC,QAAQ;CACvB,MAAM,SAAS,QAAQ,UAAU,IAAI,SAAS,QAAQ,MAAM,kBAAkB,OAAO,CAAC;CACtF,IAAI,OAAO;EACT,OAAO,KAAK,2BAA2B;EACvC,OAAO,KAAK,4BAA4B;CAC1C;CACA,MAAM,KAAK,QAAQ,QAAQ,EAAE,OAAO,CAAC;CAErC,MAAM,UAAU,qBAAqB,IAAI;EACvC,UAAU;GACR,MAAM;GACN,SAAU,WAAuD,mBAAmB;GACpF,aAAa;GACb,MAAM;GACN,UAAU;EACZ;EACA,QAAQ;EACR,eAAe;GACb,cAAc,MAAM;EACtB;EACA,aAAa;GACX,IAAI,OACF,OAAO,MAAM;EAEjB;EACA,YAAY;GACV,OAAO,QAAQ,UAAU,CAAC,CAAC,IAAI;EACjC;CACF,CAAC;CAED,OAAO,uBAAuB,UAAU,QAAQ;EAC9C,OAAO,KAAK,GAAG;CACjB,CAAC;AACH;AAEA,MAAM,sBAAsB;;AAG5B,SAAS,kBAAkB,SAA+D;CACxF,OAAO,QAAQ,aAAa,KAAA,IAAY,CAAC,IAAI,EAAE,UAAU,QAAQ,SAAS;AAC5E;AAEA,SAAS,WAAW,QAA6B,KAAmB;CAClE,IAAI;EACF,OAAO,KAAK,GAAG;CACjB,QAAQ,CAER;AACF;AAEA,SAAS,qBAAqB,QAAmC;CAI/D,IAAI,CAHQ,OACT,QAAQ,sEAAsE,CAAC,CAC/E,IACI,CAAC,EAAE,IAAI,SAAS,wCAAwC,GAC7D;CAEF,OAAO,KAAK,2BAA2B;CACvC,OAAO,KAAK;;;;;;;;;;;;;GAaX;CACD,OAAO,KAAK,iDAAiD;CAC7D,OAAO,KAAK,qBAAqB;CACjC,OAAO,KAAK,6CAA6C;CACzD,OAAO,KAAK,yEAAyE;CACrF,OAAO,KAAK,0BAA0B;AACxC;;AAGA,SAAS,cAAc,QAA6B,KAAmB;CACrE,KAAK,MAAM,CAAC,OAAO,YAAY,aAAa,GAAG,GAAG;EAChD,MAAM,OAAO,OAAO,QAAQ,qBAAqB,MAAM,EAAE,CAAC,CAAC,IAAI;EAC/D,MAAM,WAAW,IAAI,IAAI,KAAK,KAAK,QAAQ,OAAO,IAAI,IAAI,CAAC,CAAC;EAC5D,KAAK,MAAM,UAAU,SAAS;GAC5B,MAAM,CAAC,QAAQ,OAAO,MAAM,MAAM;GAClC,IAAI,QAAQ,CAAC,SAAS,IAAI,IAAI,GAC5B,WAAW,QAAQ,eAAe,MAAM,cAAc,QAAQ;EAElE;CACF;AACF;AAEA,SAAS,cAAc,QAAmC;CACxD,OAAO,KAAK,0BAA0B;CACtC,OAAO,KAAK,GAAG;CACf,cAAc,QAAQ,GAAG;CACzB,IAAI;EACF,OAAO,KAAK,mBAAmB;CACjC,QAAQ,CAER;CACA,IAAI;EACF,qBAAqB,MAAM;CAC7B,QAAQ;EACN,WAAW,QAAQ,0BAA0B;CAC/C;AACF"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import __tsdown_shims_path from 'node:path';
|
|
2
|
+
import __tsdown_shims_url from 'node:url';
|
|
3
|
+
import { Database } from "bun:sqlite";
|
|
4
|
+
import { DatabaseAdapter } from "@storyshelf/core/adapter/database";
|
|
5
|
+
//#region src/bun-sqlite.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Options for creating a Bun-backed DatabaseAdapter.
|
|
8
|
+
*
|
|
9
|
+
* When `client` is supplied, `path` is ignored and the caller owns the
|
|
10
|
+
* lifecycle of the client (teardown is a no-op). This is the hook for
|
|
11
|
+
* shared connections and hermetic tests.
|
|
12
|
+
*/
|
|
13
|
+
interface BunSqliteDatabaseOptions {
|
|
14
|
+
path?: string;
|
|
15
|
+
readonly?: boolean;
|
|
16
|
+
create?: boolean;
|
|
17
|
+
readwrite?: boolean;
|
|
18
|
+
client?: Database;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create a Bun-backed DatabaseAdapter using Drizzle ORM.
|
|
22
|
+
*
|
|
23
|
+
* @param options - Either a filesystem `path` (use `":memory:"` for an
|
|
24
|
+
* ephemeral database) or a preconfigured `bun:sqlite` `client`.
|
|
25
|
+
* @returns A DatabaseAdapter backed by the SQLite file (WAL mode).
|
|
26
|
+
*/
|
|
27
|
+
declare function createBunSqliteDatabase(options: BunSqliteDatabaseOptions): DatabaseAdapter;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { BunSqliteDatabaseOptions, createBunSqliteDatabase };
|
|
30
|
+
//# sourceMappingURL=bun-sqlite.d.mts.map
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import "node:path";
|
|
2
|
+
import "node:url";
|
|
3
|
+
import.meta.url;
|
|
4
|
+
import { DDL, tableColumns } from "./ddl.mjs";
|
|
5
|
+
import { createDrizzleAdapter, withManualTransactions } from "./drizzle-factory.mjs";
|
|
6
|
+
import { schema } from "./schema.mjs";
|
|
7
|
+
import { Database } from "bun:sqlite";
|
|
8
|
+
import { drizzle } from "drizzle-orm/bun-sqlite";
|
|
9
|
+
//#region src/bun-sqlite.ts
|
|
10
|
+
/**
|
|
11
|
+
* Bun preset for the SQLite database adapter.
|
|
12
|
+
*
|
|
13
|
+
* Same schema, DDL, and Drizzle factory as the `node:sqlite` default;
|
|
14
|
+
* only the driver differs (`bun:sqlite`, synchronous builtin). Bun-only:
|
|
15
|
+
* importing this module outside Bun throws when resolving `bun:sqlite`.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Create a Bun-backed DatabaseAdapter using Drizzle ORM.
|
|
19
|
+
*
|
|
20
|
+
* @param options - Either a filesystem `path` (use `":memory:"` for an
|
|
21
|
+
* ephemeral database) or a preconfigured `bun:sqlite` `client`.
|
|
22
|
+
* @returns A DatabaseAdapter backed by the SQLite file (WAL mode).
|
|
23
|
+
*/
|
|
24
|
+
function createBunSqliteDatabase(options) {
|
|
25
|
+
if (!options.client && options.path === void 0) throw new Error("createBunSqliteDatabase: provide `path` or a preconfigured `client`");
|
|
26
|
+
const owned = !options.client;
|
|
27
|
+
const client = options.client ?? new Database(options.path, connectionOptions(options));
|
|
28
|
+
if (owned) {
|
|
29
|
+
client.exec("PRAGMA journal_mode = WAL");
|
|
30
|
+
client.exec("PRAGMA busy_timeout = 5000");
|
|
31
|
+
}
|
|
32
|
+
const db = drizzle(client, { schema });
|
|
33
|
+
const adapter = createDrizzleAdapter(db, {
|
|
34
|
+
metadata: {
|
|
35
|
+
name: "Bun SQLite",
|
|
36
|
+
version: globalThis.__PKG_VERSION__ ?? "0.0.0",
|
|
37
|
+
description: "Bun SQLite database adapter (bun:sqlite + Drizzle)",
|
|
38
|
+
kind: "bun-sqlite",
|
|
39
|
+
category: "database"
|
|
40
|
+
},
|
|
41
|
+
tables: schema,
|
|
42
|
+
migrate: () => {
|
|
43
|
+
runMigrations(client);
|
|
44
|
+
},
|
|
45
|
+
close: () => {
|
|
46
|
+
if (owned) client.close();
|
|
47
|
+
},
|
|
48
|
+
ping: () => {
|
|
49
|
+
client.query("SELECT 1").get();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return withManualTransactions(adapter, (sql) => {
|
|
53
|
+
client.exec(sql);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
const WEBHOOK_SECRET_DROP = "ALTER TABLE webhooks DROP COLUMN secret";
|
|
57
|
+
/** Bun ignores `undefined` options, but only defined keys are passed. */
|
|
58
|
+
function connectionOptions(options) {
|
|
59
|
+
const result = {};
|
|
60
|
+
if (options.readonly !== void 0) result.readonly = options.readonly;
|
|
61
|
+
if (options.create !== void 0) result.create = options.create;
|
|
62
|
+
if (options.readwrite !== void 0) result.readwrite = options.readwrite;
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
function execIgnore(client, sql) {
|
|
66
|
+
try {
|
|
67
|
+
client.exec(sql);
|
|
68
|
+
} catch {}
|
|
69
|
+
}
|
|
70
|
+
function migrateCommentsTable(client) {
|
|
71
|
+
if (!client.query("SELECT sql FROM sqlite_master WHERE type='table' AND name='comments'").get()?.sql.includes("user_id TEXT NOT NULL REFERENCES users")) return;
|
|
72
|
+
client.exec("PRAGMA foreign_keys = OFF");
|
|
73
|
+
client.exec(`
|
|
74
|
+
CREATE TABLE comments_new (
|
|
75
|
+
id TEXT PRIMARY KEY,
|
|
76
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
77
|
+
build_id TEXT NOT NULL REFERENCES builds(id) ON DELETE CASCADE,
|
|
78
|
+
snapshot_id TEXT REFERENCES snapshots(id) ON DELETE CASCADE,
|
|
79
|
+
user_id TEXT REFERENCES users(id) ON DELETE CASCADE,
|
|
80
|
+
body TEXT NOT NULL,
|
|
81
|
+
parent_id TEXT,
|
|
82
|
+
resolved INTEGER NOT NULL DEFAULT 0,
|
|
83
|
+
created_at TEXT NOT NULL,
|
|
84
|
+
updated_at TEXT NOT NULL
|
|
85
|
+
)
|
|
86
|
+
`);
|
|
87
|
+
client.exec("INSERT INTO comments_new SELECT * FROM comments");
|
|
88
|
+
client.exec("DROP TABLE comments");
|
|
89
|
+
client.exec("ALTER TABLE comments_new RENAME TO comments");
|
|
90
|
+
client.exec("CREATE INDEX IF NOT EXISTS comments_build_id_idx ON comments (build_id)");
|
|
91
|
+
client.exec("PRAGMA foreign_keys = ON");
|
|
92
|
+
}
|
|
93
|
+
/** Add any DDL column missing from a volume created before it shipped. */
|
|
94
|
+
function ensureColumns(client, ddl) {
|
|
95
|
+
for (const [table, columns] of tableColumns(ddl)) {
|
|
96
|
+
const rows = client.query(`PRAGMA table_info(${table})`).all();
|
|
97
|
+
const existing = new Set(rows.map((row) => String(row.name)));
|
|
98
|
+
for (const column of columns) {
|
|
99
|
+
const [name] = column.split(/\s+/u);
|
|
100
|
+
if (name && !existing.has(name)) execIgnore(client, `ALTER TABLE ${table} ADD COLUMN ${column}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function runMigrations(client) {
|
|
105
|
+
client.exec("PRAGMA foreign_keys = ON");
|
|
106
|
+
client.exec(DDL);
|
|
107
|
+
ensureColumns(client, DDL);
|
|
108
|
+
try {
|
|
109
|
+
client.exec(WEBHOOK_SECRET_DROP);
|
|
110
|
+
} catch {}
|
|
111
|
+
try {
|
|
112
|
+
migrateCommentsTable(client);
|
|
113
|
+
} catch {
|
|
114
|
+
execIgnore(client, "PRAGMA foreign_keys = ON");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
export { createBunSqliteDatabase };
|
|
119
|
+
|
|
120
|
+
//# sourceMappingURL=bun-sqlite.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bun-sqlite.mjs","names":[],"sources":["../src/bun-sqlite.ts"],"sourcesContent":["import type { DatabaseAdapter } from \"@storyshelf/core/adapter/database\";\n/**\n * Bun preset for the SQLite database adapter.\n *\n * Same schema, DDL, and Drizzle factory as the `node:sqlite` default;\n * only the driver differs (`bun:sqlite`, synchronous builtin). Bun-only:\n * importing this module outside Bun throws when resolving `bun:sqlite`.\n */\nimport { Database } from \"bun:sqlite\";\nimport { drizzle } from \"drizzle-orm/bun-sqlite\";\nimport { DDL, tableColumns } from \"./ddl.ts\";\nimport { createDrizzleAdapter, withManualTransactions } from \"./drizzle-factory.ts\";\nimport { schema } from \"./schema/index.ts\";\n\ndeclare const __PKG_VERSION__: string | undefined;\n\n/**\n * Options for creating a Bun-backed DatabaseAdapter.\n *\n * When `client` is supplied, `path` is ignored and the caller owns the\n * lifecycle of the client (teardown is a no-op). This is the hook for\n * shared connections and hermetic tests.\n */\nexport interface BunSqliteDatabaseOptions {\n path?: string;\n readonly?: boolean;\n create?: boolean;\n readwrite?: boolean;\n client?: Database;\n}\n\n/**\n * Create a Bun-backed DatabaseAdapter using Drizzle ORM.\n *\n * @param options - Either a filesystem `path` (use `\":memory:\"` for an\n * ephemeral database) or a preconfigured `bun:sqlite` `client`.\n * @returns A DatabaseAdapter backed by the SQLite file (WAL mode).\n */\nexport function createBunSqliteDatabase(options: BunSqliteDatabaseOptions): DatabaseAdapter {\n if (!options.client && options.path === undefined) {\n throw new Error(\"createBunSqliteDatabase: provide `path` or a preconfigured `client`\");\n }\n\n const owned = !options.client;\n const client = options.client ?? new Database(options.path, connectionOptions(options));\n if (owned) {\n client.exec(\"PRAGMA journal_mode = WAL\");\n client.exec(\"PRAGMA busy_timeout = 5000\");\n }\n const db = drizzle(client, { schema });\n\n const adapter = createDrizzleAdapter(db, {\n metadata: {\n name: \"Bun SQLite\",\n version: (globalThis as unknown as { __PKG_VERSION__?: string }).__PKG_VERSION__ ?? \"0.0.0\",\n description: \"Bun SQLite database adapter (bun:sqlite + Drizzle)\",\n kind: \"bun-sqlite\",\n category: \"database\",\n },\n tables: schema,\n migrate: () => {\n runMigrations(client);\n },\n close: () => {\n if (owned) {\n client.close();\n }\n },\n ping: () => {\n client.query(\"SELECT 1\").get();\n },\n });\n\n return withManualTransactions(adapter, (sql) => {\n client.exec(sql);\n });\n}\n\nconst WEBHOOK_SECRET_DROP = \"ALTER TABLE webhooks DROP COLUMN secret\";\n\n/** Bun ignores `undefined` options, but only defined keys are passed. */\nfunction connectionOptions(options: BunSqliteDatabaseOptions): {\n readonly?: boolean;\n create?: boolean;\n readwrite?: boolean;\n} {\n const result: { readonly?: boolean; create?: boolean; readwrite?: boolean } = {};\n if (options.readonly !== undefined) {\n result.readonly = options.readonly;\n }\n if (options.create !== undefined) {\n result.create = options.create;\n }\n if (options.readwrite !== undefined) {\n result.readwrite = options.readwrite;\n }\n return result;\n}\n\nfunction execIgnore(client: Database, sql: string): void {\n try {\n client.exec(sql);\n } catch {\n // idempotent — column already exists or other no-op\n }\n}\n\nfunction migrateCommentsTable(client: Database): void {\n const row = client\n .query(\"SELECT sql FROM sqlite_master WHERE type='table' AND name='comments'\")\n .get() as {\n sql: string;\n } | null;\n if (!row?.sql.includes(\"user_id TEXT NOT NULL REFERENCES users\")) {\n return;\n }\n client.exec(\"PRAGMA foreign_keys = OFF\");\n client.exec(`\n CREATE TABLE comments_new (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n build_id TEXT NOT NULL REFERENCES builds(id) ON DELETE CASCADE,\n snapshot_id TEXT REFERENCES snapshots(id) ON DELETE CASCADE,\n user_id TEXT REFERENCES users(id) ON DELETE CASCADE,\n body TEXT NOT NULL,\n parent_id TEXT,\n resolved INTEGER NOT NULL DEFAULT 0,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n )\n `);\n client.exec(\"INSERT INTO comments_new SELECT * FROM comments\");\n client.exec(\"DROP TABLE comments\");\n client.exec(\"ALTER TABLE comments_new RENAME TO comments\");\n client.exec(\"CREATE INDEX IF NOT EXISTS comments_build_id_idx ON comments (build_id)\");\n client.exec(\"PRAGMA foreign_keys = ON\");\n}\n\n/** Add any DDL column missing from a volume created before it shipped. */\nfunction ensureColumns(client: Database, ddl: string): void {\n for (const [table, columns] of tableColumns(ddl)) {\n const rows = client.query(`PRAGMA table_info(${table})`).all() as { name: unknown }[];\n const existing = new Set(rows.map((row) => String(row.name)));\n for (const column of columns) {\n const [name] = column.split(/\\s+/u);\n if (name && !existing.has(name)) {\n execIgnore(client, `ALTER TABLE ${table} ADD COLUMN ${column}`);\n }\n }\n }\n}\n\nfunction runMigrations(client: Database): void {\n client.exec(\"PRAGMA foreign_keys = ON\");\n client.exec(DDL);\n ensureColumns(client, DDL);\n try {\n client.exec(WEBHOOK_SECRET_DROP);\n } catch {\n // already migrated — ignore\n }\n try {\n migrateCommentsTable(client);\n } catch {\n execIgnore(client, \"PRAGMA foreign_keys = ON\");\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,wBAAwB,SAAoD;CAC1F,IAAI,CAAC,QAAQ,UAAU,QAAQ,SAAS,KAAA,GACtC,MAAM,IAAI,MAAM,qEAAqE;CAGvF,MAAM,QAAQ,CAAC,QAAQ;CACvB,MAAM,SAAS,QAAQ,UAAU,IAAI,SAAS,QAAQ,MAAM,kBAAkB,OAAO,CAAC;CACtF,IAAI,OAAO;EACT,OAAO,KAAK,2BAA2B;EACvC,OAAO,KAAK,4BAA4B;CAC1C;CACA,MAAM,KAAK,QAAQ,QAAQ,EAAE,OAAO,CAAC;CAErC,MAAM,UAAU,qBAAqB,IAAI;EACvC,UAAU;GACR,MAAM;GACN,SAAU,WAAuD,mBAAmB;GACpF,aAAa;GACb,MAAM;GACN,UAAU;EACZ;EACA,QAAQ;EACR,eAAe;GACb,cAAc,MAAM;EACtB;EACA,aAAa;GACX,IAAI,OACF,OAAO,MAAM;EAEjB;EACA,YAAY;GACV,OAAO,MAAM,UAAU,CAAC,CAAC,IAAI;EAC/B;CACF,CAAC;CAED,OAAO,uBAAuB,UAAU,QAAQ;EAC9C,OAAO,KAAK,GAAG;CACjB,CAAC;AACH;AAEA,MAAM,sBAAsB;;AAG5B,SAAS,kBAAkB,SAIzB;CACA,MAAM,SAAwE,CAAC;CAC/E,IAAI,QAAQ,aAAa,KAAA,GACvB,OAAO,WAAW,QAAQ;CAE5B,IAAI,QAAQ,WAAW,KAAA,GACrB,OAAO,SAAS,QAAQ;CAE1B,IAAI,QAAQ,cAAc,KAAA,GACxB,OAAO,YAAY,QAAQ;CAE7B,OAAO;AACT;AAEA,SAAS,WAAW,QAAkB,KAAmB;CACvD,IAAI;EACF,OAAO,KAAK,GAAG;CACjB,QAAQ,CAER;AACF;AAEA,SAAS,qBAAqB,QAAwB;CAMpD,IAAI,CALQ,OACT,MAAM,sEAAsE,CAAC,CAC7E,IAGI,CAAC,EAAE,IAAI,SAAS,wCAAwC,GAC7D;CAEF,OAAO,KAAK,2BAA2B;CACvC,OAAO,KAAK;;;;;;;;;;;;;GAaX;CACD,OAAO,KAAK,iDAAiD;CAC7D,OAAO,KAAK,qBAAqB;CACjC,OAAO,KAAK,6CAA6C;CACzD,OAAO,KAAK,yEAAyE;CACrF,OAAO,KAAK,0BAA0B;AACxC;;AAGA,SAAS,cAAc,QAAkB,KAAmB;CAC1D,KAAK,MAAM,CAAC,OAAO,YAAY,aAAa,GAAG,GAAG;EAChD,MAAM,OAAO,OAAO,MAAM,qBAAqB,MAAM,EAAE,CAAC,CAAC,IAAI;EAC7D,MAAM,WAAW,IAAI,IAAI,KAAK,KAAK,QAAQ,OAAO,IAAI,IAAI,CAAC,CAAC;EAC5D,KAAK,MAAM,UAAU,SAAS;GAC5B,MAAM,CAAC,QAAQ,OAAO,MAAM,MAAM;GAClC,IAAI,QAAQ,CAAC,SAAS,IAAI,IAAI,GAC5B,WAAW,QAAQ,eAAe,MAAM,cAAc,QAAQ;EAElE;CACF;AACF;AAEA,SAAS,cAAc,QAAwB;CAC7C,OAAO,KAAK,0BAA0B;CACtC,OAAO,KAAK,GAAG;CACf,cAAc,QAAQ,GAAG;CACzB,IAAI;EACF,OAAO,KAAK,mBAAmB;CACjC,QAAQ,CAER;CACA,IAAI;EACF,qBAAqB,MAAM;CAC7B,QAAQ;EACN,WAAW,QAAQ,0BAA0B;CAC/C;AACF"}
|
package/dist/d1.d.mts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import __tsdown_shims_path from 'node:path';
|
|
2
|
+
import __tsdown_shims_url from 'node:url';
|
|
3
|
+
import { DatabaseAdapter } from "@storyshelf/core/adapter/database";
|
|
4
|
+
//#region src/d1.d.ts
|
|
5
|
+
/** Minimal prepared-statement surface of a D1 database binding. */
|
|
6
|
+
interface D1PreparedStatement {
|
|
7
|
+
bind(...params: unknown[]): D1PreparedStatement;
|
|
8
|
+
all(): Promise<{
|
|
9
|
+
results: unknown[];
|
|
10
|
+
}>;
|
|
11
|
+
run(): Promise<unknown>;
|
|
12
|
+
first(): Promise<unknown>;
|
|
13
|
+
raw(): Promise<unknown[]>;
|
|
14
|
+
}
|
|
15
|
+
/** Minimal surface of a Cloudflare D1 database binding. */
|
|
16
|
+
interface D1Database {
|
|
17
|
+
prepare(query: string): D1PreparedStatement;
|
|
18
|
+
batch(statements: D1PreparedStatement[]): Promise<unknown[]>;
|
|
19
|
+
exec(query: string): Promise<unknown>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Options for creating a D1-backed DatabaseAdapter. Client-only: the
|
|
23
|
+
* binding comes from the Workers runtime (`env.DB`), so the caller always
|
|
24
|
+
* owns its lifecycle (teardown is a no-op).
|
|
25
|
+
*/
|
|
26
|
+
interface D1DatabaseOptions {
|
|
27
|
+
client: D1Database;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Create a Cloudflare-D1-backed DatabaseAdapter using Drizzle ORM.
|
|
31
|
+
*
|
|
32
|
+
* @param options - The D1 database binding (`env.DB`).
|
|
33
|
+
* @returns A DatabaseAdapter backed by the D1 database.
|
|
34
|
+
*/
|
|
35
|
+
declare function createD1Database(options: D1DatabaseOptions): DatabaseAdapter;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { D1Database, D1DatabaseOptions, D1PreparedStatement, createD1Database };
|
|
38
|
+
//# sourceMappingURL=d1.d.mts.map
|
package/dist/d1.mjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import "node:path";
|
|
2
|
+
import "node:url";
|
|
3
|
+
import.meta.url;
|
|
4
|
+
import { DDL, tableColumns } from "./ddl.mjs";
|
|
5
|
+
import { createDrizzleAdapter } from "./drizzle-factory.mjs";
|
|
6
|
+
import { schema } from "./schema.mjs";
|
|
7
|
+
import { drizzle } from "drizzle-orm/d1";
|
|
8
|
+
//#region src/d1.ts
|
|
9
|
+
/**
|
|
10
|
+
* Create a Cloudflare-D1-backed DatabaseAdapter using Drizzle ORM.
|
|
11
|
+
*
|
|
12
|
+
* @param options - The D1 database binding (`env.DB`).
|
|
13
|
+
* @returns A DatabaseAdapter backed by the D1 database.
|
|
14
|
+
*/
|
|
15
|
+
function createD1Database(options) {
|
|
16
|
+
if (!options.client) throw new Error("createD1Database: provide a D1 `client` binding (env.DB)");
|
|
17
|
+
const { client } = options;
|
|
18
|
+
const db = drizzle(client, { schema });
|
|
19
|
+
return createDrizzleAdapter(db, {
|
|
20
|
+
metadata: {
|
|
21
|
+
name: "Cloudflare D1",
|
|
22
|
+
version: globalThis.__PKG_VERSION__ ?? "0.0.0",
|
|
23
|
+
description: "Cloudflare D1 database adapter (D1 binding + Drizzle)",
|
|
24
|
+
kind: "d1",
|
|
25
|
+
category: "database"
|
|
26
|
+
},
|
|
27
|
+
tables: schema,
|
|
28
|
+
migrate: async () => {
|
|
29
|
+
await runMigrations(client);
|
|
30
|
+
},
|
|
31
|
+
close: () => {},
|
|
32
|
+
ping: async () => {
|
|
33
|
+
await client.prepare("SELECT 1").bind().first();
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const WEBHOOK_SECRET_DROP = "ALTER TABLE webhooks DROP COLUMN secret";
|
|
38
|
+
async function runMigrations(client) {
|
|
39
|
+
for (const statement of splitStatements(DDL)) await execIgnore(client, statement);
|
|
40
|
+
await ensureColumns(client, DDL);
|
|
41
|
+
await execIgnore(client, WEBHOOK_SECRET_DROP);
|
|
42
|
+
await migrateCommentsTable(client);
|
|
43
|
+
}
|
|
44
|
+
/** Split a multi-statement DDL script into single statements for `exec`. */
|
|
45
|
+
function splitStatements(ddl) {
|
|
46
|
+
return ddl.split(/;\n/u).map((statement) => statement.trim()).filter((statement) => statement !== "");
|
|
47
|
+
}
|
|
48
|
+
async function execIgnore(client, sql) {
|
|
49
|
+
try {
|
|
50
|
+
await client.exec(sql);
|
|
51
|
+
} catch {}
|
|
52
|
+
}
|
|
53
|
+
/** Add any DDL column missing from a database created before it shipped. */
|
|
54
|
+
async function ensureColumns(client, ddl) {
|
|
55
|
+
for (const [table, columns] of tableColumns(ddl)) {
|
|
56
|
+
const existing = await inspectColumns(client, table);
|
|
57
|
+
for (const column of columns) {
|
|
58
|
+
const [name] = column.split(/\s+/u);
|
|
59
|
+
if (name && (existing === void 0 || !existing.has(name))) await execIgnore(client, `ALTER TABLE ${table} ADD COLUMN ${column}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Read a table's columns via `PRAGMA table_info`. Returns `undefined` when
|
|
65
|
+
* the binding cannot serve PRAGMA (callers then add columns blindly).
|
|
66
|
+
*/
|
|
67
|
+
async function inspectColumns(client, table) {
|
|
68
|
+
try {
|
|
69
|
+
const rows = (await client.prepare(`PRAGMA table_info(${table})`).bind().all()).results;
|
|
70
|
+
return new Set(rows.map((row) => String(row.name)));
|
|
71
|
+
} catch {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async function migrateCommentsTable(client) {
|
|
76
|
+
try {
|
|
77
|
+
if (!(await client.prepare("SELECT sql FROM sqlite_master WHERE type='table' AND name='comments'").bind().first())?.sql.includes("user_id TEXT NOT NULL REFERENCES users")) return;
|
|
78
|
+
await recreateCommentsTable(client);
|
|
79
|
+
} catch {
|
|
80
|
+
await execIgnore(client, "PRAGMA foreign_keys = ON");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async function recreateCommentsTable(client) {
|
|
84
|
+
await client.exec("PRAGMA foreign_keys = OFF");
|
|
85
|
+
await client.exec(`
|
|
86
|
+
CREATE TABLE comments_new (
|
|
87
|
+
id TEXT PRIMARY KEY,
|
|
88
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
89
|
+
build_id TEXT NOT NULL REFERENCES builds(id) ON DELETE CASCADE,
|
|
90
|
+
snapshot_id TEXT REFERENCES snapshots(id) ON DELETE CASCADE,
|
|
91
|
+
user_id TEXT REFERENCES users(id) ON DELETE CASCADE,
|
|
92
|
+
body TEXT NOT NULL,
|
|
93
|
+
parent_id TEXT,
|
|
94
|
+
resolved INTEGER NOT NULL DEFAULT 0,
|
|
95
|
+
created_at TEXT NOT NULL,
|
|
96
|
+
updated_at TEXT NOT NULL
|
|
97
|
+
)
|
|
98
|
+
`);
|
|
99
|
+
await client.exec("INSERT INTO comments_new SELECT * FROM comments");
|
|
100
|
+
await client.exec("DROP TABLE comments");
|
|
101
|
+
await client.exec("ALTER TABLE comments_new RENAME TO comments");
|
|
102
|
+
await client.exec("CREATE INDEX IF NOT EXISTS comments_build_id_idx ON comments (build_id)");
|
|
103
|
+
await client.exec("PRAGMA foreign_keys = ON");
|
|
104
|
+
}
|
|
105
|
+
//#endregion
|
|
106
|
+
export { createD1Database };
|
|
107
|
+
|
|
108
|
+
//# sourceMappingURL=d1.mjs.map
|
package/dist/d1.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"d1.mjs","names":[],"sources":["../src/d1.ts"],"sourcesContent":["/**\n * Cloudflare D1 preset for the SQLite database adapter.\n *\n * Same schema, DDL, and Drizzle factory as the `node:sqlite` default;\n * only the driver differs (D1 HTTP binding, async). No peer dependency:\n * the `D1Database` binding is provided by the Workers runtime and its\n * minimal surface is declared locally (no `@cloudflare/workers-types`\n * needed).\n */\nimport type { DatabaseAdapter } from \"@storyshelf/core/adapter/database\";\nimport { drizzle } from \"drizzle-orm/d1\";\nimport { DDL, tableColumns } from \"./ddl.ts\";\nimport { createDrizzleAdapter } from \"./drizzle-factory.ts\";\nimport { schema } from \"./schema/index.ts\";\n\ndeclare const __PKG_VERSION__: string | undefined;\n\n/** Minimal prepared-statement surface of a D1 database binding. */\nexport interface D1PreparedStatement {\n bind(...params: unknown[]): D1PreparedStatement;\n all(): Promise<{ results: unknown[] }>;\n run(): Promise<unknown>;\n first(): Promise<unknown>;\n raw(): Promise<unknown[]>;\n}\n\n/** Minimal surface of a Cloudflare D1 database binding. */\nexport interface D1Database {\n prepare(query: string): D1PreparedStatement;\n batch(statements: D1PreparedStatement[]): Promise<unknown[]>;\n exec(query: string): Promise<unknown>;\n}\n\n/**\n * Options for creating a D1-backed DatabaseAdapter. Client-only: the\n * binding comes from the Workers runtime (`env.DB`), so the caller always\n * owns its lifecycle (teardown is a no-op).\n */\nexport interface D1DatabaseOptions {\n client: D1Database;\n}\n\n/**\n * Create a Cloudflare-D1-backed DatabaseAdapter using Drizzle ORM.\n *\n * @param options - The D1 database binding (`env.DB`).\n * @returns A DatabaseAdapter backed by the D1 database.\n */\nexport function createD1Database(options: D1DatabaseOptions): DatabaseAdapter {\n if (!options.client) {\n throw new Error(\"createD1Database: provide a D1 `client` binding (env.DB)\");\n }\n const { client } = options;\n // oxlint-disable-next-line typescript/no-unsafe-argument -- structural D1 binding vs platform type\n const db = drizzle(client as never, { schema });\n\n return createDrizzleAdapter(db, {\n metadata: {\n name: \"Cloudflare D1\",\n version: (globalThis as unknown as { __PKG_VERSION__?: string }).__PKG_VERSION__ ?? \"0.0.0\",\n description: \"Cloudflare D1 database adapter (D1 binding + Drizzle)\",\n kind: \"d1\",\n category: \"database\",\n },\n tables: schema,\n migrate: async () => {\n await runMigrations(client);\n },\n close: () => {\n // no-op: the Workers runtime owns the binding\n },\n ping: async () => {\n await client.prepare(\"SELECT 1\").bind().first();\n },\n });\n}\n\nconst WEBHOOK_SECRET_DROP = \"ALTER TABLE webhooks DROP COLUMN secret\";\n\nasync function runMigrations(client: D1Database): Promise<void> {\n // Sequential: DDL order matters (tables before indexes) and D1 has no\n // multi-statement exec.\n for (const statement of splitStatements(DDL)) {\n // oxlint-disable-next-line no-await-in-loop -- ordered DDL, see above\n await execIgnore(client, statement);\n }\n await ensureColumns(client, DDL);\n await execIgnore(client, WEBHOOK_SECRET_DROP);\n await migrateCommentsTable(client);\n}\n\n/** Split a multi-statement DDL script into single statements for `exec`. */\nfunction splitStatements(ddl: string): string[] {\n return ddl\n .split(/;\\n/u)\n .map((statement) => statement.trim())\n .filter((statement) => statement !== \"\");\n}\n\nasync function execIgnore(client: D1Database, sql: string): Promise<void> {\n try {\n await client.exec(sql);\n } catch {\n // idempotent — already migrated\n }\n}\n\n/** Add any DDL column missing from a database created before it shipped. */\nasync function ensureColumns(client: D1Database, ddl: string): Promise<void> {\n // Sequential: one PRAGMA round-trip per table keeps the per-table round-trips ordered.\n for (const [table, columns] of tableColumns(ddl)) {\n // oxlint-disable-next-line no-await-in-loop -- ordered per-table inspection\n const existing = await inspectColumns(client, table);\n for (const column of columns) {\n const [name] = column.split(/\\s+/u);\n if (name && (existing === undefined || !existing.has(name))) {\n // oxlint-disable-next-line no-await-in-loop -- ordered column backfill\n await execIgnore(client, `ALTER TABLE ${table} ADD COLUMN ${column}`);\n }\n }\n }\n}\n\n/**\n * Read a table's columns via `PRAGMA table_info`. Returns `undefined` when\n * the binding cannot serve PRAGMA (callers then add columns blindly).\n */\nasync function inspectColumns(client: D1Database, table: string): Promise<Set<string> | undefined> {\n try {\n const result = await client.prepare(`PRAGMA table_info(${table})`).bind().all();\n const rows = result.results as { name: unknown }[];\n return new Set(rows.map((row) => String(row.name)));\n } catch {\n return undefined;\n }\n}\n\nasync function migrateCommentsTable(client: D1Database): Promise<void> {\n try {\n const row = (await client\n .prepare(\"SELECT sql FROM sqlite_master WHERE type='table' AND name='comments'\")\n .bind()\n .first()) as { sql: string } | null;\n if (!row?.sql.includes(\"user_id TEXT NOT NULL REFERENCES users\")) {\n return;\n }\n await recreateCommentsTable(client);\n } catch {\n await execIgnore(client, \"PRAGMA foreign_keys = ON\");\n }\n}\n\nasync function recreateCommentsTable(client: D1Database): Promise<void> {\n await client.exec(\"PRAGMA foreign_keys = OFF\");\n await client.exec(`\n CREATE TABLE comments_new (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n build_id TEXT NOT NULL REFERENCES builds(id) ON DELETE CASCADE,\n snapshot_id TEXT REFERENCES snapshots(id) ON DELETE CASCADE,\n user_id TEXT REFERENCES users(id) ON DELETE CASCADE,\n body TEXT NOT NULL,\n parent_id TEXT,\n resolved INTEGER NOT NULL DEFAULT 0,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n )\n `);\n await client.exec(\"INSERT INTO comments_new SELECT * FROM comments\");\n await client.exec(\"DROP TABLE comments\");\n await client.exec(\"ALTER TABLE comments_new RENAME TO comments\");\n await client.exec(\"CREATE INDEX IF NOT EXISTS comments_build_id_idx ON comments (build_id)\");\n await client.exec(\"PRAGMA foreign_keys = ON\");\n}\n"],"mappings":";;;;;;;;;;;;;;AAgDA,SAAgB,iBAAiB,SAA6C;CAC5E,IAAI,CAAC,QAAQ,QACX,MAAM,IAAI,MAAM,0DAA0D;CAE5E,MAAM,EAAE,WAAW;CAEnB,MAAM,KAAK,QAAQ,QAAiB,EAAE,OAAO,CAAC;CAE9C,OAAO,qBAAqB,IAAI;EAC9B,UAAU;GACR,MAAM;GACN,SAAU,WAAuD,mBAAmB;GACpF,aAAa;GACb,MAAM;GACN,UAAU;EACZ;EACA,QAAQ;EACR,SAAS,YAAY;GACnB,MAAM,cAAc,MAAM;EAC5B;EACA,aAAa,CAEb;EACA,MAAM,YAAY;GAChB,MAAM,OAAO,QAAQ,UAAU,CAAC,CAAC,KAAK,CAAC,CAAC,MAAM;EAChD;CACF,CAAC;AACH;AAEA,MAAM,sBAAsB;AAE5B,eAAe,cAAc,QAAmC;CAG9D,KAAK,MAAM,aAAa,gBAAgB,GAAG,GAEzC,MAAM,WAAW,QAAQ,SAAS;CAEpC,MAAM,cAAc,QAAQ,GAAG;CAC/B,MAAM,WAAW,QAAQ,mBAAmB;CAC5C,MAAM,qBAAqB,MAAM;AACnC;;AAGA,SAAS,gBAAgB,KAAuB;CAC9C,OAAO,IACJ,MAAM,MAAM,CAAC,CACb,KAAK,cAAc,UAAU,KAAK,CAAC,CAAC,CACpC,QAAQ,cAAc,cAAc,EAAE;AAC3C;AAEA,eAAe,WAAW,QAAoB,KAA4B;CACxE,IAAI;EACF,MAAM,OAAO,KAAK,GAAG;CACvB,QAAQ,CAER;AACF;;AAGA,eAAe,cAAc,QAAoB,KAA4B;CAE3E,KAAK,MAAM,CAAC,OAAO,YAAY,aAAa,GAAG,GAAG;EAEhD,MAAM,WAAW,MAAM,eAAe,QAAQ,KAAK;EACnD,KAAK,MAAM,UAAU,SAAS;GAC5B,MAAM,CAAC,QAAQ,OAAO,MAAM,MAAM;GAClC,IAAI,SAAS,aAAa,KAAA,KAAa,CAAC,SAAS,IAAI,IAAI,IAEvD,MAAM,WAAW,QAAQ,eAAe,MAAM,cAAc,QAAQ;EAExE;CACF;AACF;;;;;AAMA,eAAe,eAAe,QAAoB,OAAiD;CACjG,IAAI;EAEF,MAAM,QAAO,MADQ,OAAO,QAAQ,qBAAqB,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,EAAA,CAC1D;EACpB,OAAO,IAAI,IAAI,KAAK,KAAK,QAAQ,OAAO,IAAI,IAAI,CAAC,CAAC;CACpD,QAAQ;EACN;CACF;AACF;AAEA,eAAe,qBAAqB,QAAmC;CACrE,IAAI;EAKF,IAAI,EAAC,MAJc,OAChB,QAAQ,sEAAsE,CAAC,CAC/E,KAAK,CAAC,CACN,MAAM,EAAA,EACC,IAAI,SAAS,wCAAwC,GAC7D;EAEF,MAAM,sBAAsB,MAAM;CACpC,QAAQ;EACN,MAAM,WAAW,QAAQ,0BAA0B;CACrD;AACF;AAEA,eAAe,sBAAsB,QAAmC;CACtE,MAAM,OAAO,KAAK,2BAA2B;CAC7C,MAAM,OAAO,KAAK;;;;;;;;;;;;;GAajB;CACD,MAAM,OAAO,KAAK,iDAAiD;CACnE,MAAM,OAAO,KAAK,qBAAqB;CACvC,MAAM,OAAO,KAAK,6CAA6C;CAC/D,MAAM,OAAO,KAAK,yEAAyE;CAC3F,MAAM,OAAO,KAAK,0BAA0B;AAC9C"}
|
package/dist/ddl.d.mts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import __tsdown_shims_path from 'node:path';
|
|
2
|
+
import __tsdown_shims_url from 'node:url';
|
|
3
|
+
//#region src/ddl.d.ts
|
|
4
|
+
/** Canonical SQLite DDL for all StoryShelf tables and indexes. */
|
|
5
|
+
declare const DDL = "\nCREATE TABLE IF NOT EXISTS projects (\n id TEXT PRIMARY KEY,\n name TEXT NOT NULL,\n slug TEXT NOT NULL UNIQUE,\n git_repository TEXT,\n git_default_branch TEXT NOT NULL DEFAULT 'main',\n pixel_threshold REAL NOT NULL DEFAULT 0.1,\n max_diff_ratio REAL NOT NULL DEFAULT 0.01,\n public_branch_regex TEXT,\n storybook_meta TEXT,\n execute_play INTEGER NOT NULL DEFAULT 0,\n play_timeout_ms INTEGER NOT NULL DEFAULT 10000,\n run_a11y INTEGER NOT NULL DEFAULT 0,\n browser TEXT NOT NULL DEFAULT 'chromium',\n viewports TEXT,\n automigrate INTEGER NOT NULL DEFAULT 0,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE TABLE IF NOT EXISTS project_status_configs (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n provider TEXT NOT NULL,\n config TEXT NOT NULL,\n token_encrypted TEXT NOT NULL,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE TABLE IF NOT EXISTS builds (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n git_sha TEXT NOT NULL,\n git_branch TEXT NOT NULL,\n is_default INTEGER NOT NULL DEFAULT 0,\n author_email TEXT,\n author_name TEXT,\n message TEXT,\n public INTEGER NOT NULL DEFAULT 0,\n status TEXT NOT NULL DEFAULT 'pending',\n snapshot_count INTEGER NOT NULL DEFAULT 0,\n changed_count INTEGER NOT NULL DEFAULT 0,\n approved_count INTEGER NOT NULL DEFAULT 0,\n rejected_count INTEGER NOT NULL DEFAULT 0,\n affected_only INTEGER NOT NULL DEFAULT 1,\n baseline_sha TEXT,\n changed_files TEXT,\n affected_import_paths TEXT,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE INDEX IF NOT EXISTS builds_project_gitsha_idx ON builds (project_id, git_sha);\nCREATE INDEX IF NOT EXISTS builds_git_branch_idx ON builds (git_branch);\nCREATE TABLE IF NOT EXISTS capture_attempts (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n build_id TEXT NOT NULL REFERENCES builds(id) ON DELETE CASCADE,\n attempt_no INTEGER NOT NULL,\n status TEXT NOT NULL DEFAULT 'queued',\n error TEXT,\n req_id TEXT,\n story_count INTEGER NOT NULL DEFAULT 0,\n failed_count INTEGER NOT NULL DEFAULT 0,\n queued_at TEXT NOT NULL,\n started_at TEXT,\n finished_at TEXT,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE UNIQUE INDEX IF NOT EXISTS capture_attempts_build_no_idx ON capture_attempts (build_id, attempt_no);\nCREATE INDEX IF NOT EXISTS capture_attempts_build_id_idx ON capture_attempts (build_id);\nCREATE TABLE IF NOT EXISTS capture_logs (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n build_id TEXT NOT NULL REFERENCES builds(id) ON DELETE CASCADE,\n attempt_id TEXT NOT NULL REFERENCES capture_attempts(id) ON DELETE CASCADE,\n seq INTEGER NOT NULL,\n level TEXT NOT NULL DEFAULT 'info',\n message TEXT NOT NULL,\n fields TEXT,\n created_at TEXT NOT NULL\n);\nCREATE INDEX IF NOT EXISTS capture_logs_attempt_seq_idx ON capture_logs (attempt_id, seq);\nCREATE TABLE IF NOT EXISTS snapshots (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n build_id TEXT NOT NULL REFERENCES builds(id) ON DELETE CASCADE,\n story_id TEXT NOT NULL,\n story_name TEXT NOT NULL,\n story_title TEXT NOT NULL,\n story_import_path TEXT,\n viewport_name TEXT NOT NULL DEFAULT 'desktop',\n viewport_width INTEGER NOT NULL DEFAULT 1280,\n viewport_height INTEGER NOT NULL DEFAULT 720,\n screenshot_path TEXT NOT NULL,\n diff_path TEXT,\n diff_pixels INTEGER,\n diff_ratio REAL,\n diff_passed INTEGER,\n status TEXT NOT NULL DEFAULT 'pending',\n reviewed_by TEXT,\n reviewed_at TEXT,\n infra_hash TEXT,\n inherited INTEGER NOT NULL DEFAULT 0,\n baseline_id TEXT,\n baseline_version TEXT,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE UNIQUE INDEX IF NOT EXISTS snapshots_build_story_viewport_idx ON snapshots (build_id, story_id, viewport_name);\nCREATE INDEX IF NOT EXISTS snapshots_build_id_idx ON snapshots (build_id);\nCREATE TABLE IF NOT EXISTS baselines (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n story_id TEXT NOT NULL,\n viewport_name TEXT NOT NULL DEFAULT 'desktop',\n branch TEXT NOT NULL,\n snapshot_id TEXT,\n screenshot_path TEXT NOT NULL,\n infra_hash TEXT,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE UNIQUE INDEX IF NOT EXISTS baselines_project_story_viewport_branch_idx ON baselines (project_id, story_id, viewport_name, branch);\nCREATE INDEX IF NOT EXISTS baselines_project_story_idx ON baselines (project_id, story_id);\nCREATE TABLE IF NOT EXISTS comments (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n build_id TEXT NOT NULL REFERENCES builds(id) ON DELETE CASCADE,\n snapshot_id TEXT REFERENCES snapshots(id) ON DELETE CASCADE,\n user_id TEXT REFERENCES users(id) ON DELETE CASCADE,\n body TEXT NOT NULL,\n parent_id TEXT,\n resolved INTEGER NOT NULL DEFAULT 0,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE INDEX IF NOT EXISTS comments_build_id_idx ON comments (build_id);\nCREATE TABLE IF NOT EXISTS label_types (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n key TEXT NOT NULL,\n name TEXT NOT NULL,\n link_template TEXT,\n color TEXT,\n created_at TEXT NOT NULL\n);\nCREATE UNIQUE INDEX IF NOT EXISTS label_types_project_key_idx ON label_types (project_id, key);\nCREATE TABLE IF NOT EXISTS build_labels (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n build_id TEXT NOT NULL REFERENCES builds(id) ON DELETE CASCADE,\n type_key TEXT NOT NULL,\n value TEXT NOT NULL,\n created_at TEXT NOT NULL\n);\nCREATE UNIQUE INDEX IF NOT EXISTS build_labels_build_type_value_idx ON build_labels (build_id, type_key, value);\nCREATE TABLE IF NOT EXISTS tokens (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n name TEXT NOT NULL,\n hash TEXT NOT NULL UNIQUE,\n user_id TEXT REFERENCES users(id) ON DELETE CASCADE,\n last_used_at TEXT,\n created_at TEXT NOT NULL\n);\nCREATE TABLE IF NOT EXISTS webhooks (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n url TEXT NOT NULL,\n secret_encrypted TEXT NOT NULL,\n events TEXT,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE TABLE IF NOT EXISTS notification_channels (\n id TEXT PRIMARY KEY,\n project_id TEXT REFERENCES projects(id) ON DELETE CASCADE,\n provider TEXT NOT NULL,\n config TEXT NOT NULL,\n secret_encrypted TEXT,\n events TEXT,\n enabled INTEGER NOT NULL DEFAULT 1,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE INDEX IF NOT EXISTS notification_channels_project_id_idx ON notification_channels (project_id);\nCREATE TABLE IF NOT EXISTS notification_subscriptions (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,\n events TEXT,\n via TEXT,\n enabled INTEGER NOT NULL DEFAULT 1,\n created_at TEXT NOT NULL\n);\nCREATE UNIQUE INDEX IF NOT EXISTS notification_subscriptions_project_user_idx ON notification_subscriptions (project_id, user_id);\nCREATE TABLE IF NOT EXISTS users (\n id TEXT PRIMARY KEY,\n email TEXT NOT NULL UNIQUE,\n name TEXT NOT NULL,\n avatar_url TEXT,\n role TEXT NOT NULL DEFAULT 'member',\n last_login_at TEXT,\n created_at TEXT NOT NULL,\n password_hash TEXT,\n display_name_override TEXT,\n auth_provider TEXT NOT NULL DEFAULT 'oidc',\n disabled INTEGER NOT NULL DEFAULT 0\n);\nCREATE TABLE IF NOT EXISTS user_invite_tokens (\n id TEXT PRIMARY KEY,\n user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,\n token_hash TEXT NOT NULL UNIQUE,\n expires_at TEXT NOT NULL,\n used_at TEXT,\n created_at TEXT NOT NULL\n);\nCREATE INDEX IF NOT EXISTS user_invite_tokens_user_id_idx ON user_invite_tokens (user_id);\nCREATE TABLE IF NOT EXISTS project_members (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,\n role TEXT NOT NULL DEFAULT 'viewer',\n source TEXT NOT NULL DEFAULT 'manual',\n created_at TEXT NOT NULL\n);\nCREATE UNIQUE INDEX IF NOT EXISTS project_members_project_user_idx ON project_members (project_id, user_id);\nCREATE TABLE IF NOT EXISTS project_group_mappings (\n id TEXT PRIMARY KEY,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n group_name TEXT NOT NULL,\n role TEXT NOT NULL DEFAULT 'viewer',\n created_at TEXT NOT NULL\n);\nCREATE UNIQUE INDEX IF NOT EXISTS project_group_mappings_project_group_idx ON project_group_mappings (project_id, group_name);\nCREATE TABLE IF NOT EXISTS content_refs (\n hash TEXT PRIMARY KEY,\n ref_count INTEGER NOT NULL DEFAULT 1,\n last_seen_at TEXT NOT NULL,\n created_at TEXT NOT NULL\n);\nCREATE TABLE IF NOT EXISTS user (\n id TEXT PRIMARY KEY,\n name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n email_verified INTEGER NOT NULL DEFAULT 0,\n image TEXT,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE TABLE IF NOT EXISTS session (\n id TEXT PRIMARY KEY,\n user_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE,\n token TEXT NOT NULL UNIQUE,\n expires_at TEXT NOT NULL,\n ip_address TEXT,\n user_agent TEXT,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE INDEX IF NOT EXISTS session_token_idx ON session (token);\nCREATE INDEX IF NOT EXISTS session_user_id_idx ON session (user_id);\nCREATE TABLE IF NOT EXISTS account (\n id TEXT PRIMARY KEY,\n user_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE,\n account_id TEXT NOT NULL,\n provider_id TEXT NOT NULL,\n access_token TEXT,\n refresh_token TEXT,\n id_token TEXT,\n access_token_expires_at TEXT,\n refresh_token_expires_at TEXT,\n scope TEXT,\n expires_at TEXT,\n password TEXT,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE INDEX IF NOT EXISTS account_user_id_idx ON account (user_id);\nCREATE TABLE IF NOT EXISTS verification (\n id TEXT PRIMARY KEY,\n identifier TEXT NOT NULL,\n value TEXT NOT NULL,\n expires_at TEXT NOT NULL,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\nCREATE INDEX IF NOT EXISTS verification_identifier_idx ON verification (identifier);\nCREATE TABLE IF NOT EXISTS passkey (\n id TEXT PRIMARY KEY,\n name TEXT,\n public_key TEXT NOT NULL,\n user_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE,\n credential_id TEXT NOT NULL,\n counter INTEGER NOT NULL,\n device_type TEXT NOT NULL,\n backed_up INTEGER NOT NULL DEFAULT 0,\n transports TEXT,\n aaguid TEXT,\n created_at TEXT NOT NULL,\n updated_at TEXT\n);\nCREATE INDEX IF NOT EXISTS passkey_user_id_idx ON passkey (user_id);\nCREATE INDEX IF NOT EXISTS passkey_credential_id_idx ON passkey (credential_id);\nCREATE TABLE IF NOT EXISTS ssoProvider (\n id TEXT PRIMARY KEY,\n issuer TEXT NOT NULL,\n oidc_config TEXT,\n saml_config TEXT,\n user_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE,\n provider_id TEXT NOT NULL UNIQUE,\n organization_id TEXT,\n domain TEXT NOT NULL\n);\nCREATE INDEX IF NOT EXISTS ssoprovider_domain_idx ON ssoProvider (domain);\n";
|
|
6
|
+
/**
|
|
7
|
+
* Parse the `CREATE TABLE` statements in a DDL string into a map of table name
|
|
8
|
+
* → column definition fragments (e.g. `name TEXT NOT NULL`). Table-level
|
|
9
|
+
* constraints are skipped; every StoryShelf column is declared inline.
|
|
10
|
+
*/
|
|
11
|
+
declare function tableColumns(ddl: string): Map<string, string[]>;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { DDL, tableColumns };
|
|
14
|
+
//# sourceMappingURL=ddl.d.mts.map
|