@astrojs/db 0.9.4 → 0.9.6

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.
@@ -27,7 +27,7 @@ function printHelp({
27
27
  message.push(
28
28
  linebreak(),
29
29
  ` ${bgGreen(black(` ${commandName} `))} ${green(
30
- `v${"0.9.4"}`
30
+ `v${"0.9.6"}`
31
31
  )} ${headline}`
32
32
  );
33
33
  }
@@ -5,12 +5,6 @@ export declare const RENAME_TABLE_ERROR: (oldTable: string, newTable: string) =>
5
5
  export declare const RENAME_COLUMN_ERROR: (oldSelector: string, newSelector: string) => string;
6
6
  export declare const FILE_NOT_FOUND_ERROR: (path: string) => string;
7
7
  export declare const SHELL_QUERY_MISSING_ERROR: string;
8
- export declare const SEED_ERROR: (error: string) => string;
9
8
  export declare const EXEC_ERROR: (error: string) => string;
10
- export declare const SEED_DEFAULT_EXPORT_ERROR: (fileName: string) => string;
11
9
  export declare const EXEC_DEFAULT_EXPORT_ERROR: (fileName: string) => string;
12
- export declare const REFERENCE_DNE_ERROR: (columnName: string) => string;
13
- export declare const FOREIGN_KEY_DNE_ERROR: (tableName: string) => string;
14
- export declare const FOREIGN_KEY_REFERENCES_LENGTH_ERROR: (tableName: string) => string;
15
- export declare const FOREIGN_KEY_REFERENCES_EMPTY_ERROR: (tableName: string) => string;
16
10
  export declare const INTEGRATION_TABLE_CONFLICT_ERROR: (integrationName: string, tableName: string, isUserConflict: boolean) => string;
@@ -29,42 +29,14 @@ const SHELL_QUERY_MISSING_ERROR = `${red(
29
29
  "\u25B6 Please provide a query to execute using the --query flag."
30
30
  )}
31
31
  `;
32
- const SEED_ERROR = (error) => {
33
- return `${red(`Error while seeding database:`)}
34
-
35
- ${error}`;
36
- };
37
32
  const EXEC_ERROR = (error) => {
38
33
  return `${red(`Error while executing file:`)}
39
34
 
40
35
  ${error}`;
41
36
  };
42
- const SEED_DEFAULT_EXPORT_ERROR = (fileName) => {
43
- return SEED_ERROR(`Missing default function export in ${bold(fileName)}`);
44
- };
45
37
  const EXEC_DEFAULT_EXPORT_ERROR = (fileName) => {
46
38
  return EXEC_ERROR(`Missing default function export in ${bold(fileName)}`);
47
39
  };
48
- const REFERENCE_DNE_ERROR = (columnName) => {
49
- return `Column ${bold(
50
- columnName
51
- )} references a table that does not exist. Did you apply the referenced table to the \`tables\` object in your db config?`;
52
- };
53
- const FOREIGN_KEY_DNE_ERROR = (tableName) => {
54
- return `Table ${bold(
55
- tableName
56
- )} references a table that does not exist. Did you apply the referenced table to the \`tables\` object in your db config?`;
57
- };
58
- const FOREIGN_KEY_REFERENCES_LENGTH_ERROR = (tableName) => {
59
- return `Foreign key on ${bold(
60
- tableName
61
- )} is misconfigured. \`columns\` and \`references\` must be the same length.`;
62
- };
63
- const FOREIGN_KEY_REFERENCES_EMPTY_ERROR = (tableName) => {
64
- return `Foreign key on ${bold(
65
- tableName
66
- )} is misconfigured. \`references\` array cannot be empty.`;
67
- };
68
40
  const INTEGRATION_TABLE_CONFLICT_ERROR = (integrationName, tableName, isUserConflict) => {
69
41
  return red("\u25B6 Conflicting table name in integration " + bold(integrationName)) + isUserConflict ? `
70
42
  A user-defined table named ${bold(tableName)} already exists` : `
@@ -74,17 +46,11 @@ export {
74
46
  EXEC_DEFAULT_EXPORT_ERROR,
75
47
  EXEC_ERROR,
76
48
  FILE_NOT_FOUND_ERROR,
77
- FOREIGN_KEY_DNE_ERROR,
78
- FOREIGN_KEY_REFERENCES_EMPTY_ERROR,
79
- FOREIGN_KEY_REFERENCES_LENGTH_ERROR,
80
49
  INTEGRATION_TABLE_CONFLICT_ERROR,
81
50
  MISSING_EXECUTE_PATH_ERROR,
82
51
  MISSING_PROJECT_ID_ERROR,
83
52
  MISSING_SESSION_ID_ERROR,
84
- REFERENCE_DNE_ERROR,
85
53
  RENAME_COLUMN_ERROR,
86
54
  RENAME_TABLE_ERROR,
87
- SEED_DEFAULT_EXPORT_ERROR,
88
- SEED_ERROR,
89
55
  SHELL_QUERY_MISSING_ERROR
90
56
  };
@@ -1,8 +1,10 @@
1
1
  import { existsSync } from "fs";
2
2
  import { dirname } from "path";
3
3
  import { fileURLToPath } from "url";
4
+ import { AstroError } from "astro/errors";
4
5
  import { mkdir, writeFile } from "fs/promises";
5
6
  import { blue, yellow } from "kleur/colors";
7
+ import { loadEnv } from "vite";
6
8
  import parseArgs from "yargs-parser";
7
9
  import { CONFIG_FILE_NAMES, DB_PATH } from "../consts.js";
8
10
  import { resolveDbConfig } from "../load-file.js";
@@ -28,12 +30,14 @@ function astroDBIntegration() {
28
30
  }
29
31
  };
30
32
  let command;
33
+ let output = "server";
31
34
  return {
32
35
  name: "astro:db",
33
36
  hooks: {
34
37
  "astro:config:setup": async ({ updateConfig, config, command: _command, logger }) => {
35
38
  command = _command;
36
39
  root = config.root;
40
+ output = config.output;
37
41
  if (command === "preview")
38
42
  return;
39
43
  let dbPlugin = void 0;
@@ -98,6 +102,11 @@ function astroDBIntegration() {
98
102
  });
99
103
  },
100
104
  "astro:build:start": async ({ logger }) => {
105
+ if (!connectToStudio && !databaseFileEnvDefined() && (output === "server" || output === "hybrid")) {
106
+ 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.`;
107
+ const hint = "Learn more connecting to Studio: https://docs.astro.build/en/guides/astro-db/#connect-to-astro-studio";
108
+ throw new AstroError(message, hint);
109
+ }
101
110
  logger.info("database: " + (connectToStudio ? yellow("remote") : blue("local database.")));
102
111
  },
