alchemy 0.15.10 → 0.15.11
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/lib/cloudflare/d1-clone.d.ts +55 -0
- package/lib/cloudflare/d1-clone.d.ts.map +1 -0
- package/lib/cloudflare/d1-clone.js +45 -0
- package/lib/cloudflare/d1-clone.js.map +1 -0
- package/lib/cloudflare/d1-database.d.ts +36 -1
- package/lib/cloudflare/d1-database.d.ts.map +1 -1
- package/lib/cloudflare/d1-database.js +73 -9
- package/lib/cloudflare/d1-database.js.map +1 -1
- package/lib/cloudflare/d1-export.d.ts +63 -0
- package/lib/cloudflare/d1-export.d.ts.map +1 -0
- package/lib/cloudflare/d1-export.js +50 -0
- package/lib/cloudflare/d1-export.js.map +1 -0
- package/lib/cloudflare/d1-import.d.ts +112 -0
- package/lib/cloudflare/d1-import.d.ts.map +1 -0
- package/lib/cloudflare/d1-import.js +143 -0
- package/lib/cloudflare/d1-import.js.map +1 -0
- package/lib/cloudflare/index.d.ts +3 -0
- package/lib/cloudflare/index.d.ts.map +1 -1
- package/lib/cloudflare/index.js +3 -0
- package/lib/cloudflare/index.js.map +1 -1
- package/lib/util/sleep.d.ts +7 -0
- package/lib/util/sleep.d.ts.map +1 -0
- package/lib/util/sleep.js +9 -0
- package/lib/util/sleep.js.map +1 -0
- package/package.json +1 -1
- package/src/cloudflare/d1-clone.ts +113 -0
- package/src/cloudflare/d1-database.ts +99 -15
- package/src/cloudflare/d1-export.ts +173 -0
- package/src/cloudflare/d1-import.ts +406 -0
- package/src/cloudflare/index.ts +3 -0
- package/src/util/sleep.ts +8 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { CloudflareApi } from "./api.js";
|
|
2
|
+
import { type ExportD1DatabaseOptions, type ExportD1DatabaseResult } from "./d1-export.js";
|
|
3
|
+
import { type ImportD1DatabaseOptions, type ImportD1DatabaseResult } from "./d1-import.js";
|
|
4
|
+
/**
|
|
5
|
+
* Options for cloning a D1 database
|
|
6
|
+
*/
|
|
7
|
+
export interface CloneD1DatabaseOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The ID of the source D1 database to clone from
|
|
10
|
+
*/
|
|
11
|
+
sourceDatabaseId: string;
|
|
12
|
+
/**
|
|
13
|
+
* The ID of the target D1 database to clone to
|
|
14
|
+
*/
|
|
15
|
+
targetDatabaseId: string;
|
|
16
|
+
/**
|
|
17
|
+
* Optional export options for the source database
|
|
18
|
+
*/
|
|
19
|
+
exportOptions?: Omit<ExportD1DatabaseOptions, "databaseId">;
|
|
20
|
+
/**
|
|
21
|
+
* Optional import options for the target database
|
|
22
|
+
*/
|
|
23
|
+
importOptions?: Omit<ImportD1DatabaseOptions, "databaseId" | "sqlData">;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Response from the clone D1 database operation
|
|
27
|
+
*/
|
|
28
|
+
export interface CloneD1DatabaseResult {
|
|
29
|
+
/**
|
|
30
|
+
* Result of the export operation
|
|
31
|
+
*/
|
|
32
|
+
exportResult: ExportD1DatabaseResult;
|
|
33
|
+
/**
|
|
34
|
+
* Result of the import operation
|
|
35
|
+
*/
|
|
36
|
+
importResult: ImportD1DatabaseResult;
|
|
37
|
+
/**
|
|
38
|
+
* Whether the clone operation was successful
|
|
39
|
+
*/
|
|
40
|
+
success: boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Clones a D1 database by exporting from a source database and importing to a target database.
|
|
44
|
+
* Handles the full workflow:
|
|
45
|
+
* 1. Export source database to get a signed URL
|
|
46
|
+
* 2. Fetch SQL content from the signed URL
|
|
47
|
+
* 3. Import SQL content into the target database
|
|
48
|
+
*
|
|
49
|
+
* @param api The CloudflareApi instance to use for requests
|
|
50
|
+
* @param options Options including source and target database IDs
|
|
51
|
+
* @returns An object containing results from both export and import operations
|
|
52
|
+
* @throws Will throw an error if any part of the clone process fails
|
|
53
|
+
*/
|
|
54
|
+
export declare function cloneD1Database(api: CloudflareApi, options: CloneD1DatabaseOptions): Promise<CloneD1DatabaseResult>;
|
|
55
|
+
//# sourceMappingURL=d1-clone.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"d1-clone.d.ts","sourceRoot":"","sources":["../../src/cloudflare/d1-clone.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9C,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,sBAAsB,EAC5B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,sBAAsB,EAC5B,MAAM,gBAAgB,CAAC;AAExB;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,gBAAgB,EAAE,MAAM,CAAC;IAEzB;;OAEG;IACH,gBAAgB,EAAE,MAAM,CAAC;IAEzB;;OAEG;IACH,aAAa,CAAC,EAAE,IAAI,CAAC,uBAAuB,EAAE,YAAY,CAAC,CAAC;IAE5D;;OAEG;IACH,aAAa,CAAC,EAAE,IAAI,CAAC,uBAAuB,EAAE,YAAY,GAAG,SAAS,CAAC,CAAC;CACzE;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,YAAY,EAAE,sBAAsB,CAAC;IAErC;;OAEG;IACH,YAAY,EAAE,sBAAsB,CAAC;IAErC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,eAAe,CACnC,GAAG,EAAE,aAAa,EAClB,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,qBAAqB,CAAC,CAwChC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { exportD1Database, } from "./d1-export.js";
|
|
2
|
+
import { importD1Database, } from "./d1-import.js";
|
|
3
|
+
/**
|
|
4
|
+
* Clones a D1 database by exporting from a source database and importing to a target database.
|
|
5
|
+
* Handles the full workflow:
|
|
6
|
+
* 1. Export source database to get a signed URL
|
|
7
|
+
* 2. Fetch SQL content from the signed URL
|
|
8
|
+
* 3. Import SQL content into the target database
|
|
9
|
+
*
|
|
10
|
+
* @param api The CloudflareApi instance to use for requests
|
|
11
|
+
* @param options Options including source and target database IDs
|
|
12
|
+
* @returns An object containing results from both export and import operations
|
|
13
|
+
* @throws Will throw an error if any part of the clone process fails
|
|
14
|
+
*/
|
|
15
|
+
export async function cloneD1Database(api, options) {
|
|
16
|
+
const exportResult = await exportD1Database(api, {
|
|
17
|
+
databaseId: options.sourceDatabaseId,
|
|
18
|
+
...options.exportOptions,
|
|
19
|
+
});
|
|
20
|
+
if (!exportResult.success) {
|
|
21
|
+
throw new Error(`Failed to export source database: ${options.sourceDatabaseId}`);
|
|
22
|
+
}
|
|
23
|
+
// Step 2: Fetch SQL content from the signed URL
|
|
24
|
+
const sqlResponse = await fetch(exportResult.signed_url);
|
|
25
|
+
if (!sqlResponse.ok) {
|
|
26
|
+
throw new Error(`Failed to fetch SQL content: ${sqlResponse.statusText}`);
|
|
27
|
+
}
|
|
28
|
+
const sqlData = await sqlResponse.text();
|
|
29
|
+
// Step 3: Import SQL content into target database
|
|
30
|
+
const importResult = await importD1Database(api, {
|
|
31
|
+
databaseId: options.targetDatabaseId,
|
|
32
|
+
sqlData,
|
|
33
|
+
filename: exportResult.filename,
|
|
34
|
+
...options.importOptions,
|
|
35
|
+
});
|
|
36
|
+
if (!importResult.success) {
|
|
37
|
+
throw new Error(`Failed to import to target database: ${options.targetDatabaseId}`);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
exportResult,
|
|
41
|
+
importResult,
|
|
42
|
+
success: true,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=d1-clone.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"d1-clone.js","sourceRoot":"","sources":["../../src/cloudflare/d1-clone.ts"],"names":[],"mappings":"AACA,OAAO,EACL,gBAAgB,GAGjB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
|
|
43
43
|
*/
|
|
44
44
|
adopt?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Clone data from an existing database to this new database.
|
|
47
|
+
* Only applicable during creation phase.
|
|
48
|
+
*
|
|
49
|
+
* Can be specified as:
|
|
50
|
+
* - A D1Database object
|
|
51
|
+
* - An object with an id property
|
|
52
|
+
* - An object with a name property (will look up the ID by name)
|
|
53
|
+
*/
|
|
54
|
+
clone?: D1Database | {
|
|
55
|
+
id: string;
|
|
56
|
+
} | {
|
|
57
|
+
name: string;
|
|
58
|
+
};
|
|
45
59
|
/**
|
|
46
60
|
* These files will be generated internally with the D1Database wrapper function when migrationsDir is specified
|
|
47
61
|
*
|
|
@@ -97,7 +111,6 @@ export type D1Database = Resource<"cloudflare::D1Database"> & D1DatabaseProps &
|
|
|
97
111
|
mode: "auto" | "disabled";
|
|
98
112
|
};
|
|
99
113
|
};
|
|
100
|
-
export declare function D1Database(id: string, props: Omit<D1DatabaseProps, "migrationsFiles">): Promise<D1Database>;
|
|
101
114
|
/**
|
|
102
115
|
* Creates and manages Cloudflare D1 Databases.
|
|
103
116
|
*
|
|
@@ -134,8 +147,30 @@ export declare function D1Database(id: string, props: Omit<D1DatabaseProps, "mig
|
|
|
134
147
|
* migrationsDir: "./migrations",
|
|
135
148
|
* });
|
|
136
149
|
*
|
|
150
|
+
* @example
|
|
151
|
+
* // Clone an existing database by ID
|
|
152
|
+
* const clonedDb = await D1Database("cloned-db", {
|
|
153
|
+
* name: "cloned-db",
|
|
154
|
+
* clone: otherDb
|
|
155
|
+
* });
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* // Clone an existing database by ID
|
|
159
|
+
* const clonedDb = await D1Database("cloned-db", {
|
|
160
|
+
* name: "cloned-db",
|
|
161
|
+
* clone: { id: "existing-db-uuid" }
|
|
162
|
+
* });
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* // Clone an existing database by name
|
|
166
|
+
* const clonedDb = await D1Database("cloned-db", {
|
|
167
|
+
* name: "cloned-db",
|
|
168
|
+
* clone: { name: "existing-db-name" }
|
|
169
|
+
* });
|
|
170
|
+
*
|
|
137
171
|
* @see https://developers.cloudflare.com/d1/
|
|
138
172
|
*/
|
|
173
|
+
export declare function D1Database(id: string, props: Omit<D1DatabaseProps, "migrationsFiles">): Promise<D1Database>;
|
|
139
174
|
export declare const D1DatabaseResource: (((this: any, id: string, props?: {}) => never) & (new (_: never) => never)) | ((this: Context<D1Database>, id: string, props?: D1DatabaseProps) => Promise<D1Database>);
|
|
140
175
|
interface CloudflareD1Response {
|
|
141
176
|
result: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"d1-database.d.ts","sourceRoot":"","sources":["../../src/cloudflare/d1-database.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,
|
|
1
|
+
{"version":3,"file":"d1-database.d.ts","sourceRoot":"","sources":["../../src/cloudflare/d1-database.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,oBAAoB,EAC1B,MAAM,UAAU,CAAC;AAIlB;;GAEG;AACH,MAAM,WAAW,eAAgB,SAAQ,oBAAoB;IAC3D;;;;OAIG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;;OAGG;IACH,mBAAmB,CAAC,EAChB,MAAM,GACN,MAAM,GACN,MAAM,GACN,MAAM,GACN,MAAM,GACN,MAAM,GACN,MAAM,CAAC;IAEX;;;OAGG;IACH,eAAe,CAAC,EAAE;QAChB;;;;WAIG;QACH,IAAI,EAAE,MAAM,GAAG,UAAU,CAAC;KAC3B,CAAC;IAEF;;;;;OAKG;IACH,MAAM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,16 +1,8 @@
|
|
|
1
1
|
import { Resource } from "../resource.js";
|
|
2
2
|
import { CloudflareApiError, handleApiError } from "./api-error.js";
|
|
3
3
|
import { createCloudflareApi, } from "./api.js";
|
|
4
|
+
import { cloneD1Database } from "./d1-clone.js";
|
|
4
5
|
import { applyMigrations, listMigrationsFiles } from "./d1-migrations.js";
|
|
5
|
-
export async function D1Database(id, props) {
|
|
6
|
-
const migrationsFiles = props.migrationsDir
|
|
7
|
-
? await listMigrationsFiles(props.migrationsDir)
|
|
8
|
-
: [];
|
|
9
|
-
return D1DatabaseResource(id, {
|
|
10
|
-
...props,
|
|
11
|
-
migrationsFiles,
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
6
|
/**
|
|
15
7
|
* Creates and manages Cloudflare D1 Databases.
|
|
16
8
|
*
|
|
@@ -47,8 +39,38 @@ export async function D1Database(id, props) {
|
|
|
47
39
|
* migrationsDir: "./migrations",
|
|
48
40
|
* });
|
|
49
41
|
*
|
|
42
|
+
* @example
|
|
43
|
+
* // Clone an existing database by ID
|
|
44
|
+
* const clonedDb = await D1Database("cloned-db", {
|
|
45
|
+
* name: "cloned-db",
|
|
46
|
+
* clone: otherDb
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* // Clone an existing database by ID
|
|
51
|
+
* const clonedDb = await D1Database("cloned-db", {
|
|
52
|
+
* name: "cloned-db",
|
|
53
|
+
* clone: { id: "existing-db-uuid" }
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // Clone an existing database by name
|
|
58
|
+
* const clonedDb = await D1Database("cloned-db", {
|
|
59
|
+
* name: "cloned-db",
|
|
60
|
+
* clone: { name: "existing-db-name" }
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
50
63
|
* @see https://developers.cloudflare.com/d1/
|
|
51
64
|
*/
|
|
65
|
+
export async function D1Database(id, props) {
|
|
66
|
+
const migrationsFiles = props.migrationsDir
|
|
67
|
+
? await listMigrationsFiles(props.migrationsDir)
|
|
68
|
+
: [];
|
|
69
|
+
return D1DatabaseResource(id, {
|
|
70
|
+
...props,
|
|
71
|
+
migrationsFiles,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
52
74
|
export const D1DatabaseResource = Resource("cloudflare::D1Database", async function (id, props = {}) {
|
|
53
75
|
const api = await createCloudflareApi(props);
|
|
54
76
|
const databaseName = props.name ?? id;
|
|
@@ -67,6 +89,10 @@ export const D1DatabaseResource = Resource("cloudflare::D1Database", async funct
|
|
|
67
89
|
console.log("Creating D1 database:", databaseName);
|
|
68
90
|
try {
|
|
69
91
|
dbData = await createDatabase(api, databaseName, props);
|
|
92
|
+
// If clone property is provided, perform cloning after database creation
|
|
93
|
+
if (props.clone && dbData.result.uuid) {
|
|
94
|
+
await cloneDb(api, props.clone, dbData.result.uuid);
|
|
95
|
+
}
|
|
70
96
|
}
|
|
71
97
|
catch (error) {
|
|
72
98
|
// Check if this is a "database already exists" error and adopt is enabled
|
|
@@ -235,4 +261,42 @@ export async function updateDatabase(api, databaseId, props) {
|
|
|
235
261
|
}
|
|
236
262
|
return (await updateResponse.json());
|
|
237
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* Helper function to clone data from a source database to a target database
|
|
266
|
+
* Resolves the source database ID from different input formats and performs the cloning operation
|
|
267
|
+
*
|
|
268
|
+
* @param api CloudflareApi instance
|
|
269
|
+
* @param sourceDb Source database specification (can be an ID, a name, or a D1Database object)
|
|
270
|
+
* @param targetDbId Target database ID
|
|
271
|
+
*/
|
|
272
|
+
async function cloneDb(api, sourceDb, targetDbId) {
|
|
273
|
+
let sourceId;
|
|
274
|
+
// Determine source database ID
|
|
275
|
+
if ("id" in sourceDb && sourceDb.id) {
|
|
276
|
+
// Use provided ID directly
|
|
277
|
+
sourceId = sourceDb.id;
|
|
278
|
+
}
|
|
279
|
+
else if ("name" in sourceDb && sourceDb.name) {
|
|
280
|
+
// Look up ID by name
|
|
281
|
+
const databases = await listDatabases(api, sourceDb.name);
|
|
282
|
+
const foundDb = databases.find((db) => db.name === sourceDb.name);
|
|
283
|
+
if (!foundDb) {
|
|
284
|
+
throw new Error(`Source database with name '${sourceDb.name}' not found for cloning`);
|
|
285
|
+
}
|
|
286
|
+
sourceId = foundDb.id;
|
|
287
|
+
}
|
|
288
|
+
else if ("type" in sourceDb && sourceDb.type === "d1" && "id" in sourceDb) {
|
|
289
|
+
// It's a D1Database object
|
|
290
|
+
sourceId = sourceDb.id;
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
throw new Error("Invalid clone property: must provide either id or name");
|
|
294
|
+
}
|
|
295
|
+
// Perform the cloning
|
|
296
|
+
console.log(`Cloning data from database ${sourceId} to ${targetDbId}`);
|
|
297
|
+
await cloneD1Database(api, {
|
|
298
|
+
sourceDatabaseId: sourceId,
|
|
299
|
+
targetDatabaseId: targetDbId,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
238
302
|
//# sourceMappingURL=d1-database.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"d1-database.js","sourceRoot":"","sources":["../../src/cloudflare/d1-database.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACpE,OAAO,
|
|
1
|
+
{"version":3,"file":"d1-database.js","sourceRoot":"","sources":["../../src/cloudflare/d1-database.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACpE,OAAO,EACL,mBAAmB,GAGpB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAgI1E;;;;;;;;;;;;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C,IAAI,KAAK,IAAI,IAAI,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC5E,2BAA2B;QAC3B,QAAQ,GAAG,QAAQ,CAAC,EAAE,CAAC;IACzB,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;IAC5E,CAAC;IAED,sBAAsB;IACtB,OAAO,CAAC,GAAG,CAAC,8BAA8B,QAAQ,OAAO,UAAU,EAAE,CAAC,CAAC;IACvE,MAAM,eAAe,CAAC,GAAG,EAAE;QACzB,gBAAgB,EAAE,QAAQ;QAC1B,gBAAgB,EAAE,UAAU;KAC7B,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { CloudflareApi } from "./api.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options for exporting a D1 database
|
|
4
|
+
*/
|
|
5
|
+
export interface ExportD1DatabaseOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The ID of the D1 database to export
|
|
8
|
+
*/
|
|
9
|
+
databaseId: string;
|
|
10
|
+
/**
|
|
11
|
+
* Optional dump options to control what is exported
|
|
12
|
+
*/
|
|
13
|
+
dumpOptions?: {
|
|
14
|
+
/**
|
|
15
|
+
* Optional list of tables to export
|
|
16
|
+
*/
|
|
17
|
+
tables?: string[];
|
|
18
|
+
/**
|
|
19
|
+
* Whether to exclude schema in the export
|
|
20
|
+
*/
|
|
21
|
+
no_schema?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Whether to exclude data in the export
|
|
24
|
+
*/
|
|
25
|
+
no_data?: boolean;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Response from the export D1 database operation
|
|
30
|
+
*/
|
|
31
|
+
export interface ExportD1DatabaseResult {
|
|
32
|
+
/**
|
|
33
|
+
* The filename of the exported database
|
|
34
|
+
*/
|
|
35
|
+
filename: string;
|
|
36
|
+
/**
|
|
37
|
+
* The signed URL for downloading the exported database
|
|
38
|
+
*/
|
|
39
|
+
signed_url: string;
|
|
40
|
+
/**
|
|
41
|
+
* The status of the export operation
|
|
42
|
+
*/
|
|
43
|
+
status: "complete";
|
|
44
|
+
/**
|
|
45
|
+
* Whether the export operation was successful
|
|
46
|
+
*/
|
|
47
|
+
success: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Initiates an export of a Cloudflare D1 database and returns a download URL.
|
|
51
|
+
* Implements recursive polling with bookmark handling, similar to wrangler.
|
|
52
|
+
* Waits indefinitely until the export process completes or fails.
|
|
53
|
+
*
|
|
54
|
+
* Based on Cloudflare API:
|
|
55
|
+
* https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/export/
|
|
56
|
+
* and wrangler implementation.
|
|
57
|
+
*
|
|
58
|
+
* @param options Options including the database ID and optional dump parameters.
|
|
59
|
+
* @returns An object containing the download URL, filename, and status upon completion.
|
|
60
|
+
* @throws Will throw an error if the API call fails or the export process reports an error.
|
|
61
|
+
*/
|
|
62
|
+
export declare function exportD1Database(api: CloudflareApi, options: ExportD1DatabaseOptions, currentBookmark?: string): Promise<ExportD1DatabaseResult>;
|
|
63
|
+
//# sourceMappingURL=d1-export.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"d1-export.d.ts","sourceRoot":"","sources":["../../src/cloudflare/d1-export.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,WAAW,CAAC,EAAE;QACZ;;WAEG;QACH,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;QAElB;;WAEG;QACH,SAAS,CAAC,EAAE,OAAO,CAAC;QAEpB;;WAEG;QACH,OAAO,CAAC,EAAE,OAAO,CAAC;KACnB,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,MAAM,EAAE,UAAU,CAAC;IAEnB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;CAClB;AAuDD;;;;;;;;;;;;GAYG;AACH,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,aAAa,EAClB,OAAO,EAAE,uBAAuB,EAChC,eAAe,CAAC,EAAE,MAAM,GACvB,OAAO,CAAC,sBAAsB,CAAC,CA6CjC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Initiates an export of a Cloudflare D1 database and returns a download URL.
|
|
3
|
+
* Implements recursive polling with bookmark handling, similar to wrangler.
|
|
4
|
+
* Waits indefinitely until the export process completes or fails.
|
|
5
|
+
*
|
|
6
|
+
* Based on Cloudflare API:
|
|
7
|
+
* https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/export/
|
|
8
|
+
* and wrangler implementation.
|
|
9
|
+
*
|
|
10
|
+
* @param options Options including the database ID and optional dump parameters.
|
|
11
|
+
* @returns An object containing the download URL, filename, and status upon completion.
|
|
12
|
+
* @throws Will throw an error if the API call fails or the export process reports an error.
|
|
13
|
+
*/
|
|
14
|
+
export async function exportD1Database(api, options, currentBookmark) {
|
|
15
|
+
const response = await api.post(`/accounts/${api.accountId}/d1/database/${options.databaseId}/export`, {
|
|
16
|
+
output_format: "polling",
|
|
17
|
+
dump_options: options.dumpOptions || {},
|
|
18
|
+
current_bookmark: currentBookmark,
|
|
19
|
+
});
|
|
20
|
+
if (!response.ok) {
|
|
21
|
+
const errorData = (await response.json());
|
|
22
|
+
throw new Error(`Failed to export D1 database: ${errorData.error || errorData.errors?.[0]?.message || "Unknown error"}`);
|
|
23
|
+
}
|
|
24
|
+
const { result: data } = (await response.json());
|
|
25
|
+
if (!data.success) {
|
|
26
|
+
throw new Error(data.error || "Unknown error during D1 export");
|
|
27
|
+
}
|
|
28
|
+
// Log messages for visibility
|
|
29
|
+
if (data.messages && data.messages.length > 0) {
|
|
30
|
+
for (const message of data.messages) {
|
|
31
|
+
console.log(`D1 Export: ${message}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (data.status === "complete" && data.result) {
|
|
35
|
+
return {
|
|
36
|
+
filename: data.result.filename,
|
|
37
|
+
signed_url: data.result.signed_url,
|
|
38
|
+
status: data.status,
|
|
39
|
+
success: data.success,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
else if (data.status === "error") {
|
|
43
|
+
throw new Error(data.error || "Error during D1 export");
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
// Continue polling with bookmark
|
|
47
|
+
return exportD1Database(api, options, data.at_bookmark);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=d1-export.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"d1-export.js","sourceRoot":"","sources":["../../src/cloudflare/d1-export.ts"],"names":[],"mappings":"AA8GA;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,GAAkB,EAClB,OAAgC,EAChC,eAAwB;IAExB,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,IAAI,CAC7B,aAAa,GAAG,CAAC,SAAS,gBAAgB,OAAO,CAAC,UAAU,SAAS,EACrE;QACE,aAAa,EAAE,SAAS;QACxB,YAAY,EAAE,OAAO,CAAC,WAAW,IAAI,EAAE;QACvC,gBAAgB,EAAE,eAAe;KAClC,CACF,CAAC;IAEF,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACjB,MAAM,SAAS,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAA4B,CAAC;QACrE,MAAM,IAAI,KAAK,CACb,iCAAiC,SAAS,CAAC,KAAK,IAAI,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,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DatabaseOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The ID of the D1 database to import into
|
|
8
|
+
*/
|
|
9
|
+
databaseId: string;
|
|
10
|
+
/**
|
|
11
|
+
* The SQL file data to upload
|
|
12
|
+
*/
|
|
13
|
+
sqlData: Blob | BufferSource | string;
|
|
14
|
+
/**
|
|
15
|
+
* Optional custom filename for the imported SQL
|
|
16
|
+
*/
|
|
17
|
+
filename?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Response from the import D1 database operation
|
|
21
|
+
*/
|
|
22
|
+
export interface ImportD1DatabaseResult {
|
|
23
|
+
/**
|
|
24
|
+
* The filename of the imported database
|
|
25
|
+
*/
|
|
26
|
+
filename: string;
|
|
27
|
+
/**
|
|
28
|
+
* Additional result information from the import operation
|
|
29
|
+
*/
|
|
30
|
+
result: {
|
|
31
|
+
/**
|
|
32
|
+
* Final bookmark used during import
|
|
33
|
+
*/
|
|
34
|
+
final_bookmark?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Metadata about the import operation
|
|
37
|
+
*/
|
|
38
|
+
meta: {
|
|
39
|
+
/**
|
|
40
|
+
* Whether the database was changed
|
|
41
|
+
*/
|
|
42
|
+
changed_db: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Number of changes made
|
|
45
|
+
*/
|
|
46
|
+
changes: number;
|
|
47
|
+
/**
|
|
48
|
+
* Duration of the operation
|
|
49
|
+
*/
|
|
50
|
+
duration: number;
|
|
51
|
+
/**
|
|
52
|
+
* ID of the last row
|
|
53
|
+
*/
|
|
54
|
+
last_row_id: number;
|
|
55
|
+
/**
|
|
56
|
+
* Number of rows read
|
|
57
|
+
*/
|
|
58
|
+
rows_read: number;
|
|
59
|
+
/**
|
|
60
|
+
* Number of rows written
|
|
61
|
+
*/
|
|
62
|
+
rows_written: number;
|
|
63
|
+
/**
|
|
64
|
+
* Whether served by primary
|
|
65
|
+
*/
|
|
66
|
+
served_by_primary: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Region that served the request
|
|
69
|
+
*/
|
|
70
|
+
served_by_region: string;
|
|
71
|
+
/**
|
|
72
|
+
* Size after import
|
|
73
|
+
*/
|
|
74
|
+
size_after: number;
|
|
75
|
+
/**
|
|
76
|
+
* Timing information
|
|
77
|
+
*/
|
|
78
|
+
timings: {
|
|
79
|
+
/**
|
|
80
|
+
* SQL execution duration in milliseconds
|
|
81
|
+
*/
|
|
82
|
+
sql_duration_ms: number;
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Number of queries executed
|
|
87
|
+
*/
|
|
88
|
+
num_queries: number;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* The status of the import operation
|
|
92
|
+
*/
|
|
93
|
+
status: "complete";
|
|
94
|
+
/**
|
|
95
|
+
* Whether the import operation was successful
|
|
96
|
+
*/
|
|
97
|
+
success: boolean;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Initiates an import of SQL into a Cloudflare D1 database.
|
|
101
|
+
* Implements a flow to get an upload URL, upload the SQL file, and poll for status updates.
|
|
102
|
+
*
|
|
103
|
+
* Based on Cloudflare API:
|
|
104
|
+
* https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/import/
|
|
105
|
+
*
|
|
106
|
+
* @param api The CloudflareApi instance to use for requests
|
|
107
|
+
* @param options Options including the database ID and SQL data to import
|
|
108
|
+
* @returns An object containing the import results and status upon completion
|
|
109
|
+
* @throws Will throw an error if the API call fails or the import process reports an error
|
|
110
|
+
*/
|
|
111
|
+
export declare function importD1Database(api: CloudflareApi, options: ImportD1DatabaseOptions): Promise<ImportD1DatabaseResult>;
|
|
112
|
+
//# sourceMappingURL=d1-import.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"d1-import.d.ts","sourceRoot":"","sources":["../../src/cloudflare/d1-import.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AA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