@astrojs/db 0.14.3 → 0.14.5

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.
@@ -0,0 +1,22 @@
1
+ import { LibsqlError } from '@libsql/client';
2
+ import { AstroError } from 'astro/errors';
3
+ /**
4
+ * Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback.
5
+ */
6
+ export declare function safeFetch(url: Parameters<typeof fetch>[0], options?: Parameters<typeof fetch>[1], onNotOK?: (response: Response) => void | Promise<void>): Promise<Response>;
7
+ export declare class AstroDbError extends AstroError {
8
+ name: string;
9
+ }
10
+ export declare class DetailedLibsqlError extends LibsqlError {
11
+ name: string;
12
+ hint?: string;
13
+ constructor({ message, code, hint, rawCode, cause, }: {
14
+ message: string;
15
+ code: string;
16
+ hint?: string;
17
+ rawCode?: number;
18
+ cause?: Error;
19
+ });
20
+ }
21
+ export declare function isDbError(err: unknown): err is LibsqlError;
22
+ export declare function pathToFileURL(path: string): URL;
@@ -1,6 +1,4 @@
1
- import { LibsqlError } from '@libsql/client';
2
1
  import type { BooleanColumnInput, ColumnsConfig, DBConfigInput, DateColumnInput, JsonColumnInput, NumberColumnOpts, TableConfig, TextColumnOpts } from '../core/types.js';