103
112
  "astro:build:done": async ({}) => {
@@ -106,6 +115,10 @@ function astroDBIntegration() {
106
115
  }
107
116
  };
108
117
  }
118
+ function databaseFileEnvDefined() {
119
+ const env = loadEnv("", process.cwd());
120
+ return env.ASTRO_DATABASE_FILE != null || process.env.ASTRO_DATABASE_FILE != null;
121
+ }
109
122
  function integration() {
110
123
  return [astroDBIntegration(), fileURLIntegration()];
111
124
  }
@@ -78,7 +78,8 @@ import { asDrizzleTable, createLocalDatabaseClient } from ${RUNTIME_IMPORT};
78
78
  ${shouldSeed ? `import { seedLocal } from ${RUNTIME_IMPORT};` : ""}
79
79
  ${shouldSeed ? integrationSeedImportStatements.join("\n") : ""}
80
80
 
81
- const dbUrl = ${JSON.stringify(dbUrl)};
81
+ const dbUrl = import.meta.env.ASTRO_DATABASE_FILE ?? ${JSON.stringify(dbUrl)};
82
+
82
83
  export const db = createLocalDatabaseClient({ dbUrl });
83
84
 
84
85
  ${shouldSeed ? `await seedLocal({
@@ -0,0 +1,6 @@
1
+ export declare const FOREIGN_KEY_DNE_ERROR: (tableName: string) => string;
2
+ export declare const FOREIGN_KEY_REFERENCES_LENGTH_ERROR: (tableName: string) => string;
3
+ export declare const FOREIGN_KEY_REFERENCES_EMPTY_ERROR: (tableName: string) => string;
4
+ export declare const REFERENCE_DNE_ERROR: (columnName: string) => string;
5
+ export declare const SEED_ERROR: (error: string) => string;
6
+ export declare const SEED_DEFAULT_EXPORT_ERROR: (fileName: string) => string;
@@ -0,0 +1,37 @@
1
+ import { bold, red } from "kleur/colors";
2
+ const FOREIGN_KEY_DNE_ERROR = (tableName) => {
3
+ return `Table ${bold(
4
+ tableName
5
+ )} references a table that does not exist. Did you apply the referenced table to the \`tables\` object in your db config?`;
6
+ };
7
+ const FOREIGN_KEY_REFERENCES_LENGTH_ERROR = (tableName) => {
8
+ return `Foreign key on ${bold(
9
+ tableName
10
+ )} is misconfigured. \`columns\` and \`references\` must be the same length.`;
11
+ };
12
+ const FOREIGN_KEY_REFERENCES_EMPTY_ERROR = (tableName) => {
13
+ return `Foreign key on ${bold(
14
+ tableName
15
+ )} is misconfigured. \`references\` array cannot be empty.`;
16
+ };
17
+ const REFERENCE_DNE_ERROR = (columnName) => {
18
+ return `Column ${bold(
19
+ columnName
20
+ )} references a table that does not exist. Did you apply the referenced table to the \`tables\` object in your db config?`;
21
+ };
22
+ const SEED_ERROR = (error) => {
23
+ return `${red(`Error while seeding database:`)}
24
+
25
+ ${error}`;
26
+ };
27
+ const SEED_DEFAULT_EXPORT_ERROR = (fileName) => {
28
+ return SEED_ERROR(`Missing default function export in ${bold(fileName)}`);
29
+ };
30
+ export {
31
+ FOREIGN_KEY_DNE_ERROR,
32
+ FOREIGN_KEY_REFERENCES_EMPTY_ERROR,
33
+ FOREIGN_KEY_REFERENCES_LENGTH_ERROR,
34
+ REFERENCE_DNE_ERROR,
35
+ SEED_DEFAULT_EXPORT_ERROR,
36
+ SEED_ERROR
37
+ };
@@ -6,7 +6,7 @@ import {
6
6
  FOREIGN_KEY_REFERENCES_EMPTY_ERROR,
7
7
  FOREIGN_KEY_REFERENCES_LENGTH_ERROR,
8
8
  REFERENCE_DNE_ERROR
9
- } from "../core/errors.js";
9
+ } from "./errors.js";
10
10
  import { hasPrimaryKey } from "./index.js";
