create-db 1.0.1-pr42-claim-worker-migrate-nextjs-17058303249.0 → 1.0.1-pr43-DC-4828-json-flag-17103676514.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.
- package/index.js +65 -14
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -100,11 +100,12 @@ async function parseArgs() {
|
|
|
100
100
|
const args = process.argv.slice(2);
|
|
101
101
|
const flags = {};
|
|
102
102
|
|
|
103
|
-
const allowedFlags = ["region", "help", "list-regions", "interactive"];
|
|
103
|
+
const allowedFlags = ["region", "help", "list-regions", "interactive", "json"];
|
|
104
104
|
const shorthandMap = {
|
|
105
105
|
r: "region",
|
|
106
106
|
i: "interactive",
|
|
107
107
|
h: "help",
|
|
108
|
+
j: "json",
|
|
108
109
|
};
|
|
109
110
|
|
|
110
111
|
const exitWithError = (message) => {
|
|
@@ -184,11 +185,14 @@ async function parseArgs() {
|
|
|
184
185
|
/**
|
|
185
186
|
* Fetch available regions from the API.
|
|
186
187
|
*/
|
|
187
|
-
export async function getRegions() {
|
|
188
|
+
export async function getRegions(returnJson = false) {
|
|
188
189
|
const url = `${CREATE_DB_WORKER_URL}/regions`;
|
|
189
190
|
const res = await fetch(url);
|
|
190
191
|
|
|
191
192
|
if (!res.ok) {
|
|
193
|
+
if (returnJson) {
|
|
194
|
+
throw new Error(`Failed to fetch regions. Status: ${res.status} ${res.statusText}`);
|
|
195
|
+
}
|
|
192
196
|
handleError(
|
|
193
197
|
`Failed to fetch regions. Status: ${res.status} ${res.statusText}`
|
|
194
198
|
);
|
|
@@ -199,6 +203,9 @@ export async function getRegions() {
|
|
|
199
203
|
const regions = Array.isArray(data) ? data : data.data;
|
|
200
204
|
return regions.filter((region) => region.status === "available");
|
|
201
205
|
} catch (e) {
|
|
206
|
+
if (returnJson) {
|
|
207
|
+
throw new Error("Failed to parse JSON from /regions endpoint.");
|
|
208
|
+
}
|
|
202
209
|
handleError("Failed to parse JSON from /regions endpoint.", e);
|
|
203
210
|
}
|
|
204
211
|
}
|
|
@@ -206,11 +213,14 @@ export async function getRegions() {
|
|
|
206
213
|
/**
|
|
207
214
|
* Validate the provided region against the available list.
|
|
208
215
|
*/
|
|
209
|
-
export async function validateRegion(region) {
|
|
210
|
-
const regions = await getRegions();
|
|
216
|
+
export async function validateRegion(region, returnJson = false) {
|
|
217
|
+
const regions = await getRegions(returnJson);
|
|
211
218
|
const regionIds = regions.map((r) => r.id);
|
|
212
219
|
|
|
213
220
|
if (!regionIds.includes(region)) {
|
|
221
|
+
if (returnJson) {
|
|
222
|
+
throw new Error(`Invalid region: ${region}. Available regions: ${regionIds.join(", ")}`);
|
|
223
|
+
}
|
|
214
224
|
handleError(
|
|
215
225
|
`Invalid region: ${chalk.yellow(region)}.\nAvailable regions: ${chalk.green(
|
|
216
226
|
regionIds.join(", ")
|
|
@@ -279,10 +289,15 @@ async function promptForRegion(defaultRegion) {
|
|
|
279
289
|
return region;
|
|
280
290
|
}
|
|
281
291
|
|
|
292
|
+
|
|
293
|
+
|
|
282
294
|
// Create a database
|
|
283
|
-
async function createDatabase(name, region) {
|
|
284
|
-
|
|
285
|
-
|
|
295
|
+
async function createDatabase(name, region, returnJson = false) {
|
|
296
|
+
let s;
|
|
297
|
+
if (!returnJson) {
|
|
298
|
+
s = spinner();
|
|
299
|
+
s.start("Creating your database...");
|
|
300
|
+
}
|
|
286
301
|
|
|
287
302
|
const resp = await fetch(`${CREATE_DB_WORKER_URL}/create`, {
|
|
288
303
|
method: "POST",
|
|
@@ -292,9 +307,19 @@ async function createDatabase(name, region) {
|
|
|
292
307
|
|
|
293
308
|
// Rate limit exceeded
|
|
294
309
|
if (resp.status === 429) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
310
|
+
if (returnJson) {
|
|
311
|
+
return {
|
|
312
|
+
error: "rate_limit_exceeded",
|
|
313
|
+
message: "We're experiencing a high volume of requests. Please try again later.",
|
|
314
|
+
status: 429
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (s) {
|
|
319
|
+
s.stop(
|
|
320
|
+
"We're experiencing a high volume of requests. Please try again later."
|
|
321
|
+
);
|
|
322
|
+
}
|
|
298
323
|
|
|
299
324
|
// Track database creation failure
|
|
300
325
|
try {
|
|
@@ -314,9 +339,19 @@ async function createDatabase(name, region) {
|
|
|
314
339
|
const result = await resp.json();
|
|
315
340
|
|
|
316
341
|
if (result.error) {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
342
|
+
if (returnJson) {
|
|
343
|
+
return {
|
|
344
|
+
error: "api_error",
|
|
345
|
+
message: result.error.message || "Unknown error",
|
|
346
|
+
details: result.error
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (s) {
|
|
351
|
+
s.stop(
|
|
352
|
+
`Error creating database: ${result.error.message || "Unknown error"}`
|
|
353
|
+
);
|
|
354
|
+
}
|
|
320
355
|
|
|
321
356
|
// Track database creation failure
|
|
322
357
|
try {
|
|
@@ -332,7 +367,13 @@ async function createDatabase(name, region) {
|
|
|
332
367
|
process.exit(1);
|
|
333
368
|
}
|
|
334
369
|
|
|
335
|
-
|
|
370
|
+
if (returnJson) {
|
|
371
|
+
return result;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (s) {
|
|
375
|
+
s.stop("Database created successfully!");
|
|
376
|
+
}
|
|
336
377
|
|
|
337
378
|
const expiryDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
|
338
379
|
const expiryFormatted = expiryDate.toLocaleString();
|
|
@@ -436,6 +477,16 @@ async function main() {
|
|
|
436
477
|
process.exit(0);
|
|
437
478
|
}
|
|
438
479
|
|
|
480
|
+
if (flags.json) {
|
|
481
|
+
if (chooseRegionPrompt) {
|
|
482
|
+
region = await promptForRegion(region);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const result = await createDatabase(name, region, true);
|
|
486
|
+
console.log(JSON.stringify(result, null, 2));
|
|
487
|
+
process.exit(0);
|
|
488
|
+
}
|
|
489
|
+
|
|
439
490
|
// Apply command line flags
|
|
440
491
|
if (flags.region) {
|
|
441
492
|
region = flags.region;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-db",
|
|
3
|
-
"version": "1.0.1-
|
|
3
|
+
"version": "1.0.1-pr43-DC-4828-json-flag-17103676514.0",
|
|
4
4
|
"description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "",
|