@astrojs/db 0.15.0 → 0.16.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 (39) hide show
  1. package/dist/_internal/core/schemas.d.ts +57 -57
  2. package/dist/_internal/core/types.d.ts +1 -1
  3. package/dist/_internal/core/utils.d.ts +1 -10
  4. package/dist/_internal/runtime/utils.d.ts +0 -15
  5. package/dist/_internal/runtime/virtual.d.ts +2 -2
  6. package/dist/core/cli/commands/execute/index.js +5 -5
  7. package/dist/core/cli/commands/push/index.js +6 -40
  8. package/dist/core/cli/commands/shell/index.js +3 -9
  9. package/dist/core/cli/commands/verify/index.js +2 -7
  10. package/dist/core/cli/index.js +4 -16
  11. package/dist/core/cli/migration-queries.d.ts +1 -4
  12. package/dist/core/cli/migration-queries.js +9 -32
  13. package/dist/core/cli/print-help.js +1 -1
  14. package/dist/core/integration/index.js +5 -8
  15. package/dist/core/integration/vite-plugin-db.d.ts +4 -4
  16. package/dist/core/integration/vite-plugin-db.js +13 -22
  17. package/dist/core/load-file.d.ts +5 -5
  18. package/dist/core/load-file.js +0 -2
  19. package/dist/core/queries.d.ts +3 -3
  20. package/dist/core/schemas.d.ts +257 -257
  21. package/dist/core/schemas.js +1 -1
  22. package/dist/core/types.d.ts +1 -1
  23. package/dist/core/utils.d.ts +1 -10
  24. package/dist/core/utils.js +2 -27
  25. package/dist/index.d.ts +1 -1
  26. package/dist/runtime/db-client.d.ts +5 -6
  27. package/dist/runtime/db-client.js +10 -167
  28. package/dist/runtime/index.d.ts +1 -1
  29. package/dist/runtime/index.js +1 -1
  30. package/dist/runtime/utils.d.ts +0 -15
  31. package/dist/runtime/utils.js +1 -26
  32. package/dist/runtime/virtual.js +21 -21
  33. package/package.json +4 -9
  34. package/dist/core/cli/commands/link/index.d.ts +0 -1
  35. package/dist/core/cli/commands/link/index.js +0 -266
  36. package/dist/core/cli/commands/login/index.d.ts +0 -8
  37. package/dist/core/cli/commands/login/index.js +0 -76
  38. package/dist/core/cli/commands/logout/index.d.ts +0 -1
  39. package/dist/core/cli/commands/logout/index.js +0 -9
