gitmyhub

sql.js-httpvfs

TypeScript ★ 3.7k updated 2y ago

Hosting read-only SQLite databases on static file hosters like Github Pages

A TypeScript library that lets you host a SQLite database as a static file and run SQL queries on it from the browser by fetching only the needed chunks over HTTP.

TypeScriptSQLiteWebAssemblyJavaScriptsetup: moderatecomplexity 3/5

sql.js-httpvfs is a TypeScript library that lets you host a SQLite database as a static file and query it directly from a web browser, without downloading the entire database file first. SQLite is a common format for storing structured data in a single file. Normally, running queries against it in a browser would require either a server to answer queries or downloading the whole file upfront. This library avoids both by using HTTP Range requests, a standard way of asking a web server to send only a specific portion of a file.

When a browser runs a query, the library figures out which parts of the database file are needed to answer it, fetches only those chunks over the network, and runs the query locally using SQLite compiled to run in the browser. For queries that hit good database indexes, this can mean fetching just a few kilobytes rather than the whole file. The library includes logic to speed up fetching when it detects sequential reads.

The practical use case is publishing a read-only dataset, such as a collection of records or statistics, on a free static hosting service like GitHub Pages, without needing a backend server or database. A developer prepares the SQLite file, uploads it, and then queries it from JavaScript in the browser. The README includes guidance on optimizing the database before hosting, covering index structure, page size, and splitting large files into chunks.

The library works inside a Web Worker and requires WebAssembly support in the browser. The author notes it was built primarily for small personal projects and demonstrations, and that the virtual file system layer has no automated tests. For more production-oriented needs, the README points to related projects that take different approaches to in-browser SQLite.

The project is available as an npm package.

Where it fits