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,328 @@
|
|
|
1
|
+
import { type as type$1 } from "arktype";
|
|
2
|
+
import { SQLiteTableWithColumns } from "drizzle-orm/sqlite-core";
|
|
3
|
+
import { DurableObject } from "cloudflare:workers";
|
|
4
|
+
import { CamelCasePlugin, CamelCasePlugin as CamelCasePlugin$1, CompiledQuery, Kysely, Kysely as Kysely$1, KyselyPlugin, KyselyPlugin as KyselyPlugin$1, PluginTransformQueryArgs, PluginTransformResultArgs, QueryResult, QueryResult as QueryResult$1, RootOperationNode, UnknownRow } from "kysely";
|
|
5
|
+
import { Table } from "drizzle-orm";
|
|
6
|
+
import { studio } from "@outerbase/browsable-durable-object";
|
|
7
|
+
|
|
8
|
+
//#region src/db/SqliteDurableObject.d.ts
|
|
9
|
+
/**
|
|
10
|
+
* SQL executor function type
|
|
11
|
+
*/
|
|
12
|
+
type SqlExecutor = (sql: string) => void;
|
|
13
|
+
/**
|
|
14
|
+
* Migration definition - SQL statements with optional breakpoints
|
|
15
|
+
*
|
|
16
|
+
* Each migration is an array of "chunks" - groups of statements separated by breakpoints.
|
|
17
|
+
* Each chunk is executed as a unit. This allows long migrations to be split into
|
|
18
|
+
* smaller pieces that can survive DO restarts.
|
|
19
|
+
*/
|
|
20
|
+
interface Migration {
|
|
21
|
+
/** SQL statement chunks (split by breakpoints) */
|
|
22
|
+
chunks: string[][];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Migrations object - keys are migration names (should sort alphabetically by timestamp)
|
|
26
|
+
*/
|
|
27
|
+
type Migrations = Record<string, Migration>;
|
|
28
|
+
/**
|
|
29
|
+
* Options for creating a Kysely instance
|
|
30
|
+
*/
|
|
31
|
+
interface CreateKyselyOptions {
|
|
32
|
+
/** Drizzle schema for plugin configuration */
|
|
33
|
+
schema?: Record<string, SQLiteTableWithColumns<any>>;
|
|
34
|
+
/** Additional Kysely plugins */
|
|
35
|
+
plugins?: KyselyPlugin[];
|
|
36
|
+
/** Whether to use CamelCasePlugin (default: true) */
|
|
37
|
+
camelCase?: boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Create a Kysely instance from Cloudflare SqlStorage
|
|
41
|
+
*
|
|
42
|
+
* @param sqlStorage - The Cloudflare SqlStorage instance
|
|
43
|
+
* @param options - Configuration options including schema for plugins
|
|
44
|
+
*/
|
|
45
|
+
declare function createKyselyFromSql<T>(sqlStorage: SqlStorage, options?: CreateKyselyOptions): Kysely<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Migration attempt info returned by getMigrationAttempts()
|
|
48
|
+
*/
|
|
49
|
+
interface MigrationAttemptInfo {
|
|
50
|
+
attemptCount: number;
|
|
51
|
+
lastAttemptAt: string | null;
|
|
52
|
+
lastError: string | null;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Consolidated migration status returned by getMigrationStatus().
|
|
56
|
+
* Combines attempt diagnostics with the DO's current pending/applied
|
|
57
|
+
* state so operators can answer "what's going on with this DO?" in a
|
|
58
|
+
* single call.
|
|
59
|
+
*/
|
|
60
|
+
interface MigrationStatus {
|
|
61
|
+
/** Attempt counter from __migration_attempts. */
|
|
62
|
+
attempts: MigrationAttemptInfo;
|
|
63
|
+
/** Names of migrations that exist in code but haven't been applied. */
|
|
64
|
+
pending: string[];
|
|
65
|
+
/** Names of migrations that have been applied (at least one chunk). */
|
|
66
|
+
applied: string[];
|
|
67
|
+
/** Whether the underlying storage supports PITR bookmarks. */
|
|
68
|
+
pitrAvailable: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Reason PITR is unavailable, populated from the error thrown by
|
|
71
|
+
* `getCurrentBookmark()` on first check. Null when PITR is available
|
|
72
|
+
* or hasn't been checked yet.
|
|
73
|
+
*/
|
|
74
|
+
pitrUnavailableReason: string | null;
|
|
75
|
+
/**
|
|
76
|
+
* Remaining PITR-protected retries before PITR is disabled. Clamped to
|
|
77
|
+
* 0; always 0 when pitrAvailable is false.
|
|
78
|
+
*/
|
|
79
|
+
pitrAttemptsRemaining: number;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Base class for SQLite Durable Objects with migration support
|
|
83
|
+
*
|
|
84
|
+
* Extend this class and set the `migrations` property to define your schema.
|
|
85
|
+
* Migrations run automatically when the DO is first accessed.
|
|
86
|
+
*
|
|
87
|
+
* Migrations are SQL-based with breakpoint support for long-running migrations.
|
|
88
|
+
* Breakpoints allow migrations to be resumed if the DO restarts mid-migration.
|
|
89
|
+
*
|
|
90
|
+
* When running on Cloudflare with PITR (Point-in-Time Recovery) support,
|
|
91
|
+
* migrations are protected with automatic snapshots. If a migration fails,
|
|
92
|
+
* the DO is restored to the pre-migration state and retried on next access.
|
|
93
|
+
* After {@link MAX_PITR_ATTEMPTS} consecutive failures, PITR is skipped and
|
|
94
|
+
* the error propagates so the developer can fix the migration and redeploy.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* import { SqliteDurableObject } from 'durable-db/db';
|
|
99
|
+
*
|
|
100
|
+
* export class MyDatabaseDO extends SqliteDurableObject {
|
|
101
|
+
* migrations = {
|
|
102
|
+
* '20240101000000_initial': {
|
|
103
|
+
* chunks: [
|
|
104
|
+
* // First chunk - create users table
|
|
105
|
+
* ['CREATE TABLE users (id TEXT PRIMARY KEY, name TEXT NOT NULL)'],
|
|
106
|
+
* // Second chunk (after breakpoint) - create posts table
|
|
107
|
+
* ['CREATE TABLE posts (id TEXT PRIMARY KEY, author_id TEXT REFERENCES users(id))'],
|
|
108
|
+
* ]
|
|
109
|
+
* }
|
|
110
|
+
* };
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
declare abstract class SqliteDurableObject<Env = unknown> extends DurableObject<Env> {
|
|
115
|
+
/**
|
|
116
|
+
* Override this with your migrations object
|
|
117
|
+
*/
|
|
118
|
+
abstract migrations: Migrations;
|
|
119
|
+
/**
|
|
120
|
+
* The Kysely database instance - available after migrations run
|
|
121
|
+
*/
|
|
122
|
+
protected db: Kysely<any>;
|
|
123
|
+
/**
|
|
124
|
+
* Raw SQLite storage access
|
|
125
|
+
*/
|
|
126
|
+
protected sql: SqlStorage;
|
|
127
|
+
/**
|
|
128
|
+
* Whether migrations have been run for this instance
|
|
129
|
+
*/
|
|
130
|
+
private migrationsApplied;
|
|
131
|
+
/**
|
|
132
|
+
* Cached result of PITR availability check (null = not yet checked)
|
|
133
|
+
*/
|
|
134
|
+
private pitrAvailable;
|
|
135
|
+
/**
|
|
136
|
+
* Reason PITR was determined unavailable, if known. Null when PITR is
|
|
137
|
+
* available or hasn't been checked yet. Surfaced via getMigrationStatus()
|
|
138
|
+
* so operators can distinguish "not supported by the runtime" from
|
|
139
|
+
* "unexpected error at detection time".
|
|
140
|
+
*/
|
|
141
|
+
private pitrUnavailableReason;
|
|
142
|
+
constructor(ctx: DurableObjectState, env: Env);
|
|
143
|
+
/**
|
|
144
|
+
* Check if PITR is available on this DO's storage.
|
|
145
|
+
* Result is cached after the first call.
|
|
146
|
+
*/
|
|
147
|
+
private checkPitrAvailable;
|
|
148
|
+
/**
|
|
149
|
+
* Reset the in-memory migration flag so that the next operation
|
|
150
|
+
* will re-run migrations. Used after `ctx.storage.deleteAll()` to
|
|
151
|
+
* ensure a fresh start.
|
|
152
|
+
*/
|
|
153
|
+
protected resetMigrationState(): void;
|
|
154
|
+
/**
|
|
155
|
+
* Ensure migrations are applied before any operation
|
|
156
|
+
*/
|
|
157
|
+
protected ensureMigrations(): Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Execute raw SQL statement
|
|
160
|
+
* Used by migrations for direct SQL execution
|
|
161
|
+
*/
|
|
162
|
+
protected execSql(sql: string): void;
|
|
163
|
+
/**
|
|
164
|
+
* Run pending migrations with PITR safety.
|
|
165
|
+
*
|
|
166
|
+
* Two-write strategy for retry guard:
|
|
167
|
+
* 1. WRITE 1: Increment retry counter in __migration_attempts
|
|
168
|
+
* 2. Take PITR bookmark (captures state including the counter)
|
|
169
|
+
* 3. WRITE 2: Apply the actual migrations
|
|
170
|
+
* On failure → restore to bookmark → counter value survives
|
|
171
|
+
* After MAX_PITR_ATTEMPTS, stop restoring and let the error propagate
|
|
172
|
+
*/
|
|
173
|
+
private runMigrations;
|
|
174
|
+
/**
|
|
175
|
+
* Collect pending migration chunks that haven't been applied yet.
|
|
176
|
+
*/
|
|
177
|
+
private collectPendingMigrations;
|
|
178
|
+
/**
|
|
179
|
+
* Apply a list of pending migration chunks.
|
|
180
|
+
*/
|
|
181
|
+
private applyMigrations;
|
|
182
|
+
/**
|
|
183
|
+
* Manually restore the DO to a specific PITR bookmark.
|
|
184
|
+
* The DO will restart on next access with the restored state.
|
|
185
|
+
*/
|
|
186
|
+
restoreToBookmark(bookmark: string): Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* Get the current PITR bookmark for diagnostic purposes.
|
|
189
|
+
* Returns null if PITR is not available.
|
|
190
|
+
*/
|
|
191
|
+
getMigrationBookmark(): Promise<string | null>;
|
|
192
|
+
/**
|
|
193
|
+
* Get migration attempt diagnostics.
|
|
194
|
+
* Returns the current retry counter and last error info.
|
|
195
|
+
*/
|
|
196
|
+
getMigrationAttempts(): MigrationAttemptInfo;
|
|
197
|
+
/**
|
|
198
|
+
* Read the last recorded migration error, or null if unavailable.
|
|
199
|
+
* Used for enriching PITR-exhausted log output.
|
|
200
|
+
*/
|
|
201
|
+
private readLastMigrationError;
|
|
202
|
+
/**
|
|
203
|
+
* Get a consolidated snapshot of migration state: attempts, pending and
|
|
204
|
+
* applied migration names, and PITR availability. Useful for building
|
|
205
|
+
* admin/debug endpoints without exposing internal SQL.
|
|
206
|
+
*/
|
|
207
|
+
getMigrationStatus(): Promise<MigrationStatus>;
|
|
208
|
+
/**
|
|
209
|
+
* Override fetch to ensure migrations run before any request.
|
|
210
|
+
* Handles WebSocket upgrade requests for websocket transport.
|
|
211
|
+
* Subclasses (e.g. Browsable-wrapped DOs) should call ensureMigrations()
|
|
212
|
+
* before delegating to super.fetch().
|
|
213
|
+
*/
|
|
214
|
+
fetch(request: Request): Promise<Response>;
|
|
215
|
+
/**
|
|
216
|
+
* Handle incoming WebSocket messages.
|
|
217
|
+
* Subclasses (generated DO classes) override this to dispatch actions.
|
|
218
|
+
*/
|
|
219
|
+
webSocketMessage(_ws: WebSocket, _message: string | ArrayBuffer): Promise<void>;
|
|
220
|
+
/**
|
|
221
|
+
* Handle WebSocket close. Echoes close back to the client.
|
|
222
|
+
*/
|
|
223
|
+
webSocketClose(ws: WebSocket, code: number, reason: string, _wasClean: boolean): Promise<void>;
|
|
224
|
+
/**
|
|
225
|
+
* Handle WebSocket error. Closes with error code.
|
|
226
|
+
*/
|
|
227
|
+
webSocketError(ws: WebSocket, _error: unknown): Promise<void>;
|
|
228
|
+
}
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/db/plugins.d.ts
|
|
231
|
+
/**
|
|
232
|
+
* Schema-aware CamelCasePlugin that uses Drizzle schema for column name mapping.
|
|
233
|
+
*
|
|
234
|
+
* Extends CamelCasePlugin to handle non-standard column names that don't follow
|
|
235
|
+
* simple camelCase ↔ snake_case conversion. For example, a JS property `userId`
|
|
236
|
+
* mapped to SQL column `user_identifier` would be handled correctly.
|
|
237
|
+
*
|
|
238
|
+
* Falls back to standard CamelCasePlugin behavior for columns not in the schema.
|
|
239
|
+
*/
|
|
240
|
+
declare class SchemaPlugin extends CamelCasePlugin {
|
|
241
|
+
private jsToSql;
|
|
242
|
+
private sqlToJs;
|
|
243
|
+
constructor(schema: Record<string, SQLiteTableWithColumns<any>>);
|
|
244
|
+
protected snakeCase(str: string): string;
|
|
245
|
+
protected camelCase(str: string): string;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Plugin that handles Drizzle's defaultFn and onUpdateFn during Kysely queries.
|
|
249
|
+
*
|
|
250
|
+
* On INSERT: auto-populates columns that have a defaultFn but weren't provided,
|
|
251
|
+
* and resolves DefaultInsertValueNode markers to actual values.
|
|
252
|
+
*
|
|
253
|
+
* On UPDATE: auto-adds column updates for columns with onUpdateFn that weren't
|
|
254
|
+
* already included in the SET clause.
|
|
255
|
+
*/
|
|
256
|
+
declare class DrizzleDefaultsPlugin implements KyselyPlugin {
|
|
257
|
+
private defaults;
|
|
258
|
+
constructor(drizzleSchemas: Record<string, Table>);
|
|
259
|
+
private transformInsertQuery;
|
|
260
|
+
private transformUpdateQuery;
|
|
261
|
+
transformQuery(args: PluginTransformQueryArgs): RootOperationNode;
|
|
262
|
+
transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>>;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Date serialization helpers for SQLite
|
|
266
|
+
* SQLite stores dates as TEXT in ISO format (without timezone)
|
|
267
|
+
*/
|
|
268
|
+
declare const dateSerializers: {
|
|
269
|
+
/**
|
|
270
|
+
* Serialize a Date to SQLite format
|
|
271
|
+
* Converts to ISO string, removes timezone info
|
|
272
|
+
*/
|
|
273
|
+
serialize(value: Date): string;
|
|
274
|
+
/**
|
|
275
|
+
* Deserialize a SQLite date string to Date
|
|
276
|
+
* Parses assuming UTC timezone
|
|
277
|
+
*/
|
|
278
|
+
deserialize(value: string): Date;
|
|
279
|
+
/**
|
|
280
|
+
* Check if a string looks like an ISO date
|
|
281
|
+
*/
|
|
282
|
+
isDateString(value: string): boolean;
|
|
283
|
+
/**
|
|
284
|
+
* Check if a string matches exactly the format produced by serialize()
|
|
285
|
+
* (and SQLite's CURRENT_TIMESTAMP): `YYYY-MM-DD HH:MM:SS`.
|
|
286
|
+
*
|
|
287
|
+
* The read path only deserializes strings in this exact format. Anything
|
|
288
|
+
* else — full ISO strings with a 'T' separator, timezone, or milliseconds —
|
|
289
|
+
* was stored by the user as a plain string and must round-trip verbatim.
|
|
290
|
+
*/
|
|
291
|
+
isSerializedDateString(value: string): boolean;
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* Plugin that serializes/deserializes Date objects for SQLite
|
|
295
|
+
*
|
|
296
|
+
* On query: Converts Date objects to ISO strings
|
|
297
|
+
* On result: Converts stored date strings back to Date objects — but only
|
|
298
|
+
* for values in the exact format the write path produces, and (when a schema
|
|
299
|
+
* is provided) only for columns Drizzle declares as date-typed. Plain text
|
|
300
|
+
* columns holding date-looking strings round-trip verbatim.
|
|
301
|
+
*/
|
|
302
|
+
declare class DateSerializePlugin implements KyselyPlugin {
|
|
303
|
+
/**
|
|
304
|
+
* Names of date-typed columns (SQL and JS forms) when constructed with a
|
|
305
|
+
* schema; null means "no schema available", which falls back to matching
|
|
306
|
+
* on value format alone.
|
|
307
|
+
*/
|
|
308
|
+
private dateColumns;
|
|
309
|
+
constructor(schema?: Record<string, Table>);
|
|
310
|
+
transformQuery(args: PluginTransformQueryArgs): RootOperationNode;
|
|
311
|
+
private transformNode;
|
|
312
|
+
transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>>;
|
|
313
|
+
private transformRow;
|
|
314
|
+
private shouldDeserialize;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Create all recommended plugins for use with Drizzle schemas.
|
|
318
|
+
*
|
|
319
|
+
* Includes:
|
|
320
|
+
* - DrizzleDefaultsPlugin: auto-populates defaultFn/onUpdateFn values
|
|
321
|
+
* - SchemaPlugin (extends CamelCasePlugin): schema-aware camelCase ↔ snake_case
|
|
322
|
+
* - DateSerializePlugin: Date ↔ SQLite text serialization
|
|
323
|
+
*
|
|
324
|
+
* When camelCase is false, SchemaPlugin is omitted.
|
|
325
|
+
*/
|
|
326
|
+
declare function createDrizzlePlugins(schema: Record<string, SQLiteTableWithColumns<any>>, camelCase?: boolean): KyselyPlugin[];
|
|
327
|
+
//#endregion
|
|
328
|
+
export { SqlExecutor as _, QueryResult$1 as a, DateSerializePlugin as c, createDrizzlePlugins as d, dateSerializers as f, Migrations as g, MigrationAttemptInfo as h, KyselyPlugin$1 as i, DrizzleDefaultsPlugin as l, Migration as m, CompiledQuery as n, studio as o, CreateKyselyOptions as p, Kysely$1 as r, type$1 as s, CamelCasePlugin$1 as t, SchemaPlugin as u, SqliteDurableObject as v, createKyselyFromSql as y };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
//#region src/context/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Configure a tenant ID resolver.
|
|
4
|
+
*
|
|
5
|
+
* The resolver is called at the moment a database operation needs the tenant ID —
|
|
6
|
+
* by which point request middleware (auth, session, etc.) has already completed.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { setTenantIdResolver } from 'durable-db/context';
|
|
11
|
+
* import { getRequestInfo } from 'rwsdk/worker';
|
|
12
|
+
*
|
|
13
|
+
* setTenantIdResolver(() => getRequestInfo().ctx.session!.shop);
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
declare function setTenantIdResolver(resolver: (() => string) | null): void;
|
|
17
|
+
/**
|
|
18
|
+
* Get the current tenant ID from the configured resolver.
|
|
19
|
+
*
|
|
20
|
+
* @throws Error if no resolver is configured
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { getTenantId } from 'durable-db/context';
|
|
25
|
+
*
|
|
26
|
+
* async function myHandler() {
|
|
27
|
+
* const tenantId = getTenantId();
|
|
28
|
+
* // ...
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
declare function getTenantId(): string;
|
|
33
|
+
/**
|
|
34
|
+
* Check if a tenant ID resolver is configured.
|
|
35
|
+
*/
|
|
36
|
+
declare function hasTenantId(): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Get the dev epoch for instance key suffixing
|
|
39
|
+
* Only used in development to allow database resets
|
|
40
|
+
*
|
|
41
|
+
* Returns null in production or if no dev state exists
|
|
42
|
+
*
|
|
43
|
+
* @deprecated The epoch is now baked into generated stubs at build time via
|
|
44
|
+
* the `virtual:durable-db/__devEpoch` module — this function
|
|
45
|
+
* cannot read the dev state inside workerd (no filesystem) and returns null
|
|
46
|
+
* there. Import `devEpoch` / `applyDevEpoch` from the virtual module instead.
|
|
47
|
+
*/
|
|
48
|
+
declare function getDevInstanceKeySuffix(): string | null;
|
|
49
|
+
/**
|
|
50
|
+
* Get a database instance key, optionally suffixed with dev epoch
|
|
51
|
+
*
|
|
52
|
+
* In development, this appends an epoch suffix to enable database resets.
|
|
53
|
+
* In production, this returns the base key unchanged.
|
|
54
|
+
*
|
|
55
|
+
* @deprecated Generated stubs apply the epoch automatically via
|
|
56
|
+
* `applyDevEpoch` from `virtual:durable-db/__devEpoch`. This
|
|
57
|
+
* function only works in Node (it reads the dev state from disk) and is the
|
|
58
|
+
* identity inside workerd — combining it with the generated stubs would
|
|
59
|
+
* also double-suffix keys. Import from the virtual module instead.
|
|
60
|
+
*
|
|
61
|
+
* @param baseKey - The base instance key (e.g., tenant ID or 'global')
|
|
62
|
+
*/
|
|
63
|
+
declare function getInstanceKey(baseKey: string): string;
|
|
64
|
+
//#endregion
|
|
65
|
+
export { setTenantIdResolver as a, hasTenantId as i, getInstanceKey as n, getTenantId as r, getDevInstanceKeySuffix as t };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { c as DatabaseDefinition, d as InferArgs, i as ActionContext, n as Action, r as ActionConfig, s as DatabaseConfig, t as defineDatabase, u as DrizzleToKysely } from "./defineDatabase-jBv90-Pk.js";
|
|
2
|
+
import { a as setTenantIdResolver, i as hasTenantId, r as getTenantId } from "./index-CqVMaMYY.js";
|
|
3
|
+
import { a as integer, c as real, d as uniqueIndex, i as index, l as table, n as blob, o as numeric, r as foreignKey, s as primaryKey, t as AnySQLiteColumn, u as text } from "./schema-CpzTOfZ6.js";
|
|
4
|
+
import { type } from "arktype";
|
|
5
|
+
import { Kysely } from "kysely";
|
|
6
|
+
export { type Action, type ActionConfig, type ActionContext, type AnySQLiteColumn, type DatabaseConfig, type DatabaseDefinition, type DrizzleToKysely, type InferArgs, type Kysely, blob, defineDatabase, foreignKey, getTenantId, hasTenantId, index, integer, numeric, primaryKey, real, setTenantIdResolver, table, text, type, uniqueIndex };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import "./debug-BZJbKybf.js";
|
|
2
|
+
import { t as defineDatabase } from "./defineDatabase-CONf4xFC.js";
|
|
3
|
+
import { a as numeric, c as table, i as integer, l as text, n as foreignKey, o as primaryKey, r as index, s as real, t as blob, u as uniqueIndex } from "./schema-BOtrGnzM.js";
|
|
4
|
+
import "./state-FOEjpgJ6.js";
|
|
5
|
+
import { a as setTenantIdResolver, i as hasTenantId, r as getTenantId } from "./context-BuIiIQ6K.js";
|
|
6
|
+
import { type } from "arktype";
|
|
7
|
+
|
|
8
|
+
export { blob, defineDatabase, foreignKey, getTenantId, hasTenantId, index, integer, numeric, primaryKey, real, setTenantIdResolver, table, text, type, uniqueIndex };
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { t as Snapshot } from "../snapshot-C162qYoA.js";
|
|
2
|
+
|
|
3
|
+
//#region src/migrations/generator.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Result of migration generation
|
|
7
|
+
*/
|
|
8
|
+
interface MigrationResult {
|
|
9
|
+
/** Whether changes were detected */
|
|
10
|
+
hasChanges: boolean;
|
|
11
|
+
/** Generated SQL statements */
|
|
12
|
+
statements: string[];
|
|
13
|
+
/** Migration name/identifier */
|
|
14
|
+
migrationName: string | null;
|
|
15
|
+
/** New snapshot (after migration) */
|
|
16
|
+
snapshot: Snapshot;
|
|
17
|
+
/** Previous snapshot (before migration) */
|
|
18
|
+
previousSnapshot: Snapshot;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Options for migration generation
|
|
22
|
+
*/
|
|
23
|
+
interface GenerateMigrationOptions {
|
|
24
|
+
/** Directory to store migrations and snapshots */
|
|
25
|
+
migrationsDir: string;
|
|
26
|
+
/** Drizzle schema objects */
|
|
27
|
+
schema: Record<string, unknown>;
|
|
28
|
+
/** Optional custom migration name */
|
|
29
|
+
migrationName?: string;
|
|
30
|
+
/** Whether to write files (default: true) */
|
|
31
|
+
write?: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Load the previous snapshot from the migrations directory
|
|
35
|
+
*/
|
|
36
|
+
declare function loadSnapshot(migrationsDir: string): Snapshot;
|
|
37
|
+
/**
|
|
38
|
+
* Save a snapshot to the migrations directory
|
|
39
|
+
*/
|
|
40
|
+
declare function saveSnapshot(migrationsDir: string, snapshot: Snapshot): void;
|
|
41
|
+
/**
|
|
42
|
+
* Generate a timestamp-based migration name
|
|
43
|
+
*/
|
|
44
|
+
declare function generateMigrationName(suffix?: string): string;
|
|
45
|
+
/**
|
|
46
|
+
* Generate a migration from schema changes
|
|
47
|
+
*
|
|
48
|
+
* This compares the current schema to the stored snapshot and generates
|
|
49
|
+
* the necessary SQL statements to migrate the database.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { users, posts } from './schema';
|
|
54
|
+
*
|
|
55
|
+
* const result = await generateMigration({
|
|
56
|
+
* migrationsDir: './migrations',
|
|
57
|
+
* schema: { users, posts },
|
|
58
|
+
* });
|
|
59
|
+
*
|
|
60
|
+
* if (result.hasChanges) {
|
|
61
|
+
* console.log('Migration generated:', result.migrationName);
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
declare function generateMigration(options: GenerateMigrationOptions): Promise<MigrationResult>;
|
|
66
|
+
/**
|
|
67
|
+
* Parse SQL content into chunks (split by breakpoints) and statements
|
|
68
|
+
*
|
|
69
|
+
* @param content - Raw SQL file content
|
|
70
|
+
* @returns Array of chunks, where each chunk is an array of SQL statements
|
|
71
|
+
*/
|
|
72
|
+
declare function parseSqlMigration(content: string): string[][];
|
|
73
|
+
/**
|
|
74
|
+
* Migration data structure for embedding in generated code
|
|
75
|
+
*/
|
|
76
|
+
interface MigrationData {
|
|
77
|
+
name: string;
|
|
78
|
+
chunks: string[][];
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Load all migration SQL files from a directory
|
|
82
|
+
* Returns migrations sorted by name (timestamp order) with breakpoint parsing
|
|
83
|
+
*/
|
|
84
|
+
declare function loadMigrationFiles(migrationsDir: string): Map<string, string[][]>;
|
|
85
|
+
/**
|
|
86
|
+
* Load a schema module using dynamic import
|
|
87
|
+
* Works with both ESM and CJS modules
|
|
88
|
+
*/
|
|
89
|
+
declare function loadSchemaModule(schemaPath: string, tableNames: string[]): Promise<Record<string, unknown>>;
|
|
90
|
+
/**
|
|
91
|
+
* Build schema module with esbuild and return the exports
|
|
92
|
+
* This is useful when the schema is TypeScript and needs transpilation
|
|
93
|
+
*/
|
|
94
|
+
declare function buildAndLoadSchema(schemaPath: string, tableNames: string[]): Promise<Record<string, unknown>>;
|
|
95
|
+
/**
|
|
96
|
+
* Generate SQL to create all tables from scratch
|
|
97
|
+
* Useful for initial setup or testing
|
|
98
|
+
*/
|
|
99
|
+
declare function generateCreateTableSQL(schema: Record<string, unknown>): Promise<string[]>;
|
|
100
|
+
//#endregion
|
|
101
|
+
export { GenerateMigrationOptions, MigrationData, MigrationResult, buildAndLoadSchema, generateCreateTableSQL, generateMigration, generateMigrationName, loadMigrationFiles, loadSchemaModule, loadSnapshot, parseSqlMigration, saveSnapshot };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import "../debug-BZJbKybf.js";
|
|
2
|
+
import "../snapshot-D170nL7i.js";
|
|
3
|
+
import { a as loadMigrationFiles, c as parseSqlMigration, i as generateMigrationName, l as saveSnapshot, n as generateCreateTableSQL, o as loadSchemaModule, r as generateMigration, s as loadSnapshot, t as buildAndLoadSchema } from "../generator-BUPP9scJ.js";
|
|
4
|
+
|
|
5
|
+
export { buildAndLoadSchema, generateCreateTableSQL, generateMigration, generateMigrationName, loadMigrationFiles, loadSchemaModule, loadSnapshot, parseSqlMigration, saveSnapshot };
|
|
@@ -0,0 +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-C162qYoA.js";
|
|
2
|
+
export { Snapshot, createEmptySnapshot, generateMigrationStatements, generateSnapshotFromSchema, generateSnapshotId, hashSnapshot, snapshotsEqual };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { a as hashSnapshot, i as generateSnapshotId, n as generateMigrationStatements, o as snapshotsEqual, r as generateSnapshotFromSchema, t as createEmptySnapshot } from "../snapshot-D170nL7i.js";
|
|
2
|
+
|
|
3
|
+
export { createEmptySnapshot, generateMigrationStatements, generateSnapshotFromSchema, generateSnapshotId, hashSnapshot, snapshotsEqual };
|