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
import "https://deno.land/x/xhr@0.1.0/mod.ts";
import { PGlite } from "https://cdn.jsdelivr.net/npm/@electric-sql/pglite@0.2.2/dist/index.js";
let pg;
async function init() {
if (pg) return;
console.log(1);
pg = await PGlite.create();
console.log(2);
console.log(3);
await pg.query(`
CREATE TABLE IF NOT EXISTS test (
id SERIAL PRIMARY KEY,
test TEXT,
counter INTEGER
);
`);
console.log(4);
await pg.query(`
INSERT INTO test (id, test, counter)
VALUES (1, 'valtown', 0);
`);
}
export default async function(req: Request): Promise<Response> {
console.log(0);
await init();
console.log(6);
await pg.query(`
UPDATE test
SET counter = counter + 1
WHERE id = 1;
`);
console.log(7);
const data = [
await pg.query("SELECT version()"),
await pg.query("SELECT now()"),
await pg.query("SELECT * FROM test"),
];
console.log(8);
return Response.json(data);
}