mirror of
https://github.com/withastro/astro.git
synced 2025-01-20 22:12:38 -05:00
Create a runtime version of createRemoteDatabaseClient
This commit is contained in:
parent
aabb50afa9
commit
26a326adcc
4 changed files with 63 additions and 62 deletions
|
@ -31,8 +31,8 @@ import {
|
|||
type SQLiteColumnBuilderBase,
|
||||
} from 'drizzle-orm/sqlite-core';
|
||||
import { z } from 'zod';
|
||||
import { nanoid } from 'nanoid';
|
||||
import type { AstroIntegrationLogger } from 'astro';
|
||||
export { createRemoteDatabaseClient } from './utils-runtime.js';
|
||||
|
||||
export type SqliteDB = SqliteRemoteDatabase;
|
||||
export type { Table } from './types.js';
|
||||
|
@ -55,8 +55,6 @@ function checkIfModificationIsAllowed(collections: DBCollections, Table: SQLiteT
|
|||
}
|
||||
}
|
||||
|
||||
export { createRemoteDatabaseClient } from './utils.js';
|
||||
|
||||
export async function createLocalDatabaseClient({
|
||||
collections,
|
||||
dbUrl,
|
||||
|
|
57
packages/db/src/utils-runtime.ts
Normal file
57
packages/db/src/utils-runtime.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
import type { InStatement } from '@libsql/client';
|
||||
import { drizzle } from 'drizzle-orm/sqlite-proxy';
|
||||
import { z } from 'zod';
|
||||
|
||||
export function createRemoteDatabaseClient(appToken: string, remoteDbURL: string) {
|
||||
const url = new URL('./db/query/', remoteDbURL);
|
||||
|
||||
const db = drizzle(async (sql, parameters, method) => {
|
||||
const requestBody: InStatement = { sql, args: parameters };
|
||||
// eslint-disable-next-line no-console
|
||||
console.info(JSON.stringify(requestBody));
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${appToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
`Failed to execute query.\nQuery: ${sql}\nFull error: ${res.status} ${await res.text()}}`
|
||||
);
|
||||
}
|
||||
|
||||
const queryResultSchema = z.object({
|
||||
rows: z.array(z.unknown()),
|
||||
});
|
||||
let rows: unknown[];
|
||||
try {
|
||||
const json = await res.json();
|
||||
rows = queryResultSchema.parse(json).rows;
|
||||
} catch (e) {
|
||||
throw new Error(
|
||||
`Failed to execute query.\nQuery: ${sql}\nFull error: Unexpected JSON response. ${
|
||||
e instanceof Error ? e.message : String(e)
|
||||
}`
|
||||
);
|
||||
}
|
||||
|
||||
// Drizzle expects each row as an array of its values
|
||||
const rowValues: unknown[][] = [];
|
||||
|
||||
for (const row of rows) {
|
||||
if (row != null && typeof row === 'object') {
|
||||
rowValues.push(Object.values(row));
|
||||
}
|
||||
}
|
||||
|
||||
if (method === 'get') {
|
||||
return { rows: rowValues[0] };
|
||||
}
|
||||
|
||||
return { rows: rowValues };
|
||||
});
|
||||
return db;
|
||||
}
|
|
@ -1,10 +1,8 @@
|
|||
import type { InStatement } from '@libsql/client';
|
||||
import type { AstroConfig } from 'astro';
|
||||
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';
|
||||
import { drizzle } from 'drizzle-orm/sqlite-proxy';
|
||||
import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
|
||||
import { loadEnv } from 'vite';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { createRemoteDatabaseClient as runtimeCreateRemoteDatabaseClient } from './utils-runtime.js';
|
||||
|
||||
export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
|
||||
|
||||
export const STUDIO_ADMIN_TABLE = 'ReservedAstroStudioAdmin';
|
||||
|
@ -37,55 +35,5 @@ export function getRemoteDatabaseUrl(): string {
|
|||
}
|
||||
|
||||
export function createRemoteDatabaseClient(appToken: string) {
|
||||
const url = new URL('./db/query/', getRemoteDatabaseUrl());
|
||||
|
||||
const db = drizzle(async (sql, parameters, method) => {
|
||||
const requestBody: InStatement = { sql, args: parameters };
|
||||
// eslint-disable-next-line no-console
|
||||
console.info(JSON.stringify(requestBody));
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${appToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
`Failed to execute query.\nQuery: ${sql}\nFull error: ${res.status} ${await res.text()}}`
|
||||
);
|
||||
}
|
||||
|
||||
const queryResultSchema = z.object({
|
||||
rows: z.array(z.unknown()),
|
||||
});
|
||||
let rows: unknown[];
|
||||
try {
|
||||
const json = await res.json();
|
||||
rows = queryResultSchema.parse(json).rows;
|
||||
} catch (e) {
|
||||
throw new Error(
|
||||
`Failed to execute query.\nQuery: ${sql}\nFull error: Unexpected JSON response. ${
|
||||
e instanceof Error ? e.message : String(e)
|
||||
}`
|
||||
);
|
||||
}
|
||||
|
||||
// Drizzle expects each row as an array of its values
|
||||
const rowValues: unknown[][] = [];
|
||||
|
||||
for (const row of rows) {
|
||||
if (row != null && typeof row === 'object') {
|
||||
rowValues.push(Object.values(row));
|
||||
}
|
||||
}
|
||||
|
||||
if (method === 'get') {
|
||||
return { rows: rowValues[0] };
|
||||
}
|
||||
|
||||
return { rows: rowValues };
|
||||
});
|
||||
return db;
|
||||
return runtimeCreateRemoteDatabaseClient(appToken, getRemoteDatabaseUrl());
|
||||
}
|
||||
|
|
|
@ -68,9 +68,7 @@ export function getStudioVirtualModContents({
|
|||
return `
|
||||
import {collectionToTable, createRemoteDatabaseClient} from ${INTERNAL_MOD_IMPORT};
|
||||
|
||||
export const db = await createRemoteDatabaseClient(${JSON.stringify({
|
||||
appToken,
|
||||
})});
|
||||
export const db = await createRemoteDatabaseClient(${JSON.stringify(appToken)}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL);
|
||||
export * from ${DRIZZLE_MOD_IMPORT};
|
||||
|
||||
${getStringifiedCollectionExports(collections)}
|
||||
|
|
Loading…
Add table
Reference in a new issue