alchemy 0.71.1 → 0.73.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 (65) hide show
  1. package/bin/alchemy.js +2 -4
  2. package/lib/cloudflare/bound.d.ts +1 -2
  3. package/lib/cloudflare/bound.d.ts.map +1 -1
  4. package/lib/cloudflare/compatibility-date.gen.d.ts +1 -1
  5. package/lib/cloudflare/compatibility-date.gen.js +1 -1
  6. package/lib/cloudflare/container.d.ts +1 -0
  7. package/lib/cloudflare/container.d.ts.map +1 -1
  8. package/lib/cloudflare/container.js +3 -0
  9. package/lib/cloudflare/container.js.map +1 -1
  10. package/lib/cloudflare/index.d.ts +1 -1
  11. package/lib/cloudflare/index.d.ts.map +1 -1
  12. package/lib/cloudflare/index.js +2 -1
  13. package/lib/cloudflare/index.js.map +1 -1
  14. package/lib/cloudflare/queue-consumer.d.ts +2 -2
  15. package/lib/cloudflare/queue-consumer.d.ts.map +1 -1
  16. package/lib/cloudflare/queue-consumer.js +23 -4
  17. package/lib/cloudflare/queue-consumer.js.map +1 -1
  18. package/lib/cloudflare/redwood/redwood.js +2 -2
  19. package/lib/cloudflare/redwood/redwood.js.map +1 -1
  20. package/lib/cloudflare/worker.d.ts.map +1 -1
  21. package/lib/cloudflare/worker.js +1 -6
  22. package/lib/cloudflare/worker.js.map +1 -1
  23. package/lib/planetscale/api.d.ts +0 -4
  24. package/lib/planetscale/api.d.ts.map +1 -1
  25. package/lib/planetscale/api.js.map +1 -1
  26. package/lib/planetscale/branch.d.ts +12 -11
  27. package/lib/planetscale/branch.d.ts.map +1 -1
  28. package/lib/planetscale/branch.js +40 -23
  29. package/lib/planetscale/branch.js.map +1 -1
  30. package/lib/planetscale/database.d.ts +10 -5
  31. package/lib/planetscale/database.d.ts.map +1 -1
  32. package/lib/planetscale/database.js +30 -18
  33. package/lib/planetscale/database.js.map +1 -1
  34. package/lib/planetscale/index.d.ts +1 -0
  35. package/lib/planetscale/index.d.ts.map +1 -1
  36. package/lib/planetscale/index.js +1 -0
  37. package/lib/planetscale/index.js.map +1 -1
  38. package/lib/planetscale/organization.d.ts +5 -0
  39. package/lib/planetscale/organization.d.ts.map +1 -0
  40. package/lib/planetscale/organization.js +11 -0
  41. package/lib/planetscale/organization.js.map +1 -0
  42. package/lib/planetscale/password.d.ts +10 -9
  43. package/lib/planetscale/password.d.ts.map +1 -1
  44. package/lib/planetscale/password.js +28 -13
  45. package/lib/planetscale/password.js.map +1 -1
  46. package/lib/planetscale/role.d.ts +2 -1
  47. package/lib/planetscale/role.d.ts.map +1 -1
  48. package/lib/planetscale/role.js +11 -8
  49. package/lib/planetscale/role.js.map +1 -1
  50. package/package.json +2 -6
  51. package/src/cloudflare/bound.ts +1 -22
  52. package/src/cloudflare/compatibility-date.gen.ts +1 -1
  53. package/src/cloudflare/container.ts +9 -0
  54. package/src/cloudflare/index.ts +3 -1
  55. package/src/cloudflare/queue-consumer.ts +28 -7
  56. package/src/cloudflare/redwood/redwood.ts +2 -2
  57. package/src/cloudflare/worker.ts +1 -6
  58. package/src/planetscale/api.ts +0 -4
  59. package/src/planetscale/branch.ts +51 -35
  60. package/src/planetscale/database.ts +40 -20
  61. package/src/planetscale/index.ts +1 -0
  62. package/src/planetscale/organization.ts +17 -0
  63. package/src/planetscale/password.ts +34 -16
  64. package/src/planetscale/role.ts +16 -9
  65. package/workers/tunnel-proxy.js +1 -1
