Now Reading
SQLite Wasm within the browser backed by the Origin Personal File System

SQLite Wasm within the browser backed by the Origin Personal File System

2023-01-12 07:11:04

In our weblog put up Deprecating and removing Web SQL, we promised a alternative for Internet SQL primarily based on SQLite. The SQLite Wasm library with the Origin Personal File System persistence backend is our success of this promise.

About SQLite

SQLite is a well-liked, open-source, light-weight, embedded relational database administration system. Many builders use it to retailer knowledge in a structured, easy-to-use method. Due to its small dimension and low reminiscence necessities, SQLite is usually leveraged as a database engine in cellular units, desktop purposes, and net browsers.

One of many key options of SQLite is that it’s a serverless database, which implies that it doesn’t require a separate server course of to function. As a substitute, the database is saved in a single file on the person’s system, making it simple to combine into purposes.

SQLite logo.

SQLite primarily based on Internet Meeting

There are a selection of unofficial SQLite variations primarily based on Internet Meeting (Wasm), permitting it for use in net browsers, for instance, sql.js. The sqlite3 WASM/JS subproject is the primary effort that’s formally related to the SQLite project making Wasm builds of the library established family members of supported SQLite deliverables. The concrete targets of this challenge embody:

  • Binding a low-level sqlite3 API which is as near the C one as possible by way of utilization.
  • The next-level object-oriented API, extra akin to sql.js and Node.js-style implementations, that speaks on to the low-level API. This API should be used from the identical thread because the low-level API.
  • A Employee-based API which speaks to the earlier APIs through Employee messages. This one is meant to be used in the primary thread, with the lower-level APIs put in in a Employee thread, and speaking to them through Employee messages.
  • A Promise-based variant of the Employee API which totally hides the cross-thread communication points from the person.
  • Assist for persistent client-side storage utilizing accessible JavaScript APIs, together with the Origin Personal File System (OPFS).

Utilizing SQLite Wasm with the Origin Personal File System persistence backend

Downloading the library

Obtain the official Wasm construct from the SQLite homepage. Remember to obtain the appropriate construct. The outline wants to say sqlite3.wasm. After the obtain has completed, extract the archive.

The Origin Personal File System

The Origin Personal File System (OPFS, a part of the File System Access API) is augmented with a particular floor that brings very performant entry to knowledge. This new floor differs from current ones by providing in-place and unique write entry to a file’s content material. This alteration, together with the power to persistently learn unflushed modifications and the supply of a synchronous variant on devoted staff, considerably improves efficiency and unblocks new use circumstances.

As you possibly can think about, the final level of the challenge’s targets, Assist for persistent client-side storage utilizing accessible JavaScript APIs, comes with strict efficiency necessities relating to persisting knowledge to the database file. That is the place the Origin Personal File System, and, extra particularly, the createSyncAccessHandle() technique of FileSystemFileHandle objects comes into play. This technique returns a Promise which resolves to a FileSystemSyncAccessHandle object that can be utilized to synchronously learn from and write to a file. The synchronous nature of this technique brings efficiency benefits, however subsequently it is just usable inside devoted Web Workers for recordsdata inside the Origin Personal File System so the primary thread cannot be blocked.

Setting these headers is a required step for the Origin Personal File System persistence backend to work.

Amongst different recordsdata, the downloaded SQLite Wasm archive incorporates the sqlite3.js and sqlite3.wasm recordsdata, which make up the sqlite3 WASM/JS construct. The jswasm listing incorporates the core sqlite3 deliverables and the top-level listing incorporates demonstration and take a look at apps. Browsers won’t serve Wasm recordsdata from file:// URLs, so any apps you construct with this require an internet server and that server should embody the next headers in its response when serving the recordsdata:

The rationale for these headers is that SQLite Wasm is determined by SharedArrayBuffer, and setting these headers is a part of its security requirements.

Should you examine the visitors with DevTools, you must discover the next data:

The two headers mentioned above, Cross-Origin-Embedder-Policy and Cross-Origin-Opener-Policy, highlighted in Chrome DevTools.

