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);
}