@@ -780,6 +780,15 @@ export interface ContainerApplicationData {
780
780
  [key: string]: any;
781
781
  }
782
782
 
783
+ export async function getContainerApplicationByName(
784
+ api: CloudflareApi,
785
+ name: string,
786
+ ) {
787
+ return (await listContainerApplications(api)).find(
788
+ (app) => app.name === name,
789
+ );
790
+ }
791
+
783
792
  export async function getContainerApplication(
784
793
  api: CloudflareApi,
785
794
  applicationId: string,
@@ -1,3 +1,5 @@
1
+ /// <reference types="@cloudflare/workers-types" />
2
+
1
3
  export * from "./account-api-token.ts";
2
4
  export * from "./account-id.ts";
3
5
  export * from "./ai-gateway.ts";
@@ -66,11 +68,11 @@ export * from "./vectorize-metadata-index.ts";
66
68
  export * from "./version-metadata.ts";
67
69
  export * from "./vite/vite.ts";
68
70
  export * from "./website.ts";
71
+ export { WorkerLoader } from "./worker-loader.ts";
69
72
  export * from "./worker-ref.ts";
70
73
  export * from "./worker-stub.ts";
71
74
  export * from "./worker-subdomain.ts";
72
75
  export * from "./worker.ts";
73
76
  export { Workflow } from "./workflow.ts";
74
- export { WorkerLoader } from "./worker-loader.ts";
75
77
  export * from "./wrangler.json.ts";
76
78
  export * from "./zone.ts";
@@ -229,6 +229,12 @@ export const QueueConsumer = Resource(
229
229
  );
230
230
  }
231
231
 
232
+ const deadLetterQueue = props.settings?.deadLetterQueue
233
+ ? typeof props.settings.deadLetterQueue === "string"
234
+ ? props.settings.deadLetterQueue
235
+ : props.settings.deadLetterQueue.name
236
+ : undefined;
237
+
232
238
  return {
233
239
  id: consumerData.result.consumer_id,
234
240
  queueId,
@@ -242,9 +248,14 @@ export const QueueConsumer = Resource(
242
248
  maxRetries: consumerData.result.settings.max_retries,
243
249
  maxWaitTimeMs: consumerData.result.settings.max_wait_time_ms,
244
250
  retryDelay: consumerData.result.settings.retry_delay,
245
- deadLetterQueue: consumerData.result.settings.dead_letter_queue,
251
+ deadLetterQueue,
246
252
  }
247
- : undefined,
253
+ : props.settings
254
+ ? {
255
+ ...props.settings,
256
+ deadLetterQueue,
257
+ }
258
+ : undefined,
248
259
  createdOn: consumerData.result.created_on,
249
260
  accountId: api.accountId,
250
261
  };
@@ -258,13 +269,13 @@ interface CloudflareQueueConsumerResponse {
258
269
  result: {
259
270
  consumer_id: string;
260
271
  script_name: string;
272
+ dead_letter_queue?: string;
261
273
  settings?: {
262
274
  batch_size?: number;
263
275
  max_concurrency?: number;
264
276
  max_retries?: number;
265
277
  max_wait_time_ms?: number;
266
278
  retry_delay?: number;
267
- dead_letter_queue?: string;
268
279
  };
269
280
  type: "worker";
270
281
  queue_id?: string;
@@ -462,13 +473,13 @@ export async function listQueueConsumers(
462
473
  queue_id: string;
463
474
  queue_name: string;
464
475
  created_on: string;
476
+ dead_letter_queue?: string;
465
477
  settings?: {
466
478
  batch_size?: number;
467
479
  max_concurrency?: number;
468
480
  max_retries?: number;
469
481
  max_wait_time_ms?: number;
470
482
  retry_delay?: number;
471
- dead_letter_queue?: string;
472
483
  };
473
484
  }>;
474
485
  };
@@ -492,7 +503,10 @@ export async function listQueueConsumers(
492
503
  maxRetries: consumer.settings.max_retries,
493
504
  maxWaitTimeMs: consumer.settings.max_wait_time_ms,
494
505
  retryDelay: consumer.settings.retry_delay,
495
- deadLetterQueue: consumer.settings.dead_letter_queue,
506
+ deadLetterQueue:
507
+ consumer.dead_letter_queue && consumer.dead_letter_queue !== ""
508
+ ? consumer.dead_letter_queue
509
+ : undefined,
496
510
  }
497
511
  : undefined,
498
512
  }));
