SQLite 3.40 released with official support for Wasm – Web SQL reborn?

The SQLite team has released version 3.40.0 with official support for compiling to Wasm.

This will enable web developers to use SQL for local database queries, using the browser’s local storage or session storage, or preferably OPFS (Origin-Private FileSystem), though support for this is currently limited. “As of late 2022, only bleeding-edge versions of Chromium-derived browsers have the necessary APIs,” say SQLite’s authors.

Another issue is that SQLite’s Wasm support is still in beta, with the expectation that it will be finalized in the next release.

SQLite is an embedded open source SQL database engine; it does not directly compete with the likes of Oracle or SQL Server, but is massively popular, not least because it ships as part of macOS, Windows, Android and iOS.

This may feel like déjà vu for developers who recall Web SQL, an implementation of SQLite in the browser that was done in 2009 but abandoned as a standard in 2010. The W3C note on this states that: “This document was on the W3C Recommendation track but specification work has stopped. The specification reached an impasse: all interested implementers have used the same SQL backend (Sqlite), but we need multiple independent implementations to proceed along a standardization path.”

Despite this, Google Chrome continues to ship with Web SQL, though it was deprecated and removed for third-party contexts in Chromium 97, released in October 2021.

SQL is the standard for using relational data and including it in the browser API makes sense in the context of increasingly rich web applications. Google’s reluctance to abandon Web SQL can be seen in this document by developer advocate Thomas Steiner, which explains that the official replacement for Web SQL, such as IndexedDB, “show their strengths when it comes to key/value stores and structured data, but acknowledgedly also have weaknesses like the lack of a strong query language. People want SQL on the web for a reason.”

Despite the W3C’s statement 12 years ago, Web SQL has not been entirely removed from Chromium and Steiner said that “the final step will be to remove Web SQL completely in all contexts, but no date has been set.”

It turns out that the SQLite announcement is connected with Google’s enthusiasm for SQL in the browser. “We’re working with the SQLite community on a replacement for Web SQL based on SQLite implemented in WebAssembly (Wasm), which will be released in the near future. For developers looking for a drop-in replacement, we’re investigating if a shim script can be provided,” Steiner said.

Why was WEB SQL abandoned? SQLite was the problem as well as the solution. The engine is highly regarded for its reliability, performance, and small size, so Web SQL implementations did not just use the SQLite API, they were SQLite. This created a dependency on a specific product that was contrary to the requirements of a web standard. “It requires every single browser developer to accept SQLite as part of their platform.  This may not be possible for any number of reasons, not the least of which is it essentially means that every web browser is on the hook for potential security issues within SQLite,” said Vladimir Vukićević, a software engineer at Mozilla said in 2009.

The outcome was that Web SQL was deprecated, but that the demand from developers for SQL queries to local storage was left unmet. SQLite compiled to Wasm solves that, since it will be downloaded as part of an application rather than shipped with the browser.