Last active
August 21, 2025 02:43
-
-
Save kuc-arc-f/b34e78dfa0dd01a2cd728ea676c30bda to your computer and use it in GitHub Desktop.
node.js + SQLite WASM 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
| { | |
| "type": "module", | |
| "scripts": {}, | |
| "dependencies": { | |
| "dotenv": "^17.2.1", | |
| "sql.js": "^1.13.0" | |
| } | |
| } |
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
| CREATE TABLE todos ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| title TEXT NOT NULL, | |
| created_at TEXT NOT NULL, | |
| updated_at TEXT NOT NULL | |
| ); | |
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 initSqlJs from "sql.js"; | |
| import fs from "fs"; | |
| (async () => { | |
| const SQL = await initSqlJs({ | |
| }); | |
| // 既存のDBファイルを読み込む | |
| let db; | |
| const filename = "test.sqlite"; | |
| if (fs.existsSync(filename)) { | |
| const fileBuffer = fs.readFileSync(filename); | |
| db = new SQL.Database(fileBuffer); | |
| } else { | |
| db = new SQL.Database(); | |
| } | |
| const now = new Date().toISOString(); | |
| // データ挿入 | |
| db.run(`INSERT INTO todos (title , created_at , updated_at) VALUES (? , ? , ?)` | |
| , ["t-1", now , now ] | |
| ); | |
| // データ取得 | |
| const stmt = db.prepare("SELECT * FROM todos"); | |
| while (stmt.step()) { | |
| console.log(stmt.getAsObject()); | |
| } | |
| stmt.free(); | |
| // メモリ上のDBを Uint8Array としてエクスポート | |
| const data = db.export(); | |
| // ファイルに書き込み | |
| fs.writeFileSync(filename, Buffer.from(data)); | |
| console.log("保存しました:", filename); | |
| })(); |
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 initSqlJs from "sql.js"; | |
| import fs from "fs"; | |
| (async () => { | |
| const SQL = await initSqlJs({ | |
| }); | |
| // 既存のDBファイルを読み込む | |
| let db; | |
| const filename = "test.sqlite"; | |
| if (fs.existsSync(filename)) { | |
| const fileBuffer = fs.readFileSync(filename); | |
| db = new SQL.Database(fileBuffer); | |
| } else { | |
| db = new SQL.Database(); | |
| } | |
| const now = new Date().toISOString(); | |
| // データ取得 | |
| const stmt = db.prepare("SELECT COUNT(*) FROM todos"); | |
| while (stmt.step()) { | |
| console.log(stmt.getAsObject()); | |
| } | |
| stmt.free(); | |
| // メモリ上のDBを Uint8Array としてエクスポート | |
| const data = db.export(); | |
| // ファイルに書き込み | |
| fs.writeFileSync(filename, Buffer.from(data)); | |
| console.log("保存しました:", filename); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment