SQLite developers are advancing a project to enable the compilation of the library into WebAssembly intermediate code, capable of running in a web browser and suitable for working with databases from web applications in JavaScript. The code for WebAssembly support has been added to the main project repository. Unlike the WebSQL API, which is based on SQLite, WASM SQLite is completely isolated from the browser and does not affect its security (Google decided to drop support for WebSQL in Chrome after several vulnerabilities in SQLite could be exploited through WebSQL to attack the browser).
The aim of the project is to provide a functional JavaScript wrapper that is identical in functionality to the SQLite API. Web developers are offered a high-level object-oriented interface for working with data styled similarly to sql.js or Node.js, a wrapper over the low-level C API, and an API based on the Web Worker mechanism, allowing the creation of asynchronous handlers that run in separate threads. To hide the intricacies of thread management over the Web Worker-based API, a programming interface based on the Promise mechanism is also being developed.
Data that web applications save in the WASM version of SQLite can be localized within the current session (lost after page reloads) or stored on the client-side (persisted between sessions). For permanent storage, backends have been prepared for placing data in the local file system using OPFS (Origin-Private FileSystem, an extension to the File System Access API, currently available only in WebKit and Chromium-based browsers) and in local browser storage based on the window.localStorage and window.sessionStorage APIs. When using localStorage/sessionStorage, data is reflected in the corresponding storage in key/value format, and when using OPFS, there are two options: simulating a virtual file system using WASMFS and a separate implementation of sqlite3_vfs that offers a SQLite VFS layer based on OPFS.
To build SQLite in a WASM representation, the Emscripten compiler is utilized (simply compile the extension ext/wasm: './configure --enable-all; make sqlite3.c; cd ext/wasm; make'). The output consists of sqlite3.js and sqlite3.wasm files, which can be included in your JavaScript project (example HTML and JavaScript).
Source: opennet.ru
