reyesoft / ngx-jsonapi

JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
https://ngx-jsonapi.reyesoft.com/
MIT License
101 stars 52 forks source link

cachestore_support false does not disable caching of .all query resources #282

Open agenaille opened 4 years ago

agenaille commented 4 years ago

Issue

I've disabled caching:

NgxJsonapiModule.forRoot({
    url: '/api/',
    cache_prerequests: false,
    cachestore_support: false
})

When I make a query like so:

service.all(paramsCollection);

The Service.getAllFromServer handles the response with this stack:

node_modules/ngx-jsonapi/ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.es5.js.Service.getOrCreateResource
node_modules/ngx-jsonapi/ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.es5.js.RelatedDocumentCollection.getResourceOrFail (ngx-jsonapi.es5.js:942)
node_modules/ngx-jsonapi/ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.es5.js.RelatedDocumentCollection.fill (ngx-jsonapi.es5.js:891)
....
....
....
node_modules/ngx-jsonapi/ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.es5.js.Service.getAllFromServer (ngx-jsonapi.es5.js:3209)

Inside getOrCreateResource, the following is called:

public getOrCreateResource(id: string): R {
    let service = Converter.getServiceOrFail(this.type);
    let resource: R;

    resource = <R>CacheMemory.getInstance().getResource(this.type, id);
    if (resource === null) {
        resource = <R>service.new();
        resource.id = id;
        CacheMemory.getInstance().setResource(resource, false);
    }

    if (resource.source !== 'new') {
        resource.source = 'memory';
    }

    return resource;
}

The Problem

On the first invocation of Service.all(paramsCollection), it doesn't find anything, but it then always caches every returned resource. On each subsequent invocation of .all the CacheMemory will have the resource and return the cached resource. I have fixed this by extending the Resource service and overriding getOrCreateResource and createResource to remove the CacheMemory logic.

Suggested Solution

The CacheMemory code in Service.ts getOrCreateResource and Service.ts createResource needs a similar cachestore_support check, like this commit has: https://github.com/reyesoft/ngx-jsonapi/commit/5b964f9ee7af8cd8391f066fc9247362a8bdf213

https://github.com/reyesoft/ngx-jsonapi/blob/v2.1/src/service.ts#L369

if (Core.injectedServices.rsJsonapiConfig.cachestore_support) {
    // do the caching
}