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
import { DB } from "https://deno.land/x/sqlite/mod.ts";
import { type InStatement, sqlite as sqliteAPI } from "https://esm.town/v/std/sqlite";
import { type ResultSet } from "npm:@libsql/client";
export function createSqlite() {
const db = new DB();
function execute(statement: InStatement): Omit<ResultSet, "columnTypes" | "lastInsertRowid"> {
const stmt = typeof statement === "string"
? {
sql: statement,
args: [],
}
: statement;
const lastChanges = db.totalChanges;
try {
const query = db.prepareQuery(stmt.sql);
const rows = query.all(stmt.args);
const res = {
rows,
rowsAffected: db.totalChanges - lastChanges,
columns: query.columns().map(({ name }) => name),
};
return {
...res,
toJSON() {
return res;
},
};
} catch (e) {
console.log("sqliteWasm execute errored:", stmt.sql);
throw e;
}
}
return {
db,
execute,
batch(statements: InStatement[]) {
return statements.map(execute);
},
close() {
return db.close();
},
[Symbol.dispose]: () => db.close(),
};
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v149
April 24, 2024