使用 idb-keyval
This commit is contained in:
parent
6f267109a4
commit
5c00514610
18
demo.html
18
demo.html
@ -8,7 +8,23 @@
|
||||
</head>
|
||||
<body>
|
||||
<script type="module">
|
||||
import './src/store.js'
|
||||
import { musicStore, chatStore } from './src/store.js'
|
||||
|
||||
console.log(musicStore.name)
|
||||
console.log(chatStore.name)
|
||||
|
||||
// 检查过期的消息自动销毁
|
||||
// 查询消息列表
|
||||
|
||||
const element = document.createElement('div')
|
||||
element.innerHTML = `
|
||||
<h1>DEMO</h1>
|
||||
<h2>音乐</h2>
|
||||
<div id="music">${musicStore.name}</div>
|
||||
<h2>聊天</h2>
|
||||
<div id="chat">${chatStore.name}</div>
|
||||
`
|
||||
document.body.appendChild(element)
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -22,7 +22,7 @@
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"express-ws": "^5.0.2",
|
||||
"idb": "^7.1.1",
|
||||
"idb-keyval": "^6.2.1",
|
||||
"node-turn": "^0.0.6"
|
||||
}
|
||||
}
|
||||
|
85
src/store.js
85
src/store.js
@ -1,53 +1,70 @@
|
||||
// 使用封装为支持异步的 IndexedDB
|
||||
import { openDB, deleteDB, wrap, unwrap } from 'idb'
|
||||
const storenames = ['music', 'chat', 'user']
|
||||
const database = await openDB('database', 1, {
|
||||
upgrade(db) {
|
||||
storenames.forEach(name => db.createObjectStore(name, { keyPath: 'id' }))
|
||||
console.log('store created:', storenames)
|
||||
}
|
||||
})
|
||||
import { get, set, del, update, createStore } from 'idb-keyval'
|
||||
|
||||
export class Store {
|
||||
constructor(data) {
|
||||
this.name = data.name
|
||||
this.info = data.info
|
||||
this.store = data.store
|
||||
|
||||
// 检查过期的数据
|
||||
this.checkExpired()
|
||||
}
|
||||
|
||||
create(data) {
|
||||
async checkExpired() {
|
||||
const keys = await this.store.keys()
|
||||
const now = Date.now()
|
||||
for (const key of keys) {
|
||||
const item = await get(key, this.store)
|
||||
if (item && item.expiresAt && item.expiresAt < now) {
|
||||
await this.store.delete(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async create(data) {
|
||||
const id = window.crypto.randomUUID()
|
||||
return { id, ...data }
|
||||
const createdAt = Date.now()
|
||||
const updatedAt = createdAt
|
||||
const lastUsedAt = createdAt
|
||||
const item = { id, createdAt, updatedAt, lastUsedAt, ...data }
|
||||
await set(id, item, this.store)
|
||||
return item
|
||||
}
|
||||
|
||||
delete(id) {}
|
||||
async delete(id) {
|
||||
return await this.store.delete(id)
|
||||
}
|
||||
|
||||
query() {}
|
||||
async query(query) {
|
||||
}
|
||||
|
||||
async get(id) {
|
||||
return await get(id, this.store)
|
||||
}
|
||||
|
||||
async update(id, data) {
|
||||
const item = await this.get(id)
|
||||
if (!item) {
|
||||
return null
|
||||
}
|
||||
const updatedAt = Date.now()
|
||||
const lastUsedAt = updatedAt
|
||||
const item2 = { ...item, updatedAt, lastUsedAt, ...data }
|
||||
await set(id, item2, this.store)
|
||||
return item2
|
||||
}
|
||||
}
|
||||
|
||||
export const musicStore = new Store({
|
||||
name: '音乐(资源类型)',
|
||||
info: '音乐(类型说明)',
|
||||
存储时间: 1000 * 60 * 60 * 24 * 7, // 7天不使用的资源将被删除
|
||||
list: [{
|
||||
id: 'uuid',
|
||||
name: 'Dear big sisterremix ver.mp3',
|
||||
arrayBuffer: 'ArrayBuffer',
|
||||
createdAt: 1580000000000, // 创建时间
|
||||
updatedAt: 1580000000000, // 更新时间
|
||||
lastUsedAt: 1580000000000, // 最后使用时间
|
||||
}]
|
||||
store: createStore('db-music', 'store-music')
|
||||
})
|
||||
|
||||
export const chatStore = new Store({
|
||||
name: '聊天(资源类型)',
|
||||
info: '聊天(类型说明)',
|
||||
存储时间: 1000 * 60 * 60 * 24 * 7, // 7天不使用的资源将被删除
|
||||
list: [{
|
||||
id: 'uuid',
|
||||
name: 'Dear big sisterremix ver.mp3',
|
||||
arrayBuffer: 'ArrayBuffer',
|
||||
createdAt: 1580000000000, // 创建时间
|
||||
updatedAt: 1580000000000, // 更新时间
|
||||
lastUsedAt: 1580000000000, // 最后使用时间
|
||||
}]
|
||||
store: createStore('db-chat', 'store-chat')
|
||||
})
|
||||
|
||||
export const userStore = new Store({
|
||||
name: '用户(资源类型)',
|
||||
store: createStore('db-user', 'store-user')
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user