Skip to content

Commit 19a80d3

Browse files
authored
Merge pull request #48 from sqlite/node
Add support for Node.js without persistence
2 parents 003ed2e + 7716352 commit 19a80d3

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

demo/node.mjs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import sqlite3InitModule from '../node.mjs';
2+
3+
const log = (...args) => console.log(...args);
4+
const error = (...args) => console.error(...args);
5+
6+
const start = function (sqlite3) {
7+
log('Running SQLite3 version', sqlite3.version.libVersion);
8+
9+
const db = new sqlite3.oo1.DB('./local', 'cw');
10+
11+
try {
12+
log('Creating a table...');
13+
db.exec('CREATE TABLE IF NOT EXISTS t(a,b)');
14+
log('Insert some data using exec()...');
15+
for (let i = 20; i <= 25; ++i) {
16+
db.exec({
17+
sql: 'INSERT INTO t(a,b) VALUES (?,?)',
18+
bind: [i, i * 2],
19+
});
20+
}
21+
log('Query data with exec()...');
22+
db.exec({
23+
sql: 'SELECT a FROM t ORDER BY a LIMIT 3',
24+
callback: (row) => {
25+
log(row);
26+
},
27+
});
28+
} finally {
29+
db.close();
30+
}
31+
};
32+
33+
log('Loading and initializing SQLite3 module...');
34+
sqlite3InitModule({
35+
print: log,
36+
printErr: error,
37+
}).then((sqlite3) => {
38+
log('Done initializing. Running demo...');
39+
try {
40+
start(sqlite3);
41+
} catch (err) {
42+
error(err.name, err.message);
43+
}
44+
});

node.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { default as sqlite3InitModule } from './sqlite-wasm/jswasm/sqlite3-node.mjs';
2+
3+
export default sqlite3InitModule;

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
"origin-private-file-system"
1515
],
1616
"main": "index.mjs",
17+
"node": "node.mjs",
1718
"type": "module",
1819
"files": [
1920
"index.d.ts",
2021
"index.mjs",
22+
"node.mjs",
2123
"sqlite-wasm/"
2224
],
2325
"types": "index.d.ts",
2426
"exports": {
2527
".": {
2628
"types": "./index.d.ts",
27-
"node": "./index.mjs",
29+
"node": "./node.mjs",
2830
"import": "./index.mjs",
2931
"main": "./index.mjs",
3032
"browser": "./index.mjs"
@@ -41,6 +43,7 @@
4143
"clean": "shx rm -rf sqlite-wasm",
4244
"build": "npm run clean && node bin/index.js",
4345
"start": "npx http-server --coop",
46+
"start:node": "cd demo && node node.mjs",
4447
"fix": "npx prettier . --write",
4548
"prepublishOnly": "npm run build && npm run fix && npm run publint && npm run check-types",
4649
"deploy": "npm run prepublishOnly && git add . && git commit -am 'New release' && git push && npm publish --access public"

0 commit comments

Comments
 (0)