@@ -1,266 +0,0 @@
1
- import { mkdir, writeFile } from "node:fs/promises";
2
- import { homedir } from "node:os";
3
- import { basename } from "node:path";
4
- import {
5
- MISSING_SESSION_ID_ERROR,
6
- PROJECT_ID_FILE,
7
- getAstroStudioUrl,
8
- getSessionIdFromFile
9
- } from "@astrojs/studio";
10
- import { slug } from "github-slugger";
11
- import { bgRed, cyan } from "kleur/colors";
12
- import prompts from "prompts";
13
- import yoctoSpinner from "yocto-spinner";
14
- import { safeFetch } from "../../../../runtime/utils.js";
15
- async function cmd() {
16
- const sessionToken = await getSessionIdFromFile();
17
- if (!sessionToken) {
18
- console.error(MISSING_SESSION_ID_ERROR);
19
- process.exit(1);
20
- }
21
- await promptBegin();
22
- const isLinkExisting = await promptLinkExisting();
23
- if (isLinkExisting) {
24
- const workspaceId = await promptWorkspace(sessionToken);
25
- const existingProjectData = await promptExistingProjectName({ workspaceId });
26
- return await linkProject(existingProjectData.id);
27
- }
28
- const isLinkNew = await promptLinkNew();
29
- if (isLinkNew) {
30
- const workspaceId = await promptWorkspace(sessionToken);
31
- const newProjectName = await promptNewProjectName();
32
- const newProjectRegion = await promptNewProjectRegion();
33
- const spinner = yoctoSpinner({ text: "Creating new project..." }).start();
34
- const newProjectData = await createNewProject({
35
- workspaceId,
36
- name: newProjectName,
37
- region: newProjectRegion
38
- });
39
- await new Promise((r) => setTimeout(r, 4e3));
40
- spinner.success("Project created!");
41
- return await linkProject(newProjectData.id);
42
- }
43
- }
44
- async function linkProject(id) {
45
- await mkdir(new URL(".", PROJECT_ID_FILE), { recursive: true });
46
- await writeFile(PROJECT_ID_FILE, `${id}`);
47
- console.info("Project linked.");
48
- }
49
- async function getWorkspaces(sessionToken) {
50
- const linkUrl = new URL(getAstroStudioUrl() + "/api/cli/workspaces.list");
51
- const response = await safeFetch(
52
- linkUrl,
53
- {
54
- method: "POST",
55
- headers: {
56
- Authorization: `Bearer ${sessionToken}`,
57
- "Content-Type": "application/json"
58
- }
59
- },
60
- (res) => {
61
- if (res.status === 401) {
62
- throw new Error(
63
- `${bgRed("Unauthorized")}
64
-
65
- Are you logged in?
66
- Run ${cyan(
67
- "astro login"
68
- )} to authenticate and then try linking again.
69
-
70
- `
71
- );
72
- }
73
- throw new Error(`Failed to fetch user workspace: ${res.status} ${res.statusText}`);
74
- }
75
- );
76
- const { data, success } = await response.json();
77
- if (!success) {
78
- throw new Error(`Failed to fetch user's workspace.`);
79
- }
80
- return data;
81
- }
82
- async function promptWorkspace(sessionToken) {
83
- const workspaces = await getWorkspaces(sessionToken);
84
- if (workspaces.length === 0) {
85
- console.error("No workspaces found.");
86
- process.exit(1);
87
- }
88
- if (workspaces.length === 1) {
89
- return workspaces[0].id;
90
- }
91
- const { workspaceId } = await prompts({
92
- type: "autocomplete",
93
- name: "workspaceId",
94
- message: "Select your workspace:",
95
- limit: 5,
96
- choices: workspaces.map((w) => ({ title: w.name, value: w.id }))
97
- });
98
- if (typeof workspaceId !== "string") {
99
- console.log("Canceled.");
100
- process.exit(0);
101
- }
102
- return workspaceId;
103
- }
104
- async function createNewProject({
105
- workspaceId,
106
- name,
107
- region
108
- }) {
109
- const linkUrl = new URL(getAstroStudioUrl() + "/api/cli/projects.create");
110
- const response = await safeFetch(
111
- linkUrl,
112
- {
113
- method: "POST",
114
- headers: {
115
- Authorization: `Bearer ${await getSessionIdFromFile()}`,
116
- "Content-Type": "application/json"
117
- },
118
- body: JSON.stringify({ workspaceId, name, region })
119
- },
120
- (res) => {
121
- if (res.status === 401) {
122
- console.error(
123
- `${bgRed("Unauthorized")}
124
-
125
- Are you logged in?
126
- Run ${cyan(
127
- "astro login"
128
- )} to authenticate and then try linking again.
129
-
130
- `
131
- );
132
- process.exit(1);
133
- }
134
- console.error(`Failed to create project: ${res.status} ${res.statusText}`);
135
- process.exit(1);
136
- }
137
- );
138
- const { data, success } = await response.json();
139
- if (!success) {
140
- console.error(`Failed to create project.`);
141
- process.exit(1);
142
- }
143
- return { id: data.id, idName: data.idName };
144
- }
145
- async function promptExistingProjectName({ workspaceId }) {
146
- const linkUrl = new URL(getAstroStudioUrl() + "/api/cli/projects.list");
147
- const response = await safeFetch(
148
- linkUrl,
149
- {
150
- method: "POST",
151
- headers: {
152
- Authorization: `Bearer ${await getSessionIdFromFile()}`,
153
- "Content-Type": "application/json"
154
- },
155
- body: JSON.stringify({ workspaceId })
156
- },
157
- (res) => {
158
- if (res.status === 401) {
159
- console.error(
160
- `${bgRed("Unauthorized")}
161
-
162
- Are you logged in?
163
- Run ${cyan(
164
- "astro login"
165
- )} to authenticate and then try linking again.
166
-
167
- `
168
- );
169
- process.exit(1);
170
- }
171
- console.error(`Failed to fetch projects: ${res.status} ${res.statusText}`);
172
- process.exit(1);
173
- }
174
- );
175
- const { data, success } = await response.json();
176
- if (!success) {
177
- console.error(`Failed to fetch projects.`);
178
- process.exit(1);
179
- }
180
- const { projectId } = await prompts({
181
- type: "autocomplete",
182
- name: "projectId",
183
- message: "What is your project name?",
184
- limit: 5,
185
- choices: data.map((p) => ({ title: p.name, value: p.id }))
186
- });
187
- if (typeof projectId !== "string") {
188
- console.log("Canceled.");
189
- process.exit(0);
190
- }
191
- const selectedProjectData = data.find((p) => p.id === projectId);
192
- return selectedProjectData;
193
- }
194
- async function promptBegin() {
195
- const prettyCwd = process.cwd().replace(homedir(), "~");
196
- const { begin } = await prompts({
197
- type: "confirm",
198
- name: "begin",
199
- message: `Link "${prettyCwd}" with Astro Studio?`,
200
- initial: true
201
- });
202
- if (!begin) {
203
- console.log("Canceled.");
204
- process.exit(0);
205
- }
206
- }
207
- async function promptLinkExisting() {
208
- const { linkExisting } = await prompts({
209
- type: "confirm",
210
- name: "linkExisting",
211
- message: `Link with an existing project in Astro Studio?`,
212
- initial: true
213
- });
214
- return !!linkExisting;
215
- }
216
- async function promptLinkNew() {
217
- const { linkNew } = await prompts({
218
- type: "confirm",
219
- name: "linkNew",
220
- message: `Create a new project in Astro Studio?`,
221
- initial: true
222
- });
223
- if (!linkNew) {
224
- console.log("Canceled.");
225
- process.exit(0);
226
- }
227
- return true;
228
- }
229
- async function promptNewProjectName() {
230
- const { newProjectName } = await prompts({
231
- type: "text",
232
- name: "newProjectName",
233
- message: `What is your new project's name?`,
234
- initial: basename(process.cwd()),
235
- format: (val) => slug(val)
236
- });
237
- if (!newProjectName) {
238
- console.log("Canceled.");
239
- process.exit(0);
240
- }
241
- return newProjectName;
242
- }
243
- async function promptNewProjectRegion() {
244
- const { newProjectRegion } = await prompts({
245
- type: "select",
246
- name: "newProjectRegion",
247
- message: `Where should your new database live?`,
248
- choices: [
249
- { title: "North America (East)", value: "NorthAmericaEast" },
250
- { title: "North America (West)", value: "NorthAmericaWest" },
251
- { title: "Europe (Amsterdam)", value: "EuropeCentral" },
252
- { title: "South America (Brazil)", value: "SouthAmericaEast" },
253
- { title: "Asia (India)", value: "AsiaSouth" },
254
- { title: "Asia (Japan)", value: "AsiaNorthEast" }
255
- ],
256
- initial: 0
257
- });
258
- if (!newProjectRegion) {
259
- console.log("Canceled.");
260
- process.exit(0);
261
- }
262
- return newProjectRegion;
263
- }
264
- export {
265
- cmd
266
- };
@@ -1,8 +0,0 @@
1
- import type { AstroConfig } from 'astro';
2
- import type { Arguments } from 'yargs-parser';
3
- import type { DBConfig } from '../../../types.js';
4
- export declare function cmd({ flags, }: {
5
- astroConfig: AstroConfig;
6
- dbConfig: DBConfig;
7
- flags: Arguments;
8
- }): Promise<void>;
@@ -1,76 +0,0 @@
1
- import { mkdir, writeFile } from "node:fs/promises";
2
- import { createServer as _createServer } from "node:http";
3
- import { SESSION_LOGIN_FILE, getAstroStudioUrl } from "@astrojs/studio";
4
- import { listen } from "async-listen";
5
- import { cyan } from "kleur/colors";
6
- import open from "open";
7
- import prompt from "prompts";
8
- import yoctoSpinner from "yocto-spinner";
9
- const isWebContainer = (
10
- // Stackblitz heuristic
11
- process.versions?.webcontainer ?? // GitHub Codespaces heuristic
12
- process.env.CODESPACE_NAME
13
- );
14
- async function cmd({
15
- flags
16
- }) {
17
- let session = flags.session;
18
- if (!session && isWebContainer) {
19
- console.log(`Please visit the following URL in your web browser:`);
20
- console.log(cyan(`${getAstroStudioUrl()}/auth/cli/login`));
21
- console.log(`After login in complete, enter the verification code displayed:`);
22
- const response = await prompt({
23
- type: "text",
24
- name: "session",
25
- message: "Verification code:"
26
- });
27
- if (!response.session) {
28
- console.error("Cancelling login.");
29
- process.exit(0);
30
- }
31
- session = response.session;
32
- console.log("Successfully logged in");
33
- } else if (!session) {
34
- const { url, promise } = await createServer();
35
- const loginUrl = new URL("/auth/cli/login", getAstroStudioUrl());
36
- loginUrl.searchParams.set("returnTo", url);
37
- console.log(`Opening the following URL in your browser...`);
38
- console.log(cyan(loginUrl.href));
39
- console.log(`If something goes wrong, copy-and-paste the URL into your browser.`);
40
- open(loginUrl.href);
41
- const spinner = yoctoSpinner({ text: "Waiting for confirmation..." });
42
- session = await promise;
43
- spinner.success("Successfully logged in");
44
- }
45
- await mkdir(new URL(".", SESSION_LOGIN_FILE), { recursive: true });
46
- await writeFile(SESSION_LOGIN_FILE, `${session}`);
47
- }
48
- async function createServer() {
49
- let resolve, reject;
50
- const server = _createServer((req, res) => {
51
- const url = new URL(req.url ?? "/", `http://${req.headers.host}`);
52
- const sessionParam = url.searchParams.get("session");
53
- res.statusCode = 302;
54
- if (!sessionParam) {
55
- res.setHeader("location", getAstroStudioUrl() + "/auth/cli/error");
56
- reject(new Error("Failed to log in"));
57
- } else {
58
- res.setHeader("location", getAstroStudioUrl() + "/auth/cli/success");
59
- resolve(sessionParam);
60
- }
61
- res.end();
62
- });
63
- const { port } = await listen(server, 0, "127.0.0.1");
64
- const serverUrl = `http://localhost:${port}`;
65
- const sessionPromise = new Promise((_resolve, _reject) => {
66
- resolve = _resolve;
67
- reject = _reject;
68
- }).finally(() => {
69
- server.closeAllConnections();
70
- server.close();
71
- });
72
- return { url: serverUrl, promise: sessionPromise };
73
- }
74
- export {
75
- cmd
76
- };
@@ -1 +0,0 @@
1
- export declare function cmd(): Promise<void>;
@@ -1,9 +0,0 @@
1
- import { unlink } from "node:fs/promises";
2
- import { SESSION_LOGIN_FILE } from "@astrojs/studio";
3
- async function cmd() {
4
- await unlink(SESSION_LOGIN_FILE);
5
- console.log("Successfully logged out of Astro Studio.");
6
- }
7
- export {
8
- cmd
9
- };