durable-db 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1052 -0
- package/dist/cli/bin.d.ts +1 -0
- package/dist/cli/bin.js +18 -0
- package/dist/cli/index.d.ts +330 -0
- package/dist/cli/index.js +9 -0
- package/dist/cli-Cr9686BF.js +630 -0
- package/dist/context/index.d.ts +2 -0
- package/dist/context/index.js +5 -0
- package/dist/context-BuIiIQ6K.js +103 -0
- package/dist/db/index.d.ts +3 -0
- package/dist/db/index.js +760 -0
- package/dist/debug-BZJbKybf.js +49 -0
- package/dist/defineDatabase-CONf4xFC.js +54 -0
- package/dist/defineDatabase-jBv90-Pk.d.ts +188 -0
- package/dist/generator-BUPP9scJ.js +180 -0
- package/dist/index-BFM5d0FP.d.ts +328 -0
- package/dist/index-CqVMaMYY.d.ts +65 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +8 -0
- package/dist/migrations/generator.d.ts +101 -0
- package/dist/migrations/generator.js +5 -0
- package/dist/migrations/snapshot.d.ts +2 -0
- package/dist/migrations/snapshot.js +3 -0
- package/dist/parser-CbX54Flt.js +293 -0
- package/dist/protocol-g_plhPOY.js +35 -0
- package/dist/push-BgQ5cnfH.js +184 -0
- package/dist/push-BkkaslPd.js +8 -0
- package/dist/registry.d.ts +28 -0
- package/dist/registry.js +72 -0
- package/dist/schema-BOtrGnzM.js +8 -0
- package/dist/schema-CpzTOfZ6.d.ts +6 -0
- package/dist/schema.d.ts +2 -0
- package/dist/schema.js +3 -0
- package/dist/snapshot-C162qYoA.d.ts +125 -0
- package/dist/snapshot-D170nL7i.js +93 -0
- package/dist/state-BCcvNN-M.js +5 -0
- package/dist/state-FOEjpgJ6.js +203 -0
- package/dist/transport/protocol.d.ts +27 -0
- package/dist/transport/protocol.js +3 -0
- package/dist/transport/websocket.d.ts +31 -0
- package/dist/transport/websocket.js +3 -0
- package/dist/vite/durableDb.d.ts +20 -0
- package/dist/vite/durableDb.js +933 -0
- package/dist/websocket-Dx1fqwnF.js +104 -0
- package/package.json +114 -0
- package/virtual.d.ts +15 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { DrizzleSQLiteSnapshotJSON } from "drizzle-kit/api";
|
|
2
|
+
|
|
3
|
+
//#region src/migrations/snapshot.d.ts
|
|
4
|
+
type Snapshot = DrizzleSQLiteSnapshotJSON;
|
|
5
|
+
/**
|
|
6
|
+
* Create an empty snapshot (for initial migration from nothing)
|
|
7
|
+
*/
|
|
8
|
+
declare function createEmptySnapshot(): Snapshot;
|
|
9
|
+
/**
|
|
10
|
+
* Generate a unique snapshot ID based on timestamp
|
|
11
|
+
*/
|
|
12
|
+
declare function generateSnapshotId(): string;
|
|
13
|
+
/**
|
|
14
|
+
* Generate a snapshot from Drizzle schema objects
|
|
15
|
+
*
|
|
16
|
+
* @param schema - Object containing Drizzle table definitions
|
|
17
|
+
* @returns Snapshot representing the current schema state
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { users, posts } from './schema';
|
|
22
|
+
* const snapshot = await generateSnapshotFromSchema({ users, posts });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function generateSnapshotFromSchema(schema: Record<string, unknown>): Promise<{
|
|
26
|
+
tables: Record<string, {
|
|
27
|
+
name: string;
|
|
28
|
+
columns: Record<string, {
|
|
29
|
+
name: string;
|
|
30
|
+
type: string;
|
|
31
|
+
primaryKey: boolean;
|
|
32
|
+
notNull: boolean;
|
|
33
|
+
default?: any;
|
|
34
|
+
autoincrement?: boolean | undefined;
|
|
35
|
+
generated?: {
|
|
36
|
+
type: "stored" | "virtual";
|
|
37
|
+
as: string;
|
|
38
|
+
} | undefined;
|
|
39
|
+
}>;
|
|
40
|
+
indexes: Record<string, {
|
|
41
|
+
name: string;
|
|
42
|
+
columns: string[];
|
|
43
|
+
isUnique: boolean;
|
|
44
|
+
where?: string | undefined;
|
|
45
|
+
}>;
|
|
46
|
+
foreignKeys: Record<string, {
|
|
47
|
+
name: string;
|
|
48
|
+
tableFrom: string;
|
|
49
|
+
columnsFrom: string[];
|
|
50
|
+
tableTo: string;
|
|
51
|
+
columnsTo: string[];
|
|
52
|
+
onUpdate?: string | undefined;
|
|
53
|
+
onDelete?: string | undefined;
|
|
54
|
+
}>;
|
|
55
|
+
compositePrimaryKeys: Record<string, {
|
|
56
|
+
columns: string[];
|
|
57
|
+
name?: string | undefined;
|
|
58
|
+
}>;
|
|
59
|
+
uniqueConstraints: Record<string, {
|
|
60
|
+
name: string;
|
|
61
|
+
columns: string[];
|
|
62
|
+
}>;
|
|
63
|
+
checkConstraints: Record<string, {
|
|
64
|
+
name: string;
|
|
65
|
+
value: string;
|
|
66
|
+
}>;
|
|
67
|
+
}>;
|
|
68
|
+
id: string;
|
|
69
|
+
prevId: string;
|
|
70
|
+
version: "6";
|
|
71
|
+
dialect: "sqlite";
|
|
72
|
+
_meta: {
|
|
73
|
+
columns: Record<string, string>;
|
|
74
|
+
tables: Record<string, string>;
|
|
75
|
+
};
|
|
76
|
+
views: Record<string, {
|
|
77
|
+
name: string;
|
|
78
|
+
columns: Record<string, {
|
|
79
|
+
name: string;
|
|
80
|
+
type: string;
|
|
81
|
+
primaryKey: boolean;
|
|
82
|
+
notNull: boolean;
|
|
83
|
+
default?: any;
|
|
84
|
+
autoincrement?: boolean | undefined;
|
|
85
|
+
generated?: {
|
|
86
|
+
type: "stored" | "virtual";
|
|
87
|
+
as: string;
|
|
88
|
+
} | undefined;
|
|
89
|
+
}>;
|
|
90
|
+
isExisting: boolean;
|
|
91
|
+
definition?: string | undefined;
|
|
92
|
+
}>;
|
|
93
|
+
enums: {};
|
|
94
|
+
internal?: {
|
|
95
|
+
indexes?: Record<string, {
|
|
96
|
+
columns: Record<string, {
|
|
97
|
+
isExpression?: boolean | undefined;
|
|
98
|
+
} | undefined>;
|
|
99
|
+
} | undefined> | undefined;
|
|
100
|
+
} | undefined;
|
|
101
|
+
}>;
|
|
102
|
+
/**
|
|
103
|
+
* Generate SQL migration statements from two snapshots
|
|
104
|
+
*
|
|
105
|
+
* @param from - Previous schema state (or empty for initial migration)
|
|
106
|
+
* @param to - Target schema state
|
|
107
|
+
* @returns Array of SQL statements to migrate from `from` to `to`
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* const statements = await generateMigrationStatements(prevSnapshot, newSnapshot);
|
|
112
|
+
* // ['CREATE TABLE users (...)', 'CREATE INDEX ...']
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
declare function generateMigrationStatements(from: Snapshot, to: Snapshot): Promise<string[]>;
|
|
116
|
+
/**
|
|
117
|
+
* Check if two snapshots are equivalent (no migration needed)
|
|
118
|
+
*/
|
|
119
|
+
declare function snapshotsEqual(a: Snapshot, b: Snapshot): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Create a hash of a snapshot for quick comparison
|
|
122
|
+
*/
|
|
123
|
+
declare function hashSnapshot(snapshot: Snapshot): string;
|
|
124
|
+
//#endregion
|
|
125
|
+
export { generateSnapshotId as a, generateSnapshotFromSchema as i, createEmptySnapshot as n, hashSnapshot as o, generateMigrationStatements as r, snapshotsEqual as s, Snapshot as t };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
//#region src/migrations/snapshot.ts
|
|
4
|
+
const { generateSQLiteDrizzleJson, generateSQLiteMigration } = createRequire(import.meta.url)("drizzle-kit/api");
|
|
5
|
+
/**
|
|
6
|
+
* Create an empty snapshot (for initial migration from nothing)
|
|
7
|
+
*/
|
|
8
|
+
function createEmptySnapshot() {
|
|
9
|
+
return {
|
|
10
|
+
views: {},
|
|
11
|
+
version: "6",
|
|
12
|
+
dialect: "sqlite",
|
|
13
|
+
id: "0000000000000",
|
|
14
|
+
prevId: "",
|
|
15
|
+
tables: {},
|
|
16
|
+
enums: {},
|
|
17
|
+
_meta: {
|
|
18
|
+
tables: {},
|
|
19
|
+
columns: {}
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Generate a unique snapshot ID based on timestamp
|
|
25
|
+
*/
|
|
26
|
+
function generateSnapshotId() {
|
|
27
|
+
return Date.now().toString(36).padStart(13, "0");
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Generate a snapshot from Drizzle schema objects
|
|
31
|
+
*
|
|
32
|
+
* @param schema - Object containing Drizzle table definitions
|
|
33
|
+
* @returns Snapshot representing the current schema state
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { users, posts } from './schema';
|
|
38
|
+
* const snapshot = await generateSnapshotFromSchema({ users, posts });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
async function generateSnapshotFromSchema(schema) {
|
|
42
|
+
return generateSQLiteDrizzleJson(schema, void 0, "snake_case");
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Generate SQL migration statements from two snapshots
|
|
46
|
+
*
|
|
47
|
+
* @param from - Previous schema state (or empty for initial migration)
|
|
48
|
+
* @param to - Target schema state
|
|
49
|
+
* @returns Array of SQL statements to migrate from `from` to `to`
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const statements = await generateMigrationStatements(prevSnapshot, newSnapshot);
|
|
54
|
+
* // ['CREATE TABLE users (...)', 'CREATE INDEX ...']
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
async function generateMigrationStatements(from, to) {
|
|
58
|
+
return await generateSQLiteMigration(from, to);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Serialize the schema-relevant parts of a snapshot for comparison/hashing.
|
|
62
|
+
* Includes tables, views, and enums so that changes to any of them are
|
|
63
|
+
* detected — previously only `tables` was considered.
|
|
64
|
+
*/
|
|
65
|
+
function serializeSchemaShape(snapshot) {
|
|
66
|
+
return JSON.stringify({
|
|
67
|
+
tables: snapshot.tables ?? {},
|
|
68
|
+
views: snapshot.views ?? {},
|
|
69
|
+
enums: snapshot.enums ?? {}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Check if two snapshots are equivalent (no migration needed)
|
|
74
|
+
*/
|
|
75
|
+
function snapshotsEqual(a, b) {
|
|
76
|
+
return serializeSchemaShape(a) === serializeSchemaShape(b);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Create a hash of a snapshot for quick comparison
|
|
80
|
+
*/
|
|
81
|
+
function hashSnapshot(snapshot) {
|
|
82
|
+
const content = serializeSchemaShape(snapshot);
|
|
83
|
+
let hash = 0;
|
|
84
|
+
for (let i = 0; i < content.length; i++) {
|
|
85
|
+
const char = content.charCodeAt(i);
|
|
86
|
+
hash = (hash << 5) - hash + char;
|
|
87
|
+
hash = hash & hash;
|
|
88
|
+
}
|
|
89
|
+
return Math.abs(hash).toString(36);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
export { hashSnapshot as a, generateSnapshotId as i, generateMigrationStatements as n, snapshotsEqual as o, generateSnapshotFromSchema as r, createEmptySnapshot as t };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import "./debug-BZJbKybf.js";
|
|
2
|
+
import { _ as saveDevState, a as generateEpoch, c as getDevPaths, d as init_state, f as loadDevMigrations, g as saveDevSnapshot, h as saveDevMigration, i as findNodeModules, l as getInstanceKey, m as loadDevState, n as clearDevMigrations, o as getDatabaseDevState, p as loadDevSnapshot, r as createEmptyDevState, s as getDevEpoch, t as clearDatabaseDevState, u as hasProdSnapshotChanged } from "./state-FOEjpgJ6.js";
|
|
3
|
+
|
|
4
|
+
init_state();
|
|
5
|
+
export { loadDevMigrations };
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { a as __esmMin, i as init_debug, o as __export, t as debugCli } from "./debug-BZJbKybf.js";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
|
|
5
|
+
//#region src/cli/state.ts
|
|
6
|
+
var state_exports = /* @__PURE__ */ __export({
|
|
7
|
+
clearDatabaseDevState: () => clearDatabaseDevState,
|
|
8
|
+
clearDevMigrations: () => clearDevMigrations,
|
|
9
|
+
createEmptyDevState: () => createEmptyDevState,
|
|
10
|
+
findNodeModules: () => findNodeModules,
|
|
11
|
+
generateEpoch: () => generateEpoch,
|
|
12
|
+
getDatabaseDevState: () => getDatabaseDevState,
|
|
13
|
+
getDevEpoch: () => getDevEpoch,
|
|
14
|
+
getDevPaths: () => getDevPaths,
|
|
15
|
+
getInstanceKey: () => getInstanceKey,
|
|
16
|
+
hasProdSnapshotChanged: () => hasProdSnapshotChanged,
|
|
17
|
+
loadDevMigrations: () => loadDevMigrations,
|
|
18
|
+
loadDevSnapshot: () => loadDevSnapshot,
|
|
19
|
+
loadDevState: () => loadDevState,
|
|
20
|
+
saveDevMigration: () => saveDevMigration,
|
|
21
|
+
saveDevSnapshot: () => saveDevSnapshot,
|
|
22
|
+
saveDevState: () => saveDevState
|
|
23
|
+
});
|
|
24
|
+
/**
|
|
25
|
+
* Find the node_modules directory for the current project
|
|
26
|
+
*/
|
|
27
|
+
function findNodeModules(projectRoot) {
|
|
28
|
+
let current = projectRoot;
|
|
29
|
+
while (current !== path.dirname(current)) {
|
|
30
|
+
const nodeModules = path.join(current, "node_modules");
|
|
31
|
+
if (fs.existsSync(nodeModules)) return nodeModules;
|
|
32
|
+
current = path.dirname(current);
|
|
33
|
+
}
|
|
34
|
+
return path.join(projectRoot, "node_modules");
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get all dev-related paths for a project
|
|
38
|
+
*/
|
|
39
|
+
function getDevPaths(projectRoot) {
|
|
40
|
+
const nodeModules = findNodeModules(projectRoot);
|
|
41
|
+
const cacheDir = path.join(nodeModules, CACHE_DIR_NAME);
|
|
42
|
+
return {
|
|
43
|
+
cacheDir,
|
|
44
|
+
stateFile: path.join(cacheDir, STATE_FILE_NAME),
|
|
45
|
+
databaseDir: (dbName) => path.join(cacheDir, "databases", dbName),
|
|
46
|
+
migrationsDir: (dbName) => path.join(cacheDir, "databases", dbName, DEV_MIGRATIONS_DIR),
|
|
47
|
+
snapshotFile: (dbName) => path.join(cacheDir, "databases", dbName, DEV_SNAPSHOT_FILE)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create a new empty dev state
|
|
52
|
+
*/
|
|
53
|
+
function createEmptyDevState() {
|
|
54
|
+
return {
|
|
55
|
+
epoch: generateEpoch(),
|
|
56
|
+
databases: {}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Generate a new epoch value (timestamp-based)
|
|
61
|
+
*/
|
|
62
|
+
function generateEpoch() {
|
|
63
|
+
return Date.now().toString(36);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Load the dev state from disk
|
|
67
|
+
*/
|
|
68
|
+
function loadDevState(projectRoot) {
|
|
69
|
+
const paths = getDevPaths(projectRoot);
|
|
70
|
+
if (!fs.existsSync(paths.stateFile)) return createEmptyDevState();
|
|
71
|
+
try {
|
|
72
|
+
const content = fs.readFileSync(paths.stateFile, "utf-8");
|
|
73
|
+
return JSON.parse(content);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
debugCli("Failed to load dev state, starting fresh: %O", error);
|
|
76
|
+
return createEmptyDevState();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Save the dev state to disk
|
|
81
|
+
*/
|
|
82
|
+
function saveDevState(projectRoot, state) {
|
|
83
|
+
const paths = getDevPaths(projectRoot);
|
|
84
|
+
fs.mkdirSync(path.dirname(paths.stateFile), { recursive: true });
|
|
85
|
+
fs.writeFileSync(paths.stateFile, JSON.stringify(state, null, 2));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get or create database-specific dev state
|
|
89
|
+
*/
|
|
90
|
+
function getDatabaseDevState(state, dbName, prodSnapshotHash) {
|
|
91
|
+
if (!state.databases[dbName]) state.databases[dbName] = {
|
|
92
|
+
prodSnapshotHash,
|
|
93
|
+
lastPush: null
|
|
94
|
+
};
|
|
95
|
+
return state.databases[dbName];
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Check if prod snapshot has changed (meaning we need to reset dev state)
|
|
99
|
+
*/
|
|
100
|
+
function hasProdSnapshotChanged(state, dbName, currentProdHash) {
|
|
101
|
+
const dbState = state.databases[dbName];
|
|
102
|
+
if (!dbState) return false;
|
|
103
|
+
return dbState.prodSnapshotHash !== currentProdHash;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Load dev migrations for a database
|
|
107
|
+
*/
|
|
108
|
+
function loadDevMigrations(projectRoot, dbName) {
|
|
109
|
+
const migrationsDir = getDevPaths(projectRoot).migrationsDir(dbName);
|
|
110
|
+
if (!fs.existsSync(migrationsDir)) return /* @__PURE__ */ new Map();
|
|
111
|
+
const migrations = /* @__PURE__ */ new Map();
|
|
112
|
+
const files = fs.readdirSync(migrationsDir).filter((f) => f.endsWith(".sql")).sort();
|
|
113
|
+
for (const file of files) {
|
|
114
|
+
const name = file.replace(".sql", "");
|
|
115
|
+
const chunks = parseSqlMigration(fs.readFileSync(path.join(migrationsDir, file), "utf-8"));
|
|
116
|
+
if (chunks.length > 0) migrations.set(name, chunks);
|
|
117
|
+
}
|
|
118
|
+
return migrations;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Save a dev migration
|
|
122
|
+
*/
|
|
123
|
+
function saveDevMigration(projectRoot, dbName, name, statements) {
|
|
124
|
+
const migrationsDir = getDevPaths(projectRoot).migrationsDir(dbName);
|
|
125
|
+
fs.mkdirSync(migrationsDir, { recursive: true });
|
|
126
|
+
const filePath = path.join(migrationsDir, `${name}.sql`);
|
|
127
|
+
const content = statements.map((s) => s.endsWith(";") ? s : `${s};`).join("\n\n");
|
|
128
|
+
fs.writeFileSync(filePath, content);
|
|
129
|
+
return name;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Clear all dev migrations for a database
|
|
133
|
+
*/
|
|
134
|
+
function clearDevMigrations(projectRoot, dbName) {
|
|
135
|
+
const migrationsDir = getDevPaths(projectRoot).migrationsDir(dbName);
|
|
136
|
+
if (fs.existsSync(migrationsDir)) fs.rmSync(migrationsDir, {
|
|
137
|
+
recursive: true,
|
|
138
|
+
force: true
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Clear all dev state for a database
|
|
143
|
+
*/
|
|
144
|
+
function clearDatabaseDevState(projectRoot, dbName) {
|
|
145
|
+
const dbDir = getDevPaths(projectRoot).databaseDir(dbName);
|
|
146
|
+
if (fs.existsSync(dbDir)) fs.rmSync(dbDir, {
|
|
147
|
+
recursive: true,
|
|
148
|
+
force: true
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Load dev snapshot for a database
|
|
153
|
+
*/
|
|
154
|
+
function loadDevSnapshot(projectRoot, dbName) {
|
|
155
|
+
const snapshotFile = getDevPaths(projectRoot).snapshotFile(dbName);
|
|
156
|
+
if (!fs.existsSync(snapshotFile)) return null;
|
|
157
|
+
try {
|
|
158
|
+
const content = fs.readFileSync(snapshotFile, "utf-8");
|
|
159
|
+
return JSON.parse(content);
|
|
160
|
+
} catch {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Save dev snapshot for a database
|
|
166
|
+
*/
|
|
167
|
+
function saveDevSnapshot(projectRoot, dbName, snapshot) {
|
|
168
|
+
const snapshotFile = getDevPaths(projectRoot).snapshotFile(dbName);
|
|
169
|
+
fs.mkdirSync(path.dirname(snapshotFile), { recursive: true });
|
|
170
|
+
fs.writeFileSync(snapshotFile, JSON.stringify(snapshot, null, 2));
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Get the dev epoch for instance key suffixing
|
|
174
|
+
* Returns null in production or if no dev state exists
|
|
175
|
+
*/
|
|
176
|
+
function getDevEpoch(projectRoot) {
|
|
177
|
+
if (process.env.NODE_ENV === "production") return null;
|
|
178
|
+
return loadDevState(projectRoot).epoch;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Get a database instance key with optional dev epoch suffix
|
|
182
|
+
*/
|
|
183
|
+
function getInstanceKey(baseKey, epoch) {
|
|
184
|
+
if (!epoch) return baseKey;
|
|
185
|
+
return `${baseKey}__dev_${epoch}`;
|
|
186
|
+
}
|
|
187
|
+
function parseSqlMigration(content) {
|
|
188
|
+
return content.split(BREAKPOINT_MARKER).map((chunk) => {
|
|
189
|
+
return chunk.split("\n").filter((line) => !line.trim().startsWith("--")).join("\n").split(/;(?:\s*\n|\s*$)/).map((s) => s.trim()).filter((s) => s.length > 0);
|
|
190
|
+
}).filter((chunk) => chunk.length > 0);
|
|
191
|
+
}
|
|
192
|
+
var CACHE_DIR_NAME, STATE_FILE_NAME, DEV_MIGRATIONS_DIR, DEV_SNAPSHOT_FILE, BREAKPOINT_MARKER;
|
|
193
|
+
var init_state = __esmMin((() => {
|
|
194
|
+
init_debug();
|
|
195
|
+
CACHE_DIR_NAME = ".cache/durable-db";
|
|
196
|
+
STATE_FILE_NAME = "state.json";
|
|
197
|
+
DEV_MIGRATIONS_DIR = "migrations";
|
|
198
|
+
DEV_SNAPSHOT_FILE = "_snapshot.json";
|
|
199
|
+
BREAKPOINT_MARKER = /^-->\s*breakpoint\s*$/im;
|
|
200
|
+
}));
|
|
201
|
+
|
|
202
|
+
//#endregion
|
|
203
|
+
export { saveDevState as _, generateEpoch as a, getDevPaths as c, init_state as d, loadDevMigrations as f, saveDevSnapshot as g, saveDevMigration as h, findNodeModules as i, getInstanceKey as l, loadDevState as m, clearDevMigrations as n, getDatabaseDevState as o, loadDevSnapshot as p, createEmptyDevState as r, getDevEpoch as s, clearDatabaseDevState as t, hasProdSnapshotChanged as u, state_exports as v };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region src/transport/protocol.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* WebSocket message protocol for action calls.
|
|
4
|
+
*
|
|
5
|
+
* JSON text frames with request-response correlation via `id`.
|
|
6
|
+
*/
|
|
7
|
+
interface WsActionRequest {
|
|
8
|
+
id: string;
|
|
9
|
+
action: string;
|
|
10
|
+
args: unknown;
|
|
11
|
+
instanceKey: string;
|
|
12
|
+
}
|
|
13
|
+
type WsActionResponse = {
|
|
14
|
+
id: string;
|
|
15
|
+
ok: true;
|
|
16
|
+
result: unknown;
|
|
17
|
+
} | {
|
|
18
|
+
id: string;
|
|
19
|
+
ok: false;
|
|
20
|
+
error: string;
|
|
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 };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/transport/websocket.d.ts
|
|
2
|
+
/** Default timeout (ms) after which a pending WebSocket request rejects. */
|
|
3
|
+
declare const DEFAULT_WS_REQUEST_TIMEOUT_MS = 30000;
|
|
4
|
+
interface WebSocketTransportOptions {
|
|
5
|
+
/** Per-request timeout in ms. Set to 0 or Infinity to disable. */
|
|
6
|
+
requestTimeoutMs?: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Client-side WebSocket transport for calling DO actions over WebSocket.
|
|
10
|
+
*
|
|
11
|
+
* Manages a single WebSocket connection to a Durable Object stub,
|
|
12
|
+
* with request-response correlation via message IDs.
|
|
13
|
+
*/
|
|
14
|
+
declare class WebSocketTransport {
|
|
15
|
+
private stub;
|
|
16
|
+
private ws;
|
|
17
|
+
private pending;
|
|
18
|
+
private connectPromise;
|
|
19
|
+
private readonly requestTimeoutMs;
|
|
20
|
+
constructor(stub: {
|
|
21
|
+
fetch: (input: RequestInfo) => Promise<Response>;
|
|
22
|
+
}, options?: WebSocketTransportOptions);
|
|
23
|
+
call(action: string, args: unknown, instanceKey: string): Promise<unknown>;
|
|
24
|
+
private ensureConnected;
|
|
25
|
+
private connect;
|
|
26
|
+
private onMessage;
|
|
27
|
+
private onClose;
|
|
28
|
+
close(): void;
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { DEFAULT_WS_REQUEST_TIMEOUT_MS, WebSocketTransport, WebSocketTransportOptions };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
|
|
3
|
+
//#region src/vite/durableDb.d.ts
|
|
4
|
+
interface DurableDbOptions {
|
|
5
|
+
/** Import path for the context module. Default: 'durable-db/context' */
|
|
6
|
+
contextImport?: string;
|
|
7
|
+
/** Import path for the registry module. Default: 'durable-db/registry' */
|
|
8
|
+
registryImport?: string;
|
|
9
|
+
/** Directory containing database definitions. Default: 'src/databases' */
|
|
10
|
+
databasesDir?: string;
|
|
11
|
+
/** Directory for production migrations, relative to project root. Default: 'migrations' */
|
|
12
|
+
migrationsDir?: string;
|
|
13
|
+
}
|
|
14
|
+
declare function durableDb(options?: DurableDbOptions): Plugin;
|
|
15
|
+
/** @deprecated Renamed to {@link durableDb}. */
|
|
16
|
+
declare const databasePlugin: typeof durableDb;
|
|
17
|
+
/** @deprecated Renamed to {@link DurableDbOptions}. */
|
|
18
|
+
type DatabasePluginOptions = DurableDbOptions;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { DatabasePluginOptions, DurableDbOptions, databasePlugin, durableDb as default, durableDb };
|