@astrojs/db 0.9.2 → 0.9.4

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.
@@ -34,7 +34,8 @@ async function cmd({
34
34
  const appToken = await getManagedAppTokenOrExit(flags.token);
35
35
  virtualModContents = getStudioVirtualModContents({
36
36
  tables: dbConfig.tables ?? {},
37
- appToken: appToken.token
37
+ appToken: appToken.token,
38
+ isBuild: false
38
39
  });
39
40
  } else {
40
41
  virtualModContents = getLocalVirtualModContents({
@@ -27,7 +27,7 @@ function printHelp({
27
27
  message.push(
28
28
  linebreak(),
29
29
  ` ${bgGreen(black(` ${commandName} `))} ${green(
30
- `v${"0.9.2"}`
30
+ `v${"0.9.4"}`
31
31
  )} ${headline}`
32
32
  );
33
33
  }
@@ -38,7 +38,7 @@ function astroDBIntegration() {
38
38
  return;
39
39
  let dbPlugin = void 0;
40
40
  const args = parseArgs(process.argv.slice(3));
41
- connectToStudio = args["remote"];
41
+ connectToStudio = process.env.ASTRO_INTERNAL_TEST_REMOTE || args["remote"];
42
42
  if (connectToStudio) {
43
43
  appToken = await getManagedAppTokenOrExit();
44
44
  dbPlugin = vitePluginDb({
@@ -27,8 +27,9 @@ export declare function getLocalVirtualModContents({ tables, root, seedFiles, sh
27
27
  root: URL;
28
28
  shouldSeed: boolean;
29
29
  }): string;
30
- export declare function getStudioVirtualModContents({ tables, appToken, }: {
30
+ export declare function getStudioVirtualModContents({ tables, appToken, isBuild, }: {
31
31
  tables: DBTables;
32
32
  appToken: string;
33
+ isBuild: boolean;
33
34
  }): string;
34
35
  export {};
@@ -10,9 +10,13 @@ const resolved = {
10
10
  };
11
11
  function vitePluginDb(params) {
12
12
  const srcDirPath = normalizePath(fileURLToPath(params.srcDir));
13
+ let command = "build";
13
14
  return {
14
15
  name: "astro:db",
15
16
  enforce: "pre",
17
+ configResolved(resolvedConfig) {
18
+ command = resolvedConfig.command;
19
+ },
16
20
  async resolveId(id, rawImporter) {
17
21
  if (id !== VIRTUAL_MODULE_ID)
18
22
  return;
@@ -32,7 +36,8 @@ function vitePluginDb(params) {
32
36
  if (params.connectToStudio) {
33
37
  return getStudioVirtualModContents({
34
38
  appToken: params.appToken,
35
- tables: params.tables.get()
39
+ tables: params.tables.get(),
40
+ isBuild: command === "build"
36
41
  });
37
42
  }
38
43
  return getLocalVirtualModContents({
@@ -89,12 +94,24 @@ ${getStringifiedCollectionExports(tables)}`;
89
94
  }
90
95
  function getStudioVirtualModContents({
91
96
  tables,
92
- appToken
97
+ appToken,
98
+ isBuild
93
99
  }) {
100
+ function appTokenArg() {
101
+ if (isBuild) {
102
+ return "process.env.ASTRO_STUDIO_APP_TOKEN";
103
+ } else {
104
+ return JSON.stringify(appToken);
105
+ }
106
+ }
107
+ function dbUrlArg() {
108
+ const dbStr = JSON.stringify(getRemoteDatabaseUrl());
109
+ return `import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL ?? ${dbStr}`;
110
+ }
94
111
  return `
95
112
  import {asDrizzleTable, createRemoteDatabaseClient} from ${RUNTIME_IMPORT};
96
113
 
97
- export const db = await createRemoteDatabaseClient(process.env.ASTRO_STUDIO_APP_TOKEN);
114
+ export const db = await createRemoteDatabaseClient(${appTokenArg()}, ${dbUrlArg()});
98
115
 
99
116
  export * from ${RUNTIME_CONFIG_IMPORT};
100
117
 
@@ -147,6 +147,12 @@ async function getManagedAppTokenOrExit(token) {
147
147
  if (token) {
148
148
  return new ManagedLocalAppToken(token);
149
149
  }
150
+ if (process.env.ASTRO_INTERNAL_TEST_REMOTE) {
151
+ return new ManagedLocalAppToken(
152
+ "fake"
153
+ /* token ignored in test */
154
+ );
155
+ }
150
156
  const { ASTRO_STUDIO_APP_TOKEN } = getAstroStudioEnv();
151
157
  if (ASTRO_STUDIO_APP_TOKEN) {
152
158
  return new ManagedLocalAppToken(ASTRO_STUDIO_APP_TOKEN);
@@ -28,6 +28,9 @@ const remoteResultSchema = z.object({
28
28
  lastInsertRowid: z.unknown().optional()
29
29
  });
30
30
  function createRemoteDatabaseClient(appToken, remoteDbURL) {
31
+ if (appToken == null) {
32
+ throw new Error(`Cannot create a remote client: missing app token.`);
33
+ }
31
34
  const url = new URL("/db/query", remoteDbURL);
32
35
  const db = drizzleProxy(
33
36
  async (sql, parameters, method) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.9.2",
3
+ "version": "0.9.4",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -81,8 +81,8 @@
81
81
  "mocha": "^10.2.0",
82
82
  "typescript": "^5.2.2",
83
83
  "vite": "^5.1.4",
84
- "astro-scripts": "0.0.14",
85
- "astro": "4.5.8"
84
+ "astro": "4.5.9",
85
+ "astro-scripts": "0.0.14"
86
86
  },
87
87
  "scripts": {
88
88
  "types:config": "tsc -p ./tsconfig.config-types.json",