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
import { delay } from "https://deno.land/x/delay@v0.2.0/mod.ts";
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo?v=26";
import { Statement, StatementInstance } from "https://esm.town/v/postpostscript/sqliteBuilder";
import { sqliteDump } from "https://esm.town/v/postpostscript/sqliteDump";
import { createSqlite } from "https://esm.town/v/postpostscript/sqliteWasm";
import type { InStatement } from "https://esm.town/v/std/sqlite";
import { type ResultSet } from "npm:@libsql/client";
import { Hono } from "npm:hono";
const dumped = {};
async function database() {
const dump = await sqliteDump(dumped);
const sqlite = createSqlite();
sqlite.batch(dump);
return sqlite;
}
export const sqlitePublic = {
execute,
batch,
};
async function execute(statement: InStatement): Promise<ResultSet> {
const res = await fetch(`${ENDPOINT}/execute`, {
method: "POST",
body: JSON.stringify({ statement }),
});
if (!res.ok) {
throw new Error(await res.text());
}
return res.json();
}
async function batch(statements: InStatement[]): Promise<ResultSet[]> {
const res = await fetch(`${ENDPOINT}/batch`, {
method: "POST",
body: JSON.stringify({ statements }),
});
if (!res.ok) {
throw new Error(await res.text());
}
return res.json();
}
export const { httpEndpoint: ENDPOINT } = extractValInfo(import.meta.url);
const app = new Hono();
app.post("/execute", async (c) => {
const { statement } = await c.req.json();
const sqlite = await database();
const res = await sqlite.execute(statement);
console.log(typeof res, res);
return c.json(res);
});
app.post("/batch", async (c) => {
const { statements } = await c.req.json();
const sqlite = await database();
return c.json(sqlite.batch(statements));
});
export default app.fetch;
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 { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo?v=26";
import { Statement } from "https://esm.town/v/postpostscript/sqliteBuilder";
import { sqliteDump } from "https://esm.town/v/postpostscript/sqliteDump";
import { createSqlite } from "https://esm.town/v/postpostscript/sqliteWasm";
import type { InStatement } from "https://esm.town/v/std/sqlite";
import { createServer } from "https://esm.town/v/vladimyr/sqliteServer";
import { toHonoHandler } from "https://esm.town/v/vladimyr/toHonoHandler";
import { type ResultSet } from "npm:@libsql/client";
import { Hono } from "npm:hono";
import ky from "npm:ky";
const dumped = {
tables: ["authIdExampleComments_comment"],
subset: {
authIdExampleComments_comment:
Statement`SELECT * FROM authIdExampleComments_comment WHERE username = "postpostscript"`,
},
};
const sqliteServer = createServer(async () => {
const dump = await sqliteDump(dumped);
const sqlite = createSqlite();
sqlite.batch(dump);
return sqlite;
});
const app = new Hono();
app.post("/execute", toHonoHandler(sqliteServer.handleExecute));
app.post("/batch", toHonoHandler(sqliteServer.handleBatch));
export default app.fetch;
export const { httpEndpoint: ENDPOINT } = extractValInfo(import.meta.url);
export const sqlitePublic = {
execute,
batch,
};
async function execute(statement: InStatement): Promise<ResultSet> {
return ky.post("execute", {
json: { statement },
prefixUrl: ENDPOINT,
}).json();
}
async function batch(statements: InStatement[]): Promise<ResultSet[]> {
return ky.post("batch", {
json: { statements },
prefixUrl: ENDPOINT,
}).json();
}
1
Next