@astrojs/db 0.1.3 → 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.
- package/dist/internal.d.ts +1 -1
- package/dist/internal.js +1 -2
- package/dist/utils-runtime.d.ts +1 -0
- package/dist/utils-runtime.js +52 -0
- package/dist/utils.js +3 -48
- package/dist/vite-plugin-db.js +1 -3
- package/package.json +1 -1
package/dist/internal.d.ts
CHANGED
|
@@ -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 {
|
|
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
|
|
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 {
|
|
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
|
-
|
|
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,
|
package/dist/vite-plugin-db.js
CHANGED
|
@@ -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)}
|