durable-db 0.2.0 → 0.2.2
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/dist/{arktypeConfig-zgI5p6zF.d.ts → arktypeConfig-AXvobTPL.d.ts} +0 -1
- package/dist/{arktypeConfig-CTCJm4vA.js → arktypeConfig-DxP0OcV_.js} +1014 -1033
- package/dist/cli/bin.d.ts +1 -1
- package/dist/cli/bin.js +2 -10
- package/dist/cli/index.d.ts +27 -29
- package/dist/cli/index.js +4 -9
- package/dist/{cli-DnsOTcrH.js → cli-BUSZTS2D.js} +196 -19
- package/dist/context/index.d.ts +50 -2
- package/dist/context/index.js +119 -5
- package/dist/db/index.d.ts +4 -4
- package/dist/db/index.js +10 -12
- package/dist/{debug-DmBrUmwe.js → debug-Cz8KzMft.js} +23 -27
- package/dist/{defineDatabase-BD3k1L7d.js → defineDatabase-CHmzpzFf.js} +2 -4
- package/dist/{defineDatabase-DtkM6k1S.d.ts → defineDatabase-IUthY3Ex.d.ts} +5 -5
- package/dist/{discovery-DfimBbS_.d.ts → discovery-gHeLq8jk.d.ts} +1 -1
- package/dist/{index-BHLOQ7uV.d.ts → index-BdrcuAU7.d.ts} +4 -3
- package/dist/index.d.ts +4 -4
- package/dist/index.js +5 -8
- package/dist/{kysely-BH7QZBZw.js → kysely-Be6KwFZC.js} +45 -31
- package/dist/migrations/generator.d.ts +14 -17
- package/dist/migrations/generator.js +202 -5
- package/dist/migrations/snapshot.d.ts +1 -1
- package/dist/migrations/snapshot.js +91 -3
- package/dist/{parser-BdV7Zqcv.js → parser-DR57GOjv.js} +21 -19
- package/dist/registry.d.ts +10 -11
- package/dist/registry.js +3 -5
- package/dist/schema.d.ts +5 -2
- package/dist/schema.js +6 -3
- package/dist/snapshot-CaG5xnE2.d.ts +48 -0
- package/dist/{state-DtZ70cBR.js → state-OVuoHRC9.js} +3 -5
- package/dist/testing/index.d.ts +4 -5
- package/dist/testing/index.js +6 -10
- package/dist/transport/protocol.d.ts +7 -8
- package/dist/transport/protocol.js +34 -3
- package/dist/transport/websocket.d.ts +4 -5
- package/dist/transport/websocket.js +103 -3
- package/dist/vite/durableDb.d.ts +6 -7
- package/dist/vite/durableDb.js +23 -28
- package/package.json +12 -12
- package/dist/context-kIVZbhye.js +0 -121
- package/dist/generator-KHBZaOI1.js +0 -202
- package/dist/index-zzQS_psa.d.ts +0 -51
- package/dist/protocol-DEubbBbR.js +0 -35
- package/dist/push-BhrMZaQx.js +0 -190
- package/dist/push-cUJBpv5y.js +0 -8
- package/dist/schema-C2yy5KCT.d.ts +0 -6
- package/dist/schema-IFq7ovQq.js +0 -8
- package/dist/snapshot-DMW6j2Ix.js +0 -93
- package/dist/snapshot-DsSRYFe-.d.ts +0 -125
- package/dist/state-vav3VCId.js +0 -5
- package/dist/websocket-eao2rnHL.js +0 -104
|
@@ -1,5 +1,202 @@
|
|
|
1
|
-
import "../debug-
|
|
2
|
-
import "
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { i as init_debug, n as debugMigrations } from "../debug-Cz8KzMft.js";
|
|
2
|
+
import { createEmptySnapshot, generateMigrationStatements, generateSnapshotFromSchema, generateSnapshotId, snapshotsEqual } from "./snapshot.js";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as path$1 from "node:path";
|
|
5
|
+
import * as os from "node:os";
|
|
6
|
+
import { pathToFileURL } from "node:url";
|
|
7
|
+
//#region src/migrations/generator.ts
|
|
8
|
+
init_debug();
|
|
9
|
+
/**
|
|
10
|
+
* Snapshot file name
|
|
11
|
+
*/
|
|
12
|
+
const SNAPSHOT_FILE = "_snapshot.json";
|
|
13
|
+
/**
|
|
14
|
+
* Load the previous snapshot from the migrations directory
|
|
15
|
+
*/
|
|
16
|
+
function loadSnapshot(migrationsDir) {
|
|
17
|
+
const snapshotPath = path$1.join(migrationsDir, SNAPSHOT_FILE);
|
|
18
|
+
if (!fs.existsSync(snapshotPath)) return createEmptySnapshot();
|
|
19
|
+
const content = fs.readFileSync(snapshotPath, "utf-8");
|
|
20
|
+
try {
|
|
21
|
+
return JSON.parse(content);
|
|
22
|
+
} catch (e) {
|
|
23
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
24
|
+
throw new Error(`[db] Failed to parse snapshot at ${snapshotPath}: ${message}. The file appears corrupt — restore from git, or delete it if you intend to re-derive migrations from scratch (only safe when the migration history matches an empty-starting-point schema).`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Save a snapshot to the migrations directory
|
|
29
|
+
*/
|
|
30
|
+
function saveSnapshot(migrationsDir, snapshot) {
|
|
31
|
+
fs.mkdirSync(migrationsDir, { recursive: true });
|
|
32
|
+
const snapshotPath = path$1.join(migrationsDir, SNAPSHOT_FILE);
|
|
33
|
+
fs.writeFileSync(snapshotPath, JSON.stringify(snapshot, null, 2));
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Generate a timestamp-based migration name
|
|
37
|
+
*/
|
|
38
|
+
function generateMigrationName(suffix) {
|
|
39
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:T]/g, "").slice(0, 14);
|
|
40
|
+
return suffix ? `${timestamp}_${suffix}` : timestamp;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Generate a migration from schema changes
|
|
44
|
+
*
|
|
45
|
+
* This compares the current schema to the stored snapshot and generates
|
|
46
|
+
* the necessary SQL statements to migrate the database.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { users, posts } from './schema';
|
|
51
|
+
*
|
|
52
|
+
* const result = await generateMigration({
|
|
53
|
+
* migrationsDir: './migrations',
|
|
54
|
+
* schema: { users, posts },
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* if (result.hasChanges) {
|
|
58
|
+
* console.log('Migration generated:', result.migrationName);
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
async function generateMigration(options) {
|
|
63
|
+
const { migrationsDir, schema, migrationName, write = true } = options;
|
|
64
|
+
const previousSnapshot = loadSnapshot(migrationsDir);
|
|
65
|
+
const newSnapshot = await generateSnapshotFromSchema(schema);
|
|
66
|
+
newSnapshot.id = generateSnapshotId();
|
|
67
|
+
newSnapshot.prevId = previousSnapshot.id;
|
|
68
|
+
if (snapshotsEqual(previousSnapshot, newSnapshot)) return {
|
|
69
|
+
hasChanges: false,
|
|
70
|
+
statements: [],
|
|
71
|
+
migrationName: null,
|
|
72
|
+
snapshot: newSnapshot,
|
|
73
|
+
previousSnapshot
|
|
74
|
+
};
|
|
75
|
+
const statements = await generateMigrationStatements(previousSnapshot, newSnapshot);
|
|
76
|
+
const name = migrationName ?? generateMigrationName("auto");
|
|
77
|
+
if (write && statements.length > 0) {
|
|
78
|
+
fs.mkdirSync(migrationsDir, { recursive: true });
|
|
79
|
+
const sqlPath = path$1.join(migrationsDir, `${name}.sql`);
|
|
80
|
+
const sqlContent = statements.map((s) => s.endsWith(";") ? s : `${s};`).join("\n\n");
|
|
81
|
+
fs.writeFileSync(sqlPath, sqlContent);
|
|
82
|
+
saveSnapshot(migrationsDir, newSnapshot);
|
|
83
|
+
debugMigrations("Generated migration: %s", name);
|
|
84
|
+
debugMigrations("Statements: %d", statements.length);
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
hasChanges: true,
|
|
88
|
+
statements,
|
|
89
|
+
migrationName: name,
|
|
90
|
+
snapshot: newSnapshot,
|
|
91
|
+
previousSnapshot
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Breakpoint marker in SQL files
|
|
96
|
+
* Use: --> breakpoint
|
|
97
|
+
*/
|
|
98
|
+
const BREAKPOINT_MARKER = /^-->\s*breakpoint\s*$/im;
|
|
99
|
+
/**
|
|
100
|
+
* Parse SQL content into chunks (split by breakpoints) and statements
|
|
101
|
+
*
|
|
102
|
+
* @param content - Raw SQL file content
|
|
103
|
+
* @returns Array of chunks, where each chunk is an array of SQL statements
|
|
104
|
+
*/
|
|
105
|
+
function parseSqlMigration(content) {
|
|
106
|
+
return content.split(BREAKPOINT_MARKER).map((chunk) => {
|
|
107
|
+
return chunk.split("\n").filter((line) => !line.trim().startsWith("--")).join("\n").split(/;(?:\s*\n|\s*$)/).map((s) => s.trim()).filter((s) => s.length > 0);
|
|
108
|
+
}).filter((chunk) => chunk.length > 0);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Load all migration SQL files from a directory
|
|
112
|
+
* Returns migrations sorted by name (timestamp order) with breakpoint parsing
|
|
113
|
+
*/
|
|
114
|
+
function loadMigrationFiles(migrationsDir) {
|
|
115
|
+
const migrations = /* @__PURE__ */ new Map();
|
|
116
|
+
if (!fs.existsSync(migrationsDir)) return migrations;
|
|
117
|
+
const files = fs.readdirSync(migrationsDir).filter((f) => f.endsWith(".sql")).sort();
|
|
118
|
+
for (const file of files) {
|
|
119
|
+
const name = file.replace(".sql", "");
|
|
120
|
+
const chunks = parseSqlMigration(fs.readFileSync(path$1.join(migrationsDir, file), "utf-8"));
|
|
121
|
+
if (chunks.length > 0) migrations.set(name, chunks);
|
|
122
|
+
}
|
|
123
|
+
return migrations;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Load a schema module using dynamic import
|
|
127
|
+
* Works with both ESM and CJS modules
|
|
128
|
+
*/
|
|
129
|
+
async function loadSchemaModule(schemaPath, tableNames) {
|
|
130
|
+
const fileUrl = pathToFileURL(schemaPath).href;
|
|
131
|
+
try {
|
|
132
|
+
const module = await import(fileUrl);
|
|
133
|
+
const schema = {};
|
|
134
|
+
for (const name of tableNames) if (module[name]) schema[name] = module[name];
|
|
135
|
+
else if (module.default?.[name]) schema[name] = module.default[name];
|
|
136
|
+
return schema;
|
|
137
|
+
} catch (error) {
|
|
138
|
+
throw new Error(`Failed to load schema from ${schemaPath}: ${error}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Where to drop the transient transpiled-schema `.mjs`. We want a
|
|
143
|
+
* directory that (a) is on the app's node_modules resolution chain
|
|
144
|
+
* so the bundle's bare imports resolve, and (b) is NOT inside the
|
|
145
|
+
* watched `src/` tree (so dev-server watchers don't react to it).
|
|
146
|
+
* The app's `node_modules/.cache/durable-db/` satisfies both; if no
|
|
147
|
+
* node_modules is found walking up from the schema, fall back to the
|
|
148
|
+
* OS temp dir (the schema bundle's externals are usually resolvable
|
|
149
|
+
* relative to cwd in that case too).
|
|
150
|
+
*/
|
|
151
|
+
function resolveTempDir(schemaPath) {
|
|
152
|
+
let dir = path$1.dirname(path$1.resolve(schemaPath));
|
|
153
|
+
while (true) {
|
|
154
|
+
if (fs.existsSync(path$1.join(dir, "node_modules"))) return path$1.join(dir, "node_modules", ".cache", "durable-db");
|
|
155
|
+
const parent = path$1.dirname(dir);
|
|
156
|
+
if (parent === dir) break;
|
|
157
|
+
dir = parent;
|
|
158
|
+
}
|
|
159
|
+
return path$1.join(os.tmpdir(), "durable-db");
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Build schema module with esbuild and return the exports
|
|
163
|
+
* This is useful when the schema is TypeScript and needs transpilation
|
|
164
|
+
*/
|
|
165
|
+
async function buildAndLoadSchema(schemaPath, tableNames) {
|
|
166
|
+
const result = await (await import("esbuild")).build({
|
|
167
|
+
entryPoints: [schemaPath],
|
|
168
|
+
bundle: true,
|
|
169
|
+
write: false,
|
|
170
|
+
format: "esm",
|
|
171
|
+
platform: "node",
|
|
172
|
+
packages: "external",
|
|
173
|
+
target: "node18"
|
|
174
|
+
});
|
|
175
|
+
if (result.outputFiles?.length !== 1) throw new Error(`Unexpected esbuild output for ${schemaPath}`);
|
|
176
|
+
const tempDir = resolveTempDir(schemaPath);
|
|
177
|
+
fs.mkdirSync(tempDir, { recursive: true });
|
|
178
|
+
const tempFile = path$1.join(tempDir, `_schema_temp_${Date.now()}.mjs`);
|
|
179
|
+
try {
|
|
180
|
+
fs.writeFileSync(tempFile, result.outputFiles[0].text);
|
|
181
|
+
const module = await import(pathToFileURL(tempFile).href);
|
|
182
|
+
const schema = {};
|
|
183
|
+
for (const name of tableNames) if (module[name]) schema[name] = module[name];
|
|
184
|
+
else if (module.default?.[name]) schema[name] = module.default[name];
|
|
185
|
+
return schema;
|
|
186
|
+
} finally {
|
|
187
|
+
try {
|
|
188
|
+
fs.unlinkSync(tempFile);
|
|
189
|
+
} catch {}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Generate SQL to create all tables from scratch
|
|
194
|
+
* Useful for initial setup or testing
|
|
195
|
+
*/
|
|
196
|
+
async function generateCreateTableSQL(schema) {
|
|
197
|
+
const emptySnapshot = createEmptySnapshot();
|
|
198
|
+
const currentSnapshot = await generateSnapshotFromSchema(schema);
|
|
199
|
+
return generateMigrationStatements(emptySnapshot, currentSnapshot);
|
|
200
|
+
}
|
|
201
|
+
//#endregion
|
|
202
|
+
export { buildAndLoadSchema, generateCreateTableSQL, generateMigration, generateMigrationName, loadMigrationFiles, loadSchemaModule, loadSnapshot, parseSqlMigration, saveSnapshot };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as generateSnapshotId, i as generateSnapshotFromSchema, n as createEmptySnapshot, o as hashSnapshot, r as generateMigrationStatements, s as snapshotsEqual, t as Snapshot } from "../snapshot-
|
|
1
|
+
import { a as generateSnapshotId, i as generateSnapshotFromSchema, n as createEmptySnapshot, o as hashSnapshot, r as generateMigrationStatements, s as snapshotsEqual, t as Snapshot } from "../snapshot-CaG5xnE2.js";
|
|
2
2
|
export { Snapshot, createEmptySnapshot, generateMigrationStatements, generateSnapshotFromSchema, generateSnapshotId, hashSnapshot, snapshotsEqual };
|
|
@@ -1,3 +1,91 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
//#region src/migrations/snapshot.ts
|
|
3
|
+
const { generateSQLiteDrizzleJson, generateSQLiteMigration } = createRequire(import.meta.url)("drizzle-kit/api");
|
|
4
|
+
/**
|
|
5
|
+
* Create an empty snapshot (for initial migration from nothing)
|
|
6
|
+
*/
|
|
7
|
+
function createEmptySnapshot() {
|
|
8
|
+
return {
|
|
9
|
+
views: {},
|
|
10
|
+
version: "6",
|
|
11
|
+
dialect: "sqlite",
|
|
12
|
+
id: "0000000000000",
|
|
13
|
+
prevId: "",
|
|
14
|
+
tables: {},
|
|
15
|
+
enums: {},
|
|
16
|
+
_meta: {
|
|
17
|
+
tables: {},
|
|
18
|
+
columns: {}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Generate a unique snapshot ID based on timestamp
|
|
24
|
+
*/
|
|
25
|
+
function generateSnapshotId() {
|
|
26
|
+
return Date.now().toString(36).padStart(13, "0");
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Generate a snapshot from Drizzle schema objects
|
|
30
|
+
*
|
|
31
|
+
* @param schema - Object containing Drizzle table definitions
|
|
32
|
+
* @returns Snapshot representing the current schema state
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { users, posts } from './schema';
|
|
37
|
+
* const snapshot = await generateSnapshotFromSchema({ users, posts });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
async function generateSnapshotFromSchema(schema) {
|
|
41
|
+
return generateSQLiteDrizzleJson(schema, void 0, "snake_case");
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Generate SQL migration statements from two snapshots
|
|
45
|
+
*
|
|
46
|
+
* @param from - Previous schema state (or empty for initial migration)
|
|
47
|
+
* @param to - Target schema state
|
|
48
|
+
* @returns Array of SQL statements to migrate from `from` to `to`
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const statements = await generateMigrationStatements(prevSnapshot, newSnapshot);
|
|
53
|
+
* // ['CREATE TABLE users (...)', 'CREATE INDEX ...']
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
async function generateMigrationStatements(from, to) {
|
|
57
|
+
return await generateSQLiteMigration(from, to);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Serialize the schema-relevant parts of a snapshot for comparison/hashing.
|
|
61
|
+
* Includes tables, views, and enums so that changes to any of them are
|
|
62
|
+
* detected — previously only `tables` was considered.
|
|
63
|
+
*/
|
|
64
|
+
function serializeSchemaShape(snapshot) {
|
|
65
|
+
return JSON.stringify({
|
|
66
|
+
tables: snapshot.tables ?? {},
|
|
67
|
+
views: snapshot.views ?? {},
|
|
68
|
+
enums: snapshot.enums ?? {}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check if two snapshots are equivalent (no migration needed)
|
|
73
|
+
*/
|
|
74
|
+
function snapshotsEqual(a, b) {
|
|
75
|
+
return serializeSchemaShape(a) === serializeSchemaShape(b);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Create a hash of a snapshot for quick comparison
|
|
79
|
+
*/
|
|
80
|
+
function hashSnapshot(snapshot) {
|
|
81
|
+
const content = serializeSchemaShape(snapshot);
|
|
82
|
+
let hash = 0;
|
|
83
|
+
for (let i = 0; i < content.length; i++) {
|
|
84
|
+
const char = content.charCodeAt(i);
|
|
85
|
+
hash = (hash << 5) - hash + char;
|
|
86
|
+
hash = hash & hash;
|
|
87
|
+
}
|
|
88
|
+
return Math.abs(hash).toString(36);
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
export { createEmptySnapshot, generateMigrationStatements, generateSnapshotFromSchema, generateSnapshotId, hashSnapshot, snapshotsEqual };
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { i as init_debug, t as debugCli } from "./debug-
|
|
1
|
+
import { i as init_debug, t as debugCli } from "./debug-Cz8KzMft.js";
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
3
|
import * as path$1 from "node:path";
|
|
4
4
|
import { parse } from "@babel/parser";
|
|
5
5
|
import _traverse from "@babel/traverse";
|
|
6
6
|
import _generate from "@babel/generator";
|
|
7
7
|
import * as t from "@babel/types";
|
|
8
|
-
|
|
9
8
|
//#region src/vite/modules/aliasResolver.ts
|
|
10
9
|
init_debug();
|
|
11
10
|
/**
|
|
@@ -114,7 +113,6 @@ async function loadViteAliases(root) {
|
|
|
114
113
|
viteAliasCache.set(key, aliases);
|
|
115
114
|
return aliases;
|
|
116
115
|
}
|
|
117
|
-
|
|
118
116
|
//#endregion
|
|
119
117
|
//#region src/vite/modules/discovery.ts
|
|
120
118
|
/** The sources a `databasesDir` option names. */
|
|
@@ -217,7 +215,6 @@ function resolveImportPath(fromFile, importSource, aliases) {
|
|
|
217
215
|
function readFile(filePath) {
|
|
218
216
|
return fs.readFileSync(filePath, "utf-8");
|
|
219
217
|
}
|
|
220
|
-
|
|
221
218
|
//#endregion
|
|
222
219
|
//#region src/vite/modules/parser.ts
|
|
223
220
|
init_debug();
|
|
@@ -348,17 +345,23 @@ function extractDatabaseInfo(declaratorPath, callExpr, filePath, imports, namePr
|
|
|
348
345
|
for (const prop of configArg.properties) {
|
|
349
346
|
if (!t.isObjectProperty(prop) || !t.isIdentifier(prop.key)) continue;
|
|
350
347
|
const key = prop.key.name;
|
|
351
|
-
if (key === "instance")
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
}
|
|
348
|
+
if (key === "instance") {
|
|
349
|
+
if (t.isStringLiteral(prop.value)) {
|
|
350
|
+
const value = prop.value.value;
|
|
351
|
+
if (value === "per-tenant" || value === "global") instance = value;
|
|
352
|
+
} else warnNonLiteral(filePath, "instance", "per-tenant");
|
|
353
|
+
}
|
|
354
|
+
if (key === "browsable") {
|
|
355
|
+
if (t.isBooleanLiteral(prop.value)) browsable = prop.value.value;
|
|
356
|
+
else if (t.isStringLiteral(prop.value) && prop.value.value === "development") browsable = "development";
|
|
357
|
+
else warnNonLiteral(filePath, "browsable", String(browsable));
|
|
358
|
+
}
|
|
359
|
+
if (key === "transport") {
|
|
360
|
+
if (t.isStringLiteral(prop.value)) {
|
|
361
|
+
const value = prop.value.value;
|
|
362
|
+
if (value === "rpc" || value === "websocket") transport = value;
|
|
363
|
+
} else warnNonLiteral(filePath, "transport", "rpc");
|
|
364
|
+
}
|
|
362
365
|
if (key === "schema" && t.isObjectExpression(prop.value)) {
|
|
363
366
|
for (const schemaProp of prop.value.properties) if (t.isObjectProperty(schemaProp) || t.isSpreadElement(schemaProp)) {
|
|
364
367
|
if (t.isObjectProperty(schemaProp) && t.isIdentifier(schemaProp.key)) {
|
|
@@ -388,7 +391,7 @@ function extractDatabaseInfo(declaratorPath, callExpr, filePath, imports, namePr
|
|
|
388
391
|
/**
|
|
389
392
|
* Extract action information from an action() call
|
|
390
393
|
*/
|
|
391
|
-
function extractActionInfo(declaratorPath, callExpr, databaseName
|
|
394
|
+
function extractActionInfo(declaratorPath, callExpr, databaseName, filePath) {
|
|
392
395
|
const id = declaratorPath.node.id;
|
|
393
396
|
if (!t.isIdentifier(id)) return null;
|
|
394
397
|
const exportName = id.name;
|
|
@@ -406,7 +409,7 @@ function extractActionInfo(declaratorPath, callExpr, databaseName$1, filePath) {
|
|
|
406
409
|
exportName,
|
|
407
410
|
argsSchemaSource,
|
|
408
411
|
handlerSource,
|
|
409
|
-
databaseName
|
|
412
|
+
databaseName,
|
|
410
413
|
sourceFile: filePath
|
|
411
414
|
};
|
|
412
415
|
}
|
|
@@ -422,6 +425,5 @@ function toPascalCase(str) {
|
|
|
422
425
|
function toScreamingSnakeCase(str) {
|
|
423
426
|
return str.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[-]/g, "_").toUpperCase();
|
|
424
427
|
}
|
|
425
|
-
|
|
426
428
|
//#endregion
|
|
427
|
-
export { discoverDatabaseFiles as a, loadViteAliases as c, discoverActionFiles as i, parseDatabaseFile as n, readFile as o, databaseSources as r, resolveImportPath as s, BABEL_PARSER_PLUGINS as t };
|
|
429
|
+
export { discoverDatabaseFiles as a, loadViteAliases as c, discoverActionFiles as i, parseDatabaseFile as n, readFile as o, databaseSources as r, resolveImportPath as s, BABEL_PARSER_PLUGINS as t };
|
package/dist/registry.d.ts
CHANGED
|
@@ -1,30 +1,29 @@
|
|
|
1
|
-
import { t as type } from "./arktypeConfig-
|
|
2
|
-
|
|
1
|
+
import { t as type } from "./arktypeConfig-AXvobTPL.js";
|
|
3
2
|
//#region src/registry.d.ts
|
|
4
|
-
interface ActionDefinition {
|
|
3
|
+
export interface ActionDefinition {
|
|
5
4
|
validator: (args: unknown) => unknown;
|
|
6
5
|
handler: (db: unknown, args: unknown, ctx: unknown) => Promise<unknown>;
|
|
7
6
|
}
|
|
8
|
-
interface DoContext {
|
|
7
|
+
export interface DoContext {
|
|
9
8
|
db: unknown;
|
|
10
9
|
ctx: RpcContext;
|
|
11
10
|
dbName: string;
|
|
12
11
|
instanceKey: string;
|
|
13
12
|
}
|
|
14
|
-
interface RpcContext {
|
|
13
|
+
export interface RpcContext {
|
|
15
14
|
env: Record<string, unknown>;
|
|
16
15
|
dbName: string;
|
|
17
16
|
dbBindingNames: Record<string, string>;
|
|
18
17
|
dbTransports?: Record<string, 'rpc' | 'websocket'>;
|
|
19
18
|
instanceKey: string;
|
|
20
19
|
}
|
|
21
|
-
declare function registerAction(dbName: string, actionName: string, def: ActionDefinition): void;
|
|
22
|
-
declare function getAction(dbName: string, actionName: string): ActionDefinition | undefined;
|
|
23
|
-
declare function getDoContext(): DoContext | undefined;
|
|
24
|
-
declare function runWithDoContext<T>(store: DoContext, fn: () => T): T;
|
|
20
|
+
export declare function registerAction(dbName: string, actionName: string, def: ActionDefinition): void;
|
|
21
|
+
export declare function getAction(dbName: string, actionName: string): ActionDefinition | undefined;
|
|
22
|
+
export declare function getDoContext(): DoContext | undefined;
|
|
23
|
+
export declare function runWithDoContext<T>(store: DoContext, fn: () => T): T;
|
|
25
24
|
/**
|
|
26
25
|
* Call an action: validates args, then dispatches (direct call for same DB, RPC for cross-DB).
|
|
27
26
|
*/
|
|
28
|
-
declare function callAction(db: unknown, targetDb: string, actionName: string, args: unknown, ctx: RpcContext): Promise<unknown>;
|
|
27
|
+
export declare function callAction(db: unknown, targetDb: string, actionName: string, args: unknown, ctx: RpcContext): Promise<unknown>;
|
|
29
28
|
//#endregion
|
|
30
|
-
export {
|
|
29
|
+
export { type };
|
package/dist/registry.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { t as type } from "./arktypeConfig-
|
|
2
|
-
import {
|
|
1
|
+
import { t as type } from "./arktypeConfig-DxP0OcV_.js";
|
|
2
|
+
import { WebSocketTransport } from "./transport/websocket.js";
|
|
3
3
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
|
-
|
|
5
4
|
//#region src/registry.ts
|
|
6
5
|
const actionsByDb = /* @__PURE__ */ new Map();
|
|
7
6
|
const als = new AsyncLocalStorage();
|
|
@@ -69,6 +68,5 @@ async function callAction(db, targetDb, actionName, args, ctx) {
|
|
|
69
68
|
const result = await stub.rpc(actionName, validated, { instanceKey: ctx.instanceKey });
|
|
70
69
|
return structuredClone(result);
|
|
71
70
|
}
|
|
72
|
-
|
|
73
71
|
//#endregion
|
|
74
|
-
export { callAction, getAction, getDoContext, registerAction, runWithDoContext, type };
|
|
72
|
+
export { callAction, getAction, getDoContext, registerAction, runWithDoContext, type };
|
package/dist/schema.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { AnySQLiteColumn, blob, foreignKey, index, integer, numeric, primaryKey, real, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";
|
|
2
|
+
//#region src/schema.d.ts
|
|
3
|
+
export declare const table: typeof sqliteTable;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { type AnySQLiteColumn, blob, foreignKey, index, integer, numeric, primaryKey, real, text, uniqueIndex };
|
package/dist/schema.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { blob, foreignKey, index, integer, numeric, primaryKey, real, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";
|
|
2
|
+
import { toSnakeCase } from "drizzle-orm/casing";
|
|
3
|
+
//#region src/schema.ts
|
|
4
|
+
const table = ((name, columns, extraConfig) => sqliteTable(toSnakeCase(name), columns, extraConfig));
|
|
5
|
+
//#endregion
|
|
6
|
+
export { blob, foreignKey, index, integer, numeric, primaryKey, real, table, text, uniqueIndex };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { DrizzleSQLiteSnapshotJSON } from "drizzle-kit/api";
|
|
2
|
+
//#region src/migrations/snapshot.d.ts
|
|
3
|
+
type Snapshot = DrizzleSQLiteSnapshotJSON;
|
|
4
|
+
/**
|
|
5
|
+
* Create an empty snapshot (for initial migration from nothing)
|
|
6
|
+
*/
|
|
7
|
+
declare function createEmptySnapshot(): Snapshot;
|
|
8
|
+
/**
|
|
9
|
+
* Generate a unique snapshot ID based on timestamp
|
|
10
|
+
*/
|
|
11
|
+
declare function generateSnapshotId(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Generate a snapshot from Drizzle schema objects
|
|
14
|
+
*
|
|
15
|
+
* @param schema - Object containing Drizzle table definitions
|
|
16
|
+
* @returns Snapshot representing the current schema state
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { users, posts } from './schema';
|
|
21
|
+
* const snapshot = await generateSnapshotFromSchema({ users, posts });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function generateSnapshotFromSchema(schema: Record<string, unknown>): Promise<any>;
|
|
25
|
+
/**
|
|
26
|
+
* Generate SQL migration statements from two snapshots
|
|
27
|
+
*
|
|
28
|
+
* @param from - Previous schema state (or empty for initial migration)
|
|
29
|
+
* @param to - Target schema state
|
|
30
|
+
* @returns Array of SQL statements to migrate from `from` to `to`
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const statements = await generateMigrationStatements(prevSnapshot, newSnapshot);
|
|
35
|
+
* // ['CREATE TABLE users (...)', 'CREATE INDEX ...']
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
declare function generateMigrationStatements(from: Snapshot, to: Snapshot): Promise<string[]>;
|
|
39
|
+
/**
|
|
40
|
+
* Check if two snapshots are equivalent (no migration needed)
|
|
41
|
+
*/
|
|
42
|
+
declare function snapshotsEqual(a: Snapshot, b: Snapshot): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Create a hash of a snapshot for quick comparison
|
|
45
|
+
*/
|
|
46
|
+
declare function hashSnapshot(snapshot: Snapshot): string;
|
|
47
|
+
//#endregion
|
|
48
|
+
export { generateSnapshotId as a, generateSnapshotFromSchema as i, createEmptySnapshot as n, hashSnapshot as o, generateMigrationStatements as r, snapshotsEqual as s, Snapshot as t };
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { a as __esmMin, i as init_debug, o as
|
|
1
|
+
import { a as __esmMin, i as init_debug, o as __exportAll, t as debugCli } from "./debug-Cz8KzMft.js";
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
3
|
import * as path$1 from "node:path";
|
|
4
|
-
|
|
5
4
|
//#region src/cli/state.ts
|
|
6
|
-
var state_exports = /* @__PURE__ */
|
|
5
|
+
var state_exports = /* @__PURE__ */ __exportAll({
|
|
7
6
|
clearDatabaseDevState: () => clearDatabaseDevState,
|
|
8
7
|
clearDevMigrations: () => clearDevMigrations,
|
|
9
8
|
createEmptyDevState: () => createEmptyDevState,
|
|
@@ -198,6 +197,5 @@ var init_state = __esmMin((() => {
|
|
|
198
197
|
DEV_SNAPSHOT_FILE = "_snapshot.json";
|
|
199
198
|
BREAKPOINT_MARKER = /^-->\s*breakpoint\s*$/im;
|
|
200
199
|
}));
|
|
201
|
-
|
|
202
200
|
//#endregion
|
|
203
|
-
export {
|
|
201
|
+
export { getDevPaths as a, loadDevMigrations as c, saveDevState as d, state_exports as f, getDevEpoch as i, loadDevState as l, clearDevMigrations as n, getInstanceKey as o, generateEpoch as r, init_state as s, clearDatabaseDevState as t, saveDevMigration as u };
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { SQLiteTableWithColumns } from "drizzle-orm/sqlite-core";
|
|
2
2
|
import { Kysely } from "kysely";
|
|
3
3
|
import { DatabaseSync } from "node:sqlite";
|
|
4
|
-
|
|
5
4
|
//#region src/testing/index.d.ts
|
|
6
5
|
type Schema = Record<string, SQLiteTableWithColumns<any>>;
|
|
7
|
-
interface CreateTestDatabaseOptions {
|
|
6
|
+
export interface CreateTestDatabaseOptions {
|
|
8
7
|
/**
|
|
9
8
|
* The drizzle schema (the same object passed to `defineDatabase({ schema })`).
|
|
10
9
|
* Tables are created from it automatically.
|
|
@@ -22,7 +21,7 @@ interface CreateTestDatabaseOptions {
|
|
|
22
21
|
*/
|
|
23
22
|
tenantId?: string;
|
|
24
23
|
}
|
|
25
|
-
interface TestDatabase<TSchema = any> {
|
|
24
|
+
export interface TestDatabase<TSchema = any> {
|
|
26
25
|
/** The Kysely instance, for direct seeding and assertions. */
|
|
27
26
|
db: Kysely<TSchema>;
|
|
28
27
|
/**
|
|
@@ -52,6 +51,6 @@ interface TestDatabase<TSchema = any> {
|
|
|
52
51
|
* t.destroy();
|
|
53
52
|
* ```
|
|
54
53
|
*/
|
|
55
|
-
declare function createTestDatabase<TSchema = any>(options: CreateTestDatabaseOptions): Promise<TestDatabase<TSchema>>;
|
|
54
|
+
export declare function createTestDatabase<TSchema = any>(options: CreateTestDatabaseOptions): Promise<TestDatabase<TSchema>>;
|
|
56
55
|
//#endregion
|
|
57
|
-
export {
|
|
56
|
+
export {};
|
package/dist/testing/index.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import "../
|
|
2
|
-
import { t as
|
|
3
|
-
import {
|
|
4
|
-
import "../
|
|
5
|
-
import {
|
|
6
|
-
import { t as createKyselyFromSql } from "../kysely-BH7QZBZw.js";
|
|
7
|
-
import { n as generateMigrationStatements, r as generateSnapshotFromSchema, t as createEmptySnapshot } from "../snapshot-DMW6j2Ix.js";
|
|
1
|
+
import { t as type } from "../arktypeConfig-DxP0OcV_.js";
|
|
2
|
+
import { t as ACTION_TEST_META } from "../defineDatabase-CHmzpzFf.js";
|
|
3
|
+
import { setTenantIdResolver } from "../context/index.js";
|
|
4
|
+
import { t as createKyselyFromSql } from "../kysely-Be6KwFZC.js";
|
|
5
|
+
import { createEmptySnapshot, generateMigrationStatements, generateSnapshotFromSchema } from "../migrations/snapshot.js";
|
|
8
6
|
import { createRequire } from "node:module";
|
|
9
|
-
|
|
10
7
|
//#region src/testing/index.ts
|
|
11
8
|
const loadSqlite = () => createRequire(import.meta.url)("node:sqlite");
|
|
12
9
|
/**
|
|
@@ -67,6 +64,5 @@ async function createTestDatabase(options) {
|
|
|
67
64
|
}
|
|
68
65
|
};
|
|
69
66
|
}
|
|
70
|
-
|
|
71
67
|
//#endregion
|
|
72
|
-
export { createTestDatabase };
|
|
68
|
+
export { createTestDatabase };
|
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
*
|
|
5
5
|
* JSON text frames with request-response correlation via `id`.
|
|
6
6
|
*/
|
|
7
|
-
interface WsActionRequest {
|
|
7
|
+
export interface WsActionRequest {
|
|
8
8
|
id: string;
|
|
9
9
|
action: string;
|
|
10
10
|
args: unknown;
|
|
11
11
|
instanceKey: string;
|
|
12
12
|
}
|
|
13
|
-
type WsActionResponse = {
|
|
13
|
+
export type WsActionResponse = {
|
|
14
14
|
id: string;
|
|
15
15
|
ok: true;
|
|
16
16
|
result: unknown;
|
|
@@ -19,9 +19,8 @@ type WsActionResponse = {
|
|
|
19
19
|
ok: false;
|
|
20
20
|
error: string;
|
|
21
21
|
};
|
|
22
|
-
declare function encodeRequest(req: WsActionRequest): string;
|
|
23
|
-
declare function decodeRequest(data: string): WsActionRequest;
|
|
24
|
-
declare function encodeResponse(res: WsActionResponse): string;
|
|
25
|
-
declare function decodeResponse(data: string): WsActionResponse;
|
|
26
|
-
//#endregion
|
|
27
|
-
export { WsActionRequest, WsActionResponse, decodeRequest, decodeResponse, encodeRequest, encodeResponse };
|
|
22
|
+
export declare function encodeRequest(req: WsActionRequest): string;
|
|
23
|
+
export declare function decodeRequest(data: string): WsActionRequest;
|
|
24
|
+
export declare function encodeResponse(res: WsActionResponse): string;
|
|
25
|
+
export declare function decodeResponse(data: string): WsActionResponse;
|
|
26
|
+
//#endregion
|