xref: /sqlite-3.40.0/ext/wasm/demo-jsstorage.js (revision 8948fbee)
1/*
2  2022-09-12
3
4  The author disclaims copyright to this source code.  In place of a
5  legal notice, here is a blessing:
6
7  *   May you do good and not evil.
8  *   May you find forgiveness for yourself and forgive others.
9  *   May you share freely, never taking more than you give.
10
11  ***********************************************************************
12
13  A basic test script for sqlite3.wasm with kvvfs support. This file
14  must be run in main JS thread and sqlite3.js must have been loaded
15  before it.
16*/
17'use strict';
18(function(){
19  const T = self.SqliteTestUtil;
20  const toss = function(...args){throw new Error(args.join(' '))};
21  const debug = console.debug.bind(console);
22  const eOutput = document.querySelector('#test-output');
23  const logC = console.log.bind(console)
24  const logE = function(domElement){
25    eOutput.append(domElement);
26  };
27  const logHtml = function(cssClass,...args){
28    const ln = document.createElement('div');
29    if(cssClass) ln.classList.add(cssClass);
30    ln.append(document.createTextNode(args.join(' ')));
31    logE(ln);
32  }
33  const log = function(...args){
34    logC(...args);
35    logHtml('',...args);
36  };
37  const warn = function(...args){
38    logHtml('warning',...args);
39  };
40  const error = function(...args){
41    logHtml('error',...args);
42  };
43
44  const runTests = function(sqlite3){
45    const capi = sqlite3.capi,
46          oo = sqlite3.oo1,
47          wasm = sqlite3.wasm;
48    log("Loaded module:",capi.sqlite3_libversion(), capi.sqlite3_sourceid());
49    T.assert( 0 !== capi.sqlite3_vfs_find(null) );
50    if(!capi.sqlite3_vfs_find('kvvfs')){
51      error("This build is not kvvfs-capable.");
52      return;
53    }
54
55    const dbStorage = 0 ? 'session' : 'local';
56    const theStore = 's'===dbStorage[0] ? sessionStorage : localStorage;
57    const db = new oo.JsStorageDb( dbStorage );
58    // Or: oo.DB(dbStorage, 'c', 'kvvfs')
59    log("db.storageSize():",db.storageSize());
60    document.querySelector('#btn-clear-storage').addEventListener('click',function(){
61      const sz = db.clearStorage();
62      log("kvvfs",db.filename+"Storage cleared:",sz,"entries.");
63    });
64    document.querySelector('#btn-clear-log').addEventListener('click',function(){
65      eOutput.innerText = '';
66    });
67    document.querySelector('#btn-init-db').addEventListener('click',function(){
68      try{
69        const saveSql = [];
70        db.exec({
71          sql: ["drop table if exists t;",
72                "create table if not exists t(a);",
73                "insert into t(a) values(?),(?),(?)"],
74          bind: [performance.now() >> 0,
75                 (performance.now() * 2) >> 0,
76                 (performance.now() / 2) >> 0],
77          saveSql
78        });
79        console.log("saveSql =",saveSql,theStore);
80        log("DB (re)initialized.");
81      }catch(e){
82        error(e.message);
83      }
84    });
85    const btnSelect = document.querySelector('#btn-select1');
86    btnSelect.addEventListener('click',function(){
87      log("DB rows:");
88      try{
89        db.exec({
90          sql: "select * from t order by a",
91          rowMode: 0,
92          callback: (v)=>log(v)
93        });
94      }catch(e){
95        error(e.message);
96      }
97    });
98    document.querySelector('#btn-storage-size').addEventListener('click',function(){
99      log("size.storageSize(",dbStorage,") says", db.storageSize(),
100          "bytes");
101    });
102    log("Storage backend:",db.filename);
103    if(0===db.selectValue('select count(*) from sqlite_master')){
104      log("DB is empty. Use the init button to populate it.");
105    }else{
106      log("DB contains data from a previous session. Use the Clear Ctorage button to delete it.");
107      btnSelect.click();
108    }
109  };
110
111  sqlite3InitModule(self.sqlite3TestModule).then((sqlite3)=>{
112    runTests(sqlite3);
113  });
114})();
115