@astrojs/db 0.4.1 → 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 -6
- package/dist/core/errors.js +35 -38
- package/dist/core/integration/file-url.js +1 -1
- package/dist/core/integration/index.js +56 -112
- package/dist/core/integration/typegen.js +1 -13
- package/dist/core/integration/vite-plugin-db.d.ts +9 -5
- package/dist/core/integration/vite-plugin-db.js +66 -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 +63 -91
- 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
|
@@ -1,66 +1,27 @@
|
|
|
1
|
-
var __knownSymbol = (name, symbol) => {
|
|
2
|
-
return (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
3
|
-
};
|
|
4
|
-
var __using = (stack, value, async) => {
|
|
5
|
-
if (value != null) {
|
|
6
|
-
if (typeof value !== "object" && typeof value !== "function")
|
|
7
|
-
throw TypeError("Object expected");
|
|
8
|
-
var dispose;
|
|
9
|
-
if (async)
|
|
10
|
-
dispose = value[__knownSymbol("asyncDispose")];
|
|
11
|
-
if (dispose === void 0)
|
|
12
|
-
dispose = value[__knownSymbol("dispose")];
|
|
13
|
-
if (typeof dispose !== "function")
|
|
14
|
-
throw TypeError("Object not disposable");
|
|
15
|
-
stack.push([async, dispose, value]);
|
|
16
|
-
} else if (async) {
|
|
17
|
-
stack.push([async]);
|
|
18
|
-
}
|
|
19
|
-
return value;
|
|
20
|
-
};
|
|
21
|
-
var __callDispose = (stack, error, hasError) => {
|
|
22
|
-
var E = typeof SuppressedError === "function" ? SuppressedError : function(e, s, m, _) {
|
|
23
|
-
return _ = Error(m), _.name = "SuppressedError", _.error = e, _.suppressed = s, _;
|
|
24
|
-
};
|
|
25
|
-
var fail = (e) => error = hasError ? new E(e, error, "An error was suppressed during disposal") : (hasError = true, e);
|
|
26
|
-
var next = (it) => {
|
|
27
|
-
while (it = stack.pop()) {
|
|
28
|
-
try {
|
|
29
|
-
var result = it[1] && it[1].call(it[2]);
|
|
30
|
-
if (it[0])
|
|
31
|
-
return Promise.resolve(result).then(next, (e) => (fail(e), next()));
|
|
32
|
-
} catch (e) {
|
|
33
|
-
fail(e);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (hasError)
|
|
37
|
-
throw error;
|
|
38
|
-
};
|
|
39
|
-
return next();
|
|
40
|
-
};
|
|
41
|
-
import { vitePluginDb } from "./vite-plugin-db.js";
|
|
42
|
-
import { vitePluginInjectEnvTs } from "./vite-plugin-inject-env-ts.js";
|
|
43
|
-
import { typegen } from "./typegen.js";
|
|
44
1
|
import { existsSync } from "fs";
|
|
45
|
-
import { mkdir, rm, writeFile } from "fs/promises";
|
|
46
|
-
import { DB_PATH } from "../consts.js";
|
|
47
|
-
import { createLocalDatabaseClient } from "../../runtime/db-client.js";
|
|
48
|
-
import { astroConfigWithDbSchema } from "../types.js";
|
|
49
|
-
import {} from "../utils.js";
|
|
50
|
-
import { STUDIO_CONFIG_MISSING_WRITABLE_TABLE_ERROR, UNSAFE_WRITABLE_WARNING } from "../errors.js";
|
|
51
|
-
import { errorMap } from "./error-map.js";
|
|
52
2
|
import { dirname } from "path";
|
|
53
3
|
import { fileURLToPath } from "url";
|
|
4
|
+
import { mkdir, rm, writeFile } from "fs/promises";
|
|
54
5
|
import { blue, yellow } from "kleur/colors";
|
|
55
|
-
import {
|
|
56
|
-
import {
|
|
6
|
+
import { CONFIG_FILE_NAMES, DB_PATH } from "../consts.js";
|
|
7
|
+
import { loadDbConfigFile } from "../load-file.js";
|
|
57
8
|
import { getManagedAppTokenOrExit } from "../tokens.js";
|
|
9
|
+
import { dbConfigSchema } from "../types.js";
|
|
10
|
+
import { getDbDirectoryUrl } from "../utils.js";
|
|
11
|
+
import { errorMap } from "./error-map.js";
|
|
12
|
+
import { fileURLIntegration } from "./file-url.js";
|
|
13
|
+
import { typegen } from "./typegen.js";
|
|
14
|
+
import { vitePluginDb } from "./vite-plugin-db.js";
|
|
15
|
+
import { vitePluginInjectEnvTs } from "./vite-plugin-inject-env-ts.js";
|
|
58
16
|
function astroDBIntegration() {
|
|
59
|
-
let
|
|
17
|
+
let connectToStudio = false;
|
|
18
|
+
let configFileDependencies = [];
|
|
19
|
+
let root;
|
|
60
20
|
let appToken;
|
|
61
|
-
let
|
|
62
|
-
|
|
63
|
-
|
|
21
|
+
let dbConfig;
|
|
22
|
+
let tables = {
|
|
23
|
+
get() {
|
|
24
|
+
throw new Error("[astro:db] INTERNAL Tables not loaded yet");
|
|
64
25
|
}
|
|
65
26
|
};
|
|
66
27
|
let command;
|
|
@@ -69,24 +30,26 @@ function astroDBIntegration() {
|
|
|
69
30
|
hooks: {
|
|
70
31
|
"astro:config:setup": async ({ updateConfig, config, command: _command, logger }) => {
|
|
71
32
|
command = _command;
|
|
72
|
-
|
|
33
|
+
root = config.root;
|
|
34
|
+
if (command === "preview")
|
|
73
35
|
return;
|
|
74
36
|
let dbPlugin = void 0;
|
|
75
|
-
|
|
76
|
-
if (
|
|
37
|
+
connectToStudio = command === "build";
|
|
38
|
+
if (connectToStudio) {
|
|
77
39
|
appToken = await getManagedAppTokenOrExit();
|
|
78
|
-
connectedToRemote = true;
|
|
79
40
|
dbPlugin = vitePluginDb({
|
|
80
|
-
connectToStudio
|
|
41
|
+
connectToStudio,
|
|
81
42
|
appToken: appToken.token,
|
|
82
|
-
|
|
83
|
-
root: config.root
|
|
43
|
+
tables,
|
|
44
|
+
root: config.root,
|
|
45
|
+
srcDir: config.srcDir
|
|
84
46
|
});
|
|
85
47
|
} else {
|
|
86
48
|
dbPlugin = vitePluginDb({
|
|
87
49
|
connectToStudio: false,
|
|
88
|
-
|
|
89
|
-
root: config.root
|
|
50
|
+
tables,
|
|
51
|
+
root: config.root,
|
|
52
|
+
srcDir: config.srcDir
|
|
90
53
|
});
|
|
91
54
|
}
|
|
92
55
|
updateConfig({
|
|
@@ -96,63 +59,44 @@ function astroDBIntegration() {
|
|
|
96
59
|
}
|
|
97
60
|
});
|
|
98
61
|
},
|
|
99
|
-
"astro:config:done": async ({ config
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
} else if (unsafeWritable && foundWritableCollection) {
|
|
111
|
-
logger.warn(UNSAFE_WRITABLE_WARNING);
|
|
112
|
-
}
|
|
113
|
-
if (!connectedToRemote) {
|
|
114
|
-
var _stack = [];
|
|
115
|
-
try {
|
|
116
|
-
const dbUrl = new URL(DB_PATH, config.root);
|
|
117
|
-
if (existsSync(dbUrl)) {
|
|
118
|
-
await rm(dbUrl);
|
|
119
|
-
}
|
|
120
|
-
await mkdir(dirname(fileURLToPath(dbUrl)), { recursive: true });
|
|
121
|
-
await writeFile(dbUrl, "");
|
|
122
|
-
const db = __using(_stack, await createLocalDatabaseClient({
|
|
123
|
-
tables,
|
|
124
|
-
dbUrl: dbUrl.toString(),
|
|
125
|
-
seeding: true
|
|
126
|
-
}));
|
|
127
|
-
await recreateTables({ db, tables });
|
|
128
|
-
if (configWithDb.db?.data) {
|
|
129
|
-
await seedData({
|
|
130
|
-
db,
|
|
131
|
-
data: configWithDb.db.data,
|
|
132
|
-
logger,
|
|
133
|
-
mode: command === "dev" ? "dev" : "build"
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
logger.debug("Database setup complete.");
|
|
137
|
-
} catch (_) {
|
|
138
|
-
var _error = _, _hasError = true;
|
|
139
|
-
} finally {
|
|
140
|
-
__callDispose(_stack, _error, _hasError);
|
|
62
|
+
"astro:config:done": async ({ config }) => {
|
|
63
|
+
const { mod, dependencies } = await loadDbConfigFile(config.root);
|
|
64
|
+
configFileDependencies = dependencies;
|
|
65
|
+
dbConfig = dbConfigSchema.parse(mod?.default ?? {}, {
|
|
66
|
+
errorMap
|
|
67
|
+
});
|
|
68
|
+
tables.get = () => dbConfig.tables ?? {};
|
|
69
|
+
if (!connectToStudio && !process.env.TEST_IN_MEMORY_DB) {
|
|
70
|
+
const dbUrl = new URL(DB_PATH, config.root);
|
|
71
|
+
if (existsSync(dbUrl)) {
|
|
72
|
+
await rm(dbUrl);
|
|
141
73
|
}
|
|
74
|
+
await mkdir(dirname(fileURLToPath(dbUrl)), { recursive: true });
|
|
75
|
+
await writeFile(dbUrl, "");
|
|
142
76
|
}
|
|
143
|
-
await typegen({ tables, root: config.root });
|
|
77
|
+
await typegen({ tables: tables.get() ?? {}, root: config.root });
|
|
144
78
|
},
|
|
145
79
|
"astro:server:start": async ({ logger }) => {
|
|
146
80
|
setTimeout(() => {
|
|
147
81
|
logger.info(
|
|
148
|
-
|
|
82
|
+
connectToStudio ? "Connected to remote database." : "New local database created."
|
|
149
83
|
);
|
|
150
84
|
}, 100);
|
|
151
85
|
},
|
|
86
|
+
"astro:server:setup": async ({ server }) => {
|
|
87
|
+
const filesToWatch = [
|
|
88
|
+
...CONFIG_FILE_NAMES.map((c) => new URL(c, getDbDirectoryUrl(root))),
|
|
89
|
+
...configFileDependencies.map((c) => new URL(c, root))
|
|
90
|
+
];
|
|
91
|
+
server.watcher.on("all", (event, relativeEntry) => {
|
|
92
|
+
const entry = new URL(relativeEntry, root);
|
|
93
|
+
if (filesToWatch.some((f) => entry.href === f.href)) {
|
|
94
|
+
server.restart();
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
},
|
|
152
98
|
"astro:build:start": async ({ logger }) => {
|
|
153
|
-
logger.info(
|
|
154
|
-
"database: " + (connectedToRemote ? yellow("remote") : blue("local database."))
|
|
155
|
-
);
|
|
99
|
+
logger.info("database: " + (connectToStudio ? yellow("remote") : blue("local database.")));
|
|
156
100
|
},
|
|
157
101
|
"astro:build:done": async ({}) => {
|
|
158
102
|
await appToken?.destroy();
|
|
@@ -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
2
|
import { type VitePlugin } from '../utils.js';
|
|
3
|
-
type
|
|
4
|
-
|
|
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,75 +1,118 @@
|
|
|
1
|
-
import {
|
|
2
|
-
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";
|
|
3
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";
|
|
4
16
|
function vitePluginDb(params) {
|
|
17
|
+
const srcDirPath = normalizePath(fileURLToPath(params.srcDir));
|
|
5
18
|
return {
|
|
6
19
|
name: "astro:db",
|
|
7
20
|
enforce: "pre",
|
|
8
|
-
resolveId(id) {
|
|
9
|
-
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)
|
|
10
30
|
return resolvedVirtualModuleId;
|
|
31
|
+
if (importer.id.startsWith(srcDirPath)) {
|
|
32
|
+
return resolvedSeedVirtualModuleId;
|
|
11
33
|
}
|
|
34
|
+
return resolvedVirtualModuleId;
|
|
12
35
|
},
|
|
13
36
|
load(id) {
|
|
14
|
-
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)
|
|
15
45
|
return;
|
|
16
46
|
if (params.connectToStudio) {
|
|
17
47
|
return getStudioVirtualModContents({
|
|
18
48
|
appToken: params.appToken,
|
|
19
|
-
tables: params.
|
|
49
|
+
tables: params.tables.get()
|
|
20
50
|
});
|
|
21
51
|
}
|
|
22
|
-
return
|
|
52
|
+
return getLocalVirtualModContents({
|
|
23
53
|
root: params.root,
|
|
24
|
-
tables: params.
|
|
54
|
+
tables: params.tables.get(),
|
|
55
|
+
shouldSeed: id === resolvedSeedVirtualModuleId
|
|
25
56
|
});
|
|
26
57
|
}
|
|
27
58
|
};
|
|
28
59
|
}
|
|
29
|
-
function
|
|
30
|
-
|
|
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
|
+
);
|
|
31
72
|
return `
|
|
32
|
-
import {
|
|
33
|
-
import
|
|
73
|
+
import { asDrizzleTable, seedLocal } from ${RUNTIME_IMPORT};
|
|
74
|
+
import { db as _db } from ${JSON.stringify(LOCAL_DB_VIRTUAL_MODULE_ID)};
|
|
34
75
|
|
|
35
|
-
const
|
|
36
|
-
tables,
|
|
37
|
-
seeding: false
|
|
38
|
-
})};
|
|
39
|
-
params.dbUrl = dbUrl;
|
|
76
|
+
export const db = _db;
|
|
40
77
|
|
|
41
|
-
|
|
78
|
+
${shouldSeed ? `await seedLocal({
|
|
79
|
+
db: _db,
|
|
80
|
+
tables: ${JSON.stringify(tables)},
|
|
81
|
+
fileGlob: import.meta.glob(${JSON.stringify(seedFilePaths)}),
|
|
82
|
+
})` : ""}
|
|
42
83
|
|
|
43
84
|
export * from ${RUNTIME_DRIZZLE_IMPORT};
|
|
85
|
+
export * from ${RUNTIME_CONFIG_IMPORT};
|
|
44
86
|
|
|
45
|
-
${getStringifiedCollectionExports(tables)}
|
|
46
|
-
`;
|
|
87
|
+
${getStringifiedCollectionExports(tables)}`;
|
|
47
88
|
}
|
|
48
89
|
function getStudioVirtualModContents({
|
|
49
90
|
tables,
|
|
50
91
|
appToken
|
|
51
92
|
}) {
|
|
52
93
|
return `
|
|
53
|
-
import {
|
|
94
|
+
import {asDrizzleTable, createRemoteDatabaseClient} from ${RUNTIME_IMPORT};
|
|
54
95
|
|
|
55
96
|
export const db = await createRemoteDatabaseClient(${JSON.stringify(
|
|
56
97
|
appToken
|
|
57
98
|
// Respect runtime env for user overrides in SSR
|
|
58
99
|
)}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL ?? ${JSON.stringify(getRemoteDatabaseUrl())});
|
|
59
100
|
export * from ${RUNTIME_DRIZZLE_IMPORT};
|
|
101
|
+
export * from ${RUNTIME_CONFIG_IMPORT};
|
|
60
102
|
|
|
61
103
|
${getStringifiedCollectionExports(tables)}
|
|
62
104
|
`;
|
|
63
105
|
}
|
|
64
106
|
function getStringifiedCollectionExports(tables) {
|
|
65
107
|
return Object.entries(tables).map(
|
|
66
|
-
([name, collection]) => `export const ${name} =
|
|
108
|
+
([name, collection]) => `export const ${name} = asDrizzleTable(${JSON.stringify(name)}, ${JSON.stringify(
|
|
67
109
|
collection
|
|
68
110
|
)}, false)`
|
|
69
111
|
).join("\n");
|
|
70
112
|
}
|
|
71
113
|
export {
|
|
114
|
+
getConfigVirtualModContents,
|
|
115
|
+
getLocalVirtualModContents,
|
|
72
116
|
getStudioVirtualModContents,
|
|
73
|
-
getVirtualModContents,
|
|
74
117
|
vitePluginDb
|
|
75
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 {
|