From dcc9edf8595b19d9d5ddab30a0641d78cc9c2329 Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 11 Apr 2023 18:01:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api.js | 19 +++++++++++++++ src/helper.js | 14 +++++++++++ src/index.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/method.js | 37 +++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 src/api.js create mode 100644 src/helper.js create mode 100644 src/method.js diff --git a/src/api.js b/src/api.js new file mode 100644 index 0000000..a26b96a --- /dev/null +++ b/src/api.js @@ -0,0 +1,19 @@ +/** + * indexdb API + * @author yutent + * @date 2020/08/31 15:46:20 + */ + +import Method from './method.js' + +export default class Api { + constructor(db) { + this.db = db + } + + table(name) { + var ta = this.db.transaction(name, 'readwrite') + var table = ta.objectStore(name) + return new Method(table) + } +} diff --git a/src/helper.js b/src/helper.js new file mode 100644 index 0000000..2bf9799 --- /dev/null +++ b/src/helper.js @@ -0,0 +1,14 @@ +/** + * indexdb辅助方法 + * @author yutent + * @date 2020/08/31 15:20:56 + */ + +export function defer() { + let obj = {} + obj.promise = new Promise((resolve, reject) => { + obj.resolve = resolve + obj.reject = reject + }) + return obj +} diff --git a/src/index.js b/src/index.js index e69de29..f91b15e 100644 --- a/src/index.js +++ b/src/index.js @@ -0,0 +1,66 @@ +/** + * {} + * @author yutent + * @date 2023/04/11 15:10:04 + */ + +import { defer, createTable } from './helper.js' +import Api from './api.js' + +export function createStore(name, version = 1) { + let req = window.indexedDB.open(name, version) + let _ = defer() + + req.addEventListener('upgradeneeded', ev => { + _.resolve(ev.target.result) + }) + req.addEventListener('error', ev => { + _.reject(ev.target.result.error) + }) + return _.promise +} + +export function deleteStore(name) { + let req = window.indexedDB.deleteDatabase(name) + let _ = defer() + + req.addEventListener('success', ev => { + _.resolve() + }) + req.addEventListener('error', ev => { + _.reject(ev.target.result.error) + }) + return _.promise +} + +export function createTable(db, { name, config }) { + var opt = {} + var table + if (config.primaryKey) { + opt.keyPath = config.primaryKey + } + if (config.autoIncrement) { + opt.autoIncrement = config.autoIncrement + } + + table = db.createObjectStore(name, opt) + + if (config.indexes && config.indexes.length) { + for (let it of config.indexes) { + table.createIndex(it + '_idx', it) + } + } +} + +export function getStore(name) { + let req = window.indexedDB.open(name) + let _ = defer() + + req.addEventListener('success', ev => { + _.resolve(ev.target.result) + }) + req.addEventListener('error', ev => { + _.reject(ev.target.result.error) + }) + return _.promise +} diff --git a/src/method.js b/src/method.js new file mode 100644 index 0000000..2f7c604 --- /dev/null +++ b/src/method.js @@ -0,0 +1,37 @@ +/** + * {} + * @author yutent + * @date 2023/04/11 15:11:31 + */ + +export default class Method { + constructor(table) { + this.table = table + } + + put(obj) { + this.table.put(obj) + } + + get(id) { + var res = this.table.get(id) + return new Promise(resolve => { + res.onsuccess = function (ev) { + resolve(this.result) + } + }) + } + + getAll() { + var res = this.table.getAll() + return new Promise(resolve => { + res.onsuccess = function (ev) { + resolve(this.result) + } + }) + } + + delete(id) { + this.table.delete(id) + } +}