-
Notifications
You must be signed in to change notification settings - Fork 68
Description
(Note: This is mainly in the interest of this project being more widely known and used, since it's easy to create my own wrappers and stuff now that I know what's going on. I just want the work done by the contributors here to be as valued as it should be. Thanks for all your efforts on this!)
Here's an example from the readme for initializing this lib:
import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
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('/mydb.sqlite3', 'ct');
// Your SQLite code here.
};
log('Loading and initializing SQLite3 module...');
sqlite3InitModule({
print: log,
printErr: error,
}).then((sqlite3) => {
try {
log('Done initializing. Running demo...');
start(sqlite3);
} catch (err) {
error(err.name, err.message);
}
});
This looks a bit scary and unusual in terms of code structure. I think this code example should be restructured to something like this:
import init from '@sqlite.org/sqlite-wasm';
const sqlite3 = await init({ print: console.log, printErr: console.error });
console.log('Running SQLite3 version', sqlite3.version.libVersion);
let db = new sqlite3.oo1.DB('/mydb.sqlite3', 'ct');
// Your SQLite code here.
And it'd also be worth mentioning after that code block that this import can be used directly in the browser to get up and running quickly:
import init from "https://cdn.jsdelivr.net/npm/@sqlite.org/sqlite-wasm@3.45.1-build1/index.mjs";
The PGlite repo is a great analogous example of how to create a developer-friendly readme, and it also highlights another point that I think is worth considering to make the sqlite team's work more broadly used:
import { PGlite } from "@electric-sql/pglite"
const db = new PGlite()
await db.query("select 'Hello world' as message;")
// -> [ { message: "Hello world" } ]
Is there anything fundamentally stopping this repo from having a wrapper that's similarly straightforward? The print/printErr can use console.log/error by default, and a wrapper can me made such that the first async interaction with the DB first waits for the DB to finish init (which it started doing during the sync constructor call).