chatgpt-plugin/models/chaite/channel_storage.js
2025-03-20 22:43:39 +08:00

67 lines
1.3 KiB
JavaScript

import { ChaiteStorage, Channel } from 'chaite'
export class LowDBChannelStorage extends ChaiteStorage {
/**
*
* @param { LowDBStorage } storage
*/
constructor (storage) {
super()
this.storage = storage
/**
* 集合
* @type {LowDBCollection}
*/
this.collection = this.storage.collection('channel')
}
/**
*
* @param {string} key
* @returns {Promise<import('chaite').Channel>}
*/
async getItem (key) {
const obj = await this.collection.findOne({ id: key })
if (!obj) {
return null
}
return new Channel(obj)
}
/**
*
* @param {string} id
* @param {import('chaite').Channel} channel
* @returns {Promise<string>}
*/
async setItem (id, channel) {
if (id && await this.getItem(id)) {
await this.collection.updateById(id, channel)
return id
}
const result = await this.collection.insert(channel)
return result.id
}
/**
*
* @param {string} key
* @returns {Promise<void>}
*/
async removeItem (key) {
await this.collection.deleteById(key)
}
/**
*
* @returns {Promise<import('chaite').Channel[]>}
*/
async listItems () {
const list = await this.collection.findAll()
return list.map(item => new Channel({}).fromString(JSON.stringify(item)))
}
async clear () {
await this.collection.deleteAll()
}
}