alchemy 0.63.0 → 0.64.0

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 (181) hide show
  1. package/bin/alchemy.js +97 -87
  2. package/bin/commands/init.ts +1 -1
  3. package/bin/services/github-workflow.ts +2 -2
  4. package/lib/alchemy.d.ts +4 -0
  5. package/lib/alchemy.d.ts.map +1 -1
  6. package/lib/alchemy.js +10 -4
  7. package/lib/alchemy.js.map +1 -1
  8. package/lib/apply.d.ts +3 -0
  9. package/lib/apply.d.ts.map +1 -1
  10. package/lib/apply.js +11 -1
  11. package/lib/apply.js.map +1 -1
  12. package/lib/cloudflare/bindings.d.ts +3 -1
  13. package/lib/cloudflare/bindings.d.ts.map +1 -1
  14. package/lib/cloudflare/bucket.d.ts +2 -1
  15. package/lib/cloudflare/bucket.d.ts.map +1 -1
  16. package/lib/cloudflare/bucket.js.map +1 -1
  17. package/lib/cloudflare/bundle/cloudflare-internal.d.ts +6 -0
  18. package/lib/cloudflare/bundle/cloudflare-internal.d.ts.map +1 -0
  19. package/lib/cloudflare/bundle/cloudflare-internal.js +39 -0
  20. package/lib/cloudflare/bundle/cloudflare-internal.js.map +1 -0
  21. package/lib/cloudflare/bundle/nodejs-builtin-modules.d.ts +2 -0
  22. package/lib/cloudflare/bundle/nodejs-builtin-modules.d.ts.map +1 -0
  23. package/lib/cloudflare/bundle/nodejs-builtin-modules.js +9 -0
  24. package/lib/cloudflare/bundle/nodejs-builtin-modules.js.map +1 -0
  25. package/lib/cloudflare/bundle/plugin-als-external.d.ts +6 -0
  26. package/lib/cloudflare/bundle/plugin-als-external.d.ts.map +1 -0
  27. package/lib/cloudflare/bundle/plugin-als-external.js +12 -0
  28. package/lib/cloudflare/bundle/plugin-als-external.js.map +1 -0
  29. package/lib/cloudflare/bundle/plugin-hybrid-node-compat.d.ts +4 -1
  30. package/lib/cloudflare/bundle/plugin-hybrid-node-compat.d.ts.map +1 -1
  31. package/lib/cloudflare/bundle/plugin-hybrid-node-compat.js +23 -23
  32. package/lib/cloudflare/bundle/plugin-hybrid-node-compat.js.map +1 -1
  33. package/lib/cloudflare/bundle/plugin-node-compat.d.ts +11 -0
  34. package/lib/cloudflare/bundle/plugin-node-compat.d.ts.map +1 -0
  35. package/lib/cloudflare/bundle/plugin-node-compat.js +93 -0
  36. package/lib/cloudflare/bundle/plugin-node-compat.js.map +1 -0
  37. package/lib/cloudflare/cloudflare-env-proxy.js +1 -1
  38. package/lib/cloudflare/cloudflare-env-proxy.js.map +1 -1
  39. package/lib/cloudflare/compatibility-date.gen.d.ts +1 -1
  40. package/lib/cloudflare/compatibility-date.gen.js +1 -1
  41. package/lib/cloudflare/d1-local-migrations.js.map +1 -1
  42. package/lib/cloudflare/durable-object-namespace.d.ts +1 -0
  43. package/lib/cloudflare/durable-object-namespace.d.ts.map +1 -1
  44. package/lib/cloudflare/hyperdrive.js +1 -1
  45. package/lib/cloudflare/hyperdrive.js.map +1 -1
  46. package/lib/cloudflare/website.d.ts.map +1 -1
  47. package/lib/cloudflare/website.js +80 -82
  48. package/lib/cloudflare/website.js.map +1 -1
  49. package/lib/cloudflare/worker-bundle.d.ts +6 -0
  50. package/lib/cloudflare/worker-bundle.d.ts.map +1 -1
  51. package/lib/cloudflare/worker-bundle.js +34 -10
  52. package/lib/cloudflare/worker-bundle.js.map +1 -1
  53. package/lib/cloudflare/worker-metadata.d.ts.map +1 -1
  54. package/lib/cloudflare/worker-metadata.js +2 -1
  55. package/lib/cloudflare/worker-metadata.js.map +1 -1
  56. package/lib/cloudflare/worker.d.ts.map +1 -1
  57. package/lib/cloudflare/worker.js +24 -6
  58. package/lib/cloudflare/worker.js.map +1 -1
  59. package/lib/cloudflare/wrangler.json.d.ts +2 -4
  60. package/lib/cloudflare/wrangler.json.d.ts.map +1 -1
  61. package/lib/cloudflare/wrangler.json.js +16 -14
  62. package/lib/cloudflare/wrangler.json.js.map +1 -1
  63. package/lib/context.d.ts +3 -1
  64. package/lib/context.d.ts.map +1 -1
  65. package/lib/context.js +2 -2
  66. package/lib/context.js.map +1 -1
  67. package/lib/destroy.d.ts +8 -0
  68. package/lib/destroy.d.ts.map +1 -1
  69. package/lib/destroy.js +20 -4
  70. package/lib/destroy.js.map +1 -1
  71. package/lib/neon/project.d.ts.map +1 -1
  72. package/lib/neon/project.js +4 -5
  73. package/lib/neon/project.js.map +1 -1
  74. package/lib/planetscale/api/client.gen.d.ts +1750 -0
  75. package/lib/planetscale/api/client.gen.d.ts.map +1 -0
  76. package/lib/planetscale/api/client.gen.js +2401 -0
  77. package/lib/planetscale/api/client.gen.js.map +1 -0
  78. package/lib/planetscale/api/index.d.ts +3 -0
  79. package/lib/planetscale/api/index.d.ts.map +1 -0
  80. package/lib/planetscale/api/index.js +4 -0
  81. package/lib/planetscale/api/index.js.map +1 -0
  82. package/lib/planetscale/api/types.gen.d.ts +12156 -0
  83. package/lib/planetscale/api/types.gen.d.ts.map +1 -0
  84. package/lib/planetscale/api/types.gen.js +2 -0
  85. package/lib/planetscale/api/types.gen.js.map +1 -0
  86. package/lib/planetscale/branch.d.ts +5 -8
  87. package/lib/planetscale/branch.d.ts.map +1 -1
  88. package/lib/planetscale/branch.js +111 -97
  89. package/lib/planetscale/branch.js.map +1 -1
  90. package/lib/planetscale/database.d.ts +2 -6
  91. package/lib/planetscale/database.d.ts.map +1 -1
  92. package/lib/planetscale/database.js +157 -118
  93. package/lib/planetscale/database.js.map +1 -1
  94. package/lib/planetscale/index.d.ts +1 -1
  95. package/lib/planetscale/index.d.ts.map +1 -1
  96. package/lib/planetscale/index.js +1 -1
  97. package/lib/planetscale/index.js.map +1 -1
  98. package/lib/planetscale/password.d.ts +4 -7
  99. package/lib/planetscale/password.d.ts.map +1 -1
  100. package/lib/planetscale/password.js +71 -56
  101. package/lib/planetscale/password.js.map +1 -1
  102. package/lib/planetscale/utils.d.ts +8 -6
  103. package/lib/planetscale/utils.d.ts.map +1 -1
  104. package/lib/planetscale/utils.js +85 -70
  105. package/lib/planetscale/utils.js.map +1 -1
  106. package/lib/scope.d.ts +5 -1
  107. package/lib/scope.d.ts.map +1 -1
  108. package/lib/scope.js +11 -3
  109. package/lib/scope.js.map +1 -1
  110. package/lib/upstash/redis.js +1 -1
  111. package/lib/upstash/redis.js.map +1 -1
  112. package/lib/util/dedent.d.ts +3 -14
  113. package/lib/util/dedent.d.ts.map +1 -1
  114. package/lib/util/dedent.js +28 -52
  115. package/lib/util/dedent.js.map +1 -1
  116. package/lib/util/idempotent-spawn.d.ts.map +1 -1
  117. package/lib/util/idempotent-spawn.js +21 -2
  118. package/lib/util/idempotent-spawn.js.map +1 -1
  119. package/lib/util/xml.js +1 -1
  120. package/lib/util/xml.js.map +1 -1
  121. package/package.json +4 -3
  122. package/src/alchemy.ts +14 -6
  123. package/src/apply.ts +13 -1
  124. package/src/cloudflare/bindings.ts +3 -1
  125. package/src/cloudflare/bucket.ts +3 -1
  126. package/src/cloudflare/bundle/cloudflare-internal.ts +42 -0
  127. package/src/cloudflare/bundle/nodejs-builtin-modules.ts +14 -0
  128. package/src/cloudflare/bundle/plugin-als-external.ts +13 -0
  129. package/src/cloudflare/bundle/plugin-hybrid-node-compat.ts +29 -28
  130. package/src/cloudflare/bundle/plugin-node-compat.ts +114 -0
  131. package/src/cloudflare/cloudflare-env-proxy.ts +1 -1
  132. package/src/cloudflare/compatibility-date.gen.ts +1 -1
  133. package/src/cloudflare/d1-local-migrations.ts +2 -2
  134. package/src/cloudflare/durable-object-namespace.ts +1 -1
  135. package/src/cloudflare/hyperdrive.ts +1 -1
  136. package/src/cloudflare/website.ts +88 -89
  137. package/src/cloudflare/worker-bundle.ts +46 -9
  138. package/src/cloudflare/worker-metadata.ts +3 -1
  139. package/src/cloudflare/worker.ts +35 -7
  140. package/src/cloudflare/wrangler.json.ts +108 -110
  141. package/src/context.ts +5 -3
  142. package/src/destroy.ts +26 -8
  143. package/src/neon/project.ts +4 -5
  144. package/src/planetscale/api/client.gen.ts +2882 -0
  145. package/src/planetscale/api/index.ts +4 -0
  146. package/src/planetscale/api/types.gen.ts +13041 -0
  147. package/src/planetscale/branch.ts +152 -169
  148. package/src/planetscale/database.ts +195 -204
  149. package/src/planetscale/index.ts +1 -1
  150. package/src/planetscale/password.ts +86 -85
  151. package/src/planetscale/utils.ts +141 -122
  152. package/src/scope.ts +12 -3
  153. package/src/upstash/redis.ts +1 -1
  154. package/src/util/dedent.ts +33 -61
  155. package/src/util/idempotent-spawn.ts +23 -2
  156. package/src/util/xml.ts +1 -1
  157. package/templates/astro/alchemy.run.ts +1 -3
  158. package/templates/hono/alchemy.run.ts +0 -1
  159. package/templates/nuxt/alchemy.run.ts +1 -3
  160. package/templates/react-router/alchemy.run.ts +1 -3
  161. package/templates/rwsdk/alchemy.run.ts +0 -2
  162. package/templates/sveltekit/alchemy.run.ts +1 -3
  163. package/templates/tanstack-start/alchemy.run.ts +1 -3
  164. package/templates/typescript/alchemy.run.ts +0 -1
  165. package/templates/vite/alchemy.run.ts +0 -1
  166. package/workers/cloudflare-state-store.js +58 -58
  167. package/lib/cloudflare/bundle/externals.d.ts +0 -3
  168. package/lib/cloudflare/bundle/externals.d.ts.map +0 -1
  169. package/lib/cloudflare/bundle/externals.js +0 -35
  170. package/lib/cloudflare/bundle/externals.js.map +0 -1
  171. package/lib/cloudflare/bundle/plugin-compat-warning.d.ts +0 -10
  172. package/lib/cloudflare/bundle/plugin-compat-warning.d.ts.map +0 -1
  173. package/lib/cloudflare/bundle/plugin-compat-warning.js +0 -67
  174. package/lib/cloudflare/bundle/plugin-compat-warning.js.map +0 -1
  175. package/lib/planetscale/api.d.ts +0 -53
  176. package/lib/planetscale/api.d.ts.map +0 -1
  177. package/lib/planetscale/api.js +0 -84
  178. package/lib/planetscale/api.js.map +0 -1
  179. package/src/cloudflare/bundle/externals.ts +0 -36
  180. package/src/cloudflare/bundle/plugin-compat-warning.ts +0 -94
  181. package/src/planetscale/api.ts +0 -115
