mirror of
https://github.com/withastro/astro.git
synced 2025-01-27 22:19:04 -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,
|
type SQLiteColumnBuilderBase,
|
||||||
} from 'drizzle-orm/sqlite-core';
|
} from 'drizzle-orm/sqlite-core';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { nanoid } from 'nanoid';
|
|
||||||
import type { AstroIntegrationLogger } from 'astro';
|
import type { AstroIntegrationLogger } from 'astro';
|
||||||
|
export { createRemoteDatabaseClient } from './utils-runtime.js';
|
||||||
|
|
||||||
export type SqliteDB = SqliteRemoteDatabase;
|
export type SqliteDB = SqliteRemoteDatabase;
|
||||||
export type { Table } from './types.js';
|
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({
|
export async function createLocalDatabaseClient({
|
||||||
collections,
|
collections,
|
||||||
dbUrl,
|
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,9 +1,7 @@
|
||||||
import type { InStatement } from '@libsql/client';
|
|
||||||
import type { AstroConfig } from 'astro';
|
import type { AstroConfig } from 'astro';
|
||||||
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';
|
import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
|
||||||
import { drizzle } from 'drizzle-orm/sqlite-proxy';
|
|
||||||
import { loadEnv } from 'vite';
|
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 type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
|
||||||
|
|
||||||
|
@ -37,55 +35,5 @@ export function getRemoteDatabaseUrl(): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createRemoteDatabaseClient(appToken: string) {
|
export function createRemoteDatabaseClient(appToken: string) {
|
||||||
const url = new URL('./db/query/', getRemoteDatabaseUrl());
|
return runtimeCreateRemoteDatabaseClient(appToken, 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;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,9 +68,7 @@ export function getStudioVirtualModContents({
|
||||||
return `
|
return `
|
||||||
import {collectionToTable, createRemoteDatabaseClient} from ${INTERNAL_MOD_IMPORT};
|
import {collectionToTable, createRemoteDatabaseClient} from ${INTERNAL_MOD_IMPORT};
|
||||||
|
|
||||||
export const db = await createRemoteDatabaseClient(${JSON.stringify({
|
export const db = await createRemoteDatabaseClient(${JSON.stringify(appToken)}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL);
|
||||||
appToken,
|
|
||||||
})});
|
|
||||||
export * from ${DRIZZLE_MOD_IMPORT};
|
export * from ${DRIZZLE_MOD_IMPORT};
|
||||||
|
|
||||||
${getStringifiedCollectionExports(collections)}
|
${getStringifiedCollectionExports(collections)}
|
||||||
|
|
Loading…
Add table
Reference in a new issue