alchemy 0.15.9 → 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.
Files changed (41) hide show
  1. package/lib/aws/function.d.ts +4 -0
  2. package/lib/aws/function.d.ts.map +1 -1
  3. package/lib/aws/function.js +2 -0
  4. package/lib/aws/function.js.map +1 -1
  5. package/lib/cloudflare/d1-clone.d.ts +55 -0
  6. package/lib/cloudflare/d1-clone.d.ts.map +1 -0
  7. package/lib/cloudflare/d1-clone.js +45 -0
  8. package/lib/cloudflare/d1-clone.js.map +1 -0
  9. package/lib/cloudflare/d1-database.d.ts +36 -1
  10. package/lib/cloudflare/d1-database.d.ts.map +1 -1
  11. package/lib/cloudflare/d1-database.js +73 -9
  12. package/lib/cloudflare/d1-database.js.map +1 -1
  13. package/lib/cloudflare/d1-export.d.ts +63 -0
  14. package/lib/cloudflare/d1-export.d.ts.map +1 -0
  15. package/lib/cloudflare/d1-export.js +50 -0
  16. package/lib/cloudflare/d1-export.js.map +1 -0
  17. package/lib/cloudflare/d1-import.d.ts +112 -0
  18. package/lib/cloudflare/d1-import.d.ts.map +1 -0
  19. package/lib/cloudflare/d1-import.js +143 -0
  20. package/lib/cloudflare/d1-import.js.map +1 -0
  21. package/lib/cloudflare/index.d.ts +3 -0
  22. package/lib/cloudflare/index.d.ts.map +1 -1
  23. package/lib/cloudflare/index.js +3 -0
  24. package/lib/cloudflare/index.js.map +1 -1
  25. package/lib/stripe/price.d.ts +11 -0
  26. package/lib/stripe/price.d.ts.map +1 -1
  27. package/lib/stripe/price.js +4 -0
  28. package/lib/stripe/price.js.map +1 -1
  29. package/lib/util/sleep.d.ts +7 -0
  30. package/lib/util/sleep.d.ts.map +1 -0
  31. package/lib/util/sleep.js +9 -0
  32. package/lib/util/sleep.js.map +1 -0
  33. package/package.json +1 -1
  34. package/src/aws/function.ts +7 -0
  35. package/src/cloudflare/d1-clone.ts +113 -0
  36. package/src/cloudflare/d1-database.ts +99 -15
  37. package/src/cloudflare/d1-export.ts +173 -0
  38. package/src/cloudflare/d1-import.ts +406 -0
  39. package/src/cloudflare/index.ts +3 -0
  40. package/src/stripe/price.ts +17 -0
  41. package/src/util/sleep.ts +8 -0
@@ -2,10 +2,11 @@ import type { Context } from "../context.js";
2
2
  import { Resource } from "../resource.js";
3
3
  import { CloudflareApiError, handleApiError } from "./api-error.js";
4
4
  import {
5
- type CloudflareApi,
6
5
  createCloudflareApi,
6
+ type CloudflareApi,
7
7
  type CloudflareApiOptions,
8
8
  } from "./api.js";
9
+ import { cloneD1Database } from "./d1-clone.js";
9
10
  import { applyMigrations, listMigrationsFiles } from "./d1-migrations.js";
10
11
 
11
12
  /**
@@ -61,6 +62,17 @@ export interface D1DatabaseProps extends CloudflareApiOptions {
61
62
  */
62
63
  adopt?: boolean;
63
64
 
65
+ /**
66
+ * Clone data from an existing database to this new database.
67
+ * Only applicable during creation phase.
68
+ *
69
+ * Can be specified as:
70
+ * - A D1Database object
71
+ * - An object with an id property
72
+ * - An object with a name property (will look up the ID by name)
73
+ */
74
+ clone?: D1Database | { id: string } | { name: string };
75
+
64
76
  /**
65
77
  * These files will be generated internally with the D1Database wrapper function when migrationsDir is specified
66
78
  *
@@ -123,20 +135,6 @@ export type D1Database = Resource<"cloudflare::D1Database"> &
123
135
  };
124
136
  };
125
137
 
126
- export async function D1Database(
127
- id: string,
128
- props: Omit<D1DatabaseProps, "migrationsFiles">,
129
- ) {
130
- const migrationsFiles = props.migrationsDir
131
- ? await listMigrationsFiles(props.migrationsDir)
132
- : [];
133
-
134
- return D1DatabaseResource(id, {
135
- ...props,
136
- migrationsFiles,
137
- });
138
- }
139
-
140
138
  /**
141
139
  * Creates and manages Cloudflare D1 Databases.
142
140
  *
@@ -173,8 +171,43 @@ export async function D1Database(
173
171
  * migrationsDir: "./migrations",
174
172
  * });
175
173
  *
174
+ * @example
175
+ * // Clone an existing database by ID
176
+ * const clonedDb = await D1Database("cloned-db", {
177
+ * name: "cloned-db",
178
+ * clone: otherDb
179
+ * });
180
+ *
181
+ * @example
182
+ * // Clone an existing database by ID
183
+ * const clonedDb = await D1Database("cloned-db", {
184
+ * name: "cloned-db",
185
+ * clone: { id: "existing-db-uuid" }
186
+ * });
187
+ *
188
+ * @example
189
+ * // Clone an existing database by name
190
+ * const clonedDb = await D1Database("cloned-db", {
191
+ * name: "cloned-db",
192
+ * clone: { name: "existing-db-name" }
193
+ * });
194
+ *
176
195
  * @see https://developers.cloudflare.com/d1/
177
196
  */
