Last active
August 18, 2025 02:56
-
-
Save kuc-arc-f/669a4cc4659dbe39b6a52c4eb3047a13 to your computer and use it in GitHub Desktop.
pglite , example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { PGlite } from '@electric-sql/pglite' | |
| const db = new PGlite('./pgdata') | |
| await db.exec(` | |
| CREATE TABLE IF NOT EXISTS todo ( | |
| id SERIAL PRIMARY KEY, | |
| task TEXT, | |
| done BOOLEAN DEFAULT false | |
| ); | |
| INSERT INTO todo (task, done) VALUES ('Install PGlite from NPM', true); | |
| INSERT INTO todo (task, done) VALUES ('Load PGlite', true); | |
| INSERT INTO todo (task, done) VALUES ('Create a table', true); | |
| INSERT INTO todo (task, done) VALUES ('Insert some data', true); | |
| INSERT INTO todo (task) VALUES ('Update a task'); | |
| `) | |
| // SELECT | |
| const ret = await db.query(` | |
| SELECT * from todo; | |
| `) | |
| console.log(ret.rows) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { PGlite } from '@electric-sql/pglite' | |
| const db = new PGlite('./pgdata') | |
| // SELECT | |
| const ret = await db.query(` | |
| SELECT * from todo; | |
| `) | |
| console.log(ret.rows) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { PGlite } from '@electric-sql/pglite' | |
| const run = async function(){ | |
| try{ | |
| console.time('proc-time'); | |
| const db = new PGlite('./pgdata') | |
| let targetNum = 1; | |
| for (let step = 0; step < 1000; step++) { | |
| targetNum = step + 1; | |
| let setValue = `k:${targetNum}`; | |
| let sql = `INSERT INTO todo | |
| (task, done) VALUES ('${setValue}', true); | |
| `; | |
| await db.exec(sql); | |
| } | |
| console.timeEnd('proc-time'); | |
| }catch(e){ | |
| console.error(e); | |
| } | |
| } | |
| run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment