Public
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
Code
HTTP
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 counter FROM test where id = 1")).rows[0].counter,
);
}
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!
maxm-pglitecounter.web.val.run
Updated: August 6, 2024