@astrojs/db 0.4.0 → 0.4.1

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.
@@ -1,7 +1,9 @@
1
1
  export declare const MISSING_SESSION_ID_ERROR: string;
2
2
  export declare const MISSING_PROJECT_ID_ERROR: string;
3
- export declare const STUDIO_CONFIG_MISSING_WRITABLE_COLLECTIONS_ERROR: (collectionName: string) => string;
3
+ export declare const STUDIO_CONFIG_MISSING_WRITABLE_TABLE_ERROR: (tableName: string) => string;
4
4
  export declare const UNSAFE_WRITABLE_WARNING: string;
5
5
  export declare const STUDIO_CONFIG_MISSING_CLI_ERROR: string;
6
6
  export declare const MIGRATIONS_NOT_INITIALIZED: string;
7
- export declare const SEED_WRITABLE_IN_PROD_ERROR: (collectionName: string) => string;
7
+ export declare const SEED_WRITABLE_IN_PROD_ERROR: (tableName: string) => string;
8
+ export declare const SEED_ERROR: (tableName: string, error: string) => string;
9
+ export declare const SEED_EMPTY_ARRAY_ERROR: (tableName: string) => string;
@@ -9,8 +9,8 @@ const MISSING_PROJECT_ID_ERROR = `${red("\u25B6 Directory not linked.")}
9
9
  To link this directory to an Astro Studio project, run
10
10
  ${cyan("astro db link")}
11
11
  `;
