GitHub user phiresky recently published an interesting blog post on Hosting SQLite databases on Github Pages.
Using SQLite – or in fact any kind of SQL database – in the browser? Why should one want to do this? Now, while it’s certainly true that in the context of web applications SQL databases and RDBMS generally are relegated to the realm of back-end servers, depending on the requirements, a database in a front-end application or a self-contained application that doesn’t even have or require back-end systems can be quite useful.
For example, such an approach of having a static (i.e., not dynamically generated by a server) front-end application provide a database lends itself to the development of progressive web apps (PWA), which ideally act like locally installed applications that don’t require connectivity to external resources such as a database server.
While there certainly are APIs like IndexedDB and frameworks such as Dexie building upon those APIs that provide database-like functionality in the browser (as used in my gdpr-transparency project), there’s something to be said about the simplicity and elegance that comes with SQL and its declarative approach.
Unfortunately, Web SQL Database – a native browser API for SQL – for all intents and purposes is abandoned and support probably will be discontinued by most larger browser vendors in the not-too-distant future.
Hence, having SQL as an additional option for working with data in the browser, could come in handy, especially if you’re used to working with relational databases and the declarative data access patterns SQL entails.
Although it involves some more details, in a nutshell the solution described in the blog post revolves around compiling SQLite (which itself is written in C) to WebAssembly – an LLVM compiler target for web browsers.
Libraries and applications made available in the WebAssembly binary format can be used in JavaScript web applications, even if they themselves haven’t been written in JavaScript. Not only does this enable high-performance, compiled application components in an otherwise interpreted JavaScript environment but it also allows for usage of a wider range of languages in browser-based applications.
The main source code for phiresky’s implementation of SQLite for JavaScript environment is available here.