3
- export declare function isDbError(err: unknown): err is LibsqlError;
4
2
  export declare const column: {
5
3
  number: <T extends NumberColumnOpts>(opts?: T) => {
6
4
  type: "number";
@@ -47,3 +45,4 @@ export declare const TRUE: import("drizzle-orm").SQL<unknown>;
47
45
  export declare const FALSE: import("drizzle-orm").SQL<unknown>;
48
46
  export { sql, eq, gt, gte, lt, lte, ne, isNull, isNotNull, inArray, notInArray, exists, notExists, between, notBetween, like, notIlike, not, asc, desc, and, or, count, countDistinct, avg, avgDistinct, sum, sumDistinct, max, min, } from 'drizzle-orm';
49
47
  export { alias } from 'drizzle-orm/sqlite-core';
48
+ export { isDbError } from './utils.js';
@@ -1,6 +1,6 @@
1
1
  import { existsSync } from "node:fs";
2
- import { LibsqlError } from "@libsql/client";
3
2
  import { green } from "kleur/colors";
3
+ import { isDbError } from "../../../../runtime/utils.js";
4
4
  import {
5
5
  EXEC_DEFAULT_EXPORT_ERROR,
6
6
  EXEC_ERROR,
@@ -53,10 +53,8 @@ async function cmd({
53
53
  await mod.default();
54
54
  console.info(`${green("\u2714")} File run successfully.`);
55
55
  } catch (e) {
56
- if (e instanceof LibsqlError) {
57
- throw new Error(EXEC_ERROR(e.message));
58
- }
59
- throw e;
56
+ if (isDbError(e)) throw new Error(EXEC_ERROR(e.message));
57
+ else throw e;
60
58
  }
61
59
  }
62
60
  export {
@@ -9,8 +9,8 @@ import {
9
9
  } from "@astrojs/studio";
10
10
  import { slug } from "github-slugger";
11
11
  import { bgRed, cyan } from "kleur/colors";
12
- import ora from "ora";
13
12
  import prompts from "prompts";
13
+ import yoctoSpinner from "yocto-spinner";
14
14
  import { safeFetch } from "../../../../runtime/utils.js";
15
15
  async function cmd() {
16
16
  const sessionToken = await getSessionIdFromFile();
@@ -30,14 +30,14 @@ async function cmd() {
30
30
  const workspaceId = await promptWorkspace(sessionToken);
31
31
  const newProjectName = await promptNewProjectName();
32
32
  const newProjectRegion = await promptNewProjectRegion();
33
- const spinner = ora("Creating new project...").start();
33
+ const spinner = yoctoSpinner({ text: "Creating new project..." }).start();
34
34
  const newProjectData = await createNewProject({
35
35
  workspaceId,
36
36
  name: newProjectName,
37
37
  region: newProjectRegion
38
38
  });
39
39
  await new Promise((r) => setTimeout(r, 4e3));
40
- spinner.succeed("Project created!");
40
+ spinner.success("Project created!");
41
41
  return await linkProject(newProjectData.id);
42
42
  }
43
43
  }
@@ -4,8 +4,8 @@ import { SESSION_LOGIN_FILE, getAstroStudioUrl } from "@astrojs/studio";
4
4
  import { listen } from "async-listen";
5
5
  import { cyan } from "kleur/colors";
6
6
  import open from "open";
7
- import ora from "ora";
8
7
  import prompt from "prompts";
8
+ import yoctoSpinner from "yocto-spinner";
9
9
  const isWebContainer = (
10
10
  // Stackblitz heuristic
11
11
  process.versions?.webcontainer ?? // GitHub Codespaces heuristic
@@ -38,9 +38,9 @@ async function cmd({
38
38
  console.log(cyan(loginUrl.href));
39
39
  console.log(`If something goes wrong, copy-and-paste the URL into your browser.`);
40
40
  open(loginUrl.href);
41
- const spinner = ora("Waiting for confirmation...");
41
+ const spinner = yoctoSpinner({ text: "Waiting for confirmation..." });
42
42
  session = await promise;
43
- spinner.succeed("Successfully logged in");
43
+ spinner.success("Successfully logged in");
44
44
  }
45
45
  await mkdir(new URL(".", SESSION_LOGIN_FILE), { recursive: true });
46
46
  await writeFile(SESSION_LOGIN_FILE, `${session}`);
@@ -1,5 +1,4 @@
1
1
  import { stripVTControlCharacters } from "node:util";
2
- import { LibsqlError } from "@libsql/client";
3
2
  import deepDiff from "deep-diff";
4
3
  import { sql } from "drizzle-orm";
5
4
  import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core";
@@ -8,7 +7,7 @@ import { customAlphabet } from "nanoid";
8
7
  import { hasPrimaryKey } from "../../runtime/index.js";
9
8
  import { createRemoteDatabaseClient } from "../../runtime/index.js";
10
9
  import { isSerializedSQL } from "../../runtime/types.js";
11
- import { safeFetch } from "../../runtime/utils.js";
10
+ import { isDbError, safeFetch } from "../../runtime/utils.js";
12
11
  import { MIGRATION_VERSION } from "../consts.js";
13
12
  import { RENAME_COLUMN_ERROR, RENAME_TABLE_ERROR } from "../errors.js";
14
13
  import {
@@ -320,7 +319,7 @@ async function getDbCurrentSnapshot(appToken, remoteUrl) {
320
319
  );
321
320
  return JSON.parse(res.snapshot);
322
321
  } catch (error) {
323
- if (error instanceof LibsqlError && // If the schema was never pushed to the database yet the table won't exist.
322
+ if (isDbError(error) && // If the schema was never pushed to the database yet the table won't exist.
324
323
  // Treat a missing snapshot table as an empty table.
325
324
  // When connecting to a remote database in that condition
326
325
  // the query will fail with the following error code and message.
@@ -27,7 +27,7 @@ function printHelp({
27
27
  message.push(
28
28
  linebreak(),
29
29
  ` ${bgGreen(black(` ${commandName} `))} ${green(
30
- `v${"0.14.3"}`
30
+ `v${"0.14.5"}`
31
31
  )} ${headline}`
32
32
  );
33
33
  }
@@ -2,7 +2,6 @@ import { existsSync } from "node:fs";
2
2
  import { mkdir, writeFile } from "node:fs/promises";
3
3
  import { dirname } from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
- import { LibsqlError } from "@libsql/client";
6
5
  import { blue, yellow } from "kleur/colors";
7
6
  import {
8
7
  createServer,
@@ -10,8 +9,8 @@ import {
10
9
  mergeConfig
11
10
  } from "vite";
12
11
  import parseArgs from "yargs-parser";
13
- import { AstroDbError } from "../../runtime/utils.js";
14
- import { CONFIG_FILE_NAMES, DB_PATH } from "../consts.js";
12
+ import { AstroDbError, isDbError } from "../../runtime/utils.js";
13
+ import { CONFIG_FILE_NAMES, DB_PATH, VIRTUAL_MODULE_ID } from "../consts.js";
15
14
  import { EXEC_DEFAULT_EXPORT_ERROR, EXEC_ERROR } from "../errors.js";
16
15
  import { resolveDbConfig } from "../load-file.js";
17
16
  import { SEED_DEV_FILE_NAME } from "../queries.js";
@@ -19,7 +18,6 @@ import { getDbDirectoryUrl, getManagedRemoteToken } from "../utils.js";
19
18
  import { fileURLIntegration } from "./file-url.js";
20
19
  import { getDtsContent } from "./typegen.js";
21
20
  import {
22
- resolved,
23
21
  vitePluginDb
24
22
  } from "./vite-plugin-db.js";
25
23
  function astroDBIntegration() {
@@ -45,14 +43,13 @@ function astroDBIntegration() {
45
43
  inProgress: false
46
44
  };
47
45
  let command;
48
- let output = "server";
46
+ let finalBuildOutput;
49
47
  return {
50
48
  name: "astro:db",
51
49
  hooks: {
52
50
  "astro:config:setup": async ({ updateConfig, config, command: _command, logger }) => {
53
51
  command = _command;
54
52
  root = config.root;
55
- output = config.output;
56
53
  if (command === "preview") return;
57
54
  let dbPlugin = void 0;
58
55
  const args = parseArgs(process.argv.slice(3));
@@ -87,8 +84,9 @@ function astroDBIntegration() {
87
84
  }
88
85
  });
89
86
  },
90
- "astro:config:done": async ({ config, injectTypes }) => {
87
+ "astro:config:done": async ({ config, injectTypes, buildOutput }) => {
91
88
  if (command === "preview") return;
89
+ finalBuildOutput = buildOutput;
92
90
  const { dbConfig, dependencies, integrationSeedPaths } = await resolveDbConfig(config);
93
91
  tables.get = () => dbConfig.tables;
94
92
  seedFiles.get = () => integrationSeedPaths;
@@ -126,14 +124,14 @@ function astroDBIntegration() {
126
124
  (name) => new URL(name, getDbDirectoryUrl(root))
127
125
  );
128
126
  if (seedFiles.get().length || localSeedPaths.find((path) => existsSync(path))) {
129
- server.ssrLoadModule(resolved.module).catch((e) => {
127
+ server.ssrLoadModule(VIRTUAL_MODULE_ID).catch((e) => {
130
128
  logger.error(e instanceof Error ? e.message : String(e));
131
129
  });
132
130
  }
133
131
  }, 100);
134
132
  },
135
133
  "astro:build:start": async ({ logger }) => {
136
- if (!connectToRemote && !databaseFileEnvDefined() && (output === "server" || output === "hybrid")) {
134
+ if (!connectToRemote && !databaseFileEnvDefined() && finalBuildOutput === "server") {
137
135
  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.`;
138
136
  const hint = "Learn more connecting to Studio: https://docs.astro.build/en/guides/astro-db/#connect-to-astro-studio";
139
137
  throw new AstroDbError(message, hint);
@@ -171,7 +169,7 @@ async function executeSeedFile({
171
169
  try {
172
170
  await mod.default();
173
171
  } catch (e) {
174
- if (e instanceof LibsqlError) {
172
+ if (isDbError(e)) {
175
173
  throw new AstroDbError(EXEC_ERROR(e.message));
176
174
  }
177
175
  throw e;
@@ -18,4 +18,5 @@ export declare class DetailedLibsqlError extends LibsqlError {
18
18
  cause?: Error;
19
19
  });
20
20
  }
21
+ export declare function isDbError(err: unknown): err is LibsqlError;
21
22
  export declare function pathToFileURL(path: string): URL;
@@ -27,6 +27,9 @@ class DetailedLibsqlError extends LibsqlError {
27
27
  this.hint = hint;
28
28
  }
29
29
  }
30
+ function isDbError(err) {
31
+ return err instanceof LibsqlError || err instanceof Error && err.libsqlError === true;
32
+ }
30
33
  function slash(path) {
31
34
  const isExtendedLengthPath = path.startsWith("\\\\?\\");
32
35
  if (isExtendedLengthPath) {
@@ -47,6 +50,7 @@ function pathToFileURL(path) {
47
50
  export {
48
51
  AstroDbError,
49
52
  DetailedLibsqlError,
53
+ isDbError,
50
54
  pathToFileURL,
51
55
  safeFetch
52
56
  };
@@ -1,4 +1,3 @@
1
- import { LibsqlError } from "@libsql/client";
2
1
  import { sql as _sql } from "drizzle-orm";
3
2
  function createColumn(type, schema) {
4
3
  return {
@@ -9,9 +8,6 @@ function createColumn(type, schema) {
9
8
  schema
10
9
  };
11
10
  }
12
- function isDbError(err) {
13
- return err instanceof LibsqlError;
14
- }
15
11
  const column = {
16
12
  number: (opts = {}) => {
17
13
  return createColumn("number", opts);
@@ -71,6 +67,7 @@ import {
71
67
  min
72
68
  } from "drizzle-orm";
73
69
  import { alias } from "drizzle-orm/sqlite-core";
70
+ import { isDbError } from "./utils.js";
74
71
  export {
75
72
  FALSE,
76
73
  NOW,
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.14.3",
3
+ "version": "0.14.5",
4
4
  "description": "Add libSQL and Astro Studio support to your Astro site",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "https://github.com/withastro/astro.git",
8
+ "url": "git+https://github.com/withastro/astro.git",
9
9
  "directory": "packages/db"
10
10
  },
11
11
  "bugs": "https://github.com/withastro/astro/issues",
@@ -68,13 +68,13 @@
68
68
  "drizzle-orm": "^0.31.2",
69
69
  "github-slugger": "^2.0.0",
70
70
  "kleur": "^4.1.5",
71
- "nanoid": "^5.0.7",
71
+ "nanoid": "^5.0.9",
72
72
  "open": "^10.1.0",
73
- "ora": "^8.1.0",
74
73
  "prompts": "^2.4.2",
75
74
  "yargs-parser": "^21.1.1",
75
+ "yocto-spinner": "^0.1.0",
76
76
  "zod": "^3.23.8",
77
- "@astrojs/studio": "0.1.1"
77
+ "@astrojs/studio": "0.1.3"
78
78
  },
79
79
  "devDependencies": {
80
80
  "@types/deep-diff": "^1.0.5",
@@ -82,8 +82,8 @@
82
82
  "@types/yargs-parser": "^21.0.3",
83
83
  "cheerio": "1.0.0",
84
84
  "typescript": "^5.6.3",
85
- "vite": "^5.4.9",
86
- "astro": "4.16.7",
85
+ "vite": "^6.0.5",
86
+ "astro": "5.1.1",
87
87
  "astro-scripts": "0.0.14"
88
88
  },
89
89
  "scripts": {