@astrojs/db 0.10.6 → 0.11.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/dist/_internal/core/utils.d.ts +1 -0
- package/dist/core/cli/commands/execute/index.d.ts +1 -1
- package/dist/core/cli/commands/execute/index.js +1 -4
- package/dist/core/cli/commands/login/index.js +1 -1
- package/dist/core/cli/commands/shell/index.js +8 -2
- package/dist/core/cli/migration-queries.js +5 -5
- package/dist/core/cli/print-help.js +1 -1
- package/dist/core/errors.d.ts +1 -0
- package/dist/core/errors.js +5 -0
- package/dist/core/integration/index.js +70 -30
- package/dist/core/integration/vite-plugin-db.d.ts +11 -6
- package/dist/core/integration/vite-plugin-db.js +61 -47
- package/dist/core/load-file.d.ts +1 -0
- package/dist/core/load-file.js +11 -7
- package/dist/{runtime → core}/queries.d.ts +3 -3
- package/dist/{runtime → core}/queries.js +3 -3
- package/dist/core/tokens.js +12 -3
- package/dist/core/utils.d.ts +1 -0
- package/dist/core/utils.js +5 -0
- package/dist/runtime/index.d.ts +1 -2
- package/dist/runtime/index.js +2 -5
- package/dist/runtime/utils.d.ts +0 -1
- package/dist/runtime/utils.js +0 -1
- package/dist/utils.d.ts +3 -1
- package/dist/utils.js +5 -1
- package/package.json +4 -3
- package/dist/runtime/seed-local.d.ts +0 -10
- package/dist/runtime/seed-local.js +0 -55
|
@@ -2,6 +2,7 @@ import type { AstroConfig, AstroIntegration } from 'astro';
|
|
|
2
2
|
import type { AstroDbIntegration } from './types.js';
|
|
3
3
|
export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
|
|
4
4
|
export declare function getAstroStudioEnv(envMode?: string): Record<`ASTRO_STUDIO_${string}`, string>;
|
|
5
|
+
export declare function getAstroEnv(envMode?: string): Record<`ASTRO_${string}`, string>;
|
|
5
6
|
export declare function getRemoteDatabaseUrl(): string;
|
|
6
7
|
export declare function getAstroStudioUrl(): string;
|
|
7
8
|
export declare function getDbDirectoryUrl(root: URL | string): URL;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AstroConfig } from 'astro';
|
|
2
2
|
import type { Arguments } from 'yargs-parser';
|
|
3
|
-
import {
|
|
3
|
+
import type { DBConfig } from '../../../types.js';
|
|
4
4
|
export declare function cmd({ astroConfig, dbConfig, flags, }: {
|
|
5
5
|
astroConfig: AstroConfig;
|
|
6
6
|
dbConfig: DBConfig;
|
|
@@ -13,7 +13,6 @@ import {
|
|
|
13
13
|
} from "../../../integration/vite-plugin-db.js";
|
|
14
14
|
import { bundleFile, importBundledFile } from "../../../load-file.js";
|
|
15
15
|
import { getManagedAppTokenOrExit } from "../../../tokens.js";
|
|
16
|
-
import {} from "../../../types.js";
|
|
17
16
|
async function cmd({
|
|
18
17
|
astroConfig,
|
|
19
18
|
dbConfig,
|
|
@@ -41,9 +40,7 @@ async function cmd({
|
|
|
41
40
|
} else {
|
|
42
41
|
virtualModContents = getLocalVirtualModContents({
|
|
43
42
|
tables: dbConfig.tables ?? {},
|
|
44
|
-
root: astroConfig.root
|
|
45
|
-
shouldSeed: false,
|
|
46
|
-
seedFiles: []
|
|
43
|
+
root: astroConfig.root
|
|
47
44
|
});
|
|
48
45
|
}
|
|
49
46
|
const { code } = await bundleFile({ virtualModContents, root: astroConfig.root, fileUrl });
|
|
@@ -9,7 +9,7 @@ import { SESSION_LOGIN_FILE } from "../../../tokens.js";
|
|
|
9
9
|
import { getAstroStudioUrl } from "../../../utils.js";
|
|
10
10
|
const isWebContainer = (
|
|
11
11
|
// Stackblitz heuristic
|
|
12
|
-
process.versions?.webcontainer ?? //
|
|
12
|
+
process.versions?.webcontainer ?? // GitHub Codespaces heuristic
|
|
13
13
|
process.env.CODESPACE_NAME
|
|
14
14
|
);
|
|
15
15
|
async function cmd({
|
|
@@ -3,10 +3,11 @@ import {
|
|
|
3
3
|
createLocalDatabaseClient,
|
|
4
4
|
createRemoteDatabaseClient
|
|
5
5
|
} from "../../../../runtime/db-client.js";
|
|
6
|
+
import { normalizeDatabaseUrl } from "../../../../runtime/index.js";
|
|
6
7
|
import { DB_PATH } from "../../../consts.js";
|
|
7
8
|
import { SHELL_QUERY_MISSING_ERROR } from "../../../errors.js";
|
|
8
9
|
import { getManagedAppTokenOrExit } from "../../../tokens.js";
|
|
9
|
-
import { getRemoteDatabaseUrl } from "../../../utils.js";
|
|
10
|
+
import { getAstroEnv, getRemoteDatabaseUrl } from "../../../utils.js";
|
|
10
11
|
async function cmd({
|
|
11
12
|
flags,
|
|
12
13
|
astroConfig
|
|
@@ -23,7 +24,12 @@ async function cmd({
|
|
|
23
24
|
await appToken.destroy();
|
|
24
25
|
console.log(result);
|
|
25
26
|
} else {
|
|
26
|
-
const
|
|
27
|
+
const { ASTRO_DATABASE_FILE } = getAstroEnv();
|
|
28
|
+
const dbUrl = normalizeDatabaseUrl(
|
|
29
|
+
ASTRO_DATABASE_FILE,
|
|
30
|
+
new URL(DB_PATH, astroConfig.root).href
|
|
31
|
+
);
|
|
32
|
+
const db = createLocalDatabaseClient({ dbUrl });
|
|
27
33
|
const result = await db.run(sql.raw(query));
|
|
28
34
|
console.log(result);
|
|
29
35
|
}
|
|
@@ -4,6 +4,10 @@ import * as color from "kleur/colors";
|
|
|
4
4
|
import { customAlphabet } from "nanoid";
|
|
5
5
|
import stripAnsi from "strip-ansi";
|
|
6
6
|
import { hasPrimaryKey } from "../../runtime/index.js";
|
|
7
|
+
import { isSerializedSQL } from "../../runtime/types.js";
|
|
8
|
+
import { safeFetch } from "../../runtime/utils.js";
|
|
9
|
+
import { MIGRATION_VERSION } from "../consts.js";
|
|
10
|
+
import { RENAME_COLUMN_ERROR, RENAME_TABLE_ERROR } from "../errors.js";
|
|
7
11
|
import {
|
|
8
12
|
getCreateIndexQueries,
|
|
9
13
|
getCreateTableQuery,
|
|
@@ -12,11 +16,7 @@ import {
|
|
|
12
16
|
getReferencesConfig,
|
|
13
17
|
hasDefault,
|
|
14
18
|
schemaTypeToSqlType
|
|
15
|
-
} from "
|
|
16
|
-
import { isSerializedSQL } from "../../runtime/types.js";
|
|
17
|
-
import { safeFetch } from "../../runtime/utils.js";
|
|
18
|
-
import { MIGRATION_VERSION } from "../consts.js";
|
|
19
|
-
import { RENAME_COLUMN_ERROR, RENAME_TABLE_ERROR } from "../errors.js";
|
|
19
|
+
} from "../queries.js";
|
|
20
20
|
import { columnSchema } from "../schemas.js";
|
|
21
21
|
import {
|
|
22
22
|
} from "../types.js";
|
package/dist/core/errors.d.ts
CHANGED
package/dist/core/errors.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { bold, cyan, red } from "kleur/colors";
|
|
2
|
+
const MISSING_SESSION_ID_CI_ERROR = `${red("\u25B6 ASTRO_STUDIO_APP_TOKEN required")}
|
|
3
|
+
|
|
4
|
+
To authenticate with Astro Studio add the token to your CI's environment variables.
|
|
5
|
+
`;
|
|
2
6
|
const MISSING_SESSION_ID_ERROR = `${red("\u25B6 Login required!")}
|
|
3
7
|
|
|
4
8
|
To authenticate with Astro Studio, run
|
|
@@ -53,6 +57,7 @@ export {
|
|
|
53
57
|
INTEGRATION_TABLE_CONFLICT_ERROR,
|
|
54
58
|
MISSING_EXECUTE_PATH_ERROR,
|
|
55
59
|
MISSING_PROJECT_ID_ERROR,
|
|
60
|
+
MISSING_SESSION_ID_CI_ERROR,
|
|
56
61
|
MISSING_SESSION_ID_ERROR,
|
|
57
62
|
RENAME_COLUMN_ERROR,
|
|
58
63
|
RENAME_TABLE_ERROR,
|
|
@@ -1,25 +1,35 @@
|
|
|
1
1
|
import { existsSync } from "fs";
|
|
2
2
|
import { dirname } from "path";
|
|
3
3
|
import { fileURLToPath } from "url";
|
|
4
|
+
import { LibsqlError } from "@libsql/client";
|
|
4
5
|
import { mkdir, writeFile } from "fs/promises";
|
|
5
6
|
import { blue, yellow } from "kleur/colors";
|
|
6
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
createServer,
|
|
9
|
+
loadEnv,
|
|
10
|
+
mergeConfig
|
|
11
|
+
} from "vite";
|
|
7
12
|
import parseArgs from "yargs-parser";
|
|
8
|
-
import { SEED_DEV_FILE_NAME } from "../../runtime/queries.js";
|
|
9
13
|
import { AstroDbError } from "../../runtime/utils.js";
|
|
10
14
|
import { CONFIG_FILE_NAMES, DB_PATH } from "../consts.js";
|
|
15
|
+
import { EXEC_DEFAULT_EXPORT_ERROR, EXEC_ERROR } from "../errors.js";
|
|
11
16
|
import { resolveDbConfig } from "../load-file.js";
|
|
17
|
+
import { SEED_DEV_FILE_NAME } from "../queries.js";
|
|
12
18
|
import { getManagedAppTokenOrExit } from "../tokens.js";
|
|
13
19
|
import { getDbDirectoryUrl } from "../utils.js";
|
|
14
20
|
import { fileURLIntegration } from "./file-url.js";
|
|
15
21
|
import { typegenInternal } from "./typegen.js";
|
|
16
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
resolved,
|
|
24
|
+
vitePluginDb
|
|
25
|
+
} from "./vite-plugin-db.js";
|
|
17
26
|
import { vitePluginInjectEnvTs } from "./vite-plugin-inject-env-ts.js";
|
|
18
27
|
function astroDBIntegration() {
|
|
19
28
|
let connectToStudio = false;
|
|
20
29
|
let configFileDependencies = [];
|
|
21
30
|
let root;
|
|
22
31
|
let appToken;
|
|
32
|
+
let tempViteServer;
|
|
23
33
|
let tables = {
|
|
24
34
|
get() {
|
|
25
35
|
throw new Error("[astro:db] INTERNAL Tables not loaded yet");
|
|
@@ -30,6 +40,12 @@ function astroDBIntegration() {
|
|
|
30
40
|
throw new Error("[astro:db] INTERNAL Seed files not loaded yet");
|
|
31
41
|
}
|
|
32
42
|
};
|
|
43
|
+
let seedHandler = {
|
|
44
|
+
execute: () => {
|
|
45
|
+
throw new Error("[astro:db] INTERNAL Seed handler not loaded yet");
|
|
46
|
+
},
|
|
47
|
+
inProgress: false
|
|
48
|
+
};
|
|
33
49
|
let command;
|
|
34
50
|
let output = "server";
|
|
35
51
|
return {
|
|
@@ -52,7 +68,8 @@ function astroDBIntegration() {
|
|
|
52
68
|
tables,
|
|
53
69
|
root: config.root,
|
|
54
70
|
srcDir: config.srcDir,
|
|
55
|
-
output: config.output
|
|
71
|
+
output: config.output,
|
|
72
|
+
seedHandler
|
|
56
73
|
});
|
|
57
74
|
} else {
|
|
58
75
|
dbPlugin = vitePluginDb({
|
|
@@ -61,7 +78,9 @@ function astroDBIntegration() {
|
|
|
61
78
|
seedFiles,
|
|
62
79
|
root: config.root,
|
|
63
80
|
srcDir: config.srcDir,
|
|
64
|
-
output: config.output
|
|
81
|
+
output: config.output,
|
|
82
|
+
logger,
|
|
83
|
+
seedHandler
|
|
65
84
|
});
|
|
66
85
|
}
|
|
67
86
|
updateConfig({
|
|
@@ -86,6 +105,9 @@ function astroDBIntegration() {
|
|
|
86
105
|
await typegenInternal({ tables: tables.get() ?? {}, root: config.root });
|
|
87
106
|
},
|
|
88
107
|
"astro:server:setup": async ({ server, logger }) => {
|
|
108
|
+
seedHandler.execute = async (fileUrl) => {
|
|
109
|
+
await executeSeedFile({ fileUrl, viteServer: server });
|
|
110
|
+
};
|
|
89
111
|
const filesToWatch = [
|
|
90
112
|
...CONFIG_FILE_NAMES.map((c) => new URL(c, getDbDirectoryUrl(root))),
|
|
91
113
|
...configFileDependencies.map((c) => new URL(c, root))
|
|
@@ -105,33 +127,9 @@ function astroDBIntegration() {
|
|
|
105
127
|
const localSeedPaths = SEED_DEV_FILE_NAME.map(
|
|
106
128
|
(name) => new URL(name, getDbDirectoryUrl(root))
|
|
107
129
|
);
|
|
108
|
-
let seedInFlight = false;
|
|
109
130
|
if (seedFiles.get().length || localSeedPaths.find((path) => existsSync(path))) {
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
const eagerReloadIntegrationSeedPaths = seedFiles.get().map((s) => typeof s === "string" && s.startsWith(".") ? new URL(s, root) : s).filter((s) => s instanceof URL);
|
|
113
|
-
const eagerReloadSeedPaths = [...eagerReloadIntegrationSeedPaths, ...localSeedPaths];
|
|
114
|
-
server.watcher.on("all", (event, relativeEntry) => {
|
|
115
|
-
if (event === "unlink" || event === "unlinkDir")
|
|
116
|
-
return;
|
|
117
|
-
const entry = new URL(relativeEntry, root);
|
|
118
|
-
if (eagerReloadSeedPaths.find((path) => entry.href === path.href)) {
|
|
119
|
-
loadSeedModule();
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
function loadSeedModule() {
|
|
123
|
-
if (seedInFlight)
|
|
124
|
-
return;
|
|
125
|
-
seedInFlight = true;
|
|
126
|
-
const mod = server.moduleGraph.getModuleById(resolved.seedVirtual);
|
|
127
|
-
if (mod)
|
|
128
|
-
server.moduleGraph.invalidateModule(mod);
|
|
129
|
-
server.ssrLoadModule(resolved.seedVirtual).then(() => {
|
|
130
|
-
logger.info("Seeded database.");
|
|
131
|
-
}).catch((e) => {
|
|
131
|
+
server.ssrLoadModule(resolved.module).catch((e) => {
|
|
132
132
|
logger.error(e instanceof Error ? e.message : String(e));
|
|
133
|
-
}).finally(() => {
|
|
134
|
-
seedInFlight = false;
|
|
135
133
|
});
|
|
136
134
|
}
|
|
137
135
|
}, 100);
|
|
@@ -144,8 +142,15 @@ function astroDBIntegration() {
|
|
|
144
142
|
}
|
|
145
143
|
logger.info("database: " + (connectToStudio ? yellow("remote") : blue("local database.")));
|
|
146
144
|
},
|
|
145
|
+
"astro:build:setup": async ({ vite }) => {
|
|
146
|
+
tempViteServer = await getTempViteServer({ viteConfig: vite });
|
|
147
|
+
seedHandler.execute = async (fileUrl) => {
|
|
148
|
+
await executeSeedFile({ fileUrl, viteServer: tempViteServer });
|
|
149
|
+
};
|
|
150
|
+
},
|
|
147
151
|
"astro:build:done": async ({}) => {
|
|
148
152
|
await appToken?.destroy();
|
|
153
|
+
await tempViteServer?.close();
|
|
149
154
|
}
|
|
150
155
|
}
|
|
151
156
|
};
|
|
@@ -157,6 +162,41 @@ function databaseFileEnvDefined() {
|
|
|
157
162
|
function integration() {
|
|
158
163
|
return [astroDBIntegration(), fileURLIntegration()];
|
|
159
164
|
}
|
|
165
|
+
async function executeSeedFile({
|
|
166
|
+
fileUrl,
|
|
167
|
+
viteServer
|
|
168
|
+
}) {
|
|
169
|
+
const mod = await viteServer.ssrLoadModule(fileUrl.pathname);
|
|
170
|
+
if (typeof mod.default !== "function") {
|
|
171
|
+
throw new AstroDbError(EXEC_DEFAULT_EXPORT_ERROR(fileURLToPath(fileUrl)));
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
await mod.default();
|
|
175
|
+
} catch (e) {
|
|
176
|
+
if (e instanceof LibsqlError) {
|
|
177
|
+
throw new AstroDbError(EXEC_ERROR(e.message));
|
|
178
|
+
}
|
|
179
|
+
throw e;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async function getTempViteServer({ viteConfig }) {
|
|
183
|
+
const tempViteServer = await createServer(
|
|
184
|
+
mergeConfig(viteConfig, {
|
|
185
|
+
server: { middlewareMode: true, hmr: false, watch: null },
|
|
186
|
+
optimizeDeps: { noDiscovery: true },
|
|
187
|
+
ssr: { external: [] },
|
|
188
|
+
logLevel: "silent"
|
|
189
|
+
})
|
|
190
|
+
);
|
|
191
|
+
const hotSend = tempViteServer.hot.send;
|
|
192
|
+
tempViteServer.hot.send = (payload) => {
|
|
193
|
+
if (payload.type === "error") {
|
|
194
|
+
throw payload.err;
|
|
195
|
+
}
|
|
196
|
+
return hotSend(payload);
|
|
197
|
+
};
|
|
198
|
+
return tempViteServer;
|
|
199
|
+
}
|
|
160
200
|
export {
|
|
161
201
|
integration
|
|
162
202
|
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { AstroConfig } from 'astro';
|
|
1
|
+
import type { AstroConfig, AstroIntegrationLogger } from 'astro';
|
|
2
2
|
import type { DBTables } from '../types.js';
|
|
3
3
|
import { type VitePlugin } from '../utils.js';
|
|
4
4
|
export declare const resolved: {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
module: string;
|
|
6
|
+
importedFromSeedFile: string;
|
|
7
7
|
};
|
|
8
8
|
export type LateTables = {
|
|
9
9
|
get: () => DBTables;
|
|
@@ -11,13 +11,19 @@ export type LateTables = {
|
|
|
11
11
|
export type LateSeedFiles = {
|
|
12
12
|
get: () => Array<string | URL>;
|
|
13
13
|
};
|
|
14
|
+
export type SeedHandler = {
|
|
15
|
+
inProgress: boolean;
|
|
16
|
+
execute: (fileUrl: URL) => Promise<void>;
|
|
17
|
+
};
|
|
14
18
|
type VitePluginDBParams = {
|
|
15
19
|
connectToStudio: false;
|
|
16
20
|
tables: LateTables;
|
|
17
21
|
seedFiles: LateSeedFiles;
|
|
18
22
|
srcDir: URL;
|
|
19
23
|
root: URL;
|
|
24
|
+
logger?: AstroIntegrationLogger;
|
|
20
25
|
output: AstroConfig['output'];
|
|
26
|
+
seedHandler: SeedHandler;
|
|
21
27
|
} | {
|
|
22
28
|
connectToStudio: true;
|
|
23
29
|
tables: LateTables;
|
|
@@ -25,14 +31,13 @@ type VitePluginDBParams = {
|
|
|
25
31
|
srcDir: URL;
|
|
26
32
|
root: URL;
|
|
27
33
|
output: AstroConfig['output'];
|
|
34
|
+
seedHandler: SeedHandler;
|
|
28
35
|
};
|
|
29
36
|
export declare function vitePluginDb(params: VitePluginDBParams): VitePlugin;
|
|
30
37
|
export declare function getConfigVirtualModContents(): string;
|
|
31
|
-
export declare function getLocalVirtualModContents({ tables, root,
|
|
38
|
+
export declare function getLocalVirtualModContents({ tables, root, }: {
|
|
32
39
|
tables: DBTables;
|
|
33
|
-
seedFiles: Array<string | URL>;
|
|
34
40
|
root: URL;
|
|
35
|
-
shouldSeed: boolean;
|
|
36
41
|
}): string;
|
|
37
42
|
export declare function getStudioVirtualModContents({ tables, appToken, isBuild, output, }: {
|
|
38
43
|
tables: DBTables;
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
1
2
|
import { fileURLToPath } from "node:url";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
3
|
+
import { sql } from "drizzle-orm";
|
|
4
|
+
import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
|
|
5
|
+
import { createLocalDatabaseClient } from "../../runtime/db-client.js";
|
|
6
|
+
import { normalizeDatabaseUrl } from "../../runtime/index.js";
|
|
4
7
|
import { DB_PATH, RUNTIME_IMPORT, RUNTIME_VIRTUAL_IMPORT, VIRTUAL_MODULE_ID } from "../consts.js";
|
|
5
|
-
import {
|
|
6
|
-
|
|
8
|
+
import { getResolvedFileUrl } from "../load-file.js";
|
|
9
|
+
import { SEED_DEV_FILE_NAME, getCreateIndexQueries, getCreateTableQuery } from "../queries.js";
|
|
10
|
+
import { getAstroEnv, getDbDirectoryUrl, getRemoteDatabaseUrl } from "../utils.js";
|
|
7
11
|
const resolved = {
|
|
8
|
-
|
|
9
|
-
|
|
12
|
+
module: "\0" + VIRTUAL_MODULE_ID,
|
|
13
|
+
importedFromSeedFile: "\0" + VIRTUAL_MODULE_ID + ":seed"
|
|
10
14
|
};
|
|
11
15
|
function vitePluginDb(params) {
|
|
12
|
-
const srcDirPath = normalizePath(fileURLToPath(params.srcDir));
|
|
13
|
-
const dbDirPath = normalizePath(fileURLToPath(getDbDirectoryUrl(params.root)));
|
|
14
16
|
let command = "build";
|
|
15
17
|
return {
|
|
16
18
|
name: "astro:db",
|
|
@@ -18,21 +20,16 @@ function vitePluginDb(params) {
|
|
|
18
20
|
configResolved(resolvedConfig) {
|
|
19
21
|
command = resolvedConfig.command;
|
|
20
22
|
},
|
|
21
|
-
async resolveId(id
|
|
23
|
+
async resolveId(id) {
|
|
22
24
|
if (id !== VIRTUAL_MODULE_ID)
|
|
23
25
|
return;
|
|
24
|
-
if (params.
|
|
25
|
-
return resolved.
|
|
26
|
-
const importer = rawImporter ? await this.resolve(rawImporter) : null;
|
|
27
|
-
if (!importer)
|
|
28
|
-
return resolved.virtual;
|
|
29
|
-
if (importer.id.startsWith(srcDirPath) && !importer.id.startsWith(dbDirPath)) {
|
|
30
|
-
return resolved.seedVirtual;
|
|
26
|
+
if (params.seedHandler.inProgress) {
|
|
27
|
+
return resolved.importedFromSeedFile;
|
|
31
28
|
}
|
|
32
|
-
return resolved.
|
|
29
|
+
return resolved.module;
|
|
33
30
|
},
|
|
34
31
|
async load(id) {
|
|
35
|
-
if (id !== resolved.
|
|
32
|
+
if (id !== resolved.module && id !== resolved.importedFromSeedFile)
|
|
36
33
|
return;
|
|
37
34
|
if (params.connectToStudio) {
|
|
38
35
|
return getStudioVirtualModContents({
|
|
@@ -42,11 +39,28 @@ function vitePluginDb(params) {
|
|
|
42
39
|
output: params.output
|
|
43
40
|
});
|
|
44
41
|
}
|
|
42
|
+
if (id === resolved.importedFromSeedFile) {
|
|
43
|
+
return getLocalVirtualModContents({
|
|
44
|
+
root: params.root,
|
|
45
|
+
tables: params.tables.get()
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
await recreateTables(params);
|
|
49
|
+
const seedFiles = getResolvedSeedFiles(params);
|
|
50
|
+
for await (const seedFile of seedFiles) {
|
|
51
|
+
this.addWatchFile(fileURLToPath(seedFile));
|
|
52
|
+
if (existsSync(seedFile)) {
|
|
53
|
+
params.seedHandler.inProgress = true;
|
|
54
|
+
await params.seedHandler.execute(seedFile);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (params.seedHandler.inProgress) {
|
|
58
|
+
(params.logger ?? console).info("Seeded database.");
|
|
59
|
+
params.seedHandler.inProgress = false;
|
|
60
|
+
}
|
|
45
61
|
return getLocalVirtualModContents({
|
|
46
62
|
root: params.root,
|
|
47
|
-
tables: params.tables.get()
|
|
48
|
-
seedFiles: params.seedFiles.get(),
|
|
49
|
-
shouldSeed: id === resolved.seedVirtual
|
|
63
|
+
tables: params.tables.get()
|
|
50
64
|
});
|
|
51
65
|
}
|
|
52
66
|
};
|
|
@@ -56,40 +70,15 @@ function getConfigVirtualModContents() {
|
|
|
56
70
|
}
|
|
57
71
|
function getLocalVirtualModContents({
|
|
58
72
|
tables,
|
|
59
|
-
root
|
|
60
|
-
seedFiles,
|
|
61
|
-
shouldSeed
|
|
73
|
+
root
|
|
62
74
|
}) {
|
|
63
|
-
const userSeedFilePaths = SEED_DEV_FILE_NAME.map(
|
|
64
|
-
// Format as /db/[name].ts
|
|
65
|
-
// for Vite import.meta.glob
|
|
66
|
-
(name) => new URL(name, getDbDirectoryUrl("file:///")).pathname
|
|
67
|
-
);
|
|
68
|
-
const resolveId = (id) => id.startsWith(".") ? normalizePath(fileURLToPath(new URL(id, root))) : id;
|
|
69
|
-
const integrationSeedImportStatements = [];
|
|
70
|
-
const integrationSeedImportNames = [];
|
|
71
|
-
seedFiles.forEach((pathOrUrl, index) => {
|
|
72
|
-
const path = typeof pathOrUrl === "string" ? resolveId(pathOrUrl) : pathOrUrl.pathname;
|
|
73
|
-
const importName = "integration_seed_" + index;
|
|
74
|
-
integrationSeedImportStatements.push(`import ${importName} from ${JSON.stringify(path)};`);
|
|
75
|
-
integrationSeedImportNames.push(importName);
|
|
76
|
-
});
|
|
77
75
|
const dbUrl = new URL(DB_PATH, root);
|
|
78
76
|
return `
|
|
79
77
|
import { asDrizzleTable, createLocalDatabaseClient, normalizeDatabaseUrl } from ${RUNTIME_IMPORT};
|
|
80
|
-
${shouldSeed ? `import { seedLocal } from ${RUNTIME_IMPORT};` : ""}
|
|
81
|
-
${shouldSeed ? integrationSeedImportStatements.join("\n") : ""}
|
|
82
78
|
|
|
83
79
|
const dbUrl = normalizeDatabaseUrl(import.meta.env.ASTRO_DATABASE_FILE, ${JSON.stringify(dbUrl)});
|
|
84
80
|
export const db = createLocalDatabaseClient({ dbUrl });
|
|
85
81
|
|
|
86
|
-
${shouldSeed ? `await seedLocal({
|
|
87
|
-
db,
|
|
88
|
-
tables: ${JSON.stringify(tables)},
|
|
89
|
-
userSeedGlob: import.meta.glob(${JSON.stringify(userSeedFilePaths)}, { eager: true }),
|
|
90
|
-
integrationSeedFunctions: [${integrationSeedImportNames.join(",")}],
|
|
91
|
-
});` : ""}
|
|
92
|
-
|
|
93
82
|
export * from ${RUNTIME_VIRTUAL_IMPORT};
|
|
94
83
|
|
|
95
84
|
${getStringifiedTableExports(tables)}`;
|
|
@@ -132,6 +121,31 @@ function getStringifiedTableExports(tables) {
|
|
|
132
121
|
)}, false)`
|
|
133
122
|
).join("\n");
|
|
134
123
|
}
|
|
124
|
+
const sqlite = new SQLiteAsyncDialect();
|
|
125
|
+
async function recreateTables({ tables, root }) {
|
|
126
|
+
const { ASTRO_DATABASE_FILE } = getAstroEnv();
|
|
127
|
+
const dbUrl = normalizeDatabaseUrl(ASTRO_DATABASE_FILE, new URL(DB_PATH, root).href);
|
|
128
|
+
const db = createLocalDatabaseClient({ dbUrl });
|
|
129
|
+
const setupQueries = [];
|
|
130
|
+
for (const [name, table] of Object.entries(tables.get() ?? {})) {
|
|
131
|
+
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${sqlite.escapeName(name)}`);
|
|
132
|
+
const createQuery = sql.raw(getCreateTableQuery(name, table));
|
|
133
|
+
const indexQueries = getCreateIndexQueries(name, table);
|
|
134
|
+
setupQueries.push(dropQuery, createQuery, ...indexQueries.map((s) => sql.raw(s)));
|
|
135
|
+
}
|
|
136
|
+
await db.batch([
|
|
137
|
+
db.run(sql`pragma defer_foreign_keys=true;`),
|
|
138
|
+
...setupQueries.map((q) => db.run(q))
|
|
139
|
+
]);
|
|
140
|
+
}
|
|
141
|
+
function getResolvedSeedFiles({
|
|
142
|
+
root,
|
|
143
|
+
seedFiles
|
|
144
|
+
}) {
|
|
145
|
+
const localSeedFiles = SEED_DEV_FILE_NAME.map((name) => new URL(name, getDbDirectoryUrl(root)));
|
|
146
|
+
const integrationSeedFiles = seedFiles.get().map((s) => getResolvedFileUrl(root, s));
|
|
147
|
+
return [...integrationSeedFiles, ...localSeedFiles];
|
|
148
|
+
}
|
|
135
149
|
export {
|
|
136
150
|
getConfigVirtualModContents,
|
|
137
151
|
getLocalVirtualModContents,
|
package/dist/core/load-file.d.ts
CHANGED
|
@@ -172,6 +172,7 @@ export declare function resolveDbConfig({ root, integrations, }: Pick<AstroConfi
|
|
|
172
172
|
/** Additional `astro:db` seed file paths provided by integrations. */
|
|
173
173
|
integrationSeedPaths: (string | URL)[];
|
|
174
174
|
}>;
|
|
175
|
+
export declare function getResolvedFileUrl(root: URL, filePathOrUrl: string | URL): URL;
|
|
175
176
|
/**
|
|
176
177
|
* Bundle arbitrary `mjs` or `ts` file.
|
|
177
178
|
* Simplified fork from Vite's `bundleConfigFile` function.
|
package/dist/core/load-file.js
CHANGED
|
@@ -9,7 +9,7 @@ import { errorMap } from "./integration/error-map.js";
|
|
|
9
9
|
import { getConfigVirtualModContents } from "./integration/vite-plugin-db.js";
|
|
10
10
|
import { dbConfigSchema } from "./schemas.js";
|
|
11
11
|
import {} from "./types.js";
|
|
12
|
-
import { getDbDirectoryUrl } from "./utils.js";
|
|
12
|
+
import { getAstroEnv, getDbDirectoryUrl } from "./utils.js";
|
|
13
13
|
const isDbIntegration = (integration) => "astro:db:setup" in integration.hooks;
|
|
14
14
|
async function resolveDbConfig({
|
|
15
15
|
root,
|
|
@@ -70,15 +70,16 @@ async function loadUserConfigFile(root) {
|
|
|
70
70
|
}
|
|
71
71
|
return await loadAndBundleDbConfigFile({ root, fileUrl: configFileUrl });
|
|
72
72
|
}
|
|
73
|
-
|
|
74
|
-
let fileUrl;
|
|
73
|
+
function getResolvedFileUrl(root, filePathOrUrl) {
|
|
75
74
|
if (typeof filePathOrUrl === "string") {
|
|
76
75
|
const { resolve } = createRequire(root);
|
|
77
76
|
const resolvedFilePath = resolve(filePathOrUrl);
|
|
78
|
-
|
|
79
|
-
} else {
|
|
80
|
-
fileUrl = filePathOrUrl;
|
|
77
|
+
return pathToFileURL(resolvedFilePath);
|
|
81
78
|
}
|
|
79
|
+
return filePathOrUrl;
|
|
80
|
+
}
|
|
81
|
+
async function loadIntegrationConfigFile(root, filePathOrUrl) {
|
|
82
|
+
const fileUrl = getResolvedFileUrl(root, filePathOrUrl);
|
|
82
83
|
return await loadAndBundleDbConfigFile({ root, fileUrl });
|
|
83
84
|
}
|
|
84
85
|
async function loadAndBundleDbConfigFile({
|
|
@@ -103,6 +104,7 @@ async function bundleFile({
|
|
|
103
104
|
root,
|
|
104
105
|
virtualModContents
|
|
105
106
|
}) {
|
|
107
|
+
const { ASTRO_DATABASE_FILE } = getAstroEnv();
|
|
106
108
|
const result = await esbuild({
|
|
107
109
|
absWorkingDir: process.cwd(),
|
|
108
110
|
entryPoints: [fileURLToPath(fileUrl)],
|
|
@@ -116,7 +118,8 @@ async function bundleFile({
|
|
|
116
118
|
sourcemap: "inline",
|
|
117
119
|
metafile: true,
|
|
118
120
|
define: {
|
|
119
|
-
"import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL": "undefined"
|
|
121
|
+
"import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL": "undefined",
|
|
122
|
+
"import.meta.env.ASTRO_DATABASE_FILE": JSON.stringify(ASTRO_DATABASE_FILE ?? "")
|
|
120
123
|
},
|
|
121
124
|
plugins: [
|
|
122
125
|
{
|
|
@@ -165,6 +168,7 @@ async function importBundledFile({
|
|
|
165
168
|
}
|
|
166
169
|
export {
|
|
167
170
|
bundleFile,
|
|
171
|
+
getResolvedFileUrl,
|
|
168
172
|
importBundledFile,
|
|
169
173
|
resolveDbConfig
|
|
170
174
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BooleanColumn, ColumnType, DBColumn, DBTable, DateColumn, JsonColumn, NumberColumn, TextColumn } from '
|
|
1
|
+
import type { BooleanColumn, ColumnType, DBColumn, DBTable, DateColumn, JsonColumn, NumberColumn, TextColumn } from './types.js';
|
|
2
2
|
export declare const SEED_DEV_FILE_NAME: string[];
|
|
3
3
|
export declare function getDropTableIfExistsQuery(tableName: string): string;
|
|
4
4
|
export declare function getCreateTableQuery(tableName: string, table: DBTable): string;
|
|
@@ -17,7 +17,7 @@ export declare function getReferencesConfig(column: DBColumn): {
|
|
|
17
17
|
} & ({
|
|
18
18
|
optional: boolean;
|
|
19
19
|
primaryKey: false;
|
|
20
|
-
default?: number | import("
|
|
20
|
+
default?: number | import("../runtime/types.js").SerializedSQL | undefined;
|
|
21
21
|
} | {
|
|
22
22
|
primaryKey: true;
|
|
23
23
|
optional?: false | undefined;
|
|
@@ -33,7 +33,7 @@ export declare function getReferencesConfig(column: DBColumn): {
|
|
|
33
33
|
name?: string | undefined;
|
|
34
34
|
label?: string | undefined;
|
|
35
35
|
collection?: string | undefined;
|
|
36
|
-
default?: string | import("
|
|
36
|
+
default?: string | import("../runtime/types.js").SerializedSQL | undefined;
|
|
37
37
|
multiline?: boolean | undefined;
|
|
38
38
|
} & ({
|
|
39
39
|
optional: boolean;
|
|
@@ -6,9 +6,9 @@ import {
|
|
|
6
6
|
FOREIGN_KEY_REFERENCES_EMPTY_ERROR,
|
|
7
7
|
FOREIGN_KEY_REFERENCES_LENGTH_ERROR,
|
|
8
8
|
REFERENCE_DNE_ERROR
|
|
9
|
-
} from "
|
|
10
|
-
import { hasPrimaryKey } from "
|
|
11
|
-
import { isSerializedSQL } from "
|
|
9
|
+
} from "../runtime/errors.js";
|
|
10
|
+
import { hasPrimaryKey } from "../runtime/index.js";
|
|
11
|
+
import { isSerializedSQL } from "../runtime/types.js";
|
|
12
12
|
const sqlite = new SQLiteAsyncDialect();
|
|
13
13
|
const SEED_DEV_FILE_NAME = ["seed.ts", "seed.js", "seed.mjs", "seed.mts"];
|
|
14
14
|
function getDropTableIfExistsQuery(tableName) {
|
package/dist/core/tokens.js
CHANGED
|
@@ -2,10 +2,15 @@ import { readFile } from "node:fs/promises";
|
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { pathToFileURL } from "node:url";
|
|
5
|
+
import ci from "ci-info";
|
|
5
6
|
import { green } from "kleur/colors";
|
|
6
7
|
import ora from "ora";
|
|
7
8
|
import { safeFetch } from "../runtime/utils.js";
|
|
8
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
MISSING_PROJECT_ID_ERROR,
|
|
11
|
+
MISSING_SESSION_ID_CI_ERROR,
|
|
12
|
+
MISSING_SESSION_ID_ERROR
|
|
13
|
+
} from "./errors.js";
|
|
9
14
|
import { getAstroStudioEnv, getAstroStudioUrl } from "./utils.js";
|
|
10
15
|
const SESSION_LOGIN_FILE = pathToFileURL(join(homedir(), ".astro", "session-token"));
|
|
11
16
|
const PROJECT_ID_FILE = pathToFileURL(join(process.cwd(), ".astro", "link"));
|
|
@@ -159,11 +164,15 @@ async function getManagedAppTokenOrExit(token) {
|
|
|
159
164
|
}
|
|
160
165
|
const sessionToken = await getSessionIdFromFile();
|
|
161
166
|
if (!sessionToken) {
|
|
162
|
-
|
|
167
|
+
if (ci.isCI) {
|
|
168
|
+
console.error(MISSING_SESSION_ID_CI_ERROR);
|
|
169
|
+
} else {
|
|
170
|
+
console.error(MISSING_SESSION_ID_ERROR);
|
|
171
|
+
}
|
|
163
172
|
process.exit(1);
|
|
164
173
|
}
|
|
165
174
|
const projectId = await getProjectIdFromFile();
|
|
166
|
-
if (!
|
|
175
|
+
if (!projectId) {
|
|
167
176
|
console.error(MISSING_PROJECT_ID_ERROR);
|
|
168
177
|
process.exit(1);
|
|
169
178
|
}
|
package/dist/core/utils.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { AstroConfig, AstroIntegration } from 'astro';
|
|
|
2
2
|
import type { AstroDbIntegration } from './types.js';
|
|
3
3
|
export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
|
|
4
4
|
export declare function getAstroStudioEnv(envMode?: string): Record<`ASTRO_STUDIO_${string}`, string>;
|
|
5
|
+
export declare function getAstroEnv(envMode?: string): Record<`ASTRO_${string}`, string>;
|
|
5
6
|
export declare function getRemoteDatabaseUrl(): string;
|
|
6
7
|
export declare function getAstroStudioUrl(): string;
|
|
7
8
|
export declare function getDbDirectoryUrl(root: URL | string): URL;
|
package/dist/core/utils.js
CHANGED
|
@@ -3,6 +3,10 @@ function getAstroStudioEnv(envMode = "") {
|
|
|
3
3
|
const env = loadEnv(envMode, process.cwd(), "ASTRO_STUDIO_");
|
|
4
4
|
return env;
|
|
5
5
|
}
|
|
6
|
+
function getAstroEnv(envMode = "") {
|
|
7
|
+
const env = loadEnv(envMode, process.cwd(), "ASTRO_");
|
|
8
|
+
return env;
|
|
9
|
+
}
|
|
6
10
|
function getRemoteDatabaseUrl() {
|
|
7
11
|
const env = getAstroStudioEnv();
|
|
8
12
|
return env.ASTRO_STUDIO_REMOTE_DB_URL || "https://db.services.astro.build";
|
|
@@ -24,6 +28,7 @@ function mapObject(item, callback) {
|
|
|
24
28
|
}
|
|
25
29
|
export {
|
|
26
30
|
defineDbIntegration,
|
|
31
|
+
getAstroEnv,
|
|
27
32
|
getAstroStudioEnv,
|
|
28
33
|
getAstroStudioUrl,
|
|
29
34
|
getDbDirectoryUrl,
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { type ColumnDataType } from 'drizzle-orm';
|
|
2
|
-
import {
|
|
2
|
+
import type { DBColumn, DBTable } from '../core/types.js';
|
|
3
3
|
export type { Table } from './types.js';
|
|
4
4
|
export { createRemoteDatabaseClient, createLocalDatabaseClient } from './db-client.js';
|
|
5
|
-
export { seedLocal } from './seed-local.js';
|
|
6
5
|
export declare function hasPrimaryKey(column: DBColumn): boolean;
|
|
7
6
|
export declare function asDrizzleTable(name: string, table: DBTable): import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
8
7
|
name: string;
|
package/dist/runtime/index.js
CHANGED
|
@@ -6,11 +6,9 @@ import {
|
|
|
6
6
|
sqliteTable,
|
|
7
7
|
text
|
|
8
8
|
} from "drizzle-orm/sqlite-core";
|
|
9
|
-
import {} from "../core/types.js";
|
|
10
9
|
import { isSerializedSQL } from "./types.js";
|
|
11
10
|
import { pathToFileURL } from "./utils.js";
|
|
12
11
|
import { createRemoteDatabaseClient, createLocalDatabaseClient } from "./db-client.js";
|
|
13
|
-
import { seedLocal } from "./seed-local.js";
|
|
14
12
|
function hasPrimaryKey(column) {
|
|
15
13
|
return "primaryKey" in column.schema && !!column.schema.primaryKey;
|
|
16
14
|
}
|
|
@@ -116,7 +114,7 @@ function normalizeDatabaseUrl(envDbUrl, defaultDbUrl) {
|
|
|
116
114
|
if (envDbUrl.startsWith("file://")) {
|
|
117
115
|
return envDbUrl;
|
|
118
116
|
}
|
|
119
|
-
return new URL(envDbUrl, pathToFileURL(process.cwd())).toString();
|
|
117
|
+
return new URL(envDbUrl, pathToFileURL(process.cwd()) + "/").toString();
|
|
120
118
|
} else {
|
|
121
119
|
return defaultDbUrl;
|
|
122
120
|
}
|
|
@@ -126,6 +124,5 @@ export {
|
|
|
126
124
|
createLocalDatabaseClient,
|
|
127
125
|
createRemoteDatabaseClient,
|
|
128
126
|
hasPrimaryKey,
|
|
129
|
-
normalizeDatabaseUrl
|
|
130
|
-
seedLocal
|
|
127
|
+
normalizeDatabaseUrl
|
|
131
128
|
};
|
package/dist/runtime/utils.d.ts
CHANGED
package/dist/runtime/utils.js
CHANGED
package/dist/utils.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export { defineDbIntegration } from './core/utils.js';
|
|
2
|
-
|
|
2
|
+
import type { ColumnsConfig, TableConfig } from './core/types.js';
|
|
3
|
+
import { type Table } from './runtime/index.js';
|
|
4
|
+
export declare function asDrizzleTable<TableName extends string = string, TColumns extends ColumnsConfig = ColumnsConfig>(name: TableName, tableConfig: TableConfig<TColumns>): Table<TableName, TColumns>;
|
package/dist/utils.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { defineDbIntegration } from "./core/utils.js";
|
|
2
|
-
import {
|
|
2
|
+
import { tableSchema } from "./core/schemas.js";
|
|
3
|
+
import { asDrizzleTable as internal_asDrizzleTable } from "./runtime/index.js";
|
|
4
|
+
function asDrizzleTable(name, tableConfig) {
|
|
5
|
+
return internal_asDrizzleTable(name, tableSchema.parse(tableConfig));
|
|
6
|
+
}
|
|
3
7
|
export {
|
|
4
8
|
asDrizzleTable,
|
|
5
9
|
defineDbIntegration
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "Add libSQL and Astro Studio support to your Astro site",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -64,6 +64,7 @@
|
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"@libsql/client": "^0.6.0",
|
|
66
66
|
"async-listen": "^3.0.1",
|
|
67
|
+
"ci-info": "^4.0.0",
|
|
67
68
|
"deep-diff": "^1.0.2",
|
|
68
69
|
"drizzle-orm": "^0.30.9",
|
|
69
70
|
"github-slugger": "^2.0.0",
|
|
@@ -74,7 +75,7 @@
|
|
|
74
75
|
"prompts": "^2.4.2",
|
|
75
76
|
"strip-ansi": "^7.1.0",
|
|
76
77
|
"yargs-parser": "^21.1.1",
|
|
77
|
-
"zod": "^3.23.
|
|
78
|
+
"zod": "^3.23.5"
|
|
78
79
|
},
|
|
79
80
|
"devDependencies": {
|
|
80
81
|
"@types/chai": "^4.3.14",
|
|
@@ -88,7 +89,7 @@
|
|
|
88
89
|
"mocha": "^10.4.0",
|
|
89
90
|
"typescript": "^5.4.5",
|
|
90
91
|
"vite": "^5.2.10",
|
|
91
|
-
"astro": "4.
|
|
92
|
+
"astro": "4.7.1",
|
|
92
93
|
"astro-scripts": "0.0.14"
|
|
93
94
|
},
|
|
94
95
|
"scripts": {
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
|
|
2
|
-
import { type DBTables } from '../core/types.js';
|
|
3
|
-
export declare function seedLocal({ db, tables, userSeedGlob, integrationSeedFunctions, }: {
|
|
4
|
-
db: LibSQLDatabase;
|
|
5
|
-
tables: DBTables;
|
|
6
|
-
userSeedGlob: Record<string, {
|
|
7
|
-
default?: () => Promise<void>;
|
|
8
|
-
}>;
|
|
9
|
-
integrationSeedFunctions: Array<() => Promise<void>>;
|
|
10
|
-
}): Promise<void>;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { LibsqlError } from "@libsql/client";
|
|
2
|
-
import { sql } from "drizzle-orm";
|
|
3
|
-
import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
|
|
4
|
-
import {} from "../core/types.js";
|
|
5
|
-
import { SEED_DEFAULT_EXPORT_ERROR } from "./errors.js";
|
|
6
|
-
import { getCreateIndexQueries, getCreateTableQuery } from "./queries.js";
|
|
7
|
-
import { AstroDbError } from "./utils.js";
|
|
8
|
-
const sqlite = new SQLiteAsyncDialect();
|
|
9
|
-
async function seedLocal({
|
|
10
|
-
db,
|
|
11
|
-
tables,
|
|
12
|
-
// Glob all potential seed files to catch renames and deletions.
|
|
13
|
-
userSeedGlob,
|
|
14
|
-
integrationSeedFunctions
|
|
15
|
-
}) {
|
|
16
|
-
await recreateTables({ db, tables });
|
|
17
|
-
const seedFunctions = [];
|
|
18
|
-
const seedFilePath = Object.keys(userSeedGlob)[0];
|
|
19
|
-
if (seedFilePath) {
|
|
20
|
-
const mod = userSeedGlob[seedFilePath];
|
|
21
|
-
if (!mod.default)
|
|
22
|
-
throw new AstroDbError(SEED_DEFAULT_EXPORT_ERROR(seedFilePath));
|
|
23
|
-
seedFunctions.push(mod.default);
|
|
24
|
-
}
|
|
25
|
-
for (const seedFn of integrationSeedFunctions) {
|
|
26
|
-
seedFunctions.push(seedFn);
|
|
27
|
-
}
|
|
28
|
-
for (const seed of seedFunctions) {
|
|
29
|
-
try {
|
|
30
|
-
await seed();
|
|
31
|
-
} catch (e) {
|
|
32
|
-
if (e instanceof LibsqlError) {
|
|
33
|
-
throw new AstroDbError(`Failed to seed database:
|
|
34
|
-
${e.message}`);
|
|
35
|
-
}
|
|
36
|
-
throw e;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
async function recreateTables({ db, tables }) {
|
|
41
|
-
const setupQueries = [];
|
|
42
|
-
for (const [name, table] of Object.entries(tables)) {
|
|
43
|
-
const dropQuery = sql.raw(`DROP TABLE IF EXISTS ${sqlite.escapeName(name)}`);
|
|
44
|
-
const createQuery = sql.raw(getCreateTableQuery(name, table));
|
|
45
|
-
const indexQueries = getCreateIndexQueries(name, table);
|
|
46
|
-
setupQueries.push(dropQuery, createQuery, ...indexQueries.map((s) => sql.raw(s)));
|
|
47
|
-
}
|
|
48
|
-
await db.batch([
|
|
49
|
-
db.run(sql`pragma defer_foreign_keys=true;`),
|
|
50
|
-
...setupQueries.map((q) => db.run(q))
|
|
51
|
-
]);
|
|
52
|
-
}
|
|
53
|
-
export {
|
|
54
|
-
seedLocal
|
|
55
|
-
};
|