nalgeon / sqlime

Online SQLite playground
http://sqlime.org
MIT License
899 stars 42 forks source link

Support larger >1GB sqlite databases #21

Closed jlarmstrongiv closed 1 year ago

jlarmstrongiv commented 1 year ago

ArrayBuffer limitations

Currently, SQLite databases are loaded using arraybuffers:

Arraybuffers have limited sizes:

String limitations

Or by loading text:

Strings have limited sizes:

Solutions

Streams API / Web Streams API

To handle larger files, it is possible to use Web Streams:

WebAssembly.Memory

A workaround to creating a larger array buffer is using new WebAssembly.Memory

However, I think using the Streams API is a better fit.

Other

I was looking at the implementation:

function loadDbFromArrayBuffer(buf) {
    const bytes = new Uint8Array(buf);
    const p = sqlite3.wasm.allocFromTypedArray(bytes);
    const db = new sqlite3.oo1.DB();
    sqlite3.capi.sqlite3_deserialize(
        db.pointer,
        "main",
        p,
        bytes.length,
        bytes.length,
        sqlite3.capi.SQLITE_DESERIALIZE_FREEONCLOSE
    );
    return db;
}

And I’m thinking that if OPFS VFS support is active, then we can stream the db file to the OPFS VFS, and then open the DB from the file we created.

Using OPFS requires cross-origin isolation (COOP and COEP), which can be added to GitHub pages with coi-serviceworker.

nalgeon commented 1 year ago

Thanks for the suggestion! This project is a playground for working with snippets of SQL code. It does not support large databases by design, and probably never will. At least not until I have a very compelling reason to spend a week of my time implementing that support.