Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batch support #10361

Merged
merged 7 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/weak-weeks-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": minor
---

Add support for batch queries with `db.batch()`. This includes an internal bump to Drizzle v0.29.
2 changes: 1 addition & 1 deletion packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"@libsql/client": "^0.4.3",
"async-listen": "^3.0.1",
"deep-diff": "^1.0.2",
"drizzle-orm": "^0.28.6",
"drizzle-orm": "^0.29.5",
"kleur": "^4.1.5",
"nanoid": "^5.0.1",
"open": "^10.0.3",
Expand Down
142 changes: 99 additions & 43 deletions packages/db/src/runtime/db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,114 @@ export function createLocalDatabaseClient({ dbUrl }: { dbUrl: string }): LibSQLD
return db;
}

const remoteResultSchema = z.object({
columns: z.array(z.string()),
columnTypes: z.array(z.string()),
rows: z.array(z.array(z.unknown())),
rowsAffected: z.number(),
lastInsertRowid: z.unknown().optional(),
});

export function createRemoteDatabaseClient(appToken: string, remoteDbURL: string) {
const url = new URL('/db/query', remoteDbURL);

const db = drizzleProxy(async (sql, parameters, method) => {
const requestBody: InStatement = { sql, args: parameters };
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 db = drizzleProxy(
async (sql, parameters, method) => {
const requestBody: InStatement = { sql, args: parameters };
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)
}`
);
}
let remoteResult: z.infer<typeof remoteResultSchema>;
try {
const json = await res.json();
remoteResult = remoteResultSchema.parse(json);
} 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[][] = [];
if (method === 'run') return remoteResult;

for (const row of rows) {
if (row != null && typeof row === 'object') {
rowValues.push(Object.values(row));
// Drizzle expects each row as an array of its values
const rowValues: unknown[][] = [];

for (const row of remoteResult.rows) {
if (row != null && typeof row === 'object') {
rowValues.push(Object.values(row));
}
}
}

if (method === 'get') {
return { rows: rowValues[0] };
}
if (method === 'get') {
return { rows: rowValues[0] };
}

return { rows: rowValues };
});
return { rows: rowValues };
},
async (queries) => {
const stmts: InStatement[] = queries.map(({ sql, params }) => ({ sql, args: params }));
const res = await fetch(url, {
method: 'POST',
headers: {
Authorization: `Bearer ${appToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(stmts),
});
if (!res.ok) {
throw new Error(
`Failed to execute batch queries.\nFull error: ${res.status} ${await res.text()}}`
);
}

let remoteResults: z.infer<typeof remoteResultSchema>[];
try {
const json = await res.json();
remoteResults = z.array(remoteResultSchema).parse(json);
} catch (e) {
throw new Error(
`Failed to execute batch queries.\nFull error: Unexpected JSON response. ${
e instanceof Error ? e.message : String(e)
}`
);
}
let results: any[] = [];
for (const [idx, rawResult] of remoteResults.entries()) {
if (queries[idx]?.method === 'run') {
results.push(rawResult);
continue;
}

// Drizzle expects each row as an array of its values
const rowValues: unknown[][] = [];

(db as any).batch = (_drizzleQueries: Array<Promise<unknown>>) => {
throw new Error('db.batch() is not currently supported.');
};
for (const row of rawResult.rows) {
if (row != null && typeof row === 'object') {
rowValues.push(Object.values(row));
}
}

if (queries[idx]?.method === 'get') {
results.push({ rows: rowValues[0] });
}

results.push({ rows: rowValues });
}
return results;
}
);
return db;
}
31 changes: 16 additions & 15 deletions packages/db/test/fixtures/basics/db/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ import { asDrizzleTable } from '@astrojs/db/utils';
import { Themes as ThemesConfig } from './theme';
import { Author, db } from 'astro:db';

const Themes = asDrizzleTable('Themes', ThemesConfig);
export default async function () {
const Themes = asDrizzleTable('Themes', ThemesConfig);

await db
.insert(Themes)
.values([{ name: 'dracula' }, { name: 'monokai', added: new Date() }])
.returning({ name: Themes.name });
await db
.insert(Author)
.values([
{ name: 'Ben' },
{ name: 'Nate' },
{ name: 'Erika' },
{ name: 'Bjorn' },
{ name: 'Sarah' },
]);
await db.batch([
db
.insert(Themes)
.values([{ name: 'dracula' }, { name: 'monokai', added: new Date() }])
.returning({ name: Themes.name }),
db
.insert(Author)
.values([
{ name: 'Ben' },
{ name: 'Nate' },
{ name: 'Erika' },
{ name: 'Bjorn' },
{ name: 'Sarah' },
]),
]);
}
17 changes: 13 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading