durable-db 0.1.0 → 0.1.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/cli/bin.js +3 -3
- package/dist/cli/index.d.ts +6 -1
- package/dist/cli/index.js +3 -3
- package/dist/{cli-BsxcF5xB.js → cli-BtEsZzHF.js} +9 -8
- package/dist/{parser-V6t9KSi1.js → parser-B05psK1k.js} +129 -29
- package/dist/{push-DJExqk8A.js → push-BW7sodiO.js} +2 -2
- package/dist/{push-tAKBd8b1.js → push-gXPr9k-l.js} +11 -5
- package/dist/vite/durableDb.js +33 -9
- package/package.json +1 -1
package/dist/cli/bin.js
CHANGED
|
@@ -3,9 +3,9 @@ import "../debug-DzvNVBs5.js";
|
|
|
3
3
|
import "../state-CMRjqCDw.js";
|
|
4
4
|
import "../snapshot-DMW6j2Ix.js";
|
|
5
5
|
import "../generator-BVwDQvKA.js";
|
|
6
|
-
import "../parser-
|
|
7
|
-
import "../push-
|
|
8
|
-
import { n as registerDbCommands } from "../cli-
|
|
6
|
+
import "../parser-B05psK1k.js";
|
|
7
|
+
import "../push-gXPr9k-l.js";
|
|
8
|
+
import { n as registerDbCommands } from "../cli-BtEsZzHF.js";
|
|
9
9
|
import { Command } from "commander";
|
|
10
10
|
|
|
11
11
|
//#region src/cli/bin.ts
|
package/dist/cli/index.d.ts
CHANGED
|
@@ -319,8 +319,13 @@ declare function discoverDatabases(opts: DiscoverOptions): DatabaseInfo[];
|
|
|
319
319
|
* missing exports) throws — silently skipping a database is how a typo in
|
|
320
320
|
* schema.ts becomes "why is my migration empty" 20 minutes later, or worse,
|
|
321
321
|
* a generated migration that DROPs tables that still exist in production.
|
|
322
|
+
*
|
|
323
|
+
* `projectRoot` enables alias resolution for non-relative schema imports
|
|
324
|
+
* (e.g. `@/databases/schema`): the project's Vite `resolve.alias` is loaded
|
|
325
|
+
* (cached) and used as the source of truth. Omit it to resolve relative
|
|
326
|
+
* imports only.
|
|
322
327
|
*/
|
|
323
|
-
declare function loadSchema(db: DatabaseInfo): Promise<Record<string, unknown> | null>;
|
|
328
|
+
declare function loadSchema(db: DatabaseInfo, projectRoot?: string): Promise<Record<string, unknown> | null>;
|
|
324
329
|
/**
|
|
325
330
|
* Diff a schema against the production snapshot in migrationsDir.
|
|
326
331
|
* Returns whether there are changes, the SQL statements, and both snapshots.
|
package/dist/cli/index.js
CHANGED
|
@@ -2,8 +2,8 @@ import "../debug-DzvNVBs5.js";
|
|
|
2
2
|
import { _ as saveDevState, a as generateEpoch, c as getDevPaths, f as loadDevMigrations, l as getInstanceKey, m as loadDevState, s as getDevEpoch } from "../state-CMRjqCDw.js";
|
|
3
3
|
import "../snapshot-DMW6j2Ix.js";
|
|
4
4
|
import "../generator-BVwDQvKA.js";
|
|
5
|
-
import "../parser-
|
|
6
|
-
import { a as loadSchema, i as discoverDatabases, n as pushOne, o as reportCliError, r as diffSchema, t as push } from "../push-
|
|
7
|
-
import { a as resetAndPush, c as generate, i as reset, l as generateOne, n as registerDbCommands, o as formatStatus, r as validate, s as status, t as createDbCommand } from "../cli-
|
|
5
|
+
import "../parser-B05psK1k.js";
|
|
6
|
+
import { a as loadSchema, i as discoverDatabases, n as pushOne, o as reportCliError, r as diffSchema, t as push } from "../push-gXPr9k-l.js";
|
|
7
|
+
import { a as resetAndPush, c as generate, i as reset, l as generateOne, n as registerDbCommands, o as formatStatus, r as validate, s as status, t as createDbCommand } from "../cli-BtEsZzHF.js";
|
|
8
8
|
|
|
9
9
|
export { createDbCommand, diffSchema, discoverDatabases, formatStatus, generate, generateEpoch, generateOne, getDevEpoch, getDevPaths, getInstanceKey, loadDevMigrations, loadDevState, loadSchema, push, pushOne, registerDbCommands, reportCliError, reset, resetAndPush, saveDevState, status, validate };
|
|
@@ -2,8 +2,8 @@ import { i as init_debug, t as debugCli } from "./debug-DzvNVBs5.js";
|
|
|
2
2
|
import { _ as saveDevState, a as generateEpoch, c as getDevPaths, d as init_state, f as loadDevMigrations, m as loadDevState, t as clearDatabaseDevState } from "./state-CMRjqCDw.js";
|
|
3
3
|
import { a as hashSnapshot } from "./snapshot-DMW6j2Ix.js";
|
|
4
4
|
import { a as loadMigrationFiles, i as generateMigrationName, l as saveSnapshot, n as generateCreateTableSQL, s as loadSnapshot, t as buildAndLoadSchema } from "./generator-BVwDQvKA.js";
|
|
5
|
-
import { a as resolveImportPath } from "./parser-
|
|
6
|
-
import { a as loadSchema, i as discoverDatabases, o as reportCliError, r as diffSchema, t as push } from "./push-
|
|
5
|
+
import { a as resolveImportPath, o as loadViteAliases } from "./parser-B05psK1k.js";
|
|
6
|
+
import { a as loadSchema, i as discoverDatabases, o as reportCliError, r as diffSchema, t as push } from "./push-gXPr9k-l.js";
|
|
7
7
|
import * as fs from "node:fs";
|
|
8
8
|
import * as path from "node:path";
|
|
9
9
|
import { Command } from "commander";
|
|
@@ -39,7 +39,7 @@ async function generate(ctx = {}, options = {}) {
|
|
|
39
39
|
return results;
|
|
40
40
|
}
|
|
41
41
|
for (const db of databases) {
|
|
42
|
-
const result = await generateDatabase(db, customName);
|
|
42
|
+
const result = await generateDatabase(db, customName, projectRoot);
|
|
43
43
|
results.push(result);
|
|
44
44
|
if (result.hasChanges) {
|
|
45
45
|
clearDatabaseDevState(projectRoot, db.name);
|
|
@@ -52,7 +52,7 @@ async function generate(ctx = {}, options = {}) {
|
|
|
52
52
|
/**
|
|
53
53
|
* Generate migration for a single database
|
|
54
54
|
*/
|
|
55
|
-
async function generateDatabase(db, customName) {
|
|
55
|
+
async function generateDatabase(db, customName, projectRoot) {
|
|
56
56
|
const result = {
|
|
57
57
|
database: db.name,
|
|
58
58
|
hasChanges: false,
|
|
@@ -60,7 +60,7 @@ async function generateDatabase(db, customName) {
|
|
|
60
60
|
migrationName: null,
|
|
61
61
|
migrationPath: null
|
|
62
62
|
};
|
|
63
|
-
const schema = await loadSchema(db);
|
|
63
|
+
const schema = await loadSchema(db, projectRoot);
|
|
64
64
|
if (!schema) return result;
|
|
65
65
|
const diff = await diffSchema(schema, db.migrationsDir);
|
|
66
66
|
if (!diff.hasChanges) {
|
|
@@ -148,7 +148,7 @@ async function getDatabaseStatus(projectRoot, db, devState) {
|
|
|
148
148
|
};
|
|
149
149
|
dbStatus.prodMigrationCount = loadMigrationFiles(db.migrationsDir).size;
|
|
150
150
|
dbStatus.devMigrationCount = loadDevMigrations(projectRoot, db.name).size;
|
|
151
|
-
const schema = await loadSchema(db);
|
|
151
|
+
const schema = await loadSchema(db, projectRoot);
|
|
152
152
|
if (!schema) return dbStatus;
|
|
153
153
|
const prodSnapshotHash = hashSnapshot(loadSnapshot(db.migrationsDir));
|
|
154
154
|
const dbDevState = devState.databases[db.name];
|
|
@@ -322,7 +322,7 @@ async function reset(ctx = {}, options = {}) {
|
|
|
322
322
|
* Convenience function that combines reset + push
|
|
323
323
|
*/
|
|
324
324
|
async function resetAndPush(ctx = {}, options = {}) {
|
|
325
|
-
const { push: push$1 } = await import("./push-
|
|
325
|
+
const { push: push$1 } = await import("./push-BW7sodiO.js");
|
|
326
326
|
return {
|
|
327
327
|
reset: await reset(ctx, options),
|
|
328
328
|
push: await push$1(ctx)
|
|
@@ -443,7 +443,8 @@ async function validateDatabase(db, opts) {
|
|
|
443
443
|
}
|
|
444
444
|
}
|
|
445
445
|
if (db.schemaImport && db.schemaTableNames.length > 0) {
|
|
446
|
-
const
|
|
446
|
+
const aliases = await loadViteAliases(projectRoot);
|
|
447
|
+
const schemaPath = resolveImportPath(db.filePath, db.schemaImport, aliases);
|
|
447
448
|
if (schemaPath) try {
|
|
448
449
|
const schema = await buildAndLoadSchema(schemaPath, db.schemaTableNames);
|
|
449
450
|
if (Object.keys(schema).length > 0) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as init_debug } from "./debug-DzvNVBs5.js";
|
|
1
|
+
import { i as init_debug, t as debugCli } from "./debug-DzvNVBs5.js";
|
|
2
2
|
import * as fs from "node:fs";
|
|
3
3
|
import * as path from "node:path";
|
|
4
4
|
import { parse } from "@babel/parser";
|
|
@@ -6,6 +6,116 @@ import _traverse from "@babel/traverse";
|
|
|
6
6
|
import _generate from "@babel/generator";
|
|
7
7
|
import * as t from "@babel/types";
|
|
8
8
|
|
|
9
|
+
//#region src/vite/modules/aliasResolver.ts
|
|
10
|
+
init_debug();
|
|
11
|
+
/**
|
|
12
|
+
* Import resolution for database files, driven by **Vite's** resolved config.
|
|
13
|
+
*
|
|
14
|
+
* The relative-import resolver only understands paths that start with `.`. Apps
|
|
15
|
+
* frequently import their schema / action factory through an alias instead
|
|
16
|
+
* (e.g. `@/databases/schema`, `~/foo`). Those aliases are NOT owned by
|
|
17
|
+
* durable-db — they come from the project's Vite config (`resolve.alias`),
|
|
18
|
+
* which is where tooling like rwsdk wires tsconfig `paths` (including aliases
|
|
19
|
+
* declared in a base tsconfig the app `extends`). So Vite's resolved config is
|
|
20
|
+
* the single source of truth for aliases.
|
|
21
|
+
*
|
|
22
|
+
* Two contexts consume this:
|
|
23
|
+
* - The **Vite plugin** captures `config.resolve.alias` in `configResolved` and
|
|
24
|
+
* passes it down (the plugin's action-file transform also defers to Vite's own
|
|
25
|
+
* resolver via `this.resolve(...)`, the most faithful resolution).
|
|
26
|
+
* - The **CLI** runs standalone, so it loads the project's Vite config with
|
|
27
|
+
* `resolveConfig(...)` and reads `config.resolve.alias` (cached per root).
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* Probe a candidate path for a real file, trying the same extensions and index
|
|
31
|
+
* files the relative resolver uses. Shared between relative resolution and
|
|
32
|
+
* alias resolution so both code paths behave identically.
|
|
33
|
+
*/
|
|
34
|
+
function resolveFileCandidate(candidate) {
|
|
35
|
+
if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) return candidate;
|
|
36
|
+
for (const ext of [
|
|
37
|
+
".ts",
|
|
38
|
+
".tsx",
|
|
39
|
+
".js",
|
|
40
|
+
".jsx"
|
|
41
|
+
]) {
|
|
42
|
+
const withExt = candidate + ext;
|
|
43
|
+
if (fs.existsSync(withExt)) return withExt;
|
|
44
|
+
}
|
|
45
|
+
for (const indexFile of [
|
|
46
|
+
"index.ts",
|
|
47
|
+
"index.tsx",
|
|
48
|
+
"index.js",
|
|
49
|
+
"index.jsx"
|
|
50
|
+
]) {
|
|
51
|
+
const indexPath = path.join(candidate, indexFile);
|
|
52
|
+
if (fs.existsSync(indexPath)) return indexPath;
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Whether a Vite alias entry's `find` matches an import specifier. Mirrors
|
|
58
|
+
* `@rollup/plugin-alias` (which Vite uses): RegExp `find` is tested; string
|
|
59
|
+
* `find` matches an exact specifier or a `find/`-prefixed one.
|
|
60
|
+
*/
|
|
61
|
+
function matchesAlias(find, importee) {
|
|
62
|
+
if (find instanceof RegExp) return find.test(importee);
|
|
63
|
+
if (importee.length < find.length) return false;
|
|
64
|
+
if (importee === find) return true;
|
|
65
|
+
return importee.startsWith(find) && importee[find.length] === "/";
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Resolve an import specifier through Vite's `resolve.alias` entries.
|
|
69
|
+
*
|
|
70
|
+
* Rewrites the specifier with the first matching alias (mirroring Vite/rollup
|
|
71
|
+
* semantics), then runs the shared extension / index-file probing to land on a
|
|
72
|
+
* real file. Returns `null` when no alias matches or nothing resolves to a real
|
|
73
|
+
* file. Replacements are expected to be absolute (Vite normalizes them so);
|
|
74
|
+
* non-absolute replacements are skipped rather than resolved against `cwd`.
|
|
75
|
+
*/
|
|
76
|
+
function applyAliases(importSource, aliases) {
|
|
77
|
+
if (!aliases || aliases.length === 0) return null;
|
|
78
|
+
for (const entry of aliases) {
|
|
79
|
+
if (!matchesAlias(entry.find, importSource)) continue;
|
|
80
|
+
const replaced = importSource.replace(entry.find, entry.replacement);
|
|
81
|
+
if (!path.isAbsolute(replaced)) continue;
|
|
82
|
+
const resolved = resolveFileCandidate(replaced);
|
|
83
|
+
if (resolved) return resolved;
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Cache of resolved `resolve.alias` arrays keyed by absolute project root, so a
|
|
89
|
+
* single CLI run doesn't reload the (relatively expensive) Vite config.
|
|
90
|
+
*/
|
|
91
|
+
const viteAliasCache = /* @__PURE__ */ new Map();
|
|
92
|
+
/**
|
|
93
|
+
* Load the project's Vite config and return its resolved `resolve.alias`
|
|
94
|
+
* entries (the source of truth for aliases). Cached per root. Returns `[]` when
|
|
95
|
+
* the config can't be loaded — callers then fall back to relative-only
|
|
96
|
+
* resolution, which never needs aliases anyway.
|
|
97
|
+
*/
|
|
98
|
+
async function loadViteAliases(root) {
|
|
99
|
+
const key = path.resolve(root);
|
|
100
|
+
const cached = viteAliasCache.get(key);
|
|
101
|
+
if (cached) return cached;
|
|
102
|
+
let aliases = [];
|
|
103
|
+
try {
|
|
104
|
+
const { resolveConfig } = await import("vite");
|
|
105
|
+
aliases = (await resolveConfig({
|
|
106
|
+
root: key,
|
|
107
|
+
configFile: void 0,
|
|
108
|
+
logLevel: "silent"
|
|
109
|
+
}, "build")).resolve.alias ?? [];
|
|
110
|
+
} catch (error) {
|
|
111
|
+
debugCli("Could not load Vite config for alias resolution in %s: %O", key, error);
|
|
112
|
+
aliases = [];
|
|
113
|
+
}
|
|
114
|
+
viteAliasCache.set(key, aliases);
|
|
115
|
+
return aliases;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
//#endregion
|
|
9
119
|
//#region src/vite/modules/discovery.ts
|
|
10
120
|
/**
|
|
11
121
|
* Tracks directories we've already warned about to avoid duplicate noise
|
|
@@ -49,34 +159,24 @@ function discoverDatabaseFiles(options) {
|
|
|
49
159
|
return results;
|
|
50
160
|
}
|
|
51
161
|
/**
|
|
52
|
-
* Resolve
|
|
162
|
+
* Resolve an import path to an absolute path.
|
|
53
163
|
*
|
|
54
|
-
*
|
|
164
|
+
* Relative imports (starting with `.`) are the fast path — resolved directly
|
|
165
|
+
* against the importing file's directory. Non-relative imports are resolved
|
|
166
|
+
* through the project's Vite `resolve.alias` entries (e.g. `@/databases/schema`,
|
|
167
|
+
* `~/foo`); bare package imports that match no alias resolve to `null`. Both
|
|
168
|
+
* paths use the same extension / index file probing.
|
|
169
|
+
*
|
|
170
|
+
* `aliases` is the resolved Vite alias array — the source of truth for aliases.
|
|
171
|
+
* Callers in a standalone (no-plugin) context obtain it via `loadViteAliases()`;
|
|
172
|
+
* when omitted, only relative imports resolve.
|
|
55
173
|
*/
|
|
56
|
-
function resolveImportPath(fromFile, importSource) {
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (fs.existsSync(resolved) && fs.statSync(resolved).isFile()) return resolved;
|
|
61
|
-
for (const ext of [
|
|
62
|
-
".ts",
|
|
63
|
-
".tsx",
|
|
64
|
-
".js",
|
|
65
|
-
".jsx"
|
|
66
|
-
]) {
|
|
67
|
-
const withExt = resolved + ext;
|
|
68
|
-
if (fs.existsSync(withExt)) return withExt;
|
|
69
|
-
}
|
|
70
|
-
for (const indexFile of [
|
|
71
|
-
"index.ts",
|
|
72
|
-
"index.tsx",
|
|
73
|
-
"index.js",
|
|
74
|
-
"index.jsx"
|
|
75
|
-
]) {
|
|
76
|
-
const indexPath = path.join(resolved, indexFile);
|
|
77
|
-
if (fs.existsSync(indexPath)) return indexPath;
|
|
174
|
+
function resolveImportPath(fromFile, importSource, aliases) {
|
|
175
|
+
if (importSource.startsWith(".")) {
|
|
176
|
+
const dir = path.dirname(fromFile);
|
|
177
|
+
return resolveFileCandidate(path.resolve(dir, importSource));
|
|
78
178
|
}
|
|
79
|
-
return
|
|
179
|
+
return applyAliases(importSource, aliases);
|
|
80
180
|
}
|
|
81
181
|
/**
|
|
82
182
|
* Read file contents
|
|
@@ -121,7 +221,7 @@ function generateCode(node) {
|
|
|
121
221
|
*
|
|
122
222
|
* This uses clean AST traversal - no regexes
|
|
123
223
|
*/
|
|
124
|
-
function parseDatabaseFile(filePath, code) {
|
|
224
|
+
function parseDatabaseFile(filePath, code, options = {}) {
|
|
125
225
|
const ast = parseCode(code);
|
|
126
226
|
const result = {
|
|
127
227
|
filePath,
|
|
@@ -141,7 +241,7 @@ function parseDatabaseFile(filePath, code) {
|
|
|
141
241
|
source,
|
|
142
242
|
imported
|
|
143
243
|
});
|
|
144
|
-
if (imported === "action" && source.startsWith(".")) actionFnName = localName;
|
|
244
|
+
if (imported === "action" && (source.startsWith(".") || applyAliases(source, options.aliases) !== null)) actionFnName = localName;
|
|
145
245
|
} else if (t.isImportDefaultSpecifier(specifier)) result.localImports.set(specifier.local.name, {
|
|
146
246
|
source,
|
|
147
247
|
imported: "default"
|
|
@@ -290,4 +390,4 @@ function toScreamingSnakeCase(str) {
|
|
|
290
390
|
}
|
|
291
391
|
|
|
292
392
|
//#endregion
|
|
293
|
-
export { resolveImportPath as a, readFile as i, parseDatabaseFile as n, discoverDatabaseFiles as r, BABEL_PARSER_PLUGINS as t };
|
|
393
|
+
export { resolveImportPath as a, readFile as i, parseDatabaseFile as n, loadViteAliases as o, discoverDatabaseFiles as r, BABEL_PARSER_PLUGINS as t };
|
|
@@ -2,7 +2,7 @@ import "./debug-DzvNVBs5.js";
|
|
|
2
2
|
import "./state-CMRjqCDw.js";
|
|
3
3
|
import "./snapshot-DMW6j2Ix.js";
|
|
4
4
|
import "./generator-BVwDQvKA.js";
|
|
5
|
-
import "./parser-
|
|
6
|
-
import { n as pushOne, t as push } from "./push-
|
|
5
|
+
import "./parser-B05psK1k.js";
|
|
6
|
+
import { n as pushOne, t as push } from "./push-gXPr9k-l.js";
|
|
7
7
|
|
|
8
8
|
export { push };
|
|
@@ -2,7 +2,7 @@ import { i as init_debug, t as debugCli } from "./debug-DzvNVBs5.js";
|
|
|
2
2
|
import { d as init_state, f as loadDevMigrations, h as saveDevMigration, n as clearDevMigrations } from "./state-CMRjqCDw.js";
|
|
3
3
|
import { n as generateMigrationStatements, o as snapshotsEqual, r as generateSnapshotFromSchema } from "./snapshot-DMW6j2Ix.js";
|
|
4
4
|
import { s as loadSnapshot, t as buildAndLoadSchema } from "./generator-BVwDQvKA.js";
|
|
5
|
-
import { a as resolveImportPath, i as readFile, n as parseDatabaseFile, r as discoverDatabaseFiles } from "./parser-
|
|
5
|
+
import { a as resolveImportPath, i as readFile, n as parseDatabaseFile, o as loadViteAliases, r as discoverDatabaseFiles } from "./parser-B05psK1k.js";
|
|
6
6
|
import * as path from "node:path";
|
|
7
7
|
import * as crypto from "node:crypto";
|
|
8
8
|
|
|
@@ -51,15 +51,21 @@ function discoverDatabases(opts) {
|
|
|
51
51
|
* missing exports) throws — silently skipping a database is how a typo in
|
|
52
52
|
* schema.ts becomes "why is my migration empty" 20 minutes later, or worse,
|
|
53
53
|
* a generated migration that DROPs tables that still exist in production.
|
|
54
|
+
*
|
|
55
|
+
* `projectRoot` enables alias resolution for non-relative schema imports
|
|
56
|
+
* (e.g. `@/databases/schema`): the project's Vite `resolve.alias` is loaded
|
|
57
|
+
* (cached) and used as the source of truth. Omit it to resolve relative
|
|
58
|
+
* imports only.
|
|
54
59
|
*/
|
|
55
|
-
async function loadSchema(db) {
|
|
60
|
+
async function loadSchema(db, projectRoot) {
|
|
56
61
|
if (db.schemaTableNames.length === 0) {
|
|
57
62
|
debugCli("Skipping %s: no schema defined", db.name);
|
|
58
63
|
return null;
|
|
59
64
|
}
|
|
60
65
|
if (!db.schemaImport) throw new Error(`[db] Database '${db.name}' declares ${db.schemaTableNames.length} schema table(s) (${db.schemaTableNames.join(", ")}) but none of them are imported from a schema module. Tables defined inline in ${path.basename(db.filePath)} cannot be loaded for migration generation — move them to a schema file (e.g. src/databases/schema.ts) and import them.`);
|
|
61
|
-
const
|
|
62
|
-
|
|
66
|
+
const aliases = projectRoot ? await loadViteAliases(projectRoot) : void 0;
|
|
67
|
+
const schemaPath = resolveImportPath(db.filePath, db.schemaImport, aliases);
|
|
68
|
+
if (!schemaPath) throw new Error(`[db] Database '${db.name}': could not resolve schema import '${db.schemaImport}' from ${db.filePath}.` + (db.schemaImport.startsWith(".") ? "" : ` '${db.schemaImport}' looks like an alias — ensure it's defined in the project's Vite config (resolve.alias / tsconfig paths wired into Vite).`));
|
|
63
69
|
let schema;
|
|
64
70
|
try {
|
|
65
71
|
schema = await buildAndLoadSchema(schemaPath, db.schemaTableNames);
|
|
@@ -143,7 +149,7 @@ async function pushDatabase(projectRoot, db) {
|
|
|
143
149
|
statements: [],
|
|
144
150
|
migrationName: null
|
|
145
151
|
};
|
|
146
|
-
const schema = await loadSchema(db);
|
|
152
|
+
const schema = await loadSchema(db, projectRoot);
|
|
147
153
|
if (!schema) return result;
|
|
148
154
|
const diff = await diffSchema(schema, db.migrationsDir);
|
|
149
155
|
if (!diff.hasChanges) {
|
package/dist/vite/durableDb.js
CHANGED
|
@@ -2,7 +2,7 @@ import { i as init_debug, r as debugVite } from "../debug-DzvNVBs5.js";
|
|
|
2
2
|
import { _ as saveDevState, c as getDevPaths, d as init_state, m as loadDevState } from "../state-CMRjqCDw.js";
|
|
3
3
|
import "../snapshot-DMW6j2Ix.js";
|
|
4
4
|
import { a as loadMigrationFiles } from "../generator-BVwDQvKA.js";
|
|
5
|
-
import { i as readFile, n as parseDatabaseFile, r as discoverDatabaseFiles, t as BABEL_PARSER_PLUGINS } from "../parser-
|
|
5
|
+
import { i as readFile, n as parseDatabaseFile, r as discoverDatabaseFiles, t as BABEL_PARSER_PLUGINS } from "../parser-B05psK1k.js";
|
|
6
6
|
import * as fs from "node:fs";
|
|
7
7
|
import * as path from "node:path";
|
|
8
8
|
import { parse } from "@babel/parser";
|
|
@@ -272,6 +272,7 @@ const generate = typeof _generate === "function" ? _generate : _generate.default
|
|
|
272
272
|
* is the identity function.
|
|
273
273
|
*/
|
|
274
274
|
const DEV_EPOCH_IMPORT = "virtual:durable-db/__devEpoch";
|
|
275
|
+
const ARKTYPE_LOCAL = "__ddType";
|
|
275
276
|
function parseProgram(code) {
|
|
276
277
|
return parse(code, {
|
|
277
278
|
sourceType: "module",
|
|
@@ -286,8 +287,17 @@ function parseExpression(code) {
|
|
|
286
287
|
if (t.isExpressionStatement(stmt)) return stmt.expression;
|
|
287
288
|
throw new Error(`Expected expression, got ${stmt?.type ?? "unknown"}`);
|
|
288
289
|
}
|
|
290
|
+
function asImportName(n) {
|
|
291
|
+
return typeof n === "string" ? {
|
|
292
|
+
imported: n,
|
|
293
|
+
local: n
|
|
294
|
+
} : n;
|
|
295
|
+
}
|
|
289
296
|
function createNamedImport(names, source) {
|
|
290
|
-
const specifiers = names.map((
|
|
297
|
+
const specifiers = names.map((n) => {
|
|
298
|
+
const { imported, local } = asImportName(n);
|
|
299
|
+
return t.importSpecifier(t.identifier(local), t.identifier(imported));
|
|
300
|
+
});
|
|
291
301
|
return t.importDeclaration(specifiers, t.stringLiteral(source));
|
|
292
302
|
}
|
|
293
303
|
function ensureNamedImports(body, source, names) {
|
|
@@ -299,7 +309,10 @@ function ensureNamedImports(body, source, names) {
|
|
|
299
309
|
return;
|
|
300
310
|
}
|
|
301
311
|
const have = new Set(existing.specifiers.filter((sp) => t.isImportSpecifier(sp)).map((sp) => sp.imported.name));
|
|
302
|
-
for (const n of names)
|
|
312
|
+
for (const n of names) {
|
|
313
|
+
const { imported, local } = asImportName(n);
|
|
314
|
+
if (!have.has(imported)) existing.specifiers.push(t.importSpecifier(t.identifier(local), t.identifier(imported)));
|
|
315
|
+
}
|
|
303
316
|
}
|
|
304
317
|
function buildMigrationsObject(migrations) {
|
|
305
318
|
if (!migrations || migrations.size === 0) return t.objectExpression([]);
|
|
@@ -571,7 +584,7 @@ function buildStubBodyStatements(config) {
|
|
|
571
584
|
const bindingExpr = t.memberExpression(t.identifier("env"), t.identifier(database.bindingName));
|
|
572
585
|
const instanceKeyExpr = buildInstanceKeyExpr(database);
|
|
573
586
|
const commonStatements = [
|
|
574
|
-
t.variableDeclaration("const", [t.variableDeclarator(t.identifier("argsSchema"), t.callExpression(t.identifier(
|
|
587
|
+
t.variableDeclaration("const", [t.variableDeclarator(t.identifier("argsSchema"), t.callExpression(t.identifier(ARKTYPE_LOCAL), [parseExpression(action.argsSchemaSource)]))]),
|
|
575
588
|
t.variableDeclaration("const", [t.variableDeclarator(t.identifier("validatedArgs"), t.callExpression(t.memberExpression(t.identifier("argsSchema"), t.identifier("assert")), [t.identifier("args")]))]),
|
|
576
589
|
...buildDoShortPath(config),
|
|
577
590
|
t.variableDeclaration("const", [t.variableDeclarator(t.identifier("instanceKey"), instanceKeyExpr)]),
|
|
@@ -603,7 +616,10 @@ function applyActionTransforms(body, ctx) {
|
|
|
603
616
|
"registerAction",
|
|
604
617
|
"getDoContext",
|
|
605
618
|
"callAction",
|
|
606
|
-
|
|
619
|
+
{
|
|
620
|
+
imported: "type",
|
|
621
|
+
local: ARKTYPE_LOCAL
|
|
622
|
+
}
|
|
607
623
|
]);
|
|
608
624
|
ensureNamedImports(body, DEV_EPOCH_IMPORT, ["applyDevEpoch"]);
|
|
609
625
|
if (database.transport === "websocket") ensureNamedImports(body, "durable-db/transport/websocket", ["WebSocketTransport"]);
|
|
@@ -626,7 +642,7 @@ function applyActionTransforms(body, ctx) {
|
|
|
626
642
|
const action = actionsInFile.find((a) => a.exportName === exportName);
|
|
627
643
|
const validatorId = t.identifier(`__validator_${exportName}`);
|
|
628
644
|
const handlerId = t.identifier(`__handler_${exportName}`);
|
|
629
|
-
replaced.push(t.variableDeclaration("const", [t.variableDeclarator(validatorId, t.callExpression(t.identifier(
|
|
645
|
+
replaced.push(t.variableDeclaration("const", [t.variableDeclarator(validatorId, t.callExpression(t.identifier(ARKTYPE_LOCAL), [parseExpression(action.argsSchemaSource)]))]));
|
|
630
646
|
replaced.push(t.variableDeclaration("const", [t.variableDeclarator(handlerId, parseExpression(action.handlerSource))]));
|
|
631
647
|
replaced.push(t.expressionStatement(t.callExpression(t.identifier("registerAction"), [
|
|
632
648
|
t.stringLiteral(dbName),
|
|
@@ -753,6 +769,14 @@ var PluginState = class {
|
|
|
753
769
|
projectRoot = "";
|
|
754
770
|
config;
|
|
755
771
|
devServer = null;
|
|
772
|
+
/**
|
|
773
|
+
* Vite's resolved `resolve.alias` entries (the source of truth for aliases,
|
|
774
|
+
* including tsconfig `paths` wired in by frameworks like rwsdk). Passed to the
|
|
775
|
+
* parser so an `action` factory imported through an alias is recognized.
|
|
776
|
+
*/
|
|
777
|
+
get aliases() {
|
|
778
|
+
return this.config?.resolve?.alias ?? [];
|
|
779
|
+
}
|
|
756
780
|
selfDevStateWriteUntil = 0;
|
|
757
781
|
constructor(options) {
|
|
758
782
|
this.options = options;
|
|
@@ -813,7 +837,7 @@ var PluginState = class {
|
|
|
813
837
|
});
|
|
814
838
|
for (const file of files) {
|
|
815
839
|
const code = readFile(file.absolutePath);
|
|
816
|
-
const parsed = parseDatabaseFile(file.absolutePath, code);
|
|
840
|
+
const parsed = parseDatabaseFile(file.absolutePath, code, { aliases: this.aliases });
|
|
817
841
|
if (parsed.database) {
|
|
818
842
|
parsed.database.migrationsDir = path.resolve(this.projectRoot, this.options.migrationsDir, parsed.database.name);
|
|
819
843
|
this.databases.set(parsed.database.name, parsed.database);
|
|
@@ -980,7 +1004,7 @@ function durableDb(options = {}) {
|
|
|
980
1004
|
if (dbNameForPath) {
|
|
981
1005
|
const database$1 = state.databases.get(dbNameForPath);
|
|
982
1006
|
if (!database$1) return null;
|
|
983
|
-
const parsed$1 = parseDatabaseFile(cleanId, code);
|
|
1007
|
+
const parsed$1 = parseDatabaseFile(cleanId, code, { aliases: state.aliases });
|
|
984
1008
|
for (const action of parsed$1.actions) state.registerAction(action, dbNameForPath, cleanId);
|
|
985
1009
|
const result$1 = transformDatabaseFile({
|
|
986
1010
|
code,
|
|
@@ -993,7 +1017,7 @@ function durableDb(options = {}) {
|
|
|
993
1017
|
if (result$1 && parsed$1.actions.length > 0 && state.config.command === "serve") state.invalidateDOModule();
|
|
994
1018
|
return result$1;
|
|
995
1019
|
}
|
|
996
|
-
const parsed = parseDatabaseFile(cleanId, code);
|
|
1020
|
+
const parsed = parseDatabaseFile(cleanId, code, { aliases: state.aliases });
|
|
997
1021
|
if (parsed.actions.length === 0) return null;
|
|
998
1022
|
const importedDbName = await findImportedDatabase(this.resolve.bind(this), state, parsed.localImports, cleanId);
|
|
999
1023
|
if (!importedDbName) return null;
|