Search code examples
javascriptangularjssqlitewebassembly

Implement sqlite3.wasm Web Assembly in my Older AngularJS Solution


I need to implement a persistent Sqlite3 database in my older AngularJS solution. So far, I've managed to implement a transient Sqlite3 database, but have not successfully implemented the persistent database.

I have been successful at implementing a transient database by using this page: A Database in your Browser in sqlite3 Steps

I have followed the instructions at this page: SQLite Wasm in the browser backed by the Origin Private File System and get hung up with the import statement. I just get a Javascript console error that indicates it no longer recognizes the page. In lieu of the import statement, I have tried the following that I found at Loading and running WebAssembly code

    WebAssembly.instantiateStreaming(fetch("/js/lib/sqlite3/sqlite3.wasm"), someKindOfObject).then(
        (results) => {
            alert("Here we are");
            // Do something with the results!
        }
    );

However, I have no idea what I should use for someKindOfObject in the above snippet.

How do I correctly import/instantiate sqlite3.wasm in my AngularJS application so I can access a persistent database?


Solution

  •     const loadSQLiteWASM = async () => {
          const response = await fetch('path/to/sqlite3.wasm');
          const buffer = await response.arrayBuffer();
          const sqlite3 = await initSqlJs({ locateFile: () => 'path/to/sqlite3.wasm' });
          const db = new sqlite3.Database();
    db.run('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name demoTbl)');
    
        };
        
        loadSQLiteWASM();
    

    you can check out this that i have create an demo