UNPKG

relational-indexdb

Version:

This is an indexDB database relational encapsulation

487 lines (486 loc) 12.9 kB
class w { constructor(e, t, o) { this.version = 1, this.databaseName = e, this.version = t; const r = indexedDB.open(this.databaseName, t); r.onsuccess = (s) => { this.db = s.target.result; }, r.onupgradeneeded = (s) => { this.db = r.result, o.forEach((c) => { if (!this.db.objectStoreNames.contains(c.tableName)) { const i = this.db.createObjectStore(c.tableName, { keyPath: c.keyPath, autoIncrement: c.autoIncrement }); c.keyConfig.forEach((n) => { i.createIndex(n.name, n.keyPath, { unique: n.unique }); }); } }); }, r.onerror = (s) => { }; } static getInstance(e, t, o) { return w.instance || (w.instance = new w(e, t, o)), w.instance; } createTable2(e, t, o, r) { const s = () => { if (this.db) { const c = this.db.createObjectStore(e, { keyPath: t, autoIncrement: o }); for (const i in r) c.createIndex(i, i, { unique: !1 }); console.log(`Table '${e}' created successfully!`); } else setTimeout(s, 100); }; s(); } /** * 新增/创建表 * @param tableName {string} * @param version {number} * @param keyPath {string} * @param autoIncrement {boolean} * @param indexConfigs {Object} */ createTable(e, t, o, r, s) { const c = () => { if (this.db) { this.db.close(), this.db = null; const i = indexedDB.open(this.databaseName, t); i.onsuccess = (n) => { this.db = n.target.result; }, i.onupgradeneeded = (n) => { if (this.db = i.result, console.log("createTable", this.db), !this.db.objectStoreNames.contains(e)) { const a = this.db.createObjectStore(e, { keyPath: o, autoIncrement: !0 }); s.forEach((u) => { a.createIndex(u.name, u.keyPath, { unique: u.unique }); }); } }, i.onerror = (n) => { }; } else setTimeout(c, 100); }; c(); } /** * 添加记录到对象存储空间 * @param tableName * @param data{string| number} 主键 * @returns Promise<void> */ addRecord(e, t) { return new Promise((o, r) => { const s = () => { if (this.db) { const n = this.db.transaction([e], "readwrite").objectStore(e).add(t); n.onsuccess = () => { o(); }, n.onerror = (a) => { r(a.target.error); }; } else setTimeout(s, 100); }; s(); }); } /** * 查询指定ID的记录 * @param key 记录ID * @param tableName 表名 * @returns Promise<any> 返回查询结果 */ queryRecord(e, t) { return new Promise((o, r) => { const s = () => { if (this.db) { const n = this.db.transaction([e], "readonly").objectStore(e).get(t); n.onsuccess = () => { o(n.result); }, n.onerror = (a) => { r(a.target.error); }; } else setTimeout(s, 100); }; s(); }); } /** * 查询表所有数据 * @param tableName 表名 * @returns Promise<any> 返回查询结果 */ queryTableAll(e) { return new Promise((t, o) => { const r = () => { if (this.db) { const i = this.db.transaction([e], "readonly").objectStore(e).getAll(); i.onsuccess = () => { t(i.result); }, i.onerror = (n) => { o(n.target.error); }; } else setTimeout(r, 100); }; r(); }); } /** * 删除记录 * @param tableName 表名 * @param key 主键值 * @returns Promise<void> */ deleteRecord(e, t) { return new Promise((o, r) => { const s = () => { if (this.db) { const n = this.db.transaction([e], "readwrite").objectStore(e).delete(t); n.onsuccess = () => { o(); }, n.onerror = (a) => { r(a.target.error); }; } else setTimeout(s, 100); }; s(); }); } /** * 更新数据 * @param tableName 表名 * @param key 主键值 * @param newData 新数据对象 * @returns Promise<void> */ updateRecord(e, t, o) { return new Promise((r, s) => { const c = () => { if (this.db) { const n = this.db.transaction([e], "readwrite").objectStore(e), a = n.get(t); a.onsuccess = (u) => { const d = u.target.result; d && Object.keys(o).forEach((h) => { d[h] = o[h]; }); const b = n.put(d); b.onsuccess = () => { r(); }, b.onerror = (h) => { s(h.target.error); }; }, a.onerror = (u) => { s(u.target.error); }; } else setTimeout(c, 100); }; c(); }); } /** * 批量更新数据 * @param tableName 表名 * @param updatesToUpdate {keyPathValue:string,data:unknown}需要更新的记录对象数组 * @returns Promise<void> */ batchUpdateRecords(e, t) { return new Promise((o, r) => { const s = () => { if (this.db) { const i = this.db.transaction([e], "readwrite").objectStore(e); let n = 0; const a = () => { if (n < t.length) { const u = t[n], { keyPathValue: d, data: b } = u, h = i.get(d); h.onsuccess = (l) => { const j = l.target.result; j && Object.keys(b).forEach((g) => { j[g] = b[g]; }); const S = i.put(j); S.onsuccess = () => { n++, a(); }, S.onerror = (g) => { r(g.target.error); }; }, h.onerror = (l) => { r(l.target.error); }; } else o(); }; a(); } else setTimeout(s, 100); }; s(); }); } /** * 游标获取符合条件数据 * @param tableName 表名 * @param condition 需要更新的记录对象 * @returns Promise<unknown[]> */ async queryDataByCursor(e, t) { return new Promise((o, r) => { const s = () => { if (this.db) { const n = this.db.transaction([e], "readonly").objectStore(e).openCursor(), a = []; n.onsuccess = (u) => { const d = u.target.result; d ? (this.matchesCondition(d.value, t) && a.push(d.value), d.continue()) : o(a); }, n.onerror = (u) => { r(u.target.error); }; } else setTimeout(s, 100); }; s(); }); } async byCursor(e, t, o = () => { }) { return new Promise((r, s) => { const c = () => { if (this.db) { const a = this.db.transaction([e], "readwrite").objectStore(e).openCursor(), u = []; a.onsuccess = (d) => { const b = d.target.result; b ? (this.matchesCondition(b.value, t) && (o(b), u.push(b.value)), b.continue()) : r(u); }, a.onerror = (d) => { s(d.target.error); }; } else setTimeout(c, 100); }; c(); }); } /** * 游标获取符合条件数据 * @param tableName 表名 * @param condition 需要更新的记录对象 * @returns Promise<unknown[]> */ async queryDataByCursor(e, t, o) { return new Promise(async (r, s) => { r(await this.byCursor(e, t)); }); } /** * 游标更改符合条件数据 * @param tableName 表名 * @param condition 需要更新的记录对象 * @returns Promise<unknown[]> */ async updateDataByCursor(e, t, o) { return new Promise((r, s) => { this.byCursor(e, t, (c) => { c.update({ ...c.value, ...o }); }); }); } /** * 游标删除符合条件数据 * @param tableName 表名 * @param condition 需要更新的记录对象 * @returns Promise<unknown[]> */ async deleteDataByCursor(e, t) { return new Promise((o, r) => { this.byCursor(e, t, (s) => { s.delete(); }); }); } // 辅助方法,用于判断数据是否满足条件 matchesCondition(e, t) { for (const o in t) if (e[o] !== t[o]) return !1; return !0; } /** * 批量删除数据 * @param tableName 表名 * @param keysToDelete 需要删除的主键值数组 * @returns Promise<void> */ batchDeleteRecords(e, t) { return new Promise((o, r) => { const s = () => { if (this.db) { const c = this.db.transaction([e], "readwrite"), i = c.objectStore(e); t.forEach((n) => { const a = i.delete(n); a.onerror = (u) => { r(u.target.error); }; }), c.oncomplete = () => { o(); }; } else setTimeout(s, 100); }; s(); }); } /** * 删除表 * @param tableName 表名 * @returns Promise<void> */ deleteTable(e) { return new Promise((t, o) => { const r = () => { if (this.db) { const s = this.db.transaction([e], "readwrite"); this.db.deleteObjectStore(e), s.oncomplete = () => { t(); }; } else setTimeout(r, 100); }; r(); }); } /** * 范围查询记录 * @param tableName 表名 * @param indexName 索引名 * @param start 范围起始值 * @param end 范围结束值 * @returns Promise<any[]> */ queryRecordsInRange(e, t, o, r) { return new Promise((s, c) => { const i = () => { if (this.db) { const d = this.db.transaction([e], "readonly").objectStore(e).index(t).openCursor(IDBKeyRange.bound(o, r)), b = []; d.onsuccess = (h) => { const l = h.target.result; l ? (b.push(l.value), l.continue()) : s(b); }, d.onerror = (h) => { c(h.target.error); }; } else setTimeout(i, 100); }; i(); }); } /** * 范围删除记录 * @param tableName 表名 * @param indexName 索引名 * @param start 范围起始值 * @param end 范围结束值 * @returns Promise<void> */ deleteRecordsInRange(e, t, o, r) { return new Promise((s, c) => { const i = () => { if (this.db) { const d = this.db.transaction([e], "readwrite").objectStore(e).index(t).openCursor(IDBKeyRange.bound(o, r)); d.onsuccess = (b) => { const h = b.target.result; if (h) { const l = h.delete(); l.onsuccess = () => { h.continue(); }, l.onerror = (j) => { c(j); }; } else s(); }, d.onerror = (b) => { c(b.target.error); }; } else setTimeout(i, 100); }; i(); }); } /** * 清空表 * @param tableName 表名 * @returns Promise<void> */ clearTable(e) { return new Promise((t, o) => { const r = () => { if (this.db) { const i = this.db.transaction([e], "readwrite").objectStore(e).clear(); i.onsuccess = () => { t(); }, i.onerror = (n) => { o(n.target.error); }; } else setTimeout(r, 100); }; r(); }); } /** * 范围修改记录 * @param tableName 表名 * @param indexName 索引名 * @param start 范围起始值 * @param end 范围结束值 * @param newData 要更新的新数据 * @returns Promise<void> */ updateRecordsInRange(e, t, o, r, s) { return new Promise((c, i) => { const n = () => { if (this.db) { const b = this.db.transaction([e], "readwrite").objectStore(e).index(t).openCursor(IDBKeyRange.bound(o, r)); b.onsuccess = (h) => { const l = h.target.result; if (l) { const j = l.update(s); j.onsuccess = () => { l.continue(); }, j.onerror = (S) => { i(S); }; } else c(); }, b.onerror = (h) => { i(h.target.error); }; } else setTimeout(n, 100); }; n(); }); } /** * 删除数据库 * @returns Promise<void> */ deleteDatabase() { return new Promise((e, t) => { const o = indexedDB.deleteDatabase(this.db.name); o.onsuccess = () => { e(); }, o.onerror = (r) => { t(r.target.error); }; }); } } export { w as default };