|
| 1 | +export default class DbHandler { |
| 2 | + private idb: IDBFactory; |
| 3 | + private db!: IDBDatabase; |
| 4 | + |
| 5 | + public constructor() { |
| 6 | + if (!indexedDB) { |
| 7 | + throw new Error("Your browser doesn't support a stable version of IndexedDB. Josh is unable to run without one."); |
| 8 | + } |
| 9 | + |
| 10 | + this.idb = indexedDB; |
| 11 | + } |
| 12 | + |
| 13 | + public init() { |
| 14 | + const request = this.idb.open('josh'); |
| 15 | + |
| 16 | + return new Promise<void>((resolve, reject) => { |
| 17 | + request.onerror = reject; |
| 18 | + |
| 19 | + request.onupgradeneeded = () => { |
| 20 | + const db = request.result; |
| 21 | + |
| 22 | + if (!db.objectStoreNames.contains('store')) { |
| 23 | + db.createObjectStore('store', { keyPath: 'key' }); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + request.onsuccess = () => { |
| 28 | + this.db = request.result; |
| 29 | + resolve(); |
| 30 | + }; |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + public async set(key: string, value: unknown) { |
| 35 | + const all = this.open(); |
| 36 | + const doc = { |
| 37 | + key, |
| 38 | + value |
| 39 | + }; |
| 40 | + |
| 41 | + const request = all.put(doc); |
| 42 | + |
| 43 | + await this.handleEvents(request); |
| 44 | + } |
| 45 | + |
| 46 | + public async get(key: string) { |
| 47 | + const all = this.open(); |
| 48 | + const request = all.get(key); |
| 49 | + const result = await this.handleEvents(request); |
| 50 | + |
| 51 | + // @ts-ignore it exists f you TS |
| 52 | + return result?.value; |
| 53 | + } |
| 54 | + |
| 55 | + public async getAll() { |
| 56 | + const all = this.open(); |
| 57 | + const request = all.getAll(); |
| 58 | + const docs = await this.handleEvents(request); |
| 59 | + const final = {}; |
| 60 | + |
| 61 | + // @ts-ignore TS GO AWAY |
| 62 | + docs.forEach((x) => { |
| 63 | + // @ts-ignore TS GO AWAY |
| 64 | + final[x.key] = x.value; |
| 65 | + }); |
| 66 | + |
| 67 | + return final; |
| 68 | + } |
| 69 | + |
| 70 | + public async getKeys() { |
| 71 | + const all = this.open(); |
| 72 | + const request = all.getAllKeys(); |
| 73 | + |
| 74 | + return this.handleEvents(request); |
| 75 | + } |
| 76 | + |
| 77 | + public async count() { |
| 78 | + const all = this.open(); |
| 79 | + const request = all.count(); |
| 80 | + const data = await this.handleEvents(request); |
| 81 | + |
| 82 | + if (typeof data !== 'number') throw new Error('Something is amiss!!!'); |
| 83 | + |
| 84 | + return data; |
| 85 | + } |
| 86 | + |
| 87 | + public async delete(key: string) { |
| 88 | + const all = this.open(); |
| 89 | + const request = all.delete(key); |
| 90 | + |
| 91 | + return this.handleEvents(request); |
| 92 | + } |
| 93 | + |
| 94 | + public async clear() { |
| 95 | + const all = this.open(); |
| 96 | + const request = all.clear(); |
| 97 | + |
| 98 | + return this.handleEvents(request); |
| 99 | + } |
| 100 | + |
| 101 | + public async has(key: string) { |
| 102 | + return (await this.get(key)) !== undefined; |
| 103 | + } |
| 104 | + |
| 105 | + private handleEvents(request: IDBRequest) { |
| 106 | + return new Promise((res, rej) => { |
| 107 | + request.onsuccess = () => { |
| 108 | + res(request.result); |
| 109 | + }; |
| 110 | + |
| 111 | + request.onerror = () => { |
| 112 | + rej(new Error(request.error?.toString())); |
| 113 | + }; |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + private open() { |
| 118 | + const transaction = this.db.transaction('store', 'readwrite'); |
| 119 | + const all = transaction.objectStore('store'); |
| 120 | + |
| 121 | + return all; |
| 122 | + } |
| 123 | +} |
0 commit comments