@astrojs/db 0.4.0 → 0.5.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/core/cli/commands/execute/index.d.ts +8 -0
- package/dist/core/cli/commands/execute/index.js +32 -0
- package/dist/core/cli/commands/gen/index.d.ts +4 -2
- package/dist/core/cli/commands/gen/index.js +17 -9
- package/dist/core/cli/commands/link/index.d.ts +20 -8
- package/dist/core/cli/commands/link/index.js +191 -31
- package/dist/core/cli/commands/login/index.d.ts +4 -2
- package/dist/core/cli/commands/login/index.js +31 -22
- package/dist/core/cli/commands/logout/index.d.ts +1 -6
- package/dist/core/cli/commands/logout/index.js +1 -1
- package/dist/core/cli/commands/push/index.d.ts +4 -2
- package/dist/core/cli/commands/push/index.js +25 -77
- package/dist/core/cli/commands/shell/index.d.ts +4 -2
- package/dist/core/cli/commands/shell/index.js +3 -1
- package/dist/core/cli/commands/verify/index.d.ts +4 -2
- package/dist/core/cli/commands/verify/index.js +10 -6
- package/dist/core/cli/index.d.ts +1 -1
- package/dist/core/cli/index.js +19 -13
- package/dist/core/cli/migration-queries.d.ts +1 -1
- package/dist/core/cli/migration-queries.js +6 -6
- package/dist/core/cli/migrations.d.ts +12 -9
- package/dist/core/cli/migrations.js +27 -21
- package/dist/core/consts.d.ts +2 -0
- package/dist/core/consts.js +4 -0
- package/dist/core/errors.d.ts +7 -4
- package/dist/core/errors.js +38 -31
- package/dist/core/integration/file-url.js +1 -1
- package/dist/core/integration/index.js +56 -117
- package/dist/core/integration/typegen.js +1 -13
- package/dist/core/integration/vite-plugin-db.d.ts +10 -6
- package/dist/core/integration/vite-plugin-db.js +68 -23
- package/dist/core/integration/vite-plugin-inject-env-ts.d.ts +1 -1
- package/dist/core/load-file.d.ts +31 -0
- package/dist/core/load-file.js +98 -0
- package/dist/core/tokens.js +1 -1
- package/dist/core/types.d.ts +832 -5306
- package/dist/core/types.js +16 -84
- package/dist/core/utils.d.ts +2 -0
- package/dist/core/utils.js +8 -0
- package/dist/index.d.ts +2 -3
- package/dist/index.js +3 -5
- package/dist/runtime/config.d.ts +138 -0
- package/dist/runtime/config.js +42 -0
- package/dist/runtime/db-client.d.ts +2 -9
- package/dist/runtime/db-client.js +8 -39
- package/dist/runtime/index.d.ts +5 -4
- package/dist/runtime/index.js +12 -10
- package/dist/{core → runtime}/queries.d.ts +15 -17
- package/dist/{core → runtime}/queries.js +64 -80
- package/dist/runtime/types.d.ts +3 -3
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +4 -0
- package/index.d.ts +5 -3
- package/package.json +18 -3
- package/config-augment.d.ts +0 -4
- package/dist/core/integration/load-astro-config.d.ts +0 -6
- package/dist/core/integration/load-astro-config.js +0 -79
|
@@ -20,19 +20,7 @@ ${Object.entries(tables).map(([name, collection]) => generateTableType(name, col
|
|
|
20
20
|
function generateTableType(name, collection) {
|
|
21
21
|
let tableType = ` export const ${name}: import(${RUNTIME_IMPORT}).Table<
|
|
22
22
|
${JSON.stringify(name)},
|
|
23
|
-
${JSON.stringify(
|
|
24
|
-
Object.fromEntries(
|
|
25
|
-
Object.entries(collection.columns).map(([columnName, column]) => [
|
|
26
|
-
columnName,
|
|
27
|
-
{
|
|
28
|
-
// Only select columns Drizzle needs for inference
|
|
29
|
-
type: column.type,
|
|
30
|
-
optional: column.schema.optional,
|
|
31
|
-
default: column.schema.default
|
|
32
|
-
}
|
|
33
|
-
])
|
|
34
|
-
)
|
|
35
|
-
)}
|
|
23
|
+
${JSON.stringify(collection.columns)}
|
|
36
24
|
>;`;
|
|
37
25
|
return tableType;
|
|
38
26
|
}
|
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
import type { DBTables } from '../types.js';
|
|
2
|
-
import type
|
|
3
|
-
type
|
|
4
|
-
|
|
2
|
+
import { type VitePlugin } from '../utils.js';
|
|
3
|
+
export type LateTables = {
|
|
4
|
+
get: () => DBTables;
|
|
5
5
|
};
|
|
6
6
|
type VitePluginDBParams = {
|
|
7
7
|
connectToStudio: false;
|
|
8
|
-
|
|
8
|
+
tables: LateTables;
|
|
9
|
+
srcDir: URL;
|
|
9
10
|
root: URL;
|
|
10
11
|
} | {
|
|
11
12
|
connectToStudio: true;
|
|
12
|
-
|
|
13
|
+
tables: LateTables;
|
|
13
14
|
appToken: string;
|
|
15
|
+
srcDir: URL;
|
|
14
16
|
root: URL;
|
|
15
17
|
};
|
|
16
18
|
export declare function vitePluginDb(params: VitePluginDBParams): VitePlugin;
|
|
17
|
-
export declare function
|
|
19
|
+
export declare function getConfigVirtualModContents(): string;
|
|
20
|
+
export declare function getLocalVirtualModContents({ tables, shouldSeed, }: {
|
|
18
21
|
tables: DBTables;
|
|
19
22
|
root: URL;
|
|
23
|
+
shouldSeed: boolean;
|
|
20
24
|
}): string;
|
|
21
25
|
export declare function getStudioVirtualModContents({ tables, appToken, }: {
|
|
22
26
|
tables: DBTables;
|
|
@@ -1,73 +1,118 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { normalizePath } from "vite";
|
|
3
|
+
import { SEED_DEV_FILE_NAME } from "../../runtime/queries.js";
|
|
4
|
+
import {
|
|
5
|
+
DB_PATH,
|
|
6
|
+
RUNTIME_CONFIG_IMPORT,
|
|
7
|
+
RUNTIME_DRIZZLE_IMPORT,
|
|
8
|
+
RUNTIME_IMPORT,
|
|
9
|
+
VIRTUAL_MODULE_ID
|
|
10
|
+
} from "../consts.js";
|
|
11
|
+
import { getDbDirectoryUrl, getRemoteDatabaseUrl } from "../utils.js";
|
|
12
|
+
const LOCAL_DB_VIRTUAL_MODULE_ID = "astro:local";
|
|
2
13
|
const resolvedVirtualModuleId = "\0" + VIRTUAL_MODULE_ID;
|
|
14
|
+
const resolvedLocalDbVirtualModuleId = LOCAL_DB_VIRTUAL_MODULE_ID + "/local-db";
|
|
15
|
+
const resolvedSeedVirtualModuleId = "\0" + VIRTUAL_MODULE_ID + "?shouldSeed";
|
|
3
16
|
function vitePluginDb(params) {
|
|
17
|
+
const srcDirPath = normalizePath(fileURLToPath(params.srcDir));
|
|
4
18
|
return {
|
|
5
19
|
name: "astro:db",
|
|
6
20
|
enforce: "pre",
|
|
7
|
-
resolveId(id) {
|
|
8
|
-
if (id ===
|
|
21
|
+
async resolveId(id, rawImporter) {
|
|
22
|
+
if (id === LOCAL_DB_VIRTUAL_MODULE_ID)
|
|
23
|
+
return resolvedLocalDbVirtualModuleId;
|
|
24
|
+
if (id !== VIRTUAL_MODULE_ID)
|
|
25
|
+
return;
|
|
26
|
+
if (params.connectToStudio)
|
|
27
|
+
return resolvedVirtualModuleId;
|
|
28
|
+
const importer = rawImporter ? await this.resolve(rawImporter) : null;
|
|
29
|
+
if (!importer)
|
|
9
30
|
return resolvedVirtualModuleId;
|
|
31
|
+
if (importer.id.startsWith(srcDirPath)) {
|
|
32
|
+
return resolvedSeedVirtualModuleId;
|
|
10
33
|
}
|
|
34
|
+
return resolvedVirtualModuleId;
|
|
11
35
|
},
|
|
12
36
|
load(id) {
|
|
13
|
-
if (id
|
|
37
|
+
if (id === resolvedLocalDbVirtualModuleId) {
|
|
38
|
+
const dbUrl = new URL(DB_PATH, params.root);
|
|
39
|
+
return `import { createLocalDatabaseClient } from ${RUNTIME_IMPORT};
|
|
40
|
+
const dbUrl = ${JSON.stringify(dbUrl)};
|
|
41
|
+
|
|
42
|
+
export const db = createLocalDatabaseClient({ dbUrl });`;
|
|
43
|
+
}
|
|
44
|
+
if (id !== resolvedVirtualModuleId && id !== resolvedSeedVirtualModuleId)
|
|
14
45
|
return;
|
|
15
46
|
if (params.connectToStudio) {
|
|
16
47
|
return getStudioVirtualModContents({
|
|
17
48
|
appToken: params.appToken,
|
|
18
|
-
tables: params.
|
|
49
|
+
tables: params.tables.get()
|
|
19
50
|
});
|
|
20
51
|
}
|
|
21
|
-
return
|
|
52
|
+
return getLocalVirtualModContents({
|
|
22
53
|
root: params.root,
|
|
23
|
-
tables: params.
|
|
54
|
+
tables: params.tables.get(),
|
|
55
|
+
shouldSeed: id === resolvedSeedVirtualModuleId
|
|
24
56
|
});
|
|
25
57
|
}
|
|
26
58
|
};
|
|
27
59
|
}
|
|
28
|
-
function
|
|
29
|
-
|
|
60
|
+
function getConfigVirtualModContents() {
|
|
61
|
+
return `export * from ${RUNTIME_CONFIG_IMPORT}`;
|
|
62
|
+
}
|
|
63
|
+
function getLocalVirtualModContents({
|
|
64
|
+
tables,
|
|
65
|
+
shouldSeed
|
|
66
|
+
}) {
|
|
67
|
+
const seedFilePaths = SEED_DEV_FILE_NAME.map(
|
|
68
|
+
// Format as /db/[name].ts
|
|
69
|
+
// for Vite import.meta.glob
|
|
70
|
+
(name) => new URL(name, getDbDirectoryUrl("file:///")).pathname
|
|
71
|
+
);
|
|
30
72
|
return `
|
|
31
|
-
import {
|
|
32
|
-
import
|
|
73
|
+
import { asDrizzleTable, seedLocal } from ${RUNTIME_IMPORT};
|
|
74
|
+
import { db as _db } from ${JSON.stringify(LOCAL_DB_VIRTUAL_MODULE_ID)};
|
|
33
75
|
|
|
34
|
-
const
|
|
35
|
-
tables,
|
|
36
|
-
seeding: false
|
|
37
|
-
})};
|
|
38
|
-
params.dbUrl = dbUrl;
|
|
76
|
+
export const db = _db;
|
|
39
77
|
|
|
40
|
-
|
|
78
|
+
${shouldSeed ? `await seedLocal({
|
|
79
|
+
db: _db,
|
|
80
|
+
tables: ${JSON.stringify(tables)},
|
|
81
|
+
fileGlob: import.meta.glob(${JSON.stringify(seedFilePaths)}),
|
|
82
|
+
})` : ""}
|
|
41
83
|
|
|
42
84
|
export * from ${RUNTIME_DRIZZLE_IMPORT};
|
|
85
|
+
export * from ${RUNTIME_CONFIG_IMPORT};
|
|
43
86
|
|
|
44
|
-
${getStringifiedCollectionExports(tables)}
|
|
45
|
-
`;
|
|
87
|
+
${getStringifiedCollectionExports(tables)}`;
|
|
46
88
|
}
|
|
47
89
|
function getStudioVirtualModContents({
|
|
48
90
|
tables,
|
|
49
91
|
appToken
|
|
50
92
|
}) {
|
|
51
93
|
return `
|
|
52
|
-
import {
|
|
94
|
+
import {asDrizzleTable, createRemoteDatabaseClient} from ${RUNTIME_IMPORT};
|
|
53
95
|
|
|
54
96
|
export const db = await createRemoteDatabaseClient(${JSON.stringify(
|
|
55
97
|
appToken
|
|
56
|
-
|
|
98
|
+
// Respect runtime env for user overrides in SSR
|
|
99
|
+
)}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL ?? ${JSON.stringify(getRemoteDatabaseUrl())});
|
|
57
100
|
export * from ${RUNTIME_DRIZZLE_IMPORT};
|
|
101
|
+
export * from ${RUNTIME_CONFIG_IMPORT};
|
|
58
102
|
|
|
59
103
|
${getStringifiedCollectionExports(tables)}
|
|
60
104
|
`;
|
|
61
105
|
}
|
|
62
106
|
function getStringifiedCollectionExports(tables) {
|
|
63
107
|
return Object.entries(tables).map(
|
|
64
|
-
([name, collection]) => `export const ${name} =
|
|
108
|
+
([name, collection]) => `export const ${name} = asDrizzleTable(${JSON.stringify(name)}, ${JSON.stringify(
|
|
65
109
|
collection
|
|
66
110
|
)}, false)`
|
|
67
111
|
).join("\n");
|
|
68
112
|
}
|
|
69
113
|
export {
|
|
114
|
+
getConfigVirtualModContents,
|
|
115
|
+
getLocalVirtualModContents,
|
|
70
116
|
getStudioVirtualModContents,
|
|
71
|
-
getVirtualModContents,
|
|
72
117
|
vitePluginDb
|
|
73
118
|
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare function loadDbConfigFile(root: URL): Promise<{
|
|
2
|
+
mod: {
|
|
3
|
+
default?: unknown;
|
|
4
|
+
} | undefined;
|
|
5
|
+
dependencies: string[];
|
|
6
|
+
}>;
|
|
7
|
+
/**
|
|
8
|
+
* Bundle arbitrary `mjs` or `ts` file.
|
|
9
|
+
* Simplified fork from Vite's `bundleConfigFile` function.
|
|
10
|
+
*
|
|
11
|
+
* @see https://github.com/vitejs/vite/blob/main/packages/vite/src/node/config.ts#L961
|
|
12
|
+
*/
|
|
13
|
+
export declare function bundleFile({ fileUrl, root, virtualModContents, }: {
|
|
14
|
+
fileUrl: URL;
|
|
15
|
+
root: URL;
|
|
16
|
+
virtualModContents: string;
|
|
17
|
+
}): Promise<{
|
|
18
|
+
code: string;
|
|
19
|
+
dependencies: string[];
|
|
20
|
+
}>;
|
|
21
|
+
/**
|
|
22
|
+
* Forked from Vite config loader, replacing CJS-based path concat with ESM only
|
|
23
|
+
*
|
|
24
|
+
* @see https://github.com/vitejs/vite/blob/main/packages/vite/src/node/config.ts#L1074
|
|
25
|
+
*/
|
|
26
|
+
export declare function importBundledFile({ code, root, }: {
|
|
27
|
+
code: string;
|
|
28
|
+
root: URL;
|
|
29
|
+
}): Promise<{
|
|
30
|
+
default?: unknown;
|
|
31
|
+
}>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { unlink, writeFile } from "node:fs/promises";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { build as esbuild } from "esbuild";
|
|
5
|
+
import { CONFIG_FILE_NAMES, VIRTUAL_MODULE_ID } from "./consts.js";
|
|
6
|
+
import { getConfigVirtualModContents } from "./integration/vite-plugin-db.js";
|
|
7
|
+
import { getDbDirectoryUrl } from "./utils.js";
|
|
8
|
+
async function loadDbConfigFile(root) {
|
|
9
|
+
let configFileUrl;
|
|
10
|
+
for (const fileName of CONFIG_FILE_NAMES) {
|
|
11
|
+
const fileUrl = new URL(fileName, getDbDirectoryUrl(root));
|
|
12
|
+
if (existsSync(fileUrl)) {
|
|
13
|
+
configFileUrl = fileUrl;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (!configFileUrl) {
|
|
17
|
+
return { mod: void 0, dependencies: [] };
|
|
18
|
+
}
|
|
19
|
+
const { code, dependencies } = await bundleFile({
|
|
20
|
+
virtualModContents: getConfigVirtualModContents(),
|
|
21
|
+
root,
|
|
22
|
+
fileUrl: configFileUrl
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
mod: await importBundledFile({ code, root }),
|
|
26
|
+
dependencies
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
async function bundleFile({
|
|
30
|
+
fileUrl,
|
|
31
|
+
root,
|
|
32
|
+
virtualModContents
|
|
33
|
+
}) {
|
|
34
|
+
const result = await esbuild({
|
|
35
|
+
absWorkingDir: process.cwd(),
|
|
36
|
+
entryPoints: [fileURLToPath(fileUrl)],
|
|
37
|
+
outfile: "out.js",
|
|
38
|
+
packages: "external",
|
|
39
|
+
write: false,
|
|
40
|
+
target: ["node16"],
|
|
41
|
+
platform: "node",
|
|
42
|
+
bundle: true,
|
|
43
|
+
format: "esm",
|
|
44
|
+
sourcemap: "inline",
|
|
45
|
+
metafile: true,
|
|
46
|
+
define: {
|
|
47
|
+
"import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL": "undefined"
|
|
48
|
+
},
|
|
49
|
+
plugins: [
|
|
50
|
+
{
|
|
51
|
+
name: "resolve-astro-db",
|
|
52
|
+
setup(build) {
|
|
53
|
+
build.onResolve({ filter: /^astro:db$/ }, ({ path }) => {
|
|
54
|
+
return { path, namespace: VIRTUAL_MODULE_ID };
|
|
55
|
+
});
|
|
56
|
+
build.onLoad({ namespace: VIRTUAL_MODULE_ID, filter: /.*/ }, () => {
|
|
57
|
+
return {
|
|
58
|
+
contents: virtualModContents,
|
|
59
|
+
// Needed to resolve runtime dependencies
|
|
60
|
+
resolveDir: fileURLToPath(root)
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
});
|
|
67
|
+
const file = result.outputFiles[0];
|
|
68
|
+
if (!file) {
|
|
69
|
+
throw new Error(`Unexpected: no output file`);
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
code: file.text,
|
|
73
|
+
dependencies: Object.keys(result.metafile.inputs)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
async function importBundledFile({
|
|
77
|
+
code,
|
|
78
|
+
root
|
|
79
|
+
}) {
|
|
80
|
+
const tmpFileUrl = new URL(`./db.timestamp-${Date.now()}.mjs`, root);
|
|
81
|
+
await writeFile(tmpFileUrl, code, { encoding: "utf8" });
|
|
82
|
+
try {
|
|
83
|
+
return await import(
|
|
84
|
+
/* @vite-ignore */
|
|
85
|
+
tmpFileUrl.pathname
|
|
86
|
+
);
|
|
87
|
+
} finally {
|
|
88
|
+
try {
|
|
89
|
+
await unlink(tmpFileUrl);
|
|
90
|
+
} catch {
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
export {
|
|
95
|
+
bundleFile,
|
|
96
|
+
importBundledFile,
|
|
97
|
+
loadDbConfigFile
|
|
98
|
+
};
|
package/dist/core/tokens.js
CHANGED
|
@@ -2,8 +2,8 @@ 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 { getAstroStudioEnv, getAstroStudioUrl } from "./utils.js";
|
|
6
5
|
import { MISSING_PROJECT_ID_ERROR, MISSING_SESSION_ID_ERROR } from "./errors.js";
|
|
6
|
+
import { getAstroStudioEnv, getAstroStudioUrl } from "./utils.js";
|
|
7
7
|
const SESSION_LOGIN_FILE = pathToFileURL(join(homedir(), ".astro", "session-token"));
|
|
8
8
|
const PROJECT_ID_FILE = pathToFileURL(join(process.cwd(), ".astro", "link"));
|
|
9
9
|
class ManagedLocalAppToken {
|