@@ -1,9 +1,9 @@
1
1
  import type { Context } from "../context.ts";
2
2
  import { Resource } from "../resource.ts";
3
- import type { Secret } from "../secret.ts";
4
- import { PlanetScaleApi } from "./api.ts";
3
+ import type { PlanetScaleProps } from "./api/client.gen.ts";
4
+ import { PlanetScaleClient } from "./api/client.gen.ts";
5
5
  import {
6
- fixClusterSize,
6
+ ensureProductionBranchClusterSize,
7
7
  type PlanetScaleClusterSize,
8
8
  waitForDatabaseReady,
9
9
  } from "./utils.ts";
@@ -11,7 +11,7 @@ import {
11
11
  /**
12
12
  * Properties for creating or updating a PlanetScale Database
13
13
  */
14
- export interface DatabaseProps {
14
+ export interface DatabaseProps extends PlanetScaleProps {
15
15
  /**
16
16
  * The name of the database
17
17
  *
@@ -24,11 +24,6 @@ export interface DatabaseProps {
24
24
  */
25
25
  organizationId: string;
26
26
 
27
- /**
28
- * PlanetScale API token (overrides environment variable)
29
- */
30
- apiKey?: Secret;
31
-
32
27
  /**
33
28
  * Whether to adopt the database if it already exists in Planetscale
34
29
  */
@@ -183,233 +178,229 @@ export const Database = Resource(
183
178
  id: string,
184
179
  props: DatabaseProps,
185
180
  ): Promise<Database> {
186
- const adopt = props.adopt ?? this.scope.adopt;
187
- const apiKey =
188
- props.apiKey?.unencrypted || process.env.PLANETSCALE_API_TOKEN;
189
- if (!apiKey) {
190
- throw new Error("PLANETSCALE_API_TOKEN environment variable is required");
191
- }
192
-
193
- const api = new PlanetScaleApi({ apiKey });
181
+ const api = new PlanetScaleClient(props);
194
182
 
195
183
  const databaseName =
196
184
  props.name ?? this.output?.name ?? this.scope.createPhysicalName(id);
197
185
 
198
186
  if (this.phase === "update" && this.output.name !== databaseName) {
199
- await api.patch(
200
- `/organizations/${props.organizationId}/databases/${this.output.name}`,
201
- { new_name: databaseName },
202
- );
187
+ await api.organizations.databases.patch({
188
+ path: {
189
+ organization: props.organizationId,
190
+ name: this.output.name,
191
+ },
192
+ body: { new_name: databaseName },
193
+ });
203
194
  }
204
195
 
205
196
  if (this.phase === "delete") {
206
- try {
207
- if (this.output?.name) {
208
- const response = await api.delete(
209
- `/organizations/${props.organizationId}/databases/${this.output.name}`,
210
- );
211
-
212
- if (!response.ok && response.status !== 404) {
213
- throw new Error(
214
- `Failed to delete database: ${response.statusText} ${await response.text()}`,
215
- );
216
- }
197
+ if (this.output?.name) {
198
+ const response = await api.organizations.databases.delete({
199
+ path: {
200
+ organization: props.organizationId,
201
+ name: this.output.name,
202
+ },
203
+ result: "full",
204
+ });
205
+
206
+ if (response.error && response.error.status !== 404) {
207
+ throw new Error(`Failed to delete database "${this.output.name}"`, {
208
+ cause: response.error,
209
+ });
217
210
  }
218
- } catch (error) {
219
- console.error("Error deleting database:", error);
220
- throw error;
221
211
  }
222
212
  return this.destroy();
223
213
  }
224
214
 
225
- try {
226
- // Check if database exists
227
- const getResponse = await api.get(
228
- `/organizations/${props.organizationId}/databases/${databaseName}`,
229
- );
230
- const getData = await getResponse.json<any>();
231
- if (this.phase === "update" || (adopt && getResponse.ok)) {
232
- if (!getResponse.ok) {
233
- throw new Error(`Database ${databaseName} not found`);
234
- }
235
- // Update database settings
236
- // If updating to a non-'main' default branch, create it first
237
- if (props.defaultBranch && props.defaultBranch !== "main") {
238
- const branchResponse = await api.get(
239
- `/organizations/${props.organizationId}/databases/${databaseName}/branches/${props.defaultBranch}`,
240
- );
241
- if (!getData.ready) {
242
- await waitForDatabaseReady(api, props.organizationId, databaseName);
243
- }
244
- if (!branchResponse.ok && branchResponse.status === 404) {
245
- // Create the branch
246
- const createBranchResponse = await api.post(
247
- `/organizations/${props.organizationId}/databases/${databaseName}/branches`,
248
- {
249
- name: props.defaultBranch,
250
- parent_branch: "main",
251
- },
252
- );
253
-
254
- if (!createBranchResponse.ok) {
255
- throw new Error(
256
- `Failed to create default branch: ${createBranchResponse.statusText} ${await createBranchResponse.text()}`,
257
- );
258
- }
259
- }
260
- }
261
-
262
- const updateResponse = await api.patch(
263
- `/organizations/${props.organizationId}/databases/${databaseName}`,
264
- {
265
- automatic_migrations: props.automaticMigrations,
266
- migration_framework: props.migrationFramework,
267
- migration_table_name: props.migrationTableName,
268
- require_approval_for_deploy: props.requireApprovalForDeploy,
269
- restrict_branch_region: props.restrictBranchRegion,
270
- allow_data_branching: props.allowDataBranching,
271
- insights_raw_queries: props.insightsRawQueries,
272
- production_branch_web_console: props.productionBranchWebConsole,
273
- default_branch: props.defaultBranch,
274
- },
275
- );
276
-
277
- if (!updateResponse.ok) {
278
- throw new Error(
279
- `Failed to update database: ${updateResponse.statusText} ${await updateResponse.text()}`,
280
- );
281
- }
282
-
283
- await fixClusterSize(
284
- api,
285
- props.organizationId,
286
- databaseName,
287
- props.defaultBranch || "main",
288
- props.clusterSize,
289
- getData.ready,
290
- );
291
-
292
- const data = await updateResponse.json<any>();
293
- return this({
294
- ...props,
295
- id: data.id,
296
- name: databaseName,
297
- state: data.state,
298
- defaultBranch: data.default_branch,
299
- plan: data.plan,
300
- createdAt: data.created_at,
301
- updatedAt: data.updated_at,
302
- htmlUrl: data.html_url,
215
+ // Check if database exists
216
+ const getResponse = await api.organizations.databases.get({
217
+ path: {
218
+ organization: props.organizationId,
219
+ name: databaseName,
220
+ },
221
+ result: "full",
222
+ });
223
+ if (this.phase === "update" || (props.adopt && getResponse.data)) {
224
+ if (!getResponse.data) {
225
+ throw new Error(`Database "${databaseName}" not found`, {
226
+ cause: getResponse.error,
303
227
  });
304
228
  }
305
-
306
- if (getResponse.ok) {
307
- throw new Error(`Database with name ${databaseName} already exists`);
229
+ // Update database settings
230
+ // If updating to a non-'main' default branch, create it first
231
+ if (props.defaultBranch && props.defaultBranch !== "main") {
232
+ const branchResponse = await api.organizations.databases.branches.get({
233
+ path: {
234
+ organization: props.organizationId,
235
+ database: databaseName,
236
+ name: props.defaultBranch,
237
+ },
238
+ result: "full",
239
+ });
240
+ if (!branchResponse.data) {
241
+ await waitForDatabaseReady(api, props.organizationId, databaseName);
242
+ }
243
+ if (branchResponse.error && branchResponse.error.status === 404) {
244
+ // Create the branch
245
+ await api.organizations.databases.branches.post({
246
+ path: {
247
+ organization: props.organizationId,
248
+ database: databaseName,
249
+ },
250
+ body: {
251
+ name: props.defaultBranch,
252
+ parent_branch: "main",
253
+ },
254
+ });
255
+ }
308
256
  }
309
257
 
310
- // Create new database
311
- const createResponse = await api.post(
312
- `/organizations/${props.organizationId}/databases`,
313
- {
258
+ const updateResponse = await api.organizations.databases.patch({
259
+ path: {
260
+ organization: props.organizationId,
314
261
  name: databaseName,
315
- region_slug: props.region?.slug,
316
- require_approval_for_deploy: props.requireApprovalForDeploy,
317
- allow_data_branching: props.allowDataBranching,
262
+ },
263
+ body: {
318
264
  automatic_migrations: props.automaticMigrations,
265
+ migration_framework: props.migrationFramework,
266
+ migration_table_name: props.migrationTableName,
267
+ require_approval_for_deploy: props.requireApprovalForDeploy,
319
268
  restrict_branch_region: props.restrictBranchRegion,
269
+ allow_data_branching: props.allowDataBranching,
320
270
  insights_raw_queries: props.insightsRawQueries,
321
271
  production_branch_web_console: props.productionBranchWebConsole,
322
- migration_framework: props.migrationFramework,
323
- migration_table_name: props.migrationTableName,
324
- cluster_size: props.clusterSize,
272
+ default_branch: props.defaultBranch,
325
273
  },
274
+ });
275
+
276
+ await ensureProductionBranchClusterSize(
277
+ api,
278
+ props.organizationId,
279
+ databaseName,
280
+ props.defaultBranch || "main",
281
+ props.clusterSize,
282
+ updateResponse.ready,
326
283
  );
327
284
 
328
- if (!createResponse.ok) {
329
- throw new Error(
330
- `Failed to create database: ${createResponse.statusText} ${await createResponse.text()}`,
331
- );
332
- }
285
+ return this({
286
+ ...props,
287
+ id: updateResponse.id,
288
+ name: databaseName,
289
+ state: updateResponse.state,
290
+ defaultBranch: updateResponse.default_branch,
291
+ plan: updateResponse.plan,
292
+ createdAt: updateResponse.created_at,
293
+ updatedAt: updateResponse.updated_at,
294
+ htmlUrl: updateResponse.html_url,
295
+ });
296
+ }
333
297
 
334
- const data = await createResponse.json<any>();
298
+ if (getResponse.data) {
299
+ throw new Error(`Database with name "${databaseName}" already exists`);
300
+ }
335
301
 
336
- // If a non-'main' default branch is specified, create it
337
- if (props.defaultBranch && props.defaultBranch !== "main") {
338
- await waitForDatabaseReady(api, props.organizationId, databaseName);
302
+ // Create new database
303
+ let data = await api.organizations.databases.post({
304
+ path: {
305
+ organization: props.organizationId,
306
+ },
307
+ body: {
308
+ name: databaseName,
309
+ region: props.region?.slug,
310
+ kind: "mysql",
311
+ cluster_size: props.clusterSize,
312
+ },
313
+ });
314
+
315
+ // These settings can't be set on creation, so we need to patch them after creation.
316
+ data = await api.organizations.databases.patch({
317
+ path: {
318
+ organization: props.organizationId,
319
+ name: databaseName,
320
+ },
321
+ body: {
322
+ require_approval_for_deploy: props.requireApprovalForDeploy,
323
+ allow_data_branching: props.allowDataBranching,
324
+ automatic_migrations: props.automaticMigrations,
325
+ restrict_branch_region: props.restrictBranchRegion,
326
+ insights_raw_queries: props.insightsRawQueries,
327
+ production_branch_web_console: props.productionBranchWebConsole,
328
+ migration_framework: props.migrationFramework,
329
+ migration_table_name: props.migrationTableName,
330
+ },
331
+ });
332
+
333
+ // If a non-'main' default branch is specified, create it
334
+ if (props.defaultBranch && props.defaultBranch !== "main") {
335
+ await waitForDatabaseReady(api, props.organizationId, databaseName);
336
+
337
+ // Check if branch exists
338
+ const branchResponse = await api.organizations.databases.branches.get({
339
+ path: {
340
+ organization: props.organizationId,
341
+ database: databaseName,
342
+ name: props.defaultBranch,
343
+ },
344
+ result: "full",
345
+ });
339
346
 
340
- // Check if branch exists
341
- const branchResponse = await api.get(
342
- `/organizations/${props.organizationId}/databases/${databaseName}/branches/${props.defaultBranch}`,
347
+ if (branchResponse.error && branchResponse.error.status === 404) {
348
+ // Create the branch
349
+ await api.organizations.databases.branches.post({
350
+ path: {
351
+ organization: props.organizationId,
352
+ database: databaseName,
353
+ },
354
+ body: {
355
+ name: props.defaultBranch,
356
+ parent_branch: "main",
357
+ },
358
+ });
359
+
360
+ await ensureProductionBranchClusterSize(
361
+ api,
362
+ props.organizationId,
363
+ databaseName,
364
+ props.defaultBranch || "main",
365
+ props.clusterSize,
366
+ false,
343
367
  );
344
368
 
345
- if (!branchResponse.ok && branchResponse.status === 404) {
346
- // Create the branch
347
- const createBranchResponse = await api.post(
348
- `/organizations/${props.organizationId}/databases/${databaseName}/branches`,
349
- {
350
- name: props.defaultBranch,
351
- parent_branch: "main",
352
- },
353
- );
354
-
355
- if (!createBranchResponse.ok) {
356
- throw new Error(
357
- `Failed to create default branch: ${createBranchResponse.statusText} ${await createBranchResponse.text()}`,
358
- );
359
- }
360
-
361
- await fixClusterSize(
362
- api,
363
- props.organizationId,
364
- databaseName,
365
- props.defaultBranch || "main",
366
- props.clusterSize,
367
- false,
368
- );
369
-
370
- // Update database to use new branch as default
371
- const updateResponse = await api.patch(
372
- `/organizations/${props.organizationId}/databases/${databaseName}`,
373
- {
374
- default_branch: props.defaultBranch,
375
- },
376
- );
377
-
378
- if (!updateResponse.ok) {
379
- throw new Error(
380
- `Failed to set default branch: ${updateResponse.statusText} ${await updateResponse.text()}`,
381
- );
382
- }
383
-
384
- const updatedData = await updateResponse.json<any>();
385
- return this({
386
- ...props,
387
- id: data.id,
369
+ // Update database to use new branch as default
370
+ const updatedData = await api.organizations.databases.patch({
371
+ path: {
372
+ organization: props.organizationId,
388
373
  name: databaseName,
389
- state: updatedData.state,
390
- defaultBranch: updatedData.default_branch,
391
- plan: updatedData.plan,
392
- createdAt: updatedData.created_at,
393
- updatedAt: updatedData.updated_at,
394
- htmlUrl: updatedData.html_url,
395
- });
396
- }
397
- }
374
+ },
375
+ body: {
376
+ default_branch: props.defaultBranch,
377
+ },
378
+ });
398
379
 
399
- return this({
400
- ...props,
401
- id: data.id,
402
- name: databaseName,
403
- state: data.state,
404
- defaultBranch: data.default_branch || "main",
405
- plan: data.plan,
406
- createdAt: data.created_at,
407
- updatedAt: data.updated_at,
408
- htmlUrl: data.html_url,
409
- });
410
- } catch (error) {
411
- console.error("Error managing database:", error);
412
- throw error;
380
+ return this({
381
+ ...props,
382
+ id: data.id,
383
+ name: databaseName,
384
+ state: updatedData.state,
385
+ defaultBranch: updatedData.default_branch,
386
+ plan: updatedData.plan,
387
+ createdAt: updatedData.created_at,
388
+ updatedAt: updatedData.updated_at,
389
+ htmlUrl: updatedData.html_url,
390
+ });
391
+ }
413
392
  }
393
+
394
+ return this({
395
+ ...props,
396
+ id: data.id,
397
+ name: databaseName,
398
+ state: data.state,
399
+ defaultBranch: data.default_branch || "main",
400
+ plan: data.plan,
401
+ createdAt: data.created_at,
402
+ updatedAt: data.updated_at,
403
+ htmlUrl: data.html_url,
404
+ });
414
405
  },
415
406
  );
@@ -1,4 +1,4 @@
1
- export * from "./api.ts";
1
+ export * from "./api/client.gen.ts";
2
2
  export * from "./branch.ts";
3
3
  export * from "./database.ts";
4
4
  export * from "./password.ts";