chatgpt-plugin/models/chaite/tools_storage.js
2025-03-11 23:34:32 +08:00

65 lines
1.2 KiB
JavaScript

import { ChaiteStorage } from 'chaite'
/**
* @extends {ChaiteStorage<import('chaite').ToolDTO>}
*/
export class LowDBToolsStorage extends ChaiteStorage {
/**
*
* @param { LowDBStorage } storage
*/
constructor (storage) {
super()
this.storage = storage
/**
* 集合
* @type {LowDBCollection}
*/
this.collection = this.storage.collection('tools')
}
/**
*
* @param {string} key
* @returns {Promise<import('chaite').ToolDTO>}
*/
async getItem (key) {
return this.collection.findOne({ id: key })
}
/**
*
* @param {string} id
* @param {import('chaite').ToolDTO} tools
* @returns {Promise<string>}
*/
async setItem (id, tools) {
if (id) {
await this.collection.updateById(id, tools)
return id
}
const result = await this.collection.insert(tools)
return result.id
}
/**
*
* @param {string} key
* @returns {Promise<void>}
*/
async removeItem (key) {
await this.collection.deleteById(key)
}
/**
*
* @returns {Promise<import('chaite').ToolDTO[]>}
*/
async listItems () {
return this.collection.findAll()
}
async clear () {
await this.collection.deleteAll()
}
}