12
- const STUDIO_CONFIG_MISSING_WRITABLE_COLLECTIONS_ERROR = (collectionName) => `${red(
13
- `\u25B6 Writable collection ${bold(collectionName)} requires Astro Studio or the ${yellow(
12
+ const STUDIO_CONFIG_MISSING_WRITABLE_TABLE_ERROR = (tableName) => `${red(
13
+ `\u25B6 Writable table ${bold(tableName)} requires Astro Studio or the ${yellow(
14
14
  "unsafeWritable"
15
15
  )} option.`
16
16
  )}
@@ -36,19 +36,29 @@ const MIGRATIONS_NOT_INITIALIZED = `${yellow(
36
36
  To scaffold your migrations folder, run
37
37
  ${cyan("astro db sync")}
38
38
  `;
39
- const SEED_WRITABLE_IN_PROD_ERROR = (collectionName) => {
39
+ const SEED_WRITABLE_IN_PROD_ERROR = (tableName) => {
40
40
  return `${red(
41
41
  `Writable tables should not be seeded in production with data().`
42
42
  )} You can seed ${bold(
43
- collectionName
43
+ tableName
44
44
  )} in development mode only using the "mode" flag. See the docs for more: https://www.notion.so/astroinc/astrojs-db-README-dcf6fa10de9a4f528be56cee96e8c054?pvs=4#278aed3fc37e4cec80240d1552ff6ac5`;
45
45
  };
46
+ const SEED_ERROR = (tableName, error) => {
47
+ return `${red(`Error seeding table ${bold(tableName)}:`)}
48
+
49
+ ${error}`;
50
+ };
51
+ const SEED_EMPTY_ARRAY_ERROR = (tableName) => {
52
+ return SEED_ERROR(tableName, `Empty array was passed. seed() must receive at least one value.`);
53
+ };
46
54
  export {
47
55
  MIGRATIONS_NOT_INITIALIZED,
48
56
  MISSING_PROJECT_ID_ERROR,
49
57
  MISSING_SESSION_ID_ERROR,
58
+ SEED_EMPTY_ARRAY_ERROR,
59
+ SEED_ERROR,
50
60
  SEED_WRITABLE_IN_PROD_ERROR,
51
61
  STUDIO_CONFIG_MISSING_CLI_ERROR,
52
- STUDIO_CONFIG_MISSING_WRITABLE_COLLECTIONS_ERROR,
62
+ STUDIO_CONFIG_MISSING_WRITABLE_TABLE_ERROR,
53
63
  UNSAFE_WRITABLE_WARNING
54
64
  };
@@ -47,10 +47,7 @@ import { DB_PATH } from "../consts.js";
47
47
  import { createLocalDatabaseClient } from "../../runtime/db-client.js";
48
48
  import { astroConfigWithDbSchema } from "../types.js";
49
49
  import {} from "../utils.js";
50
- import {
51
- STUDIO_CONFIG_MISSING_WRITABLE_COLLECTIONS_ERROR,
52
- UNSAFE_WRITABLE_WARNING
53
- } from "../errors.js";
50
+ import { STUDIO_CONFIG_MISSING_WRITABLE_TABLE_ERROR, UNSAFE_WRITABLE_WARNING } from "../errors.js";
54
51
  import { errorMap } from "./error-map.js";
55
52
  import { dirname } from "path";
56
53
  import { fileURLToPath } from "url";
@@ -108,9 +105,7 @@ function astroDBIntegration() {
108
105
  const foundWritableCollection = Object.entries(tables).find(([, c]) => c.writable);
109
106
  const writableAllowed = studio || unsafeWritable;
110
107
  if (!writableAllowed && foundWritableCollection) {
111
- logger.error(
112
- STUDIO_CONFIG_MISSING_WRITABLE_COLLECTIONS_ERROR(foundWritableCollection[0])
113
- );
108
+ logger.error(STUDIO_CONFIG_MISSING_WRITABLE_TABLE_ERROR(foundWritableCollection[0]));
114
109
  process.exit(1);
115
110
  } else if (unsafeWritable && foundWritableCollection) {
116
111
  logger.warn(UNSAFE_WRITABLE_WARNING);
@@ -1,5 +1,5 @@
1
1
  import type { DBTables } from '../types.js';
2
- import type { VitePlugin } from '../utils.js';
2
+ import { type VitePlugin } from '../utils.js';
3
3
  type LateSchema = {
4
4
  tables: () => DBTables;
5
5
  };
@@ -1,4 +1,5 @@
1
1
  import { DB_PATH, RUNTIME_DRIZZLE_IMPORT, RUNTIME_IMPORT, VIRTUAL_MODULE_ID } from "../consts.js";
2
+ import { getRemoteDatabaseUrl } from "../utils.js";
2
3
  const resolvedVirtualModuleId = "\0" + VIRTUAL_MODULE_ID;
3
4
  function vitePluginDb(params) {
4
5
  return {
@@ -53,7 +54,8 @@ import {collectionToTable, createRemoteDatabaseClient} from ${RUNTIME_IMPORT};
53
54
 
54
55
  export const db = await createRemoteDatabaseClient(${JSON.stringify(
55
56
  appToken
56
- )}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL);
57
+ // Respect runtime env for user overrides in SSR
58
+ )}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL ?? ${JSON.stringify(getRemoteDatabaseUrl())});
57
59
  export * from ${RUNTIME_DRIZZLE_IMPORT};
58
60
 
59
61
  ${getStringifiedCollectionExports(tables)}
@@ -5,7 +5,7 @@ import { sql, getTableName } from "drizzle-orm";
5
5
  import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
6
6
  import { hasPrimaryKey } from "../runtime/index.js";
7
7
  import { isSerializedSQL } from "../runtime/types.js";
8
- import { SEED_WRITABLE_IN_PROD_ERROR } from "./errors.js";
8
+ import { SEED_EMPTY_ARRAY_ERROR, SEED_ERROR, SEED_WRITABLE_IN_PROD_ERROR } from "./errors.js";
9
9
  const sqlite = new SQLiteAsyncDialect();
10
10
  async function recreateTables({
11
11
  db,
@@ -28,37 +28,49 @@ async function seedData({
28
28
  logger,
29
29
  mode
30
30
  }) {
31
+ const dataFns = Array.isArray(data) ? data : [data];
31
32
  try {
32
- const dataFns = Array.isArray(data) ? data : [data];
33
33
  for (const dataFn of dataFns) {
34
34
  await dataFn({
35
- seed: async ({ table, writable }, values) => {
36
- if (writable && mode === "build" && process.env.ASTRO_DB_TEST_ENV !== "1") {
37
- (logger ?? console).error(SEED_WRITABLE_IN_PROD_ERROR(getTableName(table)));
38
- process.exit(1);
35
+ seed: async (config, values) => {
36
+ seedErrorChecks(mode, config, values);
37
+ try {
38
+ await db.insert(config.table).values(values);
39
+ } catch (e) {
40
+ const msg = e instanceof Error ? e.message : String(e);
41
+ throw new Error(SEED_ERROR(getTableName(config.table), msg));
39
42
  }
40
- await db.insert(table).values(values);
41
43
  },
42
- seedReturning: async ({ table, writable }, values) => {
43
- if (writable && mode === "build" && process.env.ASTRO_DB_TEST_ENV !== "1") {
44
- (logger ?? console).error(SEED_WRITABLE_IN_PROD_ERROR(getTableName(table)));
45
- process.exit(1);
44
+ seedReturning: async (config, values) => {
45
+ seedErrorChecks(mode, config, values);
46
+ try {
47
+ let result = db.insert(config.table).values(values).returning();
48
+ if (!Array.isArray(values)) {
49
+ result = result.get();
50
+ }
51
+ return result;
52
+ } catch (e) {
53
+ const msg = e instanceof Error ? e.message : String(e);
54
+ throw new Error(SEED_ERROR(getTableName(config.table), msg));
46
55
  }
47
- let result = db.insert(table).values(values).returning();
48
- if (!Array.isArray(values)) {
49
- result = result.get();
50
- }
51
- return result;
52
56
  },
53
57
  db,
54
58
  mode
55
59
  });
56
60
  }
57
- } catch (error) {
58
- (logger ?? console).error(
59
- `Failed to seed data. Did you update to match recent schema changes?`
60
- );
61
- (logger ?? console).error(error);
61
+ } catch (e) {
62
+ if (!(e instanceof Error))
63
+ throw e;
64
+ (logger ?? console).error(e.message);
65
+ }
66
+ }
67
+ function seedErrorChecks(mode, { table, writable }, values) {
68
+ const tableName = getTableName(table);
69
+ if (writable && mode === "build" && process.env.ASTRO_DB_TEST_ENV !== "1") {
70
+ throw new Error(SEED_WRITABLE_IN_PROD_ERROR(tableName));
71
+ }
72
+ if (Array.isArray(values) && values.length === 0) {
73
+ throw new Error(SEED_EMPTY_ARRAY_ERROR(tableName));
62
74
  }
63
75
  }
64
76
  function getCreateTableQuery(collectionName, collection) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -68,7 +68,7 @@
68
68
  "mocha": "^10.2.0",
69
69
  "typescript": "^5.2.2",
70
70
  "vite": "^4.4.11",
71
- "astro": "4.4.4",
71
+ "astro": "4.4.5",
72
72
  "astro-scripts": "0.0.14"
73
73
  },
74
74
  "scripts": {