197
+ export async function D1Database(
198
+ id: string,
199
+ props: Omit<D1DatabaseProps, "migrationsFiles">,
200
+ ) {
201
+ const migrationsFiles = props.migrationsDir
202
+ ? await listMigrationsFiles(props.migrationsDir)
203
+ : [];
204
+
205
+ return D1DatabaseResource(id, {
206
+ ...props,
207
+ migrationsFiles,
208
+ });
209
+ }
210
+
178
211
  export const D1DatabaseResource = Resource(
179
212
  "cloudflare::D1Database",
180
213
  async function (
@@ -202,6 +235,11 @@ export const D1DatabaseResource = Resource(
202
235
  console.log("Creating D1 database:", databaseName);
203
236
  try {
204
237
  dbData = await createDatabase(api, databaseName, props);
238
+
239
+ // If clone property is provided, perform cloning after database creation
240
+ if (props.clone && dbData.result.uuid) {
241
+ await cloneDb(api, props.clone, dbData.result.uuid);
242
+ }
205
243
  } catch (error) {
206
244
  // Check if this is a "database already exists" error and adopt is enabled
207
245
  if (
@@ -480,3 +518,49 @@ export async function updateDatabase(
480
518
 
481
519
  return (await updateResponse.json()) as CloudflareD1Response;
482
520
  }
521
+
522
+ /**
523
+ * Helper function to clone data from a source database to a target database
524
+ * Resolves the source database ID from different input formats and performs the cloning operation
525
+ *
526
+ * @param api CloudflareApi instance
527
+ * @param sourceDb Source database specification (can be an ID, a name, or a D1Database object)
528
+ * @param targetDbId Target database ID
529
+ */
530
+ async function cloneDb(
531
+ api: CloudflareApi,
532
+ sourceDb: D1Database | { id: string } | { name: string },
533
+ targetDbId: string,
534
+ ): Promise<void> {
535
+ let sourceId: string;
536
+
537
+ // Determine source database ID
538
+ if ("id" in sourceDb && sourceDb.id) {
539
+ // Use provided ID directly
540
+ sourceId = sourceDb.id;
541
+ } else if ("name" in sourceDb && sourceDb.name) {
542
+ // Look up ID by name
543
+ const databases = await listDatabases(api, sourceDb.name);
544
+ const foundDb = databases.find((db) => db.name === sourceDb.name);
545
+
546
+ if (!foundDb) {
547
+ throw new Error(
548
+ `Source database with name '${sourceDb.name}' not found for cloning`,
549
+ );
550
+ }
551
+
552
+ sourceId = foundDb.id;
553
+ } else if ("type" in sourceDb && sourceDb.type === "d1" && "id" in sourceDb) {
554
+ // It's a D1Database object
555
+ sourceId = sourceDb.id;
556
+ } else {
557
+ throw new Error("Invalid clone property: must provide either id or name");
558
+ }
559
+
560
+ // Perform the cloning
561
+ console.log(`Cloning data from database ${sourceId} to ${targetDbId}`);
562
+ await cloneD1Database(api, {
563
+ sourceDatabaseId: sourceId,
564
+ targetDatabaseId: targetDbId,
565
+ });
566
+ }
@@ -0,0 +1,173 @@
1
+ import type { CloudflareApi } from "./api.js"; // Ensure CloudflareApi is exported if not already
2
+
3
+ /**
4
+ * Options for exporting a D1 database
5
+ */
6
+ export interface ExportD1DatabaseOptions {
7
+ /**
8
+ * The ID of the D1 database to export
9
+ */
10
+ databaseId: string;
11
+
12
+ /**
13
+ * Optional dump options to control what is exported
14
+ */
15
+ dumpOptions?: {
16
+ /**
17
+ * Optional list of tables to export
18
+ */
19
+ tables?: string[];
20
+
21
+ /**
22
+ * Whether to exclude schema in the export
23
+ */
24
+ no_schema?: boolean;
25
+
26
+ /**
27
+ * Whether to exclude data in the export
28
+ */
29
+ no_data?: boolean;
30
+ };
31
+ }
32
+
33
+ /**
34
+ * Response from the export D1 database operation
35
+ */
36
+ export interface ExportD1DatabaseResult {
37
+ /**
38
+ * The filename of the exported database
39
+ */
40
+ filename: string;
41
+
42
+ /**
43
+ * The signed URL for downloading the exported database
44
+ */
45
+ signed_url: string;
46
+
47
+ /**
48
+ * The status of the export operation
49
+ */
50
+ status: "complete";
51
+
52
+ /**
53
+ * Whether the export operation was successful
54
+ */
55
+ success: boolean;
56
+ }
57
+
58
+ /**
59
+ * Error response from Cloudflare API
60
+ */
61
+ interface CloudflareErrorResponse {
62
+ error?: string;
63
+ errors?: Array<{
64
+ message: string;
65
+ }>;
66
+ }
67
+
68
+ /**
69
+ * Response during polling for D1 database export
70
+ */
71
+ interface ExportPollingResponse {
72
+ /**
73
+ * Bookmark for continuing the polling process
74
+ */
75
+ at_bookmark?: string;
76
+
77
+ /**
78
+ * Error message if any
79
+ */
80
+ error?: string;
81
+
82
+ /**
83
+ * Progress messages from the export process
84
+ */
85
+ messages: string[];
86
+
87
+ /**
88
+ * Result data if export is complete
89
+ */
90
+ result?: {
91
+ filename: string;
92
+ signed_url: string;
93
+ };
94
+
95
+ /**
96
+ * Status of the export process
97
+ */
98
+ status: "complete" | "error" | "processing";
99
+
100
+ /**
101
+ * Whether the current polling request was successful
102
+ */
103
+ success: boolean;
104
+
105
+ /**
106
+ * Type of operation
107
+ */
108
+ type: "export";
109
+ }
110
+
111
+ /**
112
+ * Initiates an export of a Cloudflare D1 database and returns a download URL.
113
+ * Implements recursive polling with bookmark handling, similar to wrangler.
114
+ * Waits indefinitely until the export process completes or fails.
115
+ *
116
+ * Based on Cloudflare API:
117
+ * https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/export/
118
+ * and wrangler implementation.
119
+ *
120
+ * @param options Options including the database ID and optional dump parameters.
121
+ * @returns An object containing the download URL, filename, and status upon completion.
122
+ * @throws Will throw an error if the API call fails or the export process reports an error.
123
+ */
124
+ export async function exportD1Database(
125
+ api: CloudflareApi,
126
+ options: ExportD1DatabaseOptions,
127
+ currentBookmark?: string,
128
+ ): Promise<ExportD1DatabaseResult> {
129
+ const response = await api.post(
130
+ `/accounts/${api.accountId}/d1/database/${options.databaseId}/export`,
131
+ {
132
+ output_format: "polling",
133
+ dump_options: options.dumpOptions || {},
134
+ current_bookmark: currentBookmark,
135
+ },
136
+ );
137
+
138
+ if (!response.ok) {
139
+ const errorData = (await response.json()) as CloudflareErrorResponse;
140
+ throw new Error(
141
+ `Failed to export D1 database: ${errorData.error || errorData.errors?.[0]?.message || "Unknown error"}`,
142
+ );
143
+ }
144
+
145
+ const { result: data } = (await response.json()) as {
146
+ result: ExportPollingResponse;
147
+ };
148
+
149
+ if (!data.success) {
150
+ throw new Error(data.error || "Unknown error during D1 export");
151
+ }
152
+
153
+ // Log messages for visibility
154
+ if (data.messages && data.messages.length > 0) {
155
+ for (const message of data.messages) {
156
+ console.log(`D1 Export: ${message}`);
157
+ }
158
+ }
159
+
160
+ if (data.status === "complete" && data.result) {
161
+ return {
162
+ filename: data.result.filename,
163
+ signed_url: data.result.signed_url,
164
+ status: data.status,
165
+ success: data.success,
166
+ };
167
+ } else if (data.status === "error") {
168
+ throw new Error(data.error || "Error during D1 export");
169
+ } else {
170
+ // Continue polling with bookmark
171
+ return exportD1Database(api, options, data.at_bookmark);
172
+ }
173
+ }