import type { Collection, Db } from "mongodb" import { ICache, IQuery } from "." export class MongoCache implements ICache { public cache: Collection constructor(db: Db, name: string) { this.cache = db.collection(name) } public async set(key: string, data: T): Promise { await this.cache.updateOne({ key }, { $set: data }, { upsert: true }) return } public async get(k: string): Promise { const result = await this.cache.findOne({ key: k }) if (result) { const { _id, key, ...data } = result return data as T } else { return null } } public async remove(key: string): Promise { await this.cache.deleteOne({ key }) return } public async findOne(query?: IQuery): Promise { const result = await this.cache.findOne(query || {}) if (result) { const { _id, key, ...data } = result return data as T } else { return null } } public async findMany(query?: IQuery): Promise { const result = await this.cache.find(query || {}).toArray() return result.map((item) => { const { _id, key, ...rest } = item return rest as T }) } }