@@ -532,12 +546,12 @@ export async function listQueueConsumersForWorker(
532
546
  const data = (await response.json()) as {
533
547
  result: Array<{
534
548
  script: string;
549
+ dead_letter_queue?: string;
535
550
  settings?: {
536
551
  batch_size?: number;
537
552
  max_retries?: number;
538
553
  max_wait_time_ms?: number;
539
554
  retry_delay?: number;
540
- dead_letter_queue?: string;
541
555
  };
542
556
  type: string;
543
557
  queue_name: string;
@@ -562,6 +576,13 @@ export async function listQueueConsumersForWorker(
562
576
  queueId: consumer.queue_id,
563
577
  consumerId: consumer.consumer_id,
564
578
  createdOn: consumer.created_on,
565
- settings: consumer.settings,
579
+ settings: consumer.settings
580
+ ? {
581
+ ...consumer.settings,
582
+ deadLetterQueue: consumer.dead_letter_queue,
583
+ }
584
+ : consumer.dead_letter_queue
585
+ ? { deadLetterQueue: consumer.dead_letter_queue }
586
+ : undefined,
566
587
  }));
567
588
  }
@@ -50,9 +50,9 @@ export async function Redwood<B extends Bindings>(
50
50
  },
51
51
  },
52
52
  noBundle: props?.noBundle ?? true,
53
- entrypoint: props?.entrypoint ?? path.join("dist", "worker", "worker.js"),
53
+ entrypoint: props?.entrypoint ?? path.join("dist", "worker", "index.js"),
54
54
  compatibilityFlags: ["nodejs_compat", ...(props?.compatibilityFlags ?? [])],
