webrtc/public/database.js

110 lines
3.5 KiB
JavaScript
Raw Normal View History

2023-09-28 01:14:49 +08:00
// 使用示例:
2023-09-28 17:59:23 +08:00
// const db = new IndexedDB('myDatabase', 1, 'myStore')
// await db.open()
// await db.add({ id: 1, name: 'John' })
// const data = await db.get(1)
// console.log(data)
// await db.delete(1)
2023-09-28 01:14:49 +08:00
export default class IndexedDB {
constructor(databaseName, databaseVersion, storeName) {
2023-09-28 17:59:23 +08:00
this.databaseName = databaseName
this.databaseVersion = databaseVersion
this.storeName = storeName
this.db = null
2023-09-28 01:14:49 +08:00
}
2023-09-28 17:59:23 +08:00
2023-09-28 01:14:49 +08:00
open() {
2023-09-28 17:59:23 +08:00
console.log('open')
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(this.storeName)) {
db.createObjectStore(this.storeName, { keyPath: 'id' })
}
}
})
2023-09-28 01:14:49 +08:00
}
2023-09-28 17:59:23 +08:00
2023-09-28 01:14:49 +08:00
add(data) {
2023-09-28 17:59:23 +08:00
console.log('add', data)
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], 'readwrite')
const objectStore = transaction.objectStore(this.storeName)
// 判断是否已经存在
const request = objectStore.get(data.id)
request.onerror = (event) => {
reject(event.target.error)
}
request.onsuccess = (event) => {
if (event.target.result) return resolve(event.target.result)
const request = objectStore.add(data)
request.onerror = (event) => {
reject(event.target.error)
}
request.onsuccess = (event) => {
resolve(event.target.result)
}
}
})
2023-09-28 01:14:49 +08:00
}
2023-09-28 17:59:23 +08:00
2023-09-28 01:14:49 +08:00
get(id) {
2023-09-28 17:59:23 +08:00
console.log('get', id)
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], 'readonly')
const objectStore = transaction.objectStore(this.storeName)
const request = objectStore.get(id)
request.onerror = (event) => {
reject(event.target.error)
}
request.onsuccess = (event) => {
resolve(event.target.result)
}
})
2023-09-28 01:14:49 +08:00
}
2023-09-28 17:59:23 +08:00
2023-09-28 01:14:49 +08:00
getAll() {
2023-09-28 17:59:23 +08:00
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], 'readonly')
const objectStore = transaction.objectStore(this.storeName)
const request = objectStore.getAll()
request.onerror = (event) => {
reject(event.target.error)
}
request.onsuccess = (event) => {
resolve(event.target.result)
}
})
2023-09-28 01:14:49 +08:00
}
2023-09-28 17:59:23 +08:00
2023-09-28 01:14:49 +08:00
delete(id) {
2023-09-28 17:59:23 +08:00
return new Promise((resolve, reject) => {
const transaction = this.db.transaction([this.storeName], 'readwrite')
const objectStore = transaction.objectStore(this.storeName)
const request = objectStore.delete(id)
request.onerror = (event) => {
reject(event.target.error)
}
request.onsuccess = (event) => {
resolve(event.target.result)
}
})
2023-09-28 01:14:49 +08:00
}
2023-09-28 17:59:23 +08:00
}