mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-17 13:57:10 +00:00
feat: use sqlite instead of lowdb
This commit is contained in:
parent
9c41251164
commit
fd197abb33
22 changed files with 3519 additions and 39 deletions
|
|
@ -1,84 +0,0 @@
|
|||
import { ChaiteStorage } from 'chaite'
|
||||
import * as crypto from 'node:crypto'
|
||||
|
||||
/**
|
||||
* 继承UserState
|
||||
*/
|
||||
export class YunzaiUserState {
|
||||
constructor (userId, nickname, card, conversationId = crypto.randomUUID()) {
|
||||
this.userId = userId
|
||||
this.nickname = nickname
|
||||
this.card = card
|
||||
this.conversations = []
|
||||
this.settings = {}
|
||||
this.current = {
|
||||
conversationId,
|
||||
messageId: ''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @extends {ChaiteStorage<import('chaite').UserState>}
|
||||
*/
|
||||
export class LowDBUserStateStorage extends ChaiteStorage {
|
||||
/**
|
||||
*
|
||||
* @param {LowDBStorage} storage
|
||||
*/
|
||||
constructor (storage) {
|
||||
super()
|
||||
this.storage = storage
|
||||
/**
|
||||
* 集合
|
||||
* @type {LowDBCollection}
|
||||
*/
|
||||
this.collection = this.storage.collection('user_states')
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} key
|
||||
* @returns {Promise<import('chaite').UserState>}
|
||||
*/
|
||||
async getItem (key) {
|
||||
return this.collection.findOne({ id: key })
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} id
|
||||
* @param {import('chaite').UserState} state
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
async setItem (id, state) {
|
||||
if (id && await this.getItem(id)) {
|
||||
await this.collection.updateById(id, state)
|
||||
return id
|
||||
}
|
||||
state.id = id
|
||||
const result = await this.collection.insert(state)
|
||||
return result.id
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} key
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async removeItem (key) {
|
||||
await this.collection.deleteById(key)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {Promise<import('chaite').UserState[]>}
|
||||
*/
|
||||
async listItems () {
|
||||
return this.collection.findAll()
|
||||
}
|
||||
|
||||
async clear () {
|
||||
await this.collection.deleteAll()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue