relight-cli 0.2.0 → 0.3.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.
@@ -7,110 +7,51 @@ import {
7
7
  importD1,
8
8
  getWorkersSubdomain,
9
9
  } from "../../clouds/cf.js";
10
- import { getAppConfig, pushAppConfig } from "./app.js";
11
10
  import { randomBytes } from "crypto";
12
11
 
13
- export async function createDatabase(cfg, appName, opts = {}) {
14
- if (!opts.skipAppConfig) {
15
- var appConfig = await getAppConfig(cfg, appName);
16
- if (!appConfig) {
17
- throw new Error(`App ${appName} not found.`);
18
- }
19
- if (appConfig.dbId) {
20
- throw new Error(`App ${appName} already has a database: ${appConfig.dbName}`);
21
- }
22
- }
23
-
24
- var dbName = `relight-${appName}`;
25
- var result = await createD1Database(cfg.accountId, cfg.apiToken, dbName, {
12
+ export async function createDatabase(cfg, name, opts = {}) {
13
+ var d1Name = `relight-${name}`;
14
+ var result = await createD1Database(cfg.accountId, cfg.apiToken, d1Name, {
26
15
  locationHint: opts.location,
27
16
  jurisdiction: opts.jurisdiction,
28
17
  });
29
18
 
30
19
  var subdomain = await getWorkersSubdomain(cfg.accountId, cfg.apiToken);
31
20
  var connectionUrl = subdomain
32
- ? `https://relight-${appName}.${subdomain}.workers.dev`
21
+ ? `https://relight-${name}.${subdomain}.workers.dev`
33
22
  : null;
34
23
 
35
24
  var dbToken = randomBytes(32).toString("hex");
36
25
 
37
- if (!opts.skipAppConfig) {
38
- appConfig.dbId = result.uuid;
39
- appConfig.dbName = dbName;
40
-
41
- if (!appConfig.envKeys) appConfig.envKeys = [];
42
- if (!appConfig.secretKeys) appConfig.secretKeys = [];
43
- if (!appConfig.env) appConfig.env = {};
44
-
45
- if (connectionUrl) {
46
- appConfig.env["DB_URL"] = connectionUrl;
47
- if (!appConfig.envKeys.includes("DB_URL")) appConfig.envKeys.push("DB_URL");
48
- }
49
-
50
- appConfig.env["DB_TOKEN"] = "[hidden]";
51
- appConfig.secretKeys = appConfig.secretKeys.filter((k) => k !== "DB_TOKEN");
52
- appConfig.secretKeys.push("DB_TOKEN");
53
- appConfig.envKeys = appConfig.envKeys.filter((k) => k !== "DB_TOKEN");
54
-
55
- var newSecrets = { DB_TOKEN: dbToken };
56
-
57
- await pushAppConfig(cfg, appName, appConfig, { newSecrets });
58
- }
59
-
60
26
  return {
61
27
  dbId: result.uuid,
62
- dbName,
28
+ dbName: d1Name,
63
29
  dbToken,
64
30
  connectionUrl,
65
31
  };
66
32
  }
67
33
 
68
- export async function destroyDatabase(cfg, appName, opts = {}) {
69
- var dbId = opts.dbId;
70
- if (!dbId) {
71
- var appConfig = await getAppConfig(cfg, appName);
72
- if (!appConfig || !appConfig.dbId) {
73
- throw new Error(`App ${appName} does not have a database.`);
74
- }
75
- dbId = appConfig.dbId;
76
- }
77
-
78
- await deleteD1Database(cfg.accountId, cfg.apiToken, dbId);
79
-
34
+ export async function destroyDatabase(cfg, name, opts = {}) {
80
35
  if (!opts.dbId) {
81
- delete appConfig.dbId;
82
- delete appConfig.dbName;
83
-
84
- if (appConfig.env) {
85
- delete appConfig.env["DB_URL"];
86
- delete appConfig.env["DB_TOKEN"];
87
- }
88
- if (appConfig.envKeys) appConfig.envKeys = appConfig.envKeys.filter((k) => k !== "DB_URL");
89
- if (appConfig.secretKeys) appConfig.secretKeys = appConfig.secretKeys.filter((k) => k !== "DB_TOKEN");
90
-
91
- await pushAppConfig(cfg, appName, appConfig);
36
+ throw new Error("dbId is required to destroy a CF database.");
92
37
  }
38
+ await deleteD1Database(cfg.accountId, cfg.apiToken, opts.dbId);
93
39
  }
94
40
 
95
- export async function getDatabaseInfo(cfg, appName, opts = {}) {
96
- var dbId = opts.dbId;
97
- if (!dbId) {
98
- var appConfig = await getAppConfig(cfg, appName);
99
- if (!appConfig || !appConfig.dbId) {
100
- throw new Error(`App ${appName} does not have a database.`);
101
- }
102
- dbId = appConfig.dbId;
41
+ export async function getDatabaseInfo(cfg, name, opts = {}) {
42
+ if (!opts.dbId) {
43
+ throw new Error("dbId is required to get CF database info.");
103
44
  }
104
45
 
105
- var dbDetails = await getD1Database(cfg.accountId, cfg.apiToken, dbId);
46
+ var dbDetails = await getD1Database(cfg.accountId, cfg.apiToken, opts.dbId);
106
47
  var subdomain = await getWorkersSubdomain(cfg.accountId, cfg.apiToken);
107
48
  var connectionUrl = subdomain
108
- ? `https://relight-${appName}.${subdomain}.workers.dev`
49
+ ? `https://relight-${name}.${subdomain}.workers.dev`
109
50
  : null;
110
51
 
111
52
  return {
112
- dbId,
113
- dbName: dbDetails.name || `relight-${appName}`,
53
+ dbId: opts.dbId,
54
+ dbName: dbDetails.name || `relight-${name}`,
114
55
  connectionUrl,
115
56
  size: dbDetails.file_size,
116
57
  numTables: dbDetails.num_tables,
@@ -118,27 +59,18 @@ export async function getDatabaseInfo(cfg, appName, opts = {}) {
118
59
  };
119
60
  }
120
61
 
121
- export async function queryDatabase(cfg, appName, sql, params, opts = {}) {
122
- var dbId = opts.dbId;
123
- if (!dbId) {
124
- var appConfig = await getAppConfig(cfg, appName);
125
- if (!appConfig || !appConfig.dbId) {
126
- throw new Error(`App ${appName} does not have a database.`);
127
- }
128
- dbId = appConfig.dbId;
62
+ export async function queryDatabase(cfg, name, sql, params, opts = {}) {
63
+ if (!opts.dbId) {
64
+ throw new Error("dbId is required to query a CF database.");
129
65
  }
130
- return queryD1(cfg.accountId, cfg.apiToken, dbId, sql, params);
66
+ return queryD1(cfg.accountId, cfg.apiToken, opts.dbId, sql, params);
131
67
  }
132
68
 
133
- export async function importDatabase(cfg, appName, sqlContent, opts = {}) {
134
- var dbId = opts.dbId;
135
- if (!dbId) {
136
- var appConfig = await getAppConfig(cfg, appName);
137
- if (!appConfig || !appConfig.dbId) {
138
- throw new Error(`App ${appName} does not have a database.`);
139
- }
140
- dbId = appConfig.dbId;
69
+ export async function importDatabase(cfg, name, sqlContent, opts = {}) {
70
+ if (!opts.dbId) {
71
+ throw new Error("dbId is required to import into a CF database.");
141
72
  }
73
+ var dbId = opts.dbId;
142
74
 
143
75
  // Step 1: Init import
144
76
  var initRes = await importD1(cfg.accountId, cfg.apiToken, dbId, {
@@ -183,15 +115,11 @@ export async function importDatabase(cfg, appName, sqlContent, opts = {}) {
183
115
  }
184
116
  }
185
117
 
186
- export async function exportDatabase(cfg, appName, opts = {}) {
187
- var dbId = opts.dbId;
188
- if (!dbId) {
189
- var appConfig = await getAppConfig(cfg, appName);
190
- if (!appConfig || !appConfig.dbId) {
191
- throw new Error(`App ${appName} does not have a database.`);
192
- }
193
- dbId = appConfig.dbId;
118
+ export async function exportDatabase(cfg, name, opts = {}) {
119
+ if (!opts.dbId) {
120
+ throw new Error("dbId is required to export a CF database.");
194
121
  }
122
+ var dbId = opts.dbId;
195
123
 
196
124
  var exportRes = await exportD1(cfg.accountId, cfg.apiToken, dbId, {
197
125
  output_format: "polling",
@@ -221,48 +149,22 @@ export async function exportDatabase(cfg, appName, opts = {}) {
221
149
  return dumpRes.text();
222
150
  }
223
151
 
224
- export async function rotateToken(cfg, appName, opts = {}) {
152
+ export async function rotateToken(cfg, name, opts = {}) {
225
153
  var subdomain = await getWorkersSubdomain(cfg.accountId, cfg.apiToken);
226
154
  var connectionUrl = subdomain
227
- ? `https://relight-${appName}.${subdomain}.workers.dev`
155
+ ? `https://relight-${name}.${subdomain}.workers.dev`
228
156
  : null;
229
157
 
230
158
  var dbToken = randomBytes(32).toString("hex");
231
159
 
232
- if (!opts.skipAppConfig) {
233
- var appConfig = await getAppConfig(cfg, appName);
234
- if (!appConfig || !appConfig.dbId) {
235
- throw new Error(`App ${appName} does not have a database.`);
236
- }
237
-
238
- if (!appConfig.envKeys) appConfig.envKeys = [];
239
- if (!appConfig.secretKeys) appConfig.secretKeys = [];
240
- if (!appConfig.env) appConfig.env = {};
241
-
242
- appConfig.env["DB_TOKEN"] = "[hidden]";
243
- if (!appConfig.secretKeys.includes("DB_TOKEN")) appConfig.secretKeys.push("DB_TOKEN");
244
- appConfig.envKeys = appConfig.envKeys.filter((k) => k !== "DB_TOKEN");
245
-
246
- if (connectionUrl) {
247
- appConfig.env["DB_URL"] = connectionUrl;
248
- if (!appConfig.envKeys.includes("DB_URL")) appConfig.envKeys.push("DB_URL");
249
- }
250
-
251
- await pushAppConfig(cfg, appName, appConfig, { newSecrets: { DB_TOKEN: dbToken } });
252
- }
253
-
254
160
  return { dbToken, connectionUrl };
255
161
  }
256
162
 
257
- export async function resetDatabase(cfg, appName, opts = {}) {
258
- var dbId = opts.dbId;
259
- if (!dbId) {
260
- var appConfig = await getAppConfig(cfg, appName);
261
- if (!appConfig || !appConfig.dbId) {
262
- throw new Error(`App ${appName} does not have a database.`);
263
- }
264
- dbId = appConfig.dbId;
163
+ export async function resetDatabase(cfg, name, opts = {}) {
164
+ if (!opts.dbId) {
165
+ throw new Error("dbId is required to reset a CF database.");
265
166
  }
167
+ var dbId = opts.dbId;
266
168
 
267
169
  var results = await queryD1(
268
170
  cfg.accountId, cfg.apiToken, dbId,