Skip to content
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

Add support for Node.js #48

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
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;
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +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": "./node.mjs",
"import": "./index.mjs",
"main": "./index.mjs",
"browser": "./index.mjs"
Expand All @@ -38,6 +41,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",
"prepare": "npm run build && npm run fix && npm run publint",
"deploy": "npm run prepare && git add . && git commit -am 'New release' && git push && npm publish --access public"
Expand Down