@astrojs/db 0.15.1 → 0.16.1
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/dist/_internal/core/utils.d.ts +1 -10
- package/dist/_internal/runtime/types.d.ts +17 -11
- package/dist/_internal/runtime/utils.d.ts +0 -15
- package/dist/core/cli/commands/execute/index.js +5 -5
- package/dist/core/cli/commands/push/index.js +6 -40
- package/dist/core/cli/commands/shell/index.js +3 -9
- package/dist/core/cli/commands/verify/index.js +2 -7
- package/dist/core/cli/index.js +4 -16
- package/dist/core/cli/migration-queries.d.ts +1 -4
- package/dist/core/cli/migration-queries.js +8 -30
- package/dist/core/cli/print-help.js +1 -1
- package/dist/core/integration/index.js +5 -8
- package/dist/core/integration/vite-plugin-db.d.ts +3 -3
- package/dist/core/integration/vite-plugin-db.js +10 -13
- package/dist/core/load-file.d.ts +1 -1
- package/dist/core/load-file.js +0 -2
- package/dist/core/schemas.d.ts +22 -22
- package/dist/core/schemas.js +1 -1
- package/dist/core/utils.d.ts +1 -10
- package/dist/core/utils.js +2 -27
- package/dist/runtime/db-client.d.ts +5 -6
- package/dist/runtime/db-client.js +10 -167
- package/dist/runtime/types.d.ts +17 -11
- package/dist/runtime/utils.d.ts +0 -15
- package/dist/runtime/utils.js +1 -26
- package/package.json +5 -10
- package/dist/core/cli/commands/link/index.d.ts +0 -1
- package/dist/core/cli/commands/link/index.js +0 -266
- package/dist/core/cli/commands/login/index.d.ts +0 -8
- package/dist/core/cli/commands/login/index.js +0 -76
- package/dist/core/cli/commands/logout/index.d.ts +0 -1
- package/dist/core/cli/commands/logout/index.js +0 -9
|
@@ -1,23 +1,14 @@
|
|
|
1
|
-
import { type ManagedAppToken } from '@astrojs/studio';
|
|
2
1
|
import type { AstroConfig, AstroIntegration } from 'astro';
|
|
3
2
|
import './types.js';
|
|
4
3
|
export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
|
|
5
4
|
export declare function getAstroEnv(envMode?: string): Record<`ASTRO_${string}`, string>;
|
|
6
5
|
export type RemoteDatabaseInfo = {
|
|
7
|
-
type: 'libsql' | 'studio';
|
|
8
6
|
url: string;
|
|
7
|
+
token: string;
|
|
9
8
|
};
|
|
10
9
|
export declare function getRemoteDatabaseInfo(): RemoteDatabaseInfo;
|
|
11
|
-
export declare function getManagedRemoteToken(token?: string, dbInfo?: RemoteDatabaseInfo): Promise<ManagedAppToken>;
|
|
12
10
|
export declare function getDbDirectoryUrl(root: URL | string): URL;
|
|
13
11
|
export declare function defineDbIntegration(integration: AstroIntegration): AstroIntegration;
|
|
14
|
-
export type Result<T> = {
|
|
15
|
-
success: true;
|
|
16
|
-
data: T;
|
|
17
|
-
} | {
|
|
18
|
-
success: false;
|
|
19
|
-
data: unknown;
|
|
20
|
-
};
|
|
21
12
|
/**
|
|
22
13
|
* Map an object's values to a new set of values
|
|
23
14
|
* while preserving types.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ColumnBaseConfig, ColumnDataType } from 'drizzle-orm';
|
|
2
2
|
import type { SQLiteColumn, SQLiteTableWithColumns } from 'drizzle-orm/sqlite-core';
|
|
3
3
|
import type { ColumnsConfig, DBColumn, OutputColumnsConfig } from '../core/types.js';
|
|
4
|
-
type GeneratedConfig<T extends ColumnDataType = ColumnDataType> = Pick<ColumnBaseConfig<T, string>, 'name' | 'tableName' | 'notNull' | 'hasDefault'>;
|
|
4
|
+
type GeneratedConfig<T extends ColumnDataType = ColumnDataType> = Pick<ColumnBaseConfig<T, string>, 'name' | 'tableName' | 'notNull' | 'hasDefault' | 'hasRuntimeDefault' | 'isPrimaryKey'>;
|
|
5
5
|
type AstroText<T extends GeneratedConfig<'string'>> = SQLiteColumn<T & {
|
|
6
6
|
data: string;
|
|
7
7
|
dataType: 'string';
|
|
@@ -9,9 +9,9 @@ type AstroText<T extends GeneratedConfig<'string'>> = SQLiteColumn<T & {
|
|
|
9
9
|
driverParam: string;
|
|
10
10
|
enumValues: never;
|
|
11
11
|
baseColumn: never;
|
|
12
|
-
isPrimaryKey: boolean;
|
|
13
12
|
isAutoincrement: boolean;
|
|
14
|
-
|
|
13
|
+
identity: undefined;
|
|
14
|
+
generated: undefined;
|
|
15
15
|
}>;
|
|
16
16
|
type AstroDate<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
|
|
17
17
|
data: Date;
|
|
@@ -20,9 +20,9 @@ type AstroDate<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
|
|
|
20
20
|
driverParam: string;
|
|
21
21
|
enumValues: never;
|
|
22
22
|
baseColumn: never;
|
|
23
|
-
isPrimaryKey: boolean;
|
|
24
23
|
isAutoincrement: boolean;
|
|
25
|
-
|
|
24
|
+
identity: undefined;
|
|
25
|
+
generated: undefined;
|
|
26
26
|
}>;
|
|
27
27
|
type AstroBoolean<T extends GeneratedConfig<'boolean'>> = SQLiteColumn<T & {
|
|
28
28
|
data: boolean;
|
|
@@ -31,9 +31,9 @@ type AstroBoolean<T extends GeneratedConfig<'boolean'>> = SQLiteColumn<T & {
|
|
|
31
31
|
driverParam: number;
|
|
32
32
|
enumValues: never;
|
|
33
33
|
baseColumn: never;
|
|
34
|
-
isPrimaryKey: boolean;
|
|
35
34
|
isAutoincrement: boolean;
|
|
36
|
-
|
|
35
|
+
identity: undefined;
|
|
36
|
+
generated: undefined;
|
|
37
37
|
}>;
|
|
38
38
|
type AstroNumber<T extends GeneratedConfig<'number'>> = SQLiteColumn<T & {
|
|
39
39
|
data: number;
|
|
@@ -42,9 +42,9 @@ type AstroNumber<T extends GeneratedConfig<'number'>> = SQLiteColumn<T & {
|
|
|
42
42
|
driverParam: number;
|
|
43
43
|
enumValues: never;
|
|
44
44
|
baseColumn: never;
|
|
45
|
-
isPrimaryKey: boolean;
|
|
46
45
|
isAutoincrement: boolean;
|
|
47
|
-
|
|
46
|
+
identity: undefined;
|
|
47
|
+
generated: undefined;
|
|
48
48
|
}>;
|
|
49
49
|
type AstroJson<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
|
|
50
50
|
data: unknown;
|
|
@@ -53,9 +53,9 @@ type AstroJson<T extends GeneratedConfig<'custom'>> = SQLiteColumn<T & {
|
|
|
53
53
|
driverParam: string;
|
|
54
54
|
enumValues: never;
|
|
55
55
|
baseColumn: never;
|
|
56
|
-
isPrimaryKey: boolean;
|
|
57
56
|
isAutoincrement: boolean;
|
|
58
|
-
|
|
57
|
+
identity: undefined;
|
|
58
|
+
generated: undefined;
|
|
59
59
|
}>;
|
|
60
60
|
type Column<T extends DBColumn['type'], S extends GeneratedConfig> = T extends 'boolean' ? AstroBoolean<S> : T extends 'number' ? AstroNumber<S> : T extends 'text' ? AstroText<S> : T extends 'date' ? AstroDate<S> : T extends 'json' ? AstroJson<S> : never;
|
|
61
61
|
export type Table<TTableName extends string, TColumns extends OutputColumnsConfig | ColumnsConfig> = SQLiteTableWithColumns<{
|
|
@@ -66,11 +66,17 @@ export type Table<TTableName extends string, TColumns extends OutputColumnsConfi
|
|
|
66
66
|
[K in Extract<keyof TColumns, string>]: Column<TColumns[K]['type'], {
|
|
67
67
|
tableName: TTableName;
|
|
68
68
|
name: K;
|
|
69
|
+
isPrimaryKey: TColumns[K]['schema'] extends {
|
|
70
|
+
primaryKey: true;
|
|
71
|
+
} ? true : false;
|
|
69
72
|
hasDefault: TColumns[K]['schema'] extends {
|
|
70
73
|
default: NonNullable<unknown>;
|
|
71
74
|
} ? true : TColumns[K]['schema'] extends {
|
|
72
75
|
primaryKey: true;
|
|
73
76
|
} ? true : false;
|
|
77
|
+
hasRuntimeDefault: TColumns[K]['schema'] extends {
|
|
78
|
+
default: NonNullable<unknown>;
|
|
79
|
+
} ? true : false;
|
|
74
80
|
notNull: TColumns[K]['schema']['optional'] extends true ? false : true;
|
|
75
81
|
}>;
|
|
76
82
|
};
|
|
@@ -1,22 +1,7 @@
|
|
|
1
1
|
import { LibsqlError } from '@libsql/client';
|
|
2
2
|
import { AstroError } from 'astro/errors';
|
|
3
|
-
/**
|
|
4
|
-
* Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback.
|
|
5
|
-
*/
|
|
6
|
-
export declare function safeFetch(url: Parameters<typeof fetch>[0], options?: Parameters<typeof fetch>[1], onNotOK?: (response: Response) => void | Promise<void>): Promise<Response>;
|
|
7
3
|
export declare class AstroDbError extends AstroError {
|
|
8
4
|
name: string;
|
|
9
5
|
}
|
|
10
|
-
export declare class DetailedLibsqlError extends LibsqlError {
|
|
11
|
-
name: string;
|
|
12
|
-
hint?: string;
|
|
13
|
-
constructor({ message, code, hint, rawCode, cause, }: {
|
|
14
|
-
message: string;
|
|
15
|
-
code: string;
|
|
16
|
-
hint?: string;
|
|
17
|
-
rawCode?: number;
|
|
18
|
-
cause?: Error;
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
6
|
export declare function isDbError(err: unknown): err is LibsqlError;
|
|
22
7
|
export declare function pathToFileURL(path: string): URL;
|
|
@@ -9,10 +9,10 @@ import {
|
|
|
9
9
|
} from "../../../errors.js";
|
|
10
10
|
import {
|
|
11
11
|
getLocalVirtualModContents,
|
|
12
|
-
|
|
12
|
+
getRemoteVirtualModContents
|
|
13
13
|
} from "../../../integration/vite-plugin-db.js";
|
|
14
14
|
import { bundleFile, importBundledFile } from "../../../load-file.js";
|
|
15
|
-
import {
|
|
15
|
+
import { getRemoteDatabaseInfo } from "../../../utils.js";
|
|
16
16
|
async function cmd({
|
|
17
17
|
astroConfig,
|
|
18
18
|
dbConfig,
|
|
@@ -30,10 +30,10 @@ async function cmd({
|
|
|
30
30
|
}
|
|
31
31
|
let virtualModContents;
|
|
32
32
|
if (flags.remote) {
|
|
33
|
-
const
|
|
34
|
-
virtualModContents =
|
|
33
|
+
const dbInfo = getRemoteDatabaseInfo();
|
|
34
|
+
virtualModContents = getRemoteVirtualModContents({
|
|
35
35
|
tables: dbConfig.tables ?? {},
|
|
36
|
-
appToken:
|
|
36
|
+
appToken: flags.token ?? dbInfo.token,
|
|
37
37
|
isBuild: false,
|
|
38
38
|
output: "server"
|
|
39
39
|
});
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import { sql } from "drizzle-orm";
|
|
2
2
|
import prompts from "prompts";
|
|
3
3
|
import { createRemoteDatabaseClient } from "../../../../runtime/index.js";
|
|
4
|
-
import { safeFetch } from "../../../../runtime/utils.js";
|
|
5
4
|
import { MIGRATION_VERSION } from "../../../consts.js";
|
|
6
|
-
import {
|
|
7
|
-
getManagedRemoteToken,
|
|
8
|
-
getRemoteDatabaseInfo
|
|
9
|
-
} from "../../../utils.js";
|
|
5
|
+
import { getRemoteDatabaseInfo } from "../../../utils.js";
|
|
10
6
|
import {
|
|
11
7
|
createCurrentSnapshot,
|
|
12
8
|
createEmptySnapshot,
|
|
@@ -21,11 +17,7 @@ async function cmd({
|
|
|
21
17
|
const isDryRun = flags.dryRun;
|
|
22
18
|
const isForceReset = flags.forceReset;
|
|
23
19
|
const dbInfo = getRemoteDatabaseInfo();
|
|
24
|
-
const
|
|
25
|
-
const productionSnapshot = await getProductionCurrentSnapshot({
|
|
26
|
-
dbInfo,
|
|
27
|
-
appToken: appToken.token
|
|
28
|
-
});
|
|
20
|
+
const productionSnapshot = await getProductionCurrentSnapshot(dbInfo);
|
|
29
21
|
const currentSnapshot = createCurrentSnapshot(dbConfig);
|
|
30
22
|
const isFromScratch = !productionSnapshot;
|
|
31
23
|
const { queries: migrationQueries, confirmations } = await getMigrationQueries({
|
|
@@ -61,12 +53,11 @@ async function cmd({
|
|
|
61
53
|
await pushSchema({
|
|
62
54
|
statements: migrationQueries,
|
|
63
55
|
dbInfo,
|
|
64
|
-
appToken:
|
|
56
|
+
appToken: flags.token ?? dbInfo.token,
|
|
65
57
|
isDryRun,
|
|
66
58
|
currentSnapshot
|
|
67
59
|
});
|
|
68
60
|
}
|
|
69
|
-
await appToken.destroy();
|
|
70
61
|
console.info("Push complete!");
|
|
71
62
|
}
|
|
72
63
|
async function pushSchema({
|
|
@@ -85,13 +76,12 @@ async function pushSchema({
|
|
|
85
76
|
console.info("[DRY RUN] Batch query:", JSON.stringify(requestBody, null, 2));
|
|
86
77
|
return new Response(null, { status: 200 });
|
|
87
78
|
}
|
|
88
|
-
return
|
|
79
|
+
return pushToDb(requestBody, appToken, dbInfo.url);
|
|
89
80
|
}
|
|
90
81
|
async function pushToDb(requestBody, appToken, remoteUrl) {
|
|
91
82
|
const client = createRemoteDatabaseClient({
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
remoteUrl
|
|
83
|
+
token: appToken,
|
|
84
|
+
url: remoteUrl
|
|
95
85
|
});
|
|
96
86
|
await client.run(sql`create table if not exists _astro_db_snapshot (
|
|
97
87
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -108,30 +98,6 @@ async function pushToDb(requestBody, appToken, remoteUrl) {
|
|
|
108
98
|
)`);
|
|
109
99
|
});
|
|
110
100
|
}
|
|
111
|
-
async function pushToStudio(requestBody, appToken, remoteUrl) {
|
|
112
|
-
const url = new URL("/db/push", remoteUrl);
|
|
113
|
-
const response = await safeFetch(
|
|
114
|
-
url,
|
|
115
|
-
{
|
|
116
|
-
method: "POST",
|
|
117
|
-
headers: new Headers({
|
|
118
|
-
Authorization: `Bearer ${appToken}`
|
|
119
|
-
}),
|
|
120
|
-
body: JSON.stringify(requestBody)
|
|
121
|
-
},
|
|
122
|
-
async (res) => {
|
|
123
|
-
console.error(`${url.toString()} failed: ${res.status} ${res.statusText}`);
|
|
124
|
-
console.error(await res.text());
|
|
125
|
-
throw new Error(`/db/push fetch failed: ${res.status} ${res.statusText}`);
|
|
126
|
-
}
|
|
127
|
-
);
|
|
128
|
-
const result = await response.json();
|
|
129
|
-
if (!result.success) {
|
|
130
|
-
console.error(`${url.toString()} unsuccessful`);
|
|
131
|
-
console.error(await response.text());
|
|
132
|
-
throw new Error(`/db/push fetch unsuccessful`);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
101
|
export {
|
|
136
102
|
cmd
|
|
137
103
|
};
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
import { normalizeDatabaseUrl } from "../../../../runtime/index.js";
|
|
7
7
|
import { DB_PATH } from "../../../consts.js";
|
|
8
8
|
import { SHELL_QUERY_MISSING_ERROR } from "../../../errors.js";
|
|
9
|
-
import { getAstroEnv,
|
|
9
|
+
import { getAstroEnv, getRemoteDatabaseInfo } from "../../../utils.js";
|
|
10
10
|
async function cmd({
|
|
11
11
|
flags,
|
|
12
12
|
astroConfig
|
|
@@ -18,14 +18,8 @@ async function cmd({
|
|
|
18
18
|
}
|
|
19
19
|
const dbInfo = getRemoteDatabaseInfo();
|
|
20
20
|
if (flags.remote) {
|
|
21
|
-
const
|
|
22
|
-
const db = createRemoteDatabaseClient({
|
|
23
|
-
dbType: dbInfo.type,
|
|
24
|
-
remoteUrl: dbInfo.url,
|
|
25
|
-
appToken: appToken.token
|
|
26
|
-
});
|
|
21
|
+
const db = createRemoteDatabaseClient(dbInfo);
|
|
27
22
|
const result = await db.run(sql.raw(query));
|
|
28
|
-
await appToken.destroy();
|
|
29
23
|
console.log(result);
|
|
30
24
|
} else {
|
|
31
25
|
const { ASTRO_DATABASE_FILE } = getAstroEnv();
|
|
@@ -33,7 +27,7 @@ async function cmd({
|
|
|
33
27
|
ASTRO_DATABASE_FILE,
|
|
34
28
|
new URL(DB_PATH, astroConfig.root).href
|
|
35
29
|
);
|
|
36
|
-
const db = createLocalDatabaseClient({ dbUrl
|
|
30
|
+
const db = createLocalDatabaseClient({ dbUrl });
|
|
37
31
|
const result = await db.run(sql.raw(query));
|
|
38
32
|
console.log(result);
|
|
39
33
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getRemoteDatabaseInfo } from "../../../utils.js";
|
|
2
2
|
import {
|
|
3
3
|
createCurrentSnapshot,
|
|
4
4
|
createEmptySnapshot,
|
|
@@ -12,11 +12,7 @@ async function cmd({
|
|
|
12
12
|
}) {
|
|
13
13
|
const isJson = flags.json;
|
|
14
14
|
const dbInfo = getRemoteDatabaseInfo();
|
|
15
|
-
const
|
|
16
|
-
const productionSnapshot = await getProductionCurrentSnapshot({
|
|
17
|
-
dbInfo,
|
|
18
|
-
appToken: appToken.token
|
|
19
|
-
});
|
|
15
|
+
const productionSnapshot = await getProductionCurrentSnapshot(dbInfo);
|
|
20
16
|
const currentSnapshot = createCurrentSnapshot(dbConfig);
|
|
21
17
|
const { queries: migrationQueries, confirmations } = await getMigrationQueries({
|
|
22
18
|
oldSnapshot: productionSnapshot || createEmptySnapshot(),
|
|
@@ -42,7 +38,6 @@ Run 'astro db push' to push up your latest changes.`;
|
|
|
42
38
|
} else {
|
|
43
39
|
console.log(result.message);
|
|
44
40
|
}
|
|
45
|
-
await appToken.destroy();
|
|
46
41
|
process.exit(result.exitCode);
|
|
47
42
|
}
|
|
48
43
|
export {
|
package/dist/core/cli/index.js
CHANGED
|
@@ -32,18 +32,6 @@ async function cli({
|
|
|
32
32
|
const { cmd } = await import("./commands/execute/index.js");
|
|
33
33
|
return await cmd({ astroConfig, dbConfig, flags });
|
|
34
34
|
}
|
|
35
|
-
case "login": {
|
|
36
|
-
const { cmd } = await import("./commands/login/index.js");
|
|
37
|
-
return await cmd({ astroConfig, dbConfig, flags });
|
|
38
|
-
}
|
|
39
|
-
case "logout": {
|
|
40
|
-
const { cmd } = await import("./commands/logout/index.js");
|
|
41
|
-
return await cmd();
|
|
42
|
-
}
|
|
43
|
-
case "link": {
|
|
44
|
-
const { cmd } = await import("./commands/link/index.js");
|
|
45
|
-
return await cmd();
|
|
46
|
-
}
|
|
47
35
|
default: {
|
|
48
36
|
if (command != null) {
|
|
49
37
|
console.error(`Unknown command: ${command}`);
|
|
@@ -54,15 +42,15 @@ async function cli({
|
|
|
54
42
|
headline: " ",
|
|
55
43
|
tables: {
|
|
56
44
|
Commands: [
|
|
57
|
-
["push", "Push table schema updates to
|
|
58
|
-
["verify", "Test schema updates
|
|
45
|
+
["push", "Push table schema updates to libSQL."],
|
|
46
|
+
["verify", "Test schema updates with libSQL (good for CI)."],
|
|
59
47
|
[
|
|
60
48
|
"astro db execute <file-path>",
|
|
61
|
-
"Execute a ts/js file using astro:db. Use --remote to connect to
|
|
49
|
+
"Execute a ts/js file using astro:db. Use --remote to connect to libSQL."
|
|
62
50
|
],
|
|
63
51
|
[
|
|
64
52
|
"astro db shell --query <sql-string>",
|
|
65
|
-
"Execute a SQL string. Use --remote to connect to
|
|
53
|
+
"Execute a SQL string. Use --remote to connect to libSQL."
|
|
66
54
|
]
|
|
67
55
|
]
|
|
68
56
|
}
|
|
@@ -16,10 +16,7 @@ export declare function getTableChangeQueries({ tableName, oldTable, newTable, }
|
|
|
16
16
|
queries: string[];
|
|
17
17
|
confirmations: string[];
|
|
18
18
|
}>;
|
|
19
|
-
export declare function getProductionCurrentSnapshot(
|
|
20
|
-
dbInfo: RemoteDatabaseInfo;
|
|
21
|
-
appToken: string;
|
|
22
|
-
}): Promise<DBSnapshot | undefined>;
|
|
19
|
+
export declare function getProductionCurrentSnapshot({ url, token, }: RemoteDatabaseInfo): Promise<DBSnapshot | undefined>;
|
|
23
20
|
export declare function createCurrentSnapshot({ tables }: DBConfig): DBSnapshot;
|
|
24
21
|
export declare function createEmptySnapshot(): DBSnapshot;
|
|
25
22
|
export declare function formatDataLossMessage(confirmations: string[], isColor?: boolean): string;
|
|
@@ -6,7 +6,7 @@ import * as color from "kleur/colors";
|
|
|
6
6
|
import { customAlphabet } from "nanoid";
|
|
7
7
|
import { createRemoteDatabaseClient, hasPrimaryKey } from "../../runtime/index.js";
|
|
8
8
|
import { isSerializedSQL } from "../../runtime/types.js";
|
|
9
|
-
import { isDbError
|
|
9
|
+
import { isDbError } from "../../runtime/utils.js";
|
|
10
10
|
import { MIGRATION_VERSION } from "../consts.js";
|
|
11
11
|
import { RENAME_COLUMN_ERROR, RENAME_TABLE_ERROR } from "../errors.js";
|
|
12
12
|
import {
|
|
@@ -302,14 +302,16 @@ function canChangeTypeWithoutQuery(oldColumn, newColumn) {
|
|
|
302
302
|
function hasRuntimeDefault(column) {
|
|
303
303
|
return !!(column.schema.default && isSerializedSQL(column.schema.default));
|
|
304
304
|
}
|
|
305
|
-
function getProductionCurrentSnapshot(
|
|
306
|
-
|
|
305
|
+
function getProductionCurrentSnapshot({
|
|
306
|
+
url,
|
|
307
|
+
token
|
|
308
|
+
}) {
|
|
309
|
+
return getDbCurrentSnapshot(token, url);
|
|
307
310
|
}
|
|
308
311
|
async function getDbCurrentSnapshot(appToken, remoteUrl) {
|
|
309
312
|
const client = createRemoteDatabaseClient({
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
remoteUrl
|
|
313
|
+
token: appToken,
|
|
314
|
+
url: remoteUrl
|
|
313
315
|
});
|
|
314
316
|
try {
|
|
315
317
|
const res = await client.get(
|
|
@@ -330,30 +332,6 @@ async function getDbCurrentSnapshot(appToken, remoteUrl) {
|
|
|
330
332
|
throw error;
|
|
331
333
|
}
|
|
332
334
|
}
|
|
333
|
-
async function getStudioCurrentSnapshot(appToken, remoteUrl) {
|
|
334
|
-
const url = new URL("/db/schema", remoteUrl);
|
|
335
|
-
const response = await safeFetch(
|
|
336
|
-
url,
|
|
337
|
-
{
|
|
338
|
-
method: "POST",
|
|
339
|
-
headers: new Headers({
|
|
340
|
-
Authorization: `Bearer ${appToken}`
|
|
341
|
-
})
|
|
342
|
-
},
|
|
343
|
-
async (res) => {
|
|
344
|
-
console.error(`${url.toString()} failed: ${res.status} ${res.statusText}`);
|
|
345
|
-
console.error(await res.text());
|
|
346
|
-
throw new Error(`/db/schema fetch failed: ${res.status} ${res.statusText}`);
|
|
347
|
-
}
|
|
348
|
-
);
|
|
349
|
-
const result = await response.json();
|
|
350
|
-
if (!result.success) {
|
|
351
|
-
console.error(`${url.toString()} unsuccessful`);
|
|
352
|
-
console.error(await response.text());
|
|
353
|
-
throw new Error(`/db/schema fetch unsuccessful`);
|
|
354
|
-
}
|
|
355
|
-
return result.data;
|
|
356
|
-
}
|
|
357
335
|
function getDropTableQueriesForSnapshot(snapshot) {
|
|
358
336
|
const queries = [];
|
|
359
337
|
for (const tableName of Object.keys(snapshot.schema)) {
|
|
@@ -14,7 +14,7 @@ import { CONFIG_FILE_NAMES, DB_PATH, VIRTUAL_MODULE_ID } from "../consts.js";
|
|
|
14
14
|
import { EXEC_DEFAULT_EXPORT_ERROR, EXEC_ERROR } from "../errors.js";
|
|
15
15
|
import { resolveDbConfig } from "../load-file.js";
|
|
16
16
|
import { SEED_DEV_FILE_NAME } from "../queries.js";
|
|
17
|
-
import { getDbDirectoryUrl,
|
|
17
|
+
import { getDbDirectoryUrl, getRemoteDatabaseInfo } from "../utils.js";
|
|
18
18
|
import { fileURLIntegration } from "./file-url.js";
|
|
19
19
|
import { getDtsContent } from "./typegen.js";
|
|
20
20
|
import {
|
|
@@ -24,7 +24,6 @@ function astroDBIntegration() {
|
|
|
24
24
|
let connectToRemote = false;
|
|
25
25
|
let configFileDependencies = [];
|
|
26
26
|
let root;
|
|
27
|
-
let appToken;
|
|
28
27
|
let tempViteServer;
|
|
29
28
|
let tables = {
|
|
30
29
|
get() {
|
|
@@ -55,10 +54,9 @@ function astroDBIntegration() {
|
|
|
55
54
|
const args = parseArgs(process.argv.slice(3));
|
|
56
55
|
connectToRemote = process.env.ASTRO_INTERNAL_TEST_REMOTE || args["remote"];
|
|
57
56
|
if (connectToRemote) {
|
|
58
|
-
appToken = await getManagedRemoteToken();
|
|
59
57
|
dbPlugin = vitePluginDb({
|
|
60
|
-
|
|
61
|
-
appToken:
|
|
58
|
+
connectToRemote,
|
|
59
|
+
appToken: getRemoteDatabaseInfo().token,
|
|
62
60
|
tables,
|
|
63
61
|
root: config.root,
|
|
64
62
|
srcDir: config.srcDir,
|
|
@@ -67,7 +65,7 @@ function astroDBIntegration() {
|
|
|
67
65
|
});
|
|
68
66
|
} else {
|
|
69
67
|
dbPlugin = vitePluginDb({
|
|
70
|
-
|
|
68
|
+
connectToRemote,
|
|
71
69
|
tables,
|
|
72
70
|
seedFiles,
|
|
73
71
|
root: config.root,
|
|
@@ -133,7 +131,7 @@ function astroDBIntegration() {
|
|
|
133
131
|
"astro:build:start": async ({ logger }) => {
|
|
134
132
|
if (!connectToRemote && !databaseFileEnvDefined() && finalBuildOutput === "server") {
|
|
135
133
|
const message = `Attempting to build without the --remote flag or the ASTRO_DATABASE_FILE environment variable defined. You probably want to pass --remote to astro build.`;
|
|
136
|
-
const hint = "Learn more connecting to
|
|
134
|
+
const hint = "Learn more connecting to libSQL: https://docs.astro.build/en/guides/astro-db/#connect-a-libsql-database-for-production";
|
|
137
135
|
throw new AstroDbError(message, hint);
|
|
138
136
|
}
|
|
139
137
|
logger.info("database: " + (connectToRemote ? yellow("remote") : blue("local database.")));
|
|
@@ -145,7 +143,6 @@ function astroDBIntegration() {
|
|
|
145
143
|
};
|
|
146
144
|
},
|
|
147
145
|
"astro:build:done": async ({}) => {
|
|
148
|
-
await appToken?.destroy();
|
|
149
146
|
await tempViteServer?.close();
|
|
150
147
|
}
|
|
151
148
|
}
|
|
@@ -12,7 +12,7 @@ export type SeedHandler = {
|
|
|
12
12
|
execute: (fileUrl: URL) => Promise<void>;
|
|
13
13
|
};
|
|
14
14
|
type VitePluginDBParams = {
|
|
15
|
-
|
|
15
|
+
connectToRemote: false;
|
|
16
16
|
tables: LateTables;
|
|
17
17
|
seedFiles: LateSeedFiles;
|
|
18
18
|
srcDir: URL;
|
|
@@ -21,7 +21,7 @@ type VitePluginDBParams = {
|
|
|
21
21
|
output: AstroConfig['output'];
|
|
22
22
|
seedHandler: SeedHandler;
|
|
23
23
|
} | {
|
|
24
|
-
|
|
24
|
+
connectToRemote: true;
|
|
25
25
|
tables: LateTables;
|
|
26
26
|
appToken: string;
|
|
27
27
|
srcDir: URL;
|
|
@@ -35,7 +35,7 @@ export declare function getLocalVirtualModContents({ tables, root }: {
|
|
|
35
35
|
tables: DBTables;
|
|
36
36
|
root: URL;
|
|
37
37
|
}): string;
|
|
38
|
-
export declare function
|
|
38
|
+
export declare function getRemoteVirtualModContents({ tables, appToken, isBuild, output, }: {
|
|
39
39
|
tables: DBTables;
|
|
40
40
|
appToken: string;
|
|
41
41
|
isBuild: boolean;
|
|
@@ -33,8 +33,8 @@ function vitePluginDb(params) {
|
|
|
33
33
|
},
|
|
34
34
|
async load(id) {
|
|
35
35
|
if (id !== resolved.module && id !== resolved.importedFromSeedFile) return;
|
|
36
|
-
if (params.
|
|
37
|
-
return
|
|
36
|
+
if (params.connectToRemote) {
|
|
37
|
+
return getRemoteVirtualModContents({
|
|
38
38
|
appToken: params.appToken,
|
|
39
39
|
tables: params.tables.get(),
|
|
40
40
|
isBuild: command === "build",
|
|
@@ -84,7 +84,7 @@ export * from ${RUNTIME_VIRTUAL_IMPORT};
|
|
|
84
84
|
|
|
85
85
|
${getStringifiedTableExports(tables)}`;
|
|
86
86
|
}
|
|
87
|
-
function
|
|
87
|
+
function getRemoteVirtualModContents({
|
|
88
88
|
tables,
|
|
89
89
|
appToken,
|
|
90
90
|
isBuild,
|
|
@@ -93,11 +93,10 @@ function getStudioVirtualModContents({
|
|
|
93
93
|
const dbInfo = getRemoteDatabaseInfo();
|
|
94
94
|
function appTokenArg() {
|
|
95
95
|
if (isBuild) {
|
|
96
|
-
const envPrefix = dbInfo.type === "studio" ? "ASTRO_STUDIO" : "ASTRO_DB";
|
|
97
96
|
if (output === "server") {
|
|
98
|
-
return `process.env
|
|
97
|
+
return `process.env.ASTRO_DB_APP_TOKEN`;
|
|
99
98
|
} else {
|
|
100
|
-
return `process.env
|
|
99
|
+
return `process.env.ASTRO_DB_APP_TOKEN ?? ${JSON.stringify(appToken)}`;
|
|
101
100
|
}
|
|
102
101
|
} else {
|
|
103
102
|
return JSON.stringify(appToken);
|
|
@@ -106,7 +105,7 @@ function getStudioVirtualModContents({
|
|
|
106
105
|
function dbUrlArg() {
|
|
107
106
|
const dbStr = JSON.stringify(dbInfo.url);
|
|
108
107
|
if (isBuild) {
|
|
109
|
-
return
|
|
108
|
+
return `import.meta.env.ASTRO_DB_REMOTE_URL ?? ${dbStr}`;
|
|
110
109
|
} else {
|
|
111
110
|
return dbStr;
|
|
112
111
|
}
|
|
@@ -115,9 +114,8 @@ function getStudioVirtualModContents({
|
|
|
115
114
|
import {asDrizzleTable, createRemoteDatabaseClient} from ${RUNTIME_IMPORT};
|
|
116
115
|
|
|
117
116
|
export const db = await createRemoteDatabaseClient({
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
appToken: ${appTokenArg()},
|
|
117
|
+
url: ${dbUrlArg()},
|
|
118
|
+
token: ${appTokenArg()},
|
|
121
119
|
});
|
|
122
120
|
|
|
123
121
|
export * from ${RUNTIME_VIRTUAL_IMPORT};
|
|
@@ -134,10 +132,9 @@ function getStringifiedTableExports(tables) {
|
|
|
134
132
|
}
|
|
135
133
|
const sqlite = new SQLiteAsyncDialect();
|
|
136
134
|
async function recreateTables({ tables, root }) {
|
|
137
|
-
const dbInfo = getRemoteDatabaseInfo();
|
|
138
135
|
const { ASTRO_DATABASE_FILE } = getAstroEnv();
|
|
139
136
|
const dbUrl = normalizeDatabaseUrl(ASTRO_DATABASE_FILE, new URL(DB_PATH, root).href);
|
|
140
|
-
const db = createLocalDatabaseClient({ dbUrl
|
|
137
|
+
const db = createLocalDatabaseClient({ dbUrl });
|
|
141
138
|
const setupQueries = [];
|
|
142
139
|
for (const [name, table] of Object.entries(tables.get() ?? {})) {
|
|
143
140
|
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${sqlite.escapeName(name)}`);
|
|
@@ -158,6 +155,6 @@ function getResolvedSeedFiles({ root, seedFiles }) {
|
|
|
158
155
|
export {
|
|
159
156
|
getConfigVirtualModContents,
|
|
160
157
|
getLocalVirtualModContents,
|
|
161
|
-
|
|
158
|
+
getRemoteVirtualModContents,
|
|
162
159
|
vitePluginDb
|
|
163
160
|
};
|
package/dist/core/load-file.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export declare function resolveDbConfig({ root, integrations, }: Pick<AstroConfi
|
|
|
11
11
|
on: string | string[];
|
|
12
12
|
unique?: boolean | undefined;
|
|
13
13
|
}>;
|
|
14
|
+
deprecated: boolean;
|
|
14
15
|
columns: Record<string, {
|
|
15
16
|
type: "boolean";
|
|
16
17
|
schema: {
|
|
@@ -83,7 +84,6 @@ export declare function resolveDbConfig({ root, integrations, }: Pick<AstroConfi
|
|
|
83
84
|
collection?: string | undefined;
|
|
84
85
|
};
|
|
85
86
|
}>;
|
|
86
|
-
deprecated: boolean;
|
|
87
87
|
foreignKeys?: (Omit<{
|
|
88
88
|
columns: import("./schemas.js").MaybeArray<string>;
|
|
89
89
|
references: () => import("./schemas.js").MaybeArray<Omit<import("zod").input<typeof import("./schemas.js").referenceableColumnSchema>, "references">>;
|
package/dist/core/load-file.js
CHANGED
|
@@ -115,8 +115,6 @@ async function bundleFile({
|
|
|
115
115
|
sourcemap: "inline",
|
|
116
116
|
metafile: true,
|
|
117
117
|
define: {
|
|
118
|
-
"import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL": "undefined",
|
|
119
|
-
"import.meta.env.ASTRO_DB_REMOTE_DB_URL": "undefined",
|
|
120
118
|
"import.meta.env.ASTRO_DATABASE_FILE": JSON.stringify(ASTRO_DATABASE_FILE ?? "")
|
|
121
119
|
},
|
|
122
120
|
plugins: [
|