@astrojs/db 0.1.3 → 0.1.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.
package/dist/consts.d.ts CHANGED
@@ -3,4 +3,5 @@ export declare const INTERNAL_MOD_IMPORT: string;
3
3
  export declare const DRIZZLE_MOD_IMPORT: string;
4
4
  export declare const DB_TYPES_FILE = "db-types.d.ts";
5
5
  export declare const VIRTUAL_MODULE_ID = "astro:db";
6
+ export declare const DB_PATH = ".astro/content.db";
6
7
  export declare function getLocalDbUrl(root: URL): URL;
package/dist/consts.js CHANGED
@@ -6,10 +6,12 @@ const INTERNAL_MOD_IMPORT = JSON.stringify(`${PACKAGE_NAME}/internal`);
6
6
  const DRIZZLE_MOD_IMPORT = JSON.stringify(`${PACKAGE_NAME}/internal-drizzle`);
7
7
  const DB_TYPES_FILE = "db-types.d.ts";
8
8
  const VIRTUAL_MODULE_ID = "astro:db";
9
+ const DB_PATH = ".astro/content.db";
9
10
  function getLocalDbUrl(root) {
10
- return new URL(".astro/content.db", root);
11
+ return new URL(DB_PATH, root);
11
12
  }
12
13
  export {
14
+ DB_PATH,
13
15
  DB_TYPES_FILE,
14
16
  DRIZZLE_MOD_IMPORT,
15
17
  INTERNAL_MOD_IMPORT,
@@ -36,7 +36,11 @@ function integration() {
36
36
  logger.error(appTokenError);
37
37
  process.exit(0);
38
38
  }
39
- dbPlugin = vitePluginDb({ connectToStudio: true, collections, appToken });
39
+ dbPlugin = vitePluginDb({
40
+ connectToStudio: true,
41
+ collections,
42
+ appToken
43
+ });
40
44
  } else {
41
45
  const dbUrl = getLocalDbUrl(config.root);
42
46
  if (existsSync(dbUrl)) {
@@ -3,10 +3,10 @@ import { type BooleanField, type DBCollection, type DBCollections, type DBField,
3
3
  import { type LibSQLDatabase } from 'drizzle-orm/libsql';
4
4
  import { type ColumnDataType } from 'drizzle-orm';
5
5
  import type { AstroIntegrationLogger } from 'astro';
6
+ export { createRemoteDatabaseClient } from './utils-runtime.js';
6
7
  export type SqliteDB = SqliteRemoteDatabase;
7
8
  export type { Table } from './types.js';
8
9
  export declare function hasPrimaryKey(field: DBField): boolean;
9
- export { createRemoteDatabaseClient } from './utils.js';
10
10
  export declare function createLocalDatabaseClient({ collections, dbUrl, seeding, }: {
11
11
  dbUrl: string;
12
12
  collections: DBCollections;
package/dist/internal.js CHANGED
@@ -15,7 +15,7 @@ import {
15
15
  text
16
16
  } from "drizzle-orm/sqlite-core";
17
17
  import { z } from "zod";
18
- import { nanoid } from "nanoid";
18
+ import { createRemoteDatabaseClient } from "./utils-runtime.js";
19
19
  const sqlite = new SQLiteAsyncDialect();
20
20
  function hasPrimaryKey(field) {
21
21
  return "primaryKey" in field && !!field.primaryKey;
@@ -30,7 +30,6 @@ function checkIfModificationIsAllowed(collections, Table) {
30
30
  throw new Error(`The [${tableName}] collection is read-only.`);
31
31
  }
32
32
  }
33
- import { createRemoteDatabaseClient } from "./utils.js";
34
33
  async function createLocalDatabaseClient({
35
34
  collections,
36
35
  dbUrl,
@@ -0,0 +1 @@
1
+ export declare function createRemoteDatabaseClient(appToken: string, remoteDbURL: string): import("drizzle-orm/sqlite-proxy").SqliteRemoteDatabase<Record<string, never>>;
@@ -0,0 +1,52 @@
1
+ import { drizzle } from "drizzle-orm/sqlite-proxy";
2
+ import { z } from "zod";
3
+ function createRemoteDatabaseClient(appToken, remoteDbURL) {
4
+ const url = new URL("./db/query/", remoteDbURL);
5
+ const db = drizzle(async (sql, parameters, method) => {
6
+ const requestBody = { sql, args: parameters };
7
+ console.info(JSON.stringify(requestBody));
8
+ const res = await fetch(url, {
9
+ method: "POST",
10
+ headers: {
11
+ Authorization: `Bearer ${appToken}`,
12
+ "Content-Type": "application/json"
13
+ },
14
+ body: JSON.stringify(requestBody)
15
+ });
16
+ if (!res.ok) {
17
+ throw new Error(
18
+ `Failed to execute query.
19
+ Query: ${sql}
20
+ Full error: ${res.status} ${await res.text()}}`
21
+ );
22
+ }
23
+ const queryResultSchema = z.object({
24
+ rows: z.array(z.unknown())
25
+ });
26
+ let rows;
27
+ try {
28
+ const json = await res.json();
29
+ rows = queryResultSchema.parse(json).rows;
30
+ } catch (e) {
31
+ throw new Error(
32
+ `Failed to execute query.
33
+ Query: ${sql}
34
+ Full error: Unexpected JSON response. ${e instanceof Error ? e.message : String(e)}`
35
+ );
36
+ }
37
+ const rowValues = [];
38
+ for (const row of rows) {
39
+ if (row != null && typeof row === "object") {
40
+ rowValues.push(Object.values(row));
41
+ }
42
+ }
43
+ if (method === "get") {
44
+ return { rows: rowValues[0] };
45
+ }
46
+ return { rows: rowValues };
47
+ });
48
+ return db;
49
+ }
50
+ export {
51
+ createRemoteDatabaseClient
52
+ };
package/dist/utils.js CHANGED
@@ -1,7 +1,6 @@
1
- import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";
2
- import { drizzle } from "drizzle-orm/sqlite-proxy";
1
+ import { sqliteTable, text } from "drizzle-orm/sqlite-core";
3
2
  import { loadEnv } from "vite";
4
- import { z } from "zod";
3
+ import { createRemoteDatabaseClient as runtimeCreateRemoteDatabaseClient } from "./utils-runtime.js";
5
4
  const STUDIO_ADMIN_TABLE = "ReservedAstroStudioAdmin";
6
5
  const STUDIO_ADMIN_TABLE_ROW_ID = "admin";
7
6
  const adminTable = sqliteTable(STUDIO_ADMIN_TABLE, {
@@ -25,51 +24,7 @@ function getRemoteDatabaseUrl() {
25
24
  return env.ASTRO_STUDIO_REMOTE_DB_URL;
26
25
  }
27
26
  function createRemoteDatabaseClient(appToken) {
28
- const url = new URL("./db/query/", getRemoteDatabaseUrl());
29
- const db = drizzle(async (sql, parameters, method) => {
30
- const requestBody = { sql, args: parameters };
31
- console.info(JSON.stringify(requestBody));
32
- const res = await fetch(url, {
33
- method: "POST",
34
- headers: {
35
- Authorization: `Bearer ${appToken}`,
36
- "Content-Type": "application/json"
37
- },
38
- body: JSON.stringify(requestBody)
39
- });
40
- if (!res.ok) {
41
- throw new Error(
42
- `Failed to execute query.
43
- Query: ${sql}
44
- Full error: ${res.status} ${await res.text()}}`
45
- );
46
- }
47
- const queryResultSchema = z.object({
48
- rows: z.array(z.unknown())
49
- });
50
- let rows;
51
- try {
52
- const json = await res.json();
53
- rows = queryResultSchema.parse(json).rows;
54
- } catch (e) {
55
- throw new Error(
56
- `Failed to execute query.
57
- Query: ${sql}
58
- Full error: Unexpected JSON response. ${e instanceof Error ? e.message : String(e)}`
59
- );
60
- }
61
- const rowValues = [];
62
- for (const row of rows) {
63
- if (row != null && typeof row === "object") {
64
- rowValues.push(Object.values(row));
65
- }
66
- }
67
- if (method === "get") {
68
- return { rows: rowValues[0] };
69
- }
70
- return { rows: rowValues };
71
- });
72
- return db;
27
+ return runtimeCreateRemoteDatabaseClient(appToken, getRemoteDatabaseUrl());
73
28
  }
74
29
  export {
75
30
  STUDIO_ADMIN_TABLE,
@@ -1,4 +1,4 @@
1
- import { DRIZZLE_MOD_IMPORT, INTERNAL_MOD_IMPORT, VIRTUAL_MODULE_ID } from "./consts.js";
1
+ import { DRIZZLE_MOD_IMPORT, INTERNAL_MOD_IMPORT, VIRTUAL_MODULE_ID, DB_PATH } from "./consts.js";
2
2
  const resolvedVirtualModuleId = "\0" + VIRTUAL_MODULE_ID;
3
3
  function vitePluginDb(params) {
4
4
  return {
@@ -26,11 +26,13 @@ function getVirtualModContents({
26
26
  return `
27
27
  import { collectionToTable, createLocalDatabaseClient } from ${INTERNAL_MOD_IMPORT};
28
28
 
29
- export const db = await createLocalDatabaseClient(${JSON.stringify({
29
+ const params = ${JSON.stringify({
30
30
  collections,
31
- dbUrl,
32
31
  seeding: false
33
- })});
32
+ })};
33
+
34
+ params.dbUrl = new URL(${JSON.stringify(DB_PATH)}, 'file://' + process.cwd() + '/');
35
+ export const db = await createLocalDatabaseClient(params);
34
36
 
35
37
  export * from ${DRIZZLE_MOD_IMPORT};
36
38
 
@@ -44,9 +46,7 @@ function getStudioVirtualModContents({
44
46
  return `
45
47
  import {collectionToTable, createRemoteDatabaseClient} from ${INTERNAL_MOD_IMPORT};
46
48
 
47
- export const db = await createRemoteDatabaseClient(${JSON.stringify({
48
- appToken
49
- })});
49
+ export const db = await createRemoteDatabaseClient(${JSON.stringify(appToken)}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL);
50
50
  export * from ${DRIZZLE_MOD_IMPORT};
51
51
 
52
52
  ${getStringifiedCollectionExports(collections)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -61,8 +61,8 @@
61
61
  "mocha": "^10.2.0",
62
62
  "typescript": "^5.2.2",
63
63
  "vite": "^4.4.11",
64
- "astro": "4.2.4",
65
- "astro-scripts": "0.0.14"
64
+ "astro-scripts": "0.0.14",
65
+ "astro": "4.2.4"
66
66
  },
67
67
  "scripts": {
68
68
  "build": "astro-scripts build \"src/**/*.ts\" && tsc",