@astrojs/db 0.1.2 → 0.1.4

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,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,
@@ -44,9 +44,7 @@ function getStudioVirtualModContents({
44
44
  return `
45
45
  import {collectionToTable, createRemoteDatabaseClient} from ${INTERNAL_MOD_IMPORT};
46
46
 
47
- export const db = await createRemoteDatabaseClient(${JSON.stringify({
48
- appToken
49
- })});
47
+ export const db = await createRemoteDatabaseClient(${JSON.stringify(appToken)}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL);
50
48
  export * from ${DRIZZLE_MOD_IMPORT};
51
49
 
52
50
  ${getStringifiedCollectionExports(collections)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -61,7 +61,7 @@
61
61
  "mocha": "^10.2.0",
62
62
  "typescript": "^5.2.2",
63
63
  "vite": "^4.4.11",
64
- "astro": "4.2.1",
64
+ "astro": "4.2.4",
65
65
  "astro-scripts": "0.0.14"
66
66
  },
67
67
  "scripts": {
File without changes
File without changes