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