demo-jsstorage.js 3.7 KB

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