@astrojs/db 0.0.0-edge-nested-20240223135627
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/LICENSE +59 -0
- package/config-augment.d.ts +4 -0
- package/dist/core/cli/commands/gen/index.d.ts +6 -0
- package/dist/core/cli/commands/gen/index.js +39 -0
- package/dist/core/cli/commands/link/index.d.ts +8 -0
- package/dist/core/cli/commands/link/index.js +78 -0
- package/dist/core/cli/commands/login/index.d.ts +6 -0
- package/dist/core/cli/commands/login/index.js +46 -0
- package/dist/core/cli/commands/logout/index.d.ts +6 -0
- package/dist/core/cli/commands/logout/index.js +9 -0
- package/dist/core/cli/commands/push/index.d.ts +6 -0
- package/dist/core/cli/commands/push/index.js +209 -0
- package/dist/core/cli/commands/shell/index.d.ts +6 -0
- package/dist/core/cli/commands/shell/index.js +15 -0
- package/dist/core/cli/commands/verify/index.d.ts +6 -0
- package/dist/core/cli/commands/verify/index.js +43 -0
- package/dist/core/cli/index.d.ts +6 -0
- package/dist/core/cli/index.js +68 -0
- package/dist/core/cli/migration-queries.d.ts +26 -0
- package/dist/core/cli/migration-queries.js +418 -0
- package/dist/core/cli/migrations.d.ts +34 -0
- package/dist/core/cli/migrations.js +129 -0
- package/dist/core/consts.d.ts +6 -0
- package/dist/core/consts.js +17 -0
- package/dist/core/errors.d.ts +7 -0
- package/dist/core/errors.js +54 -0
- package/dist/core/integration/error-map.d.ts +6 -0
- package/dist/core/integration/error-map.js +79 -0
- package/dist/core/integration/file-url.d.ts +2 -0
- package/dist/core/integration/file-url.js +84 -0
- package/dist/core/integration/index.d.ts +2 -0
- package/dist/core/integration/index.js +173 -0
- package/dist/core/integration/load-astro-config.d.ts +6 -0
- package/dist/core/integration/load-astro-config.js +79 -0
- package/dist/core/integration/typegen.d.ts +5 -0
- package/dist/core/integration/typegen.js +41 -0
- package/dist/core/integration/vite-plugin-db.d.ts +25 -0
- package/dist/core/integration/vite-plugin-db.js +73 -0
- package/dist/core/integration/vite-plugin-inject-env-ts.d.ts +11 -0
- package/dist/core/integration/vite-plugin-inject-env-ts.js +53 -0
- package/dist/core/queries.d.ts +78 -0
- package/dist/core/queries.js +218 -0
- package/dist/core/tokens.d.ts +11 -0
- package/dist/core/tokens.js +131 -0
- package/dist/core/types.d.ts +8059 -0
- package/dist/core/types.js +209 -0
- package/dist/core/utils.d.ts +5 -0
- package/dist/core/utils.js +18 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +16 -0
- package/dist/runtime/db-client.d.ts +12 -0
- package/dist/runtime/db-client.js +96 -0
- package/dist/runtime/drizzle.d.ts +1 -0
- package/dist/runtime/drizzle.js +48 -0
- package/dist/runtime/index.d.ts +30 -0
- package/dist/runtime/index.js +124 -0
- package/dist/runtime/types.d.ts +69 -0
- package/dist/runtime/types.js +8 -0
- package/index.d.ts +3 -0
- package/package.json +81 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Fred K. Schott
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
"""
|
|
24
|
+
This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/sveltejs/kit repository:
|
|
25
|
+
|
|
26
|
+
Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
|
|
27
|
+
|
|
28
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
29
|
+
|
|
30
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
31
|
+
|
|
32
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
"""
|
|
36
|
+
This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/vitejs/vite repository:
|
|
37
|
+
|
|
38
|
+
MIT License
|
|
39
|
+
|
|
40
|
+
Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
|
|
41
|
+
|
|
42
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
43
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
44
|
+
in the Software without restriction, including without limitation the rights
|
|
45
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
46
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
47
|
+
furnished to do so, subject to the following conditions:
|
|
48
|
+
|
|
49
|
+
The above copyright notice and this permission notice shall be included in all
|
|
50
|
+
copies or substantial portions of the Software.
|
|
51
|
+
|
|
52
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
53
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
54
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
55
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
56
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
57
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
58
|
+
SOFTWARE.
|
|
59
|
+
"""
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
import {
|
|
3
|
+
MIGRATIONS_CREATED,
|
|
4
|
+
MIGRATIONS_UP_TO_DATE,
|
|
5
|
+
getMigrationStatus,
|
|
6
|
+
initializeMigrationsDirectory
|
|
7
|
+
} from "../../migrations.js";
|
|
8
|
+
import { getMigrationQueries } from "../../migration-queries.js";
|
|
9
|
+
import { bgRed, red, reset } from "kleur/colors";
|
|
10
|
+
async function cmd({ config }) {
|
|
11
|
+
const migration = await getMigrationStatus(config);
|
|
12
|
+
if (migration.state === "no-migrations-found") {
|
|
13
|
+
await initializeMigrationsDirectory(migration.currentSnapshot);
|
|
14
|
+
console.log(MIGRATIONS_CREATED);
|
|
15
|
+
return;
|
|
16
|
+
} else if (migration.state === "up-to-date") {
|
|
17
|
+
console.log(MIGRATIONS_UP_TO_DATE);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const { oldSnapshot, newSnapshot, newFilename, diff } = migration;
|
|
21
|
+
const { queries: migrationQueries, confirmations } = await getMigrationQueries({
|
|
22
|
+
oldSnapshot,
|
|
23
|
+
newSnapshot
|
|
24
|
+
});
|
|
25
|
+
confirmations.map((message) => console.log(bgRed(" !!! ") + " " + red(message)));
|
|
26
|
+
const migrationFileContent = {
|
|
27
|
+
diff,
|
|
28
|
+
db: migrationQueries,
|
|
29
|
+
// TODO(fks): Encode the relevant data, instead of the raw message.
|
|
30
|
+
// This will give `db push` more control over the formatting of the message.
|
|
31
|
+
confirm: confirmations.map((c) => reset(c))
|
|
32
|
+
};
|
|
33
|
+
const migrationFileName = `./migrations/${newFilename}`;
|
|
34
|
+
await writeFile(migrationFileName, JSON.stringify(migrationFileContent, void 0, 2));
|
|
35
|
+
console.log(migrationFileName + " created!");
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
cmd
|
|
39
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AstroConfig } from 'astro';
|
|
2
|
+
import type { Arguments } from 'yargs-parser';
|
|
3
|
+
export declare function cmd({ flags }: {
|
|
4
|
+
config: AstroConfig;
|
|
5
|
+
flags: Arguments;
|
|
6
|
+
}): Promise<void>;
|
|
7
|
+
export declare function promptProjectName(defaultName?: string): Promise<string>;
|
|
8
|
+
export declare function promptWorkspaceName(defaultName?: string): Promise<string>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { bgRed, cyan } from "kleur/colors";
|
|
3
|
+
import prompts from "prompts";
|
|
4
|
+
import { PROJECT_ID_FILE, getSessionIdFromFile } from "../../../tokens.js";
|
|
5
|
+
import { getAstroStudioUrl } from "../../../utils.js";
|
|
6
|
+
import { MISSING_SESSION_ID_ERROR } from "../../../errors.js";
|
|
7
|
+
async function cmd({ flags }) {
|
|
8
|
+
const linkUrl = new URL(getAstroStudioUrl() + "/auth/cli/link");
|
|
9
|
+
const sessionToken = await getSessionIdFromFile();
|
|
10
|
+
if (!sessionToken) {
|
|
11
|
+
console.error(MISSING_SESSION_ID_ERROR);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
let body = { id: flags._[4] };
|
|
15
|
+
if (!body.id) {
|
|
16
|
+
const workspaceIdName = await promptWorkspaceName();
|
|
17
|
+
const projectIdName = await promptProjectName();
|
|
18
|
+
body = { projectIdName, workspaceIdName };
|
|
19
|
+
}
|
|
20
|
+
const response = await fetch(linkUrl, {
|
|
21
|
+
method: "POST",
|
|
22
|
+
headers: {
|
|
23
|
+
Authorization: `Bearer ${await getSessionIdFromFile()}`,
|
|
24
|
+
"Content-Type": "application/json"
|
|
25
|
+
},
|
|
26
|
+
body: JSON.stringify(body)
|
|
27
|
+
});
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
if (response.status === 401) {
|
|
30
|
+
console.error(
|
|
31
|
+
`${bgRed("Unauthorized")}
|
|
32
|
+
|
|
33
|
+
Are you logged in?
|
|
34
|
+
Run ${cyan(
|
|
35
|
+
"astro db login"
|
|
36
|
+
)} to authenticate and then try linking again.
|
|
37
|
+
|
|
38
|
+
`
|
|
39
|
+
);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
}
|
|
42
|
+
console.error(`Failed to link project: ${response.status} ${response.statusText}`);
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
const { data } = await response.json();
|
|
46
|
+
await mkdir(new URL(".", PROJECT_ID_FILE), { recursive: true });
|
|
47
|
+
await writeFile(PROJECT_ID_FILE, `${data.id}`);
|
|
48
|
+
console.info("Project linked.");
|
|
49
|
+
}
|
|
50
|
+
async function promptProjectName(defaultName) {
|
|
51
|
+
const { projectName } = await prompts({
|
|
52
|
+
type: "text",
|
|
53
|
+
name: "projectName",
|
|
54
|
+
message: "Project ID",
|
|
55
|
+
initial: defaultName
|
|
56
|
+
});
|
|
57
|
+
if (typeof projectName !== "string") {
|
|
58
|
+
process.exit(0);
|
|
59
|
+
}
|
|
60
|
+
return projectName;
|
|
61
|
+
}
|
|
62
|
+
async function promptWorkspaceName(defaultName) {
|
|
63
|
+
const { workspaceName } = await prompts({
|
|
64
|
+
type: "text",
|
|
65
|
+
name: "workspaceName",
|
|
66
|
+
message: "Workspace ID",
|
|
67
|
+
initial: defaultName
|
|
68
|
+
});
|
|
69
|
+
if (typeof workspaceName !== "string") {
|
|
70
|
+
process.exit(0);
|
|
71
|
+
}
|
|
72
|
+
return workspaceName;
|
|
73
|
+
}
|
|
74
|
+
export {
|
|
75
|
+
cmd,
|
|
76
|
+
promptProjectName,
|
|
77
|
+
promptWorkspaceName
|
|
78
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { cyan } from "kleur/colors";
|
|
2
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import { createServer } from "node:http";
|
|
4
|
+
import ora from "ora";
|
|
5
|
+
import { getAstroStudioUrl } from "../../../utils.js";
|
|
6
|
+
import open from "open";
|
|
7
|
+
import { SESSION_LOGIN_FILE } from "../../../tokens.js";
|
|
8
|
+
function serveAndResolveSession() {
|
|
9
|
+
let resolve, reject;
|
|
10
|
+
const sessionPromise = new Promise((_resolve, _reject) => {
|
|
11
|
+
resolve = _resolve;
|
|
12
|
+
reject = _reject;
|
|
13
|
+
});
|
|
14
|
+
const server = createServer((req, res) => {
|
|
15
|
+
res.writeHead(200);
|
|
16
|
+
res.end();
|
|
17
|
+
const url = new URL(req.url ?? "/", `http://${req.headers.host}`);
|
|
18
|
+
const session = url.searchParams.get("session");
|
|
19
|
+
if (!session) {
|
|
20
|
+
reject();
|
|
21
|
+
} else {
|
|
22
|
+
resolve(session);
|
|
23
|
+
}
|
|
24
|
+
}).listen(5710, "localhost");
|
|
25
|
+
return sessionPromise.finally(() => {
|
|
26
|
+
server.closeAllConnections();
|
|
27
|
+
server.close();
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
async function cmd({ flags }) {
|
|
31
|
+
let session = flags.session;
|
|
32
|
+
const loginUrl = getAstroStudioUrl() + "/auth/cli";
|
|
33
|
+
if (!session) {
|
|
34
|
+
console.log(`Opening ${cyan(loginUrl)} in your browser...`);
|
|
35
|
+
console.log(`If something goes wrong, copy-and-paste the URL into your browser.`);
|
|
36
|
+
open(loginUrl);
|
|
37
|
+
const spinner = ora("Waiting for confirmation...");
|
|
38
|
+
session = await serveAndResolveSession();
|
|
39
|
+
spinner.succeed("Successfully logged in!");
|
|
40
|
+
}
|
|
41
|
+
await mkdir(new URL(".", SESSION_LOGIN_FILE), { recursive: true });
|
|
42
|
+
await writeFile(SESSION_LOGIN_FILE, `${session}`);
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
cmd
|
|
46
|
+
};
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { createClient } from "@libsql/client";
|
|
2
|
+
import { drizzle as drizzleProxy } from "drizzle-orm/sqlite-proxy";
|
|
3
|
+
import { drizzle as drizzleLibsql } from "drizzle-orm/libsql";
|
|
4
|
+
import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
|
|
5
|
+
import { red } from "kleur/colors";
|
|
6
|
+
import prompts from "prompts";
|
|
7
|
+
import { recreateTables, seedData } from "../../../queries.js";
|
|
8
|
+
import { getManagedAppTokenOrExit } from "../../../tokens.js";
|
|
9
|
+
import { tablesSchema } from "../../../types.js";
|
|
10
|
+
import { getRemoteDatabaseUrl } from "../../../utils.js";
|
|
11
|
+
import { getMigrationQueries } from "../../migration-queries.js";
|
|
12
|
+
import {
|
|
13
|
+
createEmptySnapshot,
|
|
14
|
+
getMigrations,
|
|
15
|
+
getMigrationStatus,
|
|
16
|
+
loadInitialSnapshot,
|
|
17
|
+
loadMigration,
|
|
18
|
+
MIGRATION_NEEDED,
|
|
19
|
+
MIGRATIONS_NOT_INITIALIZED,
|
|
20
|
+
MIGRATIONS_UP_TO_DATE
|
|
21
|
+
} from "../../migrations.js";
|
|
22
|
+
import { MISSING_SESSION_ID_ERROR } from "../../../errors.js";
|
|
23
|
+
async function cmd({ config, flags }) {
|
|
24
|
+
const isDryRun = flags.dryRun;
|
|
25
|
+
const appToken = await getManagedAppTokenOrExit(flags.token);
|
|
26
|
+
const migration = await getMigrationStatus(config);
|
|
27
|
+
if (migration.state === "no-migrations-found") {
|
|
28
|
+
console.log(MIGRATIONS_NOT_INITIALIZED);
|
|
29
|
+
process.exit(1);
|
|
30
|
+
} else if (migration.state === "ahead") {
|
|
31
|
+
console.log(MIGRATION_NEEDED);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
const allLocalMigrations = await getMigrations();
|
|
35
|
+
let missingMigrations = [];
|
|
36
|
+
try {
|
|
37
|
+
const { data } = await prepareMigrateQuery({
|
|
38
|
+
migrations: allLocalMigrations,
|
|
39
|
+
appToken: appToken.token
|
|
40
|
+
});
|
|
41
|
+
missingMigrations = data;
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (error instanceof Error) {
|
|
44
|
+
if (error.message.startsWith("{")) {
|
|
45
|
+
const { error: { code } = { code: "" } } = JSON.parse(error.message);
|
|
46
|
+
if (code === "TOKEN_UNAUTHORIZED") {
|
|
47
|
+
console.error(MISSING_SESSION_ID_ERROR);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
console.error(error);
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
if (missingMigrations.length === 0) {
|
|
55
|
+
console.log(MIGRATIONS_UP_TO_DATE);
|
|
56
|
+
} else {
|
|
57
|
+
console.log(`Pushing ${missingMigrations.length} migrations...`);
|
|
58
|
+
await pushSchema({
|
|
59
|
+
migrations: missingMigrations,
|
|
60
|
+
appToken: appToken.token,
|
|
61
|
+
isDryRun,
|
|
62
|
+
currentSnapshot: migration.currentSnapshot
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
console.info("Pushing data...");
|
|
66
|
+
await pushData({ config, appToken: appToken.token, isDryRun });
|
|
67
|
+
await appToken.destroy();
|
|
68
|
+
console.info("Push complete!");
|
|
69
|
+
}
|
|
70
|
+
async function pushSchema({
|
|
71
|
+
migrations,
|
|
72
|
+
appToken,
|
|
73
|
+
isDryRun,
|
|
74
|
+
currentSnapshot
|
|
75
|
+
}) {
|
|
76
|
+
const initialSnapshot = migrations.find((m) => m === "0000_snapshot.json");
|
|
77
|
+
const filteredMigrations = migrations.filter((m) => m !== "0000_snapshot.json");
|
|
78
|
+
const missingMigrationContents = await Promise.all(filteredMigrations.map(loadMigration));
|
|
79
|
+
const initialMigrationBatch = initialSnapshot ? (await getMigrationQueries({
|
|
80
|
+
oldSnapshot: createEmptySnapshot(),
|
|
81
|
+
newSnapshot: await loadInitialSnapshot()
|
|
82
|
+
})).queries : [];
|
|
83
|
+
const confirmations = missingMigrationContents.reduce((acc, curr) => {
|
|
84
|
+
return [...acc, ...curr.confirm || []];
|
|
85
|
+
}, []);
|
|
86
|
+
if (confirmations.length > 0) {
|
|
87
|
+
const response = await prompts([
|
|
88
|
+
...confirmations.map((message, index) => ({
|
|
89
|
+
type: "confirm",
|
|
90
|
+
name: String(index),
|
|
91
|
+
message: red("Warning: ") + message + "\nContinue?",
|
|
92
|
+
initial: true
|
|
93
|
+
}))
|
|
94
|
+
]);
|
|
95
|
+
if (Object.values(response).length === 0 || Object.values(response).some((value) => value === false)) {
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const queries = missingMigrationContents.reduce((acc, curr) => {
|
|
100
|
+
return [...acc, ...curr.db];
|
|
101
|
+
}, initialMigrationBatch);
|
|
102
|
+
await runMigrateQuery({ queries, migrations, snapshot: currentSnapshot, appToken, isDryRun });
|
|
103
|
+
}
|
|
104
|
+
const sqlite = new SQLiteAsyncDialect();
|
|
105
|
+
async function pushData({
|
|
106
|
+
config,
|
|
107
|
+
appToken,
|
|
108
|
+
isDryRun
|
|
109
|
+
}) {
|
|
110
|
+
const queries = [];
|
|
111
|
+
if (config.db?.data) {
|
|
112
|
+
const libsqlClient = createClient({ url: ":memory:" });
|
|
113
|
+
await recreateTables({
|
|
114
|
+
db: drizzleLibsql(libsqlClient),
|
|
115
|
+
tables: tablesSchema.parse(config.db.tables ?? {})
|
|
116
|
+
});
|
|
117
|
+
for (const [collectionName, { writable }] of Object.entries(config.db.tables ?? {})) {
|
|
118
|
+
if (!writable) {
|
|
119
|
+
queries.push({
|
|
120
|
+
sql: `DELETE FROM ${sqlite.escapeName(collectionName)}`,
|
|
121
|
+
args: []
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const db = await drizzleProxy(async (sqlQuery, params, method) => {
|
|
126
|
+
const stmt = { sql: sqlQuery, args: params };
|
|
127
|
+
queries.push(stmt);
|
|
128
|
+
const { rows } = await libsqlClient.execute(stmt);
|
|
129
|
+
const rowValues = [];
|
|
130
|
+
for (const row of rows) {
|
|
131
|
+
if (row != null && typeof row === "object") {
|
|
132
|
+
rowValues.push(Object.values(row));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (method === "get") {
|
|
136
|
+
return { rows: rowValues[0] };
|
|
137
|
+
}
|
|
138
|
+
return { rows: rowValues };
|
|
139
|
+
});
|
|
140
|
+
await seedData({
|
|
141
|
+
db,
|
|
142
|
+
mode: "build",
|
|
143
|
+
data: config.db.data
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
const url = new URL("/db/query", getRemoteDatabaseUrl());
|
|
147
|
+
if (isDryRun) {
|
|
148
|
+
console.info("[DRY RUN] Batch data seed:", JSON.stringify(queries, null, 2));
|
|
149
|
+
return new Response(null, { status: 200 });
|
|
150
|
+
}
|
|
151
|
+
return await fetch(url, {
|
|
152
|
+
method: "POST",
|
|
153
|
+
headers: new Headers({
|
|
154
|
+
Authorization: `Bearer ${appToken}`
|
|
155
|
+
}),
|
|
156
|
+
body: JSON.stringify(queries)
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
async function runMigrateQuery({
|
|
160
|
+
queries: baseQueries,
|
|
161
|
+
migrations,
|
|
162
|
+
snapshot,
|
|
163
|
+
appToken,
|
|
164
|
+
isDryRun
|
|
165
|
+
}) {
|
|
166
|
+
const queries = ["pragma defer_foreign_keys=true;", ...baseQueries];
|
|
167
|
+
const requestBody = {
|
|
168
|
+
snapshot,
|
|
169
|
+
migrations,
|
|
170
|
+
sql: queries,
|
|
171
|
+
experimentalVersion: 1
|
|
172
|
+
};
|
|
173
|
+
if (isDryRun) {
|
|
174
|
+
console.info("[DRY RUN] Batch query:", JSON.stringify(requestBody, null, 2));
|
|
175
|
+
return new Response(null, { status: 200 });
|
|
176
|
+
}
|
|
177
|
+
const url = new URL("/migrations/run", getRemoteDatabaseUrl());
|
|
178
|
+
return await fetch(url, {
|
|
179
|
+
method: "POST",
|
|
180
|
+
headers: new Headers({
|
|
181
|
+
Authorization: `Bearer ${appToken}`
|
|
182
|
+
}),
|
|
183
|
+
body: JSON.stringify(requestBody)
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
async function prepareMigrateQuery({
|
|
187
|
+
migrations,
|
|
188
|
+
appToken
|
|
189
|
+
}) {
|
|
190
|
+
const url = new URL("/migrations/prepare", getRemoteDatabaseUrl());
|
|
191
|
+
const requestBody = {
|
|
192
|
+
migrations,
|
|
193
|
+
experimentalVersion: 1
|
|
194
|
+
};
|
|
195
|
+
const result = await fetch(url, {
|
|
196
|
+
method: "POST",
|
|
197
|
+
headers: new Headers({
|
|
198
|
+
Authorization: `Bearer ${appToken}`
|
|
199
|
+
}),
|
|
200
|
+
body: JSON.stringify(requestBody)
|
|
201
|
+
});
|
|
202
|
+
if (result.status >= 400) {
|
|
203
|
+
throw new Error(await result.text());
|
|
204
|
+
}
|
|
205
|
+
return await result.json();
|
|
206
|
+
}
|
|
207
|
+
export {
|
|
208
|
+
cmd
|
|
209
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { sql } from "drizzle-orm";
|
|
2
|
+
import { createRemoteDatabaseClient } from "../../../../runtime/db-client.js";
|
|
3
|
+
import { getManagedAppTokenOrExit } from "../../../tokens.js";
|
|
4
|
+
import { getRemoteDatabaseUrl } from "../../../utils.js";
|
|
5
|
+
async function cmd({ flags }) {
|
|
6
|
+
const query = flags.query;
|
|
7
|
+
const appToken = await getManagedAppTokenOrExit(flags.token);
|
|
8
|
+
const db = createRemoteDatabaseClient(appToken.token, getRemoteDatabaseUrl());
|
|
9
|
+
const result = await db.run(sql.raw(query));
|
|
10
|
+
await appToken.destroy();
|
|
11
|
+
console.log(result);
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
cmd
|
|
15
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getMigrationStatus,
|
|
3
|
+
MIGRATION_NEEDED,
|
|
4
|
+
MIGRATIONS_NOT_INITIALIZED,
|
|
5
|
+
MIGRATIONS_UP_TO_DATE
|
|
6
|
+
} from "../../migrations.js";
|
|
7
|
+
import { getMigrationQueries } from "../../migration-queries.js";
|
|
8
|
+
async function cmd({ config, flags }) {
|
|
9
|
+
const status = await getMigrationStatus(config);
|
|
10
|
+
const { state } = status;
|
|
11
|
+
if (flags.json) {
|
|
12
|
+
if (state === "ahead") {
|
|
13
|
+
const { queries: migrationQueries } = await getMigrationQueries({
|
|
14
|
+
oldSnapshot: status.oldSnapshot,
|
|
15
|
+
newSnapshot: status.newSnapshot
|
|
16
|
+
});
|
|
17
|
+
const newFileContent = {
|
|
18
|
+
diff: status.diff,
|
|
19
|
+
db: migrationQueries
|
|
20
|
+
};
|
|
21
|
+
status.newFileContent = JSON.stringify(newFileContent, null, 2);
|
|
22
|
+
}
|
|
23
|
+
console.log(JSON.stringify(status));
|
|
24
|
+
process.exit(state === "up-to-date" ? 0 : 1);
|
|
25
|
+
}
|
|
26
|
+
switch (state) {
|
|
27
|
+
case "no-migrations-found": {
|
|
28
|
+
console.log(MIGRATIONS_NOT_INITIALIZED);
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
case "ahead": {
|
|
32
|
+
console.log(MIGRATION_NEEDED);
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
case "up-to-date": {
|
|
36
|
+
console.log(MIGRATIONS_UP_TO_DATE);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export {
|
|
42
|
+
cmd
|
|
43
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { STUDIO_CONFIG_MISSING_CLI_ERROR } from "../errors.js";
|
|
2
|
+
async function cli({ flags, config }) {
|
|
3
|
+
const args = flags._;
|
|
4
|
+
const command = args[2] === "db" ? args[3] : args[2];
|
|
5
|
+
if (!config.db?.studio) {
|
|
6
|
+
console.log(STUDIO_CONFIG_MISSING_CLI_ERROR);
|
|
7
|
+
process.exit(1);
|
|
8
|
+
}
|
|
9
|
+
switch (command) {
|
|
10
|
+
case "shell": {
|
|
11
|
+
const { cmd } = await import("./commands/shell/index.js");
|
|
12
|
+
return await cmd({ config, flags });
|
|
13
|
+
}
|
|
14
|
+
case "gen":
|
|
15
|
+
case "sync": {
|
|
16
|
+
const { cmd } = await import("./commands/gen/index.js");
|
|
17
|
+
return await cmd({ config, flags });
|
|
18
|
+
}
|
|
19
|
+
case "push": {
|
|
20
|
+
const { cmd } = await import("./commands/push/index.js");
|
|
21
|
+
return await cmd({ config, flags });
|
|
22
|
+
}
|
|
23
|
+
case "verify": {
|
|
24
|
+
const { cmd } = await import("./commands/verify/index.js");
|
|
25
|
+
return await cmd({ config, flags });
|
|
26
|
+
}
|
|
27
|
+
case "login": {
|
|
28
|
+
const { cmd } = await import("./commands/login/index.js");
|
|
29
|
+
return await cmd({ config, flags });
|
|
30
|
+
}
|
|
31
|
+
case "logout": {
|
|
32
|
+
const { cmd } = await import("./commands/logout/index.js");
|
|
33
|
+
return await cmd({ config, flags });
|
|
34
|
+
}
|
|
35
|
+
case "link": {
|
|
36
|
+
const { cmd } = await import("./commands/link/index.js");
|
|
37
|
+
return await cmd({ config, flags });
|
|
38
|
+
}
|
|
39
|
+
default: {
|
|
40
|
+
if (command == null) {
|
|
41
|
+
console.error(`No command provided.
|
|
42
|
+
|
|
43
|
+
${showHelp()}`);
|
|
44
|
+
} else {
|
|
45
|
+
console.error(`Unknown command: ${command}
|
|
46
|
+
|
|
47
|
+
${showHelp()}`);
|
|
48
|
+
}
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function showHelp() {
|
|
53
|
+
return `astro db <command>
|
|
54
|
+
|
|
55
|
+
Usage:
|
|
56
|
+
|
|
57
|
+
astro login Authenticate your machine with Astro Studio
|
|
58
|
+
astro logout End your authenticated session with Astro Studio
|
|
59
|
+
astro link Link this directory to an Astro Studio project
|
|
60
|
+
|
|
61
|
+
astro db gen Creates snapshot based on your schema
|
|
62
|
+
astro db push Pushes migrations to Astro Studio
|
|
63
|
+
astro db verify Verifies migrations have been pushed and errors if not`;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
cli
|
|
68
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type DBTable, type DBSnapshot } from '../types.js';
|
|
2
|
+
/** Dependency injected for unit testing */
|
|
3
|
+
type AmbiguityResponses = {
|
|
4
|
+
collectionRenames: Record<string, string>;
|
|
5
|
+
columnRenames: {
|
|
6
|
+
[collectionName: string]: Record<string, string>;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export declare function getMigrationQueries({ oldSnapshot, newSnapshot, ambiguityResponses, }: {
|
|
10
|
+
oldSnapshot: DBSnapshot;
|
|
11
|
+
newSnapshot: DBSnapshot;
|
|
12
|
+
ambiguityResponses?: AmbiguityResponses;
|
|
13
|
+
}): Promise<{
|
|
14
|
+
queries: string[];
|
|
15
|
+
confirmations: string[];
|
|
16
|
+
}>;
|
|
17
|
+
export declare function getCollectionChangeQueries({ collectionName, oldCollection, newCollection, ambiguityResponses, }: {
|
|
18
|
+
collectionName: string;
|
|
19
|
+
oldCollection: DBTable;
|
|
20
|
+
newCollection: DBTable;
|
|
21
|
+
ambiguityResponses?: AmbiguityResponses;
|
|
22
|
+
}): Promise<{
|
|
23
|
+
queries: string[];
|
|
24
|
+
confirmations: string[];
|
|
25
|
+
}>;
|
|
26
|
+
export {};
|