import { Class, Doc, DocumentQuery, FindOptions, FindResult, MeasureContext, Ref, ServerStorage } from '@hcengineering/core' /** * @public */ export function createCacheFindAll (storage: ServerStorage): ServerStorage['findAll'] { // We will cache all queries for same objects for all derived data checks. const queryCache = new Map>() return async ( ctx: MeasureContext, clazz: Ref>, query: DocumentQuery, options?: FindOptions ): Promise> => { const key = JSON.stringify(clazz) + JSON.stringify(query) + JSON.stringify(options) let cacheResult = queryCache.get(key) if (cacheResult !== undefined) { return cacheResult as FindResult } cacheResult = await storage.findAll(ctx, clazz, query, options) queryCache.set(key, cacheResult) return storage.hierarchy.clone(cacheResult) as FindResult } }