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