移除对 indexeddb 的额外封装
This commit is contained in:
parent
099e36e1b5
commit
03e19fbf58
@ -1,92 +0,0 @@
|
|||||||
export default class IndexedDB {
|
|
||||||
constructor(databaseName, databaseVersion) {
|
|
||||||
this.databaseName = databaseName
|
|
||||||
this.databaseVersion = databaseVersion
|
|
||||||
this.db = null
|
|
||||||
}
|
|
||||||
|
|
||||||
open(name) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const request = indexedDB.open(this.databaseName, this.databaseVersion)
|
|
||||||
request.onerror = (event) => {
|
|
||||||
reject(event.target.error)
|
|
||||||
}
|
|
||||||
request.onsuccess = (event) => {
|
|
||||||
this.db = event.target.result
|
|
||||||
resolve(this.db)
|
|
||||||
}
|
|
||||||
request.onupgradeneeded = (event) => {
|
|
||||||
const db = event.target.result
|
|
||||||
if (!db.objectStoreNames.contains(name)) {
|
|
||||||
db.createObjectStore(name, { keyPath: 'id' })
|
|
||||||
console.log('store created:', name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async store(name) {
|
|
||||||
if (!this.db) await this.open(name)
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const transaction = this.db.transaction([name], 'readwrite')
|
|
||||||
const objectStore = transaction.objectStore(name)
|
|
||||||
resolve(objectStore)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
getAll(name) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const transaction = this.db.transaction([name], 'readonly')
|
|
||||||
const objectStore = transaction.objectStore(name)
|
|
||||||
const request = objectStore.getAll()
|
|
||||||
request.onerror = (event) => {
|
|
||||||
reject(event.target.error)
|
|
||||||
}
|
|
||||||
request.onsuccess = (event) => {
|
|
||||||
resolve(event.target.result)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
add(name, data) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const transaction = this.db.transaction([name], 'readwrite')
|
|
||||||
const objectStore = transaction.objectStore(name)
|
|
||||||
const request = objectStore.add(data)
|
|
||||||
request.onerror = (event) => {
|
|
||||||
reject(event.target.error)
|
|
||||||
}
|
|
||||||
request.onsuccess = (event) => {
|
|
||||||
resolve(event.target.result)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
put(name, data) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const transaction = this.db.transaction([name], 'readwrite')
|
|
||||||
const objectStore = transaction.objectStore(name)
|
|
||||||
const request = objectStore.put(data)
|
|
||||||
request.onerror = (event) => {
|
|
||||||
reject(event.target.error)
|
|
||||||
}
|
|
||||||
request.onsuccess = (event) => {
|
|
||||||
resolve(event.target.result)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(name, id) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const transaction = this.db.transaction([name], 'readwrite')
|
|
||||||
const objectStore = transaction.objectStore(name)
|
|
||||||
const request = objectStore.delete(id)
|
|
||||||
request.onerror = (event) => {
|
|
||||||
reject(event.target.error)
|
|
||||||
}
|
|
||||||
request.onsuccess = (event) => {
|
|
||||||
resolve(event.target.result)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user