Skip to content

Add support for Node.js without persistence #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 26, 2025
44 changes: 44 additions & 0 deletions demo/node.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import sqlite3InitModule from '../node.mjs';

const log = (...args) => console.log(...args);
const error = (...args) => console.error(...args);

const start = function (sqlite3) {
log('Running SQLite3 version', sqlite3.version.libVersion);

const db = new sqlite3.oo1.DB('./local', 'cw');

try {
log('Creating a table...');
db.exec('CREATE TABLE IF NOT EXISTS t(a,b)');
log('Insert some data using exec()...');
for (let i = 20; i <= 25; ++i) {
db.exec({
sql: 'INSERT INTO t(a,b) VALUES (?,?)',
bind: [i, i * 2],
});
}
log('Query data with exec()...');
db.exec({
sql: 'SELECT a FROM t ORDER BY a LIMIT 3',
callback: (row) => {
log(row);
},
});
} finally {
db.close();
}
};

log('Loading and initializing SQLite3 module...');
sqlite3InitModule({
print: log,
printErr: error,
}).then((sqlite3) => {
log('Done initializing. Running demo...');
try {
start(sqlite3);
} catch (err) {
error(err.name, err.message);
}
});
3 changes: 3 additions & 0 deletions node.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { default as sqlite3InitModule } from './sqlite-wasm/jswasm/sqlite3-node.mjs';

export default sqlite3InitModule;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@
"origin-private-file-system"
],
"main": "index.mjs",
"node": "node.mjs",
"type": "module",
"files": [
"index.d.ts",
"index.mjs",
"node.mjs",
"sqlite-wasm/"
],
"types": "index.d.ts",
"exports": {
".": {
"types": "./index.d.ts",
"node": "./index.mjs",
"node": "./node.mjs",
"import": "./index.mjs",
"main": "./index.mjs",
"browser": "./index.mjs"
Expand All @@ -41,6 +43,7 @@
"clean": "shx rm -rf sqlite-wasm",
"build": "npm run clean && node bin/index.js",
"start": "npx http-server --coop",
"start:node": "cd demo && node node.mjs",
"fix": "npx prettier . --write",
"prepublishOnly": "npm run build && npm run fix && npm run publint && npm run check-types",
"deploy": "npm run prepublishOnly && git add . && git commit -am 'New release' && git push && npm publish --access public"
Expand Down