samwillis-pglite_http_preview_runtime.web.val.run
Readme

PGlite on Val Town - A whole Postgres in your val

All you need to do is first import the Deno xmlhttprequest polyfill and then PGlite:

import "https://deno.land/x/xhr@0.1.0/mod.ts"; import { PGlite } from "https://cdn.jsdelivr.net/npm/@electric-sql/pglite/dist/index.js";

Importing using esm.sh doesn't seem to work, see https://github.com/electric-sql/pglite/issues/110

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