@astrojs/db 0.14.1 → 0.14.3

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,9 +1,10 @@
1
+ import { stripVTControlCharacters } from "node:util";
2
+ import { LibsqlError } from "@libsql/client";
1
3
  import deepDiff from "deep-diff";
2
4
  import { sql } from "drizzle-orm";
3
5
  import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
4
6
  import * as color from "kleur/colors";
5
7
  import { customAlphabet } from "nanoid";
6
- import stripAnsi from "strip-ansi";
7
8
  import { hasPrimaryKey } from "../../runtime/index.js";
8
9
  import { createRemoteDatabaseClient } from "../../runtime/index.js";
9
10
  import { isSerializedSQL } from "../../runtime/types.js";
@@ -319,7 +320,13 @@ async function getDbCurrentSnapshot(appToken, remoteUrl) {
319
320
  );
320
321
  return JSON.parse(res.snapshot);
321
322
  } catch (error) {
322
- if (error.code === "SQLITE_UNKNOWN") {
323
+ if (error instanceof LibsqlError && // If the schema was never pushed to the database yet the table won't exist.
324
+ // Treat a missing snapshot table as an empty table.
325
+ // When connecting to a remote database in that condition
326
+ // the query will fail with the following error code and message.
327
+ (error.code === "SQLITE_UNKNOWN" && error.message === "SQLITE_UNKNOWN: SQLite error: no such table: _astro_db_snapshot" || // When connecting to a local or in-memory database that does not have a snapshot table yet
328
+ // the query will fail with the following error code and message.
329
+ error.code === "SQLITE_ERROR" && error.message === "SQLITE_ERROR: no such table: _astro_db_snapshot")) {
323
330
  return;
324
331
  }
325
332
  throw error;
@@ -376,7 +383,7 @@ function formatDataLossMessage(confirmations, isColor = true) {
376
383
  );
377
384
  let finalMessage = messages.join("\n");
378
385
  if (!isColor) {
379
- finalMessage = stripAnsi(finalMessage);
386
+ finalMessage = stripVTControlCharacters(finalMessage);
380
387
  }
381
388
  return finalMessage;
382
389
  }
@@ -27,7 +27,7 @@ function printHelp({
27
27
  message.push(
28
28
  linebreak(),
29
29
  ` ${bgGreen(black(` ${commandName} `))} ${green(
30
- `v${"0.14.1"}`
30
+ `v${"0.14.3"}`
31
31
  )} ${headline}`
32
32
  );
33
33
  }
@@ -111,7 +111,7 @@ function astroDBIntegration() {
111
111
  ...CONFIG_FILE_NAMES.map((c) => new URL(c, getDbDirectoryUrl(root))),
112
112
  ...configFileDependencies.map((c) => new URL(c, root))
113
113
  ];
114
- server.watcher.on("all", (event, relativeEntry) => {
114
+ server.watcher.on("all", (_event, relativeEntry) => {
115
115
  const entry = new URL(relativeEntry, root);
116
116
  if (filesToWatch.some((f) => entry.href === f.href)) {
117
117
  server.restart();
@@ -74,12 +74,13 @@ function getLocalVirtualModContents({
74
74
  tables,
75
75
  root
76
76
  }) {
77
+ const { ASTRO_DATABASE_FILE } = getAstroEnv();
77
78
  const dbInfo = getRemoteDatabaseInfo();
78
79
  const dbUrl = new URL(DB_PATH, root);
79
80
  return `
80
81
  import { asDrizzleTable, createLocalDatabaseClient, normalizeDatabaseUrl } from ${RUNTIME_IMPORT};
81
82
 
82
- const dbUrl = normalizeDatabaseUrl(import.meta.env.ASTRO_DATABASE_FILE, ${JSON.stringify(dbUrl)});
83
+ const dbUrl = normalizeDatabaseUrl(${JSON.stringify(ASTRO_DATABASE_FILE)}, ${JSON.stringify(dbUrl)});
83
84
  export const db = createLocalDatabaseClient({ dbUrl, enableTransactions: ${dbInfo.url === "libsql"} });
84
85
 
85
86
  export * from ${RUNTIME_VIRTUAL_IMPORT};
@@ -107,7 +108,11 @@ function getStudioVirtualModContents({
107
108
  }
108
109
  function dbUrlArg() {
109
110
  const dbStr = JSON.stringify(dbInfo.url);
110
- return dbInfo.type === "studio" ? `import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL ?? ${dbStr}` : `import.meta.env.ASTRO_DB_REMOTE_URL ?? ${dbStr}`;
111
+ if (isBuild) {
112
+ return dbInfo.type === "studio" ? `import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL ?? ${dbStr}` : `import.meta.env.ASTRO_DB_REMOTE_URL ?? ${dbStr}`;
113
+ } else {
114
+ return dbStr;
115
+ }
111
116
  }
112
117
  return `
113
118
  import {asDrizzleTable, createRemoteDatabaseClient} from ${RUNTIME_IMPORT};
@@ -31,15 +31,21 @@ const remoteResultSchema = z.object({
31
31
  });
32
32
  function createRemoteDatabaseClient(options) {
33
33
  const remoteUrl = new URL(options.remoteUrl);
34
- return options.dbType === "studio" ? createStudioDatabaseClient(options.appToken, remoteUrl) : createRemoteLibSQLClient(options.appToken, remoteUrl);
34
+ return options.dbType === "studio" ? createStudioDatabaseClient(options.appToken, remoteUrl) : createRemoteLibSQLClient(options.appToken, remoteUrl, options.remoteUrl.toString());
35
35
  }
36
- function createRemoteLibSQLClient(appToken, remoteDbURL) {
36
+ function createRemoteLibSQLClient(appToken, remoteDbURL, rawUrl) {
37
37
  const options = Object.fromEntries(remoteDbURL.searchParams.entries());
38
38
  remoteDbURL.search = "";
39
+ let url = remoteDbURL.toString();
40
+ if (remoteDbURL.protocol === "memory:") {
41
+ url = ":memory:";
42
+ } else if (remoteDbURL.protocol === "file:" && remoteDbURL.pathname.startsWith("/") && !rawUrl.startsWith("file:/")) {
43
+ url = "file:" + remoteDbURL.pathname.substring(1);
44
+ }
39
45
  const client = createClient({
40
46
  ...options,
41
47
  authToken: appToken,
42
- url: remoteDbURL.protocol === "memory:" ? ":memory:" : remoteDbURL.toString()
48
+ url
43
49
  });
44
50
  return drizzleLibsql(client);
45
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.14.1",
3
+ "version": "0.14.3",
4
4
  "description": "Add libSQL and Astro Studio support to your Astro site",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -62,7 +62,7 @@
62
62
  "astro-integration"
63
63
  ],
64
64
  "dependencies": {
65
- "@libsql/client": "^0.10.0",
65
+ "@libsql/client": "^0.14.0",
66
66
  "async-listen": "^3.0.1",
67
67
  "deep-diff": "^1.0.2",
68
68
  "drizzle-orm": "^0.31.2",
@@ -72,7 +72,6 @@
72
72
  "open": "^10.1.0",
73
73
  "ora": "^8.1.0",
74
74
  "prompts": "^2.4.2",
75
- "strip-ansi": "^7.1.0",
76
75
  "yargs-parser": "^21.1.1",
77
76
  "zod": "^3.23.8",
78
77
  "@astrojs/studio": "0.1.1"
@@ -82,9 +81,9 @@
82
81
  "@types/prompts": "^2.4.9",
83
82
  "@types/yargs-parser": "^21.0.3",
84
83
  "cheerio": "1.0.0",
85
- "typescript": "^5.5.4",
86
- "vite": "^5.4.2",
87
- "astro": "4.15.3",
84
+ "typescript": "^5.6.3",
85
+ "vite": "^5.4.9",
86
+ "astro": "4.16.7",
88
87
  "astro-scripts": "0.0.14"
89
88
  },
90
89
  "scripts": {