1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
import type { InStatement } from '@libsql/client';
import { createClient } from '@libsql/client';
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
import { drizzle as drizzleLibsql } from 'drizzle-orm/libsql';
import { type SqliteRemoteDatabase, drizzle as drizzleProxy } from 'drizzle-orm/sqlite-proxy';
import { z } from 'zod';
import { safeFetch } from './utils.js';
const isWebContainer = !!process.versions?.webcontainer;
function applyTransactionNotSupported(db: SqliteRemoteDatabase) {
Object.assign(db, {
transaction() {
throw new Error(
'`db.transaction()` is not currently supported. We recommend `db.batch()` for automatic error rollbacks across multiple queries.'
);
},
});
}
export function createLocalDatabaseClient({ dbUrl }: { dbUrl: string }): LibSQLDatabase {
const url = isWebContainer ? 'file:content.db' : dbUrl;
const client = createClient({ url });
const db = drizzleLibsql(client);
applyTransactionNotSupported(db);
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) {
if (appToken == null) {
throw new Error(`Cannot create a remote client: missing app token.`);
}
const url = new URL('/db/query', remoteDbURL);
const db = drizzleProxy(
async (sql, parameters, method) => {
const requestBody: InStatement = { sql, args: parameters };
const res = await safeFetch(
url,
{
method: 'POST',
headers: {
Authorization: `Bearer ${appToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
},
(response) => {
throw new Error(
`Failed to execute query.\nQuery: ${sql}\nFull error: ${response.status} ${response.statusText}`
);
}
);
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)
}`
);
}
if (method === 'run') return remoteResult;
// 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] };
}
return { rows: rowValues };
},
async (queries) => {
const stmts: InStatement[] = queries.map(({ sql, params }) => ({ sql, args: params }));
const res = await safeFetch(
url,
{
method: 'POST',
headers: {
Authorization: `Bearer ${appToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(stmts),
},
(response) => {
throw new Error(
`Failed to execute batch queries.\nFull error: ${response.status} ${response.statusText}}`
);
}
);
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[][] = [];
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;
}
);
applyTransactionNotSupported(db);
return db;
}
|