55
- compatibilityDate: props?.compatibilityDate ?? "2025-04-02",
55
+ compatibilityDate: props?.compatibilityDate ?? "2025-08-21",
56
56
  wrangler: {
57
57
  main: props?.wrangler?.main ?? "src/worker.tsx",
58
58
  transform: props?.wrangler?.transform,
@@ -1313,15 +1313,10 @@ async function provisionResources<B extends Bindings>(
1313
1313
  ? Promise.all(
1314
1314
  input.containers.map(async (container) => {
1315
1315
  return await ContainerApplication(container.id, {
1316
- image: container.image,
1317
- name: container.name,
1318
- instanceType: container.instanceType,
1319
- observability: container.observability,
1316
+ ...container,
1320
1317
  durableObjects: {
1321
1318
  namespaceId: await getContainerNamespaceId(container),
1322
1319
  },
1323
- schedulingPolicy: container.schedulingPolicy,
1324
- adopt: container.adopt,
1325
1320
  dev: options.local,
1326
1321
  ...input.api,
1327
1322
  });
@@ -28,10 +28,6 @@ export interface PlanetScaleProps {
28
28
  * @deprecated Use serviceTokenId and serviceToken instead.
29
29
  */
30
30
  apiKey?: Secret;
31
- /**
32
- * The organization to use for authentication. Defaults to the value of the PLANETSCALE_ORGANIZATION or PLANETSCALE_ORG_ID environment variable.
33
- */
34
- organizationId?: string;
35
31
  }
36
32
 
37
33
  export class PlanetScaleError extends Error {
@@ -1,6 +1,7 @@
1
1
  import type { Context } from "../context.ts";
2
2
  import { Resource } from "../resource.ts";
3
3
  import { createPlanetScaleClient, type PlanetScaleProps } from "./api.ts";
4
+ import type { Database } from "./database.ts";
4
5
  import {
5
6
  ensureProductionBranchClusterSize,
6
7
  sanitizeClusterSize,
@@ -20,14 +21,14 @@ export interface BranchProps extends PlanetScaleProps {
20
21
  name?: string;
21
22
 
22
23
  /**
23
- * The organization ID
24
+ * The organization name. Automatically inferred from the database if the database is provided as an object.
24
25
  */
25
- organizationId: string;
26
+ organization?: string;
26
27
 
27
28
  /**
28
29
  * The database name
29
30
  */
30
- databaseName: string;
31
+ database: string | Database;
31
32
 
32
33
  /**
33
34
  * Whether or not the branch should be set to a production branch or not.
@@ -114,8 +115,8 @@ export interface Branch extends BranchProps {
114
115
  * // Create a branch from 'main'
115
116
  * const branch = await Branch("feature-123", {
116
117
  * name: "feature-123",
117
- * organizationId: "my-org",
118
- * databaseName: "my-database",
118
+ * organization: "my-org",
119
+ * database: "my-database",
119
120
  * parentBranch: "main"
120
121
  * });
121
122
  *
@@ -123,15 +124,15 @@ export interface Branch extends BranchProps {
123
124
  * // Create a branch from another branch object
124
125
  * const parentBranch = await Branch("staging", {
125
126
  * name: "staging",
126
- * organizationId: "my-org",
127
- * databaseName: "my-database",
127
+ * organization: "my-org",
128
+ * database: "my-database",
128
129
  * parentBranch: "main"
129
130
  * });
130
131
  *
131
132
  * const featureBranch = await Branch("feature-456", {
132
133
  * name: "feature-456",
133
- * organizationId: "my-org",
134
- * databaseName: "my-database",
134
+ * organization: "my-org",
135
+ * database: "my-database",
135
136
  * parentBranch: parentBranch // Using Branch object instead of string
136
137
  * });
137
138
  *
@@ -139,8 +140,8 @@ export interface Branch extends BranchProps {
139
140
  * // Create a branch from a backup
140
141
  * const branch = await Branch("restored-branch", {
141
142
  * name: "restored-branch",
142
- * organizationId: "my-org",
143
- * databaseName: "my-database",
143
+ * organization: "my-org",
144
+ * database: "my-database",
144
145
  * parentBranch: "main",
145
146
  * backupId: "backup-123",
146
147
  * clusterSize: "PS_10"
@@ -164,6 +165,26 @@ export const Branch = Resource(
164
165
  : typeof props.parentBranch === "string"
165
166
  ? props.parentBranch
166
167
  : props.parentBranch.name;
168
+ const organization =
169
+ // @ts-expect-error - organizationId is a legacy thing, we keep this so we can destroy
170
+ this.output?.organizationId ??
171
+ props.organization ??
172
+ (typeof props.database !== "string"
173
+ ? props.database.organization
174
+ : (process.env.PLANETSCALE_ORGANIZATION ??
175
+ process.env.PLANETSCALE_ORG_ID));
176
+ const database =
177
+ // @ts-expect-error - databaseName is a legacy thing, we keep this so we can destroy
178
+ this.output?.databaseName ??
179
+ (typeof props.database === "string"
180
+ ? props.database
181
+ : props.database.name);
182
+
183
+ if (!organization) {
184
+ throw new Error(
185
+ "PlanetScale organization is required. Please set the `organization` property or the `PLANETSCALE_ORGANIZATION` environment variable.",
186
+ );
187
+ }
167
188
 
168
189
  if (this.phase === "update" && this.output.name !== branchName) {
169
190
  // TODO(sam): maybe we don't need to replace? just branch again? or rename?
@@ -174,8 +195,8 @@ export const Branch = Resource(
174
195
  if (this.output?.name) {
175
196
  const response = await api.deleteBranch({
176
197
  path: {
177
- organization: props.organizationId,
178
- database: props.databaseName,
198
+ organization,
199
+ database,
179
200
  name: this.output.name,
180
201
  },
181
202
  throwOnError: false,
@@ -196,8 +217,8 @@ export const Branch = Resource(
196
217
  if (typeof props.parentBranch !== "string" && props.parentBranch) {
197
218
  await waitForBranchReady(
198
219
  api,
199
- props.organizationId,
200
- props.databaseName,
220
+ organization,
221
+ database,
201
222
  props.parentBranch.name,
202
223
  );
203
224
  }
@@ -205,8 +226,8 @@ export const Branch = Resource(
205
226
  // Check if branch exists
206
227
  const getResponse = await api.getBranch({
207
228
  path: {
208
- organization: props.organizationId,
209
- database: props.databaseName,
229
+ organization,
230
+ database,
210
231
  name: branchName,
211
232
  },
212
233
  throwOnError: false,
@@ -256,8 +277,8 @@ export const Branch = Resource(
256
277
  : "disableSafeMigrations"
257
278
  ]({
258
279
  path: {
259
- organization: props.organizationId,
260
- database: props.databaseName,
280
+ organization,
281
+ database,
261
282
  name: branchName,
262
283
  },
263
284
  });
@@ -279,8 +300,8 @@ export const Branch = Resource(
279
300
  }
280
301
  await ensureProductionBranchClusterSize(
281
302
  api,
282
- props.organizationId,
283
- props.databaseName,
303
+ organization,
304
+ database,
284
305
  branchName,
285
306
  data.kind,
286
307
  clusterSize,
@@ -299,8 +320,8 @@ export const Branch = Resource(
299
320
  let clusterSize: string | undefined;
300
321
  const parent = await waitForBranchReady(
301
322
  api,
302
- props.organizationId,
303
- props.databaseName,
323
+ organization,
324
+ database,
304
325
  parentBranchName,
305
326
  );
306
327
  if (props.clusterSize) {
@@ -315,8 +336,8 @@ export const Branch = Resource(
315
336
  // Branch doesn't exist, create it
316
337
  const { data } = await api.createBranch({
317
338
  path: {
318
- organization: props.organizationId,
319
- database: props.databaseName,
339
+ organization,
340
+ database,
320
341
  },
321
342
  body: {
322
343
  name: branchName,
@@ -331,18 +352,13 @@ export const Branch = Resource(
331
352
  // Handle safe migrations if specified
332
353
  if (props.safeMigrations !== undefined) {
333
354
  // We can't change the migrations mode if the branch is not ready
334
- await waitForBranchReady(
335
- api,
336
- props.organizationId,
337
- props.databaseName,
338
- branchName,
339
- );
355
+ await waitForBranchReady(api, organization, database, branchName);
340
356
  await api[
341
357
  props.safeMigrations ? "enableSafeMigrations" : "disableSafeMigrations"
342
358
  ]({
343
359
  path: {
344
- organization: props.organizationId,
345
- database: props.databaseName,
360
+ organization,
361
+ database,
346
362
  name: branchName,
347
363
  },
348
364
  });
@@ -352,8 +368,8 @@ export const Branch = Resource(
352
368
  if (clusterSize) {
353
369
  await ensureProductionBranchClusterSize(
354
370
  api,
355
- props.organizationId,
356
- props.databaseName,
371
+ organization,
372
+ database,
357
373
  branchName,
358
374
  data.kind,
359
375
  clusterSize,
@@ -18,9 +18,10 @@ interface BaseDatabaseProps extends PlanetScaleProps {
18
18
  name?: string;
19
19
 
20
20
  /**
21
- * The organization ID where the database will be created
21
+ * The organization name where the database will be created
22
+ * @default process.env.PLANETSCALE_ORGANIZATION
22
23
  */
23
- organizationId: string;
24
+ organization?: string;
24
25
 
25
26
  /**
26
27
  * Whether to adopt the database if it already exists in Planetscale
@@ -157,6 +158,11 @@ export type Database = DatabaseProps & {
157
158
  * HTML URL to access the database
158
159
  */
159
160
  htmlUrl: string;
161
+
162
+ /**
163
+ * The organization of the database
164
+ */
165
+ organization: string;
160
166
  };
161
167
 
162
168
  /**
@@ -166,7 +172,7 @@ export type Database = DatabaseProps & {
166
172
  * // Create a basic database in a specific organization
167
173
  * const db = await Database("my-app-db", {
168
174
  * name: "my-app-db",
169
- * organizationId: "my-org",
175
+ * organization: "my-org",
170
176
  * clusterSize: "PS_10"
171
177
  * });
172
178
  *
@@ -174,7 +180,7 @@ export type Database = DatabaseProps & {
174
180
  * // Create a database with specific region and settings
175
181
  * const db = await Database("my-app-db", {
176
182
  * name: "my-app-db",
177
- * organizationId: "my-org",
183
+ * organization: "my-org",
178
184
  * region: {
179
185
  * slug: "us-east"
180
186
  * },
@@ -188,7 +194,7 @@ export type Database = DatabaseProps & {
188
194
  * // Create a database with custom API key
189
195
  * const db = await Database("my-app-db", {
190
196
  * name: "my-app-db",
191
- * organizationId: "my-org",
197
+ * organization: "my-org",
192
198
  * apiKey: alchemy.secret(process.env.CUSTOM_PLANETSCALE_TOKEN),
193
199
  * clusterSize: "PS_10"
194
200
  * });
@@ -210,11 +216,22 @@ export const Database = Resource(
210
216
  arch: props.arch,
211
217
  region: props.region?.slug,
212
218
  });
219
+ const organization =
220
+ // @ts-expect-error - organizationId is a legacy thing, we keep this so we can destroy
221
+ this.output?.organizationId ??
222
+ props.organization ??
223
+ process.env.PLANETSCALE_ORGANIZATION ??
224
+ process.env.PLANETSCALE_ORG_ID;
225
+ if (!organization) {
226
+ throw new Error(
227
+ "PlanetScale organization is required. Please set the `organization` property or the `PLANETSCALE_ORGANIZATION` environment variable.",
228
+ );
229
+ }
213
230
 
214
231
  if (this.phase === "update" && this.output.name !== databaseName) {
215
232
  await api.updateDatabaseSettings({
216
233
  path: {
217
- organization: props.organizationId,
234
+ organization,
218
235
  name: this.output.name,
219
236
  },
220
237
  body: { new_name: databaseName },
@@ -225,7 +242,7 @@ export const Database = Resource(
225
242
  if (this.output?.name) {
226
243
  const response = await api.deleteDatabase({
227
244
  path: {
228
- organization: props.organizationId,
245
+ organization,
229
246
  name: this.output.name,
230
247
  },
231
248
  throwOnError: false,
@@ -243,7 +260,7 @@ export const Database = Resource(
243
260
  // Check if database exists
244
261
  const getResponse = await api.getDatabase({
245
262
  path: {
246
- organization: props.organizationId,
263
+ organization,
247
264
  name: databaseName,
248
265
  },
249
266
  throwOnError: false,
@@ -259,20 +276,20 @@ export const Database = Resource(
259
276
  if (props.defaultBranch && props.defaultBranch !== "main") {
260
277
  const branchResponse = await api.getBranch({
261
278
  path: {
262
- organization: props.organizationId,
279
+ organization,
263
280
  database: databaseName,
264
281
  name: props.defaultBranch,
265
282
  },
266
283
  throwOnError: false,
267
284
  });
268
285
  if (!branchResponse.data) {
269
- await waitForDatabaseReady(api, props.organizationId, databaseName);
286
+ await waitForDatabaseReady(api, organization, databaseName);
270
287
  }
271
288
  if (branchResponse.error && branchResponse.response.status === 404) {
272
289
  // Create the branch
273
290
  await api.createBranch({
274
291
  path: {
275
- organization: props.organizationId,
292
+ organization,
276
293
  database: databaseName,
277
294
  },
278
295
  body: {
@@ -285,7 +302,7 @@ export const Database = Resource(
285
302
 
286
303
  const { data } = await api.updateDatabaseSettings({
287
304
  path: {
288
- organization: props.organizationId,
305
+ organization,
289
306
  name: databaseName,
290
307
  },
291
308
  body: {
@@ -303,7 +320,7 @@ export const Database = Resource(
303
320
 
304
321
  await ensureProductionBranchClusterSize(
305
322
  api,
306
- props.organizationId,
323
+ organization,
307
324
  databaseName,
308
325
  props.defaultBranch || "main",
309
326
  data.kind,
@@ -320,6 +337,7 @@ export const Database = Resource(
320
337
  createdAt: data.created_at,
321
338
  updatedAt: data.updated_at,
322
339
  htmlUrl: data.html_url,
340
+ organization,
323
341
  };
324
342
  }
325
343
 
@@ -330,7 +348,7 @@ export const Database = Resource(
330
348
  // Create new database
331
349
  await api.createDatabase({
332
350
  path: {
333
- organization: props.organizationId,
351
+ organization,
334
352
  },
335
353
  body: {
336
354
  name: databaseName,
@@ -343,7 +361,7 @@ export const Database = Resource(
343
361
  // These settings can't be set on creation, so we need to patch them after creation.
344
362
  const { data } = await api.updateDatabaseSettings({
345
363
  path: {
346
- organization: props.organizationId,
364
+ organization,
347
365
  name: databaseName,
348
366
  },
349
367
  body: {
@@ -360,12 +378,12 @@ export const Database = Resource(
360
378
 
361
379
  // If a non-'main' default branch is specified, create it
362
380
  if (props.defaultBranch && props.defaultBranch !== "main") {
363
- await waitForDatabaseReady(api, props.organizationId, databaseName);
381
+ await waitForDatabaseReady(api, organization, databaseName);
364
382
 
365
383
  // Check if branch exists
366
384
  const branchResponse = await api.getBranch({
367
385
  path: {
368
- organization: props.organizationId,
386
+ organization,
369
387
  database: databaseName,
370
388
  name: props.defaultBranch,
371
389
  },
@@ -376,7 +394,7 @@ export const Database = Resource(
376
394
  // Create the branch
377
395
  await api.createBranch({
378
396
  path: {
379
- organization: props.organizationId,
397
+ organization,
380
398
  database: databaseName,
381
399
  },
382
400
  body: {
@@ -387,7 +405,7 @@ export const Database = Resource(
387
405
 
388
406
  await ensureProductionBranchClusterSize(
389
407
  api,
390
- props.organizationId,
408
+ organization,
391
409
  databaseName,
392
410
  props.defaultBranch || "main",
393
411
  data.kind,
@@ -397,7 +415,7 @@ export const Database = Resource(
397
415
  // Update database to use new branch as default
398
416
  const { data: updatedData } = await api.updateDatabaseSettings({
399
417
  path: {
400
- organization: props.organizationId,
418
+ organization,
401
419
  name: databaseName,
402
420
  },
403
421
  body: {
@@ -415,6 +433,7 @@ export const Database = Resource(
415
433
  createdAt: updatedData.created_at,
416
434
  updatedAt: updatedData.updated_at,
417
435
  htmlUrl: updatedData.html_url,
436
+ organization,
418
437
  };
419
438
  }
420
439
  }
@@ -429,6 +448,7 @@ export const Database = Resource(
429
448
  createdAt: data.created_at,
430
449
  updatedAt: data.updated_at,
431
450
  htmlUrl: data.html_url,
451
+ organization,
432
452
  };
433
453
  },
434
454
  );
@@ -1,6 +1,7 @@
1
1
  export * from "./api.ts";
2
2
  export * from "./branch.ts";
3
3
  export * from "./database.ts";
4
+ export * from "./organization.ts";
4
5
  export * from "./password.ts";
5
6
  export * from "./role.ts";
6
7
  export * from "./utils.ts";
@@ -0,0 +1,17 @@
1
+ import { createPlanetScaleClient, type PlanetScaleProps } from "./api.ts";
2
+ import type { Organization } from "./api/types.gen.ts";
3
+
4
+ export type OrganizationRef = Organization;
5
+
6
+ export async function OrganizationRef(
7
+ name: string | Organization,
8
+ options: PlanetScaleProps = {},
9
+ ) {
10
+ const api = createPlanetScaleClient(options);
11
+ const organization = await api.getOrganization({
12
+ path: {
13
+ name: typeof name === "string" ? name : name.id,
14
+ },
15
+ });
16
+ return organization.data;
17
+ }