Getting began code pattern

As talked about beforehand, SQLite Wasm with the Origin Personal File System persistence backend must run from a Employee context. So to make use of it, in the primary thread, you might want to create the employee and take heed to messages from it.

When utilizing SQLite Wasm in manufacturing, remember to additionally try its Worker1 Promiser API, which hides the employee logic behind an abstraction. The instance under makes use of a extra basic Employee setup for the sake of readability.

const logHtml = perform (cssClass, ...args) {
const ln = doc.createElement('div');
if (cssClass) {
ln.append(doc.createTextNode( a part of(' ')));

const employee = new Employee('employee.js?sqlite3.dir=jswasm');
employee.onmessage = perform ({ knowledge }) {
swap (knowledge.kind) {
case 'log':
logHtml(knowledge.payload.cssClass, ...knowledge.payload.args);
logHtml('error', 'Unhandled message:', knowledge.kind);

After that, within the employee thread, you possibly can then arrange the communication with the primary thread, initialize the Wasm module, and eventually begin working with SQLite and execute queries.

const logHtml = perform (cssClass, ...args) {
kind: 'log',
payload: { cssClass, args },

const log = (...args) => logHtml('', ...args);
const error = (...args) => logHtml('error', ...args);

const begin = perform (sqlite3) {
const capi = sqlite3.capi;
const oo = sqlite3.oo1;
log('sqlite3 model', capi.sqlite3_libversion(), capi.sqlite3_sourceid());
let db;
if (sqlite3.opfs) {
db = new sqlite3.opfs.OpfsDb('/mydb.sqlite3');
log('The OPFS is offered.');
} else {
db = new oo.DB('/mydb.sqlite3', 'ct');
log('The OPFS isn't accessible.');
log('transient db =', db.filename);

attempt {
log('Create a desk...');
db.exec('CREATE TABLE IF NOT EXISTS t(a,b)');
log('Insert some knowledge utilizing exec()...');
let i;
for (i = 20; i <= 25; ++i) {
sql: INSERT INTO t(a,b) VALUES (?,?)',
bind: [i, i * 2],
log("Question knowledge with exec() utilizing rowMode 'array'...");
rowMode: 'array',
callback: perform (row) {
log('row ', ++this.counter, '=', row);
}.bind({ counter: 0 }),
} lastly {

log('Loading and initializing sqlite3 module...');
let sqlite3Js = 'sqlite3.js';
const urlParams = new URL(self.location.href).searchParams;
if (urlParams.has('sqlite3.dir')) {
sqlite3Js = urlParams.get('sqlite3.dir') + '/' + sqlite3Js;

print: log,
printErr: error,
.then(perform (sqlite3) {
log('Achieved initializing. Working demo...');
attempt {
} catch (e) {
error('Exception:', e.message);

See Also


See the above code in motion within the demo. Remember to try the source code on Glitch. Word how the embedded model under doesn’t use the OPFS backend, however whenever you open the demo in a separate tab it does.

Debugging the Origin Personal File System

To debug SQLite Wasm’s Origin Personal File System output, use the OPFS Explorer Chrome extension.

OPFS Explorer in the Chrome Web Store.

After putting in the extension, open the Chrome DevTools, choose the OPFS Explorer tab, and also you’re then prepared to examine what SQLite Wasm writes to the Origin Personal File System.

OPFS Explorer Chrome extension showing the Origin Private File System structure of the demo app.

Should you click on on any of the recordsdata within the OPFS Explorer window in DevTools, it can save you it to the native disk. You possibly can then use an app like SQLite Viewer to examine the database, so you possibly can guarantee your self that SQLite Wasm truly works as promised.

SQLite Viewer app used to open a database file from the SQLite Wasm demo.

Getting assist and offering suggestions

SQLite Wasm is developed and maintained by the SQLite group. Get assist and supply suggestions by looking in and posting to the support forum. The complete documentation is offered on the SQLite web site.


Hero picture by Tobias Fischer on Unsplash.

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top