11
11
  import { isSerializedSQL } from "./types.js";
12
12
  const sqlite = new SQLiteAsyncDialect();
@@ -1,8 +1,8 @@
1
1
  import { LibsqlError } from "@libsql/client";
2
2
  import { sql } from "drizzle-orm";
3
3
  import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
4
- import { SEED_DEFAULT_EXPORT_ERROR, SEED_ERROR } from "../core/errors.js";
5
4
  import {} from "../core/types.js";
5
+ import { SEED_DEFAULT_EXPORT_ERROR, SEED_ERROR } from "./errors.js";
6
6
  import { getCreateIndexQueries, getCreateTableQuery } from "./queries.js";
7
7
  const sqlite = new SQLiteAsyncDialect();
8
8
  async function seedLocal({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.9.4",
3
+ "version": "0.9.6",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -58,7 +58,7 @@
58
58
  "@libsql/client": "^0.5.5",
59
59
  "async-listen": "^3.0.1",
60
60
  "deep-diff": "^1.0.2",
61
- "drizzle-orm": "^0.30.2",
61
+ "drizzle-orm": "^0.30.4",
62
62
  "github-slugger": "^2.0.0",
63
63
  "kleur": "^4.1.5",
64
64
  "nanoid": "^5.0.1",
@@ -81,7 +81,7 @@
81
81
  "mocha": "^10.2.0",
82
82
  "typescript": "^5.2.2",
83
83
  "vite": "^5.1.4",
84
- "astro": "4.5.9",
84
+ "astro": "4.5.10",
85
85
  "astro-scripts": "0.0.14"
86
86
  },
87
87
  "scripts": {