Last active
          September 11, 2024 15:11 
        
      - 
      
 - 
        
Save noam-honig/9195316391784b9f34f09d2035b473e2 to your computer and use it in GitHub Desktop.  
    cache-data-provider-results - see https://github.com/remult/remult/discussions/222
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | dataProvider: async () => { | |
| const result = await createKnexDataProvider({ | |
| client: 'mssql', | |
| //debug: true, | |
| connection: { | |
| server: process.env['DBServer'], | |
| database: process.env['DBDatabase'], | |
| user: process.env['DBUser'], | |
| password: process.env['DBPassword'], | |
| query_timeout: 30000, | |
| requestTimeout: 30000, | |
| connectTimeout: 30000, | |
| options: { | |
| enableArithAbort: true, | |
| encrypt: false, | |
| instanceName: process.env['DBInstance'], | |
| }, | |
| }, | |
| pool: { | |
| min: 10, | |
| max: 200, | |
| }, | |
| }); | |
| let entitiesToClearCache!: Record<string, boolean>; | |
| const cache = new Map<string, { result: Promise<any[]>; when: number }>(); | |
| return new Proxy(result, { | |
| get(target, prop, receiver) { | |
| if (prop == 'getEntityDataProvider') { | |
| return (meta: EntityMetadata) => { | |
| const isRequests = meta.key == 'v_requests'; | |
| return new Proxy(result.getEntityDataProvider(meta), { | |
| get(target, prop, receiver) { | |
| if (prop == 'find') { | |
| return async (options: EntityDataProviderFindOptions) => { | |
| if (entitiesToClearCache == undefined) { | |
| entitiesToClearCache = {}; | |
| for (const element of [ | |
| mn_Service_Requests, | |
| mn_Request_Notes, | |
| Test_Tubes_For_Requests, | |
| ]) { | |
| entitiesToClearCache[ | |
| remult.repo(element as any).metadata.key | |
| ] = true; | |
| } | |
| } | |
| //@ts-ignore | |
| let key = remult.context.request.url; | |
| const start = performance.now(); | |
| if (isRequests) { | |
| if (key?.includes('__action')) | |
| key = key?.substring(0, key.lastIndexOf('__action')); | |
| key += JSON.stringify( | |
| //@ts-ignore | |
| remult.context.request.body | |
| ); | |
| const z = cache.get(key); | |
| if (z && z.when > start - 60000) { | |
| console.log('v_requests cache hit', key); | |
| return z.result; | |
| } | |
| } | |
| const resultPromise = target.find(options); | |
| if (isRequests) { | |
| cache.set(key, { result: resultPromise, when: start }); | |
| } | |
| const result = await resultPromise; | |
| const duration = performance.now() - start; | |
| if (result.length > 201 || duration > 1000) | |
| console.log( | |
| duration > 1000 ? 'LongRequest:' : 'Many Rows:', | |
| meta.key, | |
| options?.limit, | |
| '=>', | |
| result.length, | |
| duration.toFixed(2), | |
| //@ts-ignore | |
| remult.context.request.url, | |
| //@ts-ignore | |
| remult.context.request.body | |
| ); | |
| return result; | |
| }; | |
| } else if ( | |
| entitiesToClearCache[meta.key] && | |
| ['update', 'insert', 'delete'].includes(prop as string) | |
| ) { | |
| return async (...args: any[]) => { | |
| cache.clear(); | |
| console.log('cache cleared'); | |
| //@ts-ignore | |
| return await target[prop as string](...args); | |
| }; | |
| } | |
| //@ts-ignore | |
| return target[prop as any]; | |
| }, | |
| }); | |
| }; | |
| } | |
| //@ts-ignore | |
| else return target[prop as any]; | |
| }, | |
| }); | |
| }, | |
| contextSerializer: { | |
| serialize: async (x) => ({ | |
| //@ts-ignore | |
| url: remult.context.request.url, | |
| //@ts-ignore | |
| body: remult.context.request.body, | |
| }), | |
| deserialize: async (x) => { | |
| //@ts-ignore | |
| remult.context.request = {url = 'LiveQueryUpdate: ' + x.url,body:x.body} | |
| }, | |
| }, | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment