BaseTable.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. const log$e = new Logger("db");
  2. class BaseTable {
  3. constructor(e, t) {
  4. E(this, "db");
  5. E(this, "isCreatingTable", !1);
  6. E(this, "hasCleared", !1);
  7. this.dbName = e,
  8. this.dbVersion = t
  9. }
  10. async clearDataBase(e) {
  11. return this.hasCleared || (e && (this.hasCleared = !0),
  12. !window.indexedDB.databases) ? Promise.resolve() : new Promise((t,r)=>{
  13. const n = window.indexedDB.deleteDatabase(this.dbName);
  14. n.onsuccess = ()=>{
  15. t()
  16. }
  17. ,
  18. n.onerror = r
  19. }
  20. )
  21. }
  22. tableName() {
  23. throw new Error("Derived class have to override 'tableName', and set a proper table name!")
  24. }
  25. keyPath() {
  26. throw new Error("Derived class have to override 'keyPath', and set a proper index name!")
  27. }
  28. index() {
  29. throw new Error("Derived class have to override 'index', and set a proper index name!")
  30. }
  31. async checkAndOpenDatabase() {
  32. return this.db ? Promise.resolve(this.db) : new Promise((e,t)=>{
  33. const n = setTimeout(()=>{
  34. log$e.info("wait db to open for", 200),
  35. this.db ? e(this.db) : e(this.checkAndOpenDatabase()),
  36. clearTimeout(n)
  37. }
  38. , 200);
  39. this.openDatabase(this.dbName, this.dbVersion || 1, ()=>{
  40. this.db && !this.isCreatingTable && e(this.db),
  41. log$e.info(`successCallback called, this.db: ${!!this.db}, this.isCreatingStore: ${this.isCreatingTable}`),
  42. clearTimeout(n)
  43. }
  44. , ()=>{
  45. t(new Error("Failed to open database!")),
  46. clearTimeout(n)
  47. }
  48. , ()=>{
  49. this.db && e(this.db),
  50. clearTimeout(n),
  51. log$e.info(`successCallback called, this.db: ${!!this.db}, this.isCreatingStore: ${this.isCreatingTable}`)
  52. }
  53. )
  54. }
  55. )
  56. }
  57. openDatabase(e, t, r, n, o) {
  58. if (this.isCreatingTable)
  59. return;
  60. this.isCreatingTable = !0,
  61. log$e.info(e, t);
  62. const a = window.indexedDB.open(e, t)
  63. , s = this.tableName();
  64. a.onsuccess = l=>{
  65. this.db = a.result,
  66. log$e.info(`IndexedDb ${e} is opened.`),
  67. this.db.objectStoreNames.contains(s) && (this.isCreatingTable = !1),
  68. r && r(l)
  69. }
  70. ,
  71. a.onerror = l=>{
  72. var u;
  73. log$e.error("Failed to open database", (u = l == null ? void 0 : l.srcElement) == null ? void 0 : u.error),
  74. this.isCreatingTable = !1,
  75. n && n(l),
  76. this.clearDataBase(!0)
  77. }
  78. ,
  79. a.onupgradeneeded = l=>{
  80. const u = l.target.result
  81. , c = this.index();
  82. log$e.info(`Creating table ${s}.`);
  83. let h = u.objectStoreNames.contains(s);
  84. if (h)
  85. h = u.transaction([s], "readwrite").objectStore(s);
  86. else {
  87. const f = this.keyPath();
  88. h = u.createObjectStore(s, {
  89. keyPath: f
  90. })
  91. }
  92. c.map(f=>{
  93. h.createIndex(f, f, {
  94. unique: !1
  95. })
  96. }
  97. ),
  98. this.isCreatingTable = !1,
  99. log$e.info(`Table ${s} opened`),
  100. o && o(l)
  101. }
  102. }
  103. async add(e) {
  104. const t = this.tableName()
  105. , o = (await this.checkAndOpenDatabase()).transaction([t], "readwrite").objectStore(t).add(e);
  106. return new Promise(function(a, s) {
  107. o.onsuccess = l=>{
  108. a(l)
  109. }
  110. ,
  111. o.onerror = l=>{
  112. var u;
  113. log$e.error((u = l.srcElement) == null ? void 0 : u.error),
  114. s(l)
  115. }
  116. }
  117. )
  118. }
  119. async put(e) {
  120. const t = this.tableName()
  121. , o = (await this.checkAndOpenDatabase()).transaction([t], "readwrite").objectStore(t).put(e);
  122. return new Promise(function(a, s) {
  123. o.onsuccess = l=>{
  124. a(l)
  125. }
  126. ,
  127. o.onerror = l=>{
  128. var u;
  129. log$e.error("db put error", (u = l.srcElement) == null ? void 0 : u.error),
  130. s(l)
  131. }
  132. }
  133. )
  134. }
  135. delete(e, t, r) {
  136. const n = this.tableName();
  137. this.checkAndOpenDatabase().then(o=>{
  138. const s = o.transaction([n], "readwrite").objectStore(n).delete(e);
  139. s.onsuccess = t,
  140. s.onerror = r
  141. }
  142. )
  143. }
  144. update() {
  145. this.checkAndOpenDatabase().then(e=>{}
  146. )
  147. }
  148. async getAllKeys() {
  149. const e = this.tableName()
  150. , t = await this.checkAndOpenDatabase();
  151. return new Promise((r,n)=>{
  152. const a = t.transaction([e], "readonly").objectStore(e).getAllKeys();
  153. a.onsuccess = s=>{
  154. r(s.target.result)
  155. }
  156. ,
  157. a.onerror = s=>{
  158. log$e.error("db getAllKeys error", s),
  159. n(s)
  160. }
  161. }
  162. )
  163. }
  164. async query(e, t) {
  165. const r = this.tableName()
  166. , n = await this.checkAndOpenDatabase();
  167. return new Promise((o,a)=>{
  168. const u = n.transaction([r], "readonly").objectStore(r).index(e).get(t);
  169. u.onsuccess = function(c) {
  170. var f;
  171. const h = (f = c == null ? void 0 : c.target) == null ? void 0 : f.result;
  172. o && o(h)
  173. }
  174. ,
  175. u.onerror = c=>{
  176. log$e.error("db query error", c),
  177. a(c)
  178. }
  179. }
  180. )
  181. }
  182. async sleep(e) {
  183. return new Promise(t=>{
  184. setTimeout(()=>{
  185. t("")
  186. }
  187. , e)
  188. }
  189. )
  190. }
  191. }