Search code examples
breeze

breeze: querying local cache when using client-side model


Consider the below code. It works fine when getting data from the server. I have a custom data adapter (staffManagemetnService) which creates client-side entities from the json returned by the server.

However, if I make a call to executeQueryLocally, it fails and raises the following exception: Cannot find an entityType for resourceName: 'GetInternalResourcesByCompetence'. Consider adding an 'EntityQuery.toType' call to your query or calling the MetadataStore.setEntityTypeForResourceName method to register an entityType for this resourceName

        var query = breeze.EntityQuery.from('GetInternalResourcesByCompetence').withParameters(parameters);

        var result = self.manager.executeQueryLocally(query.using(dataService.staffManagementService));
        if (result) {
            return $q.resolve(result);
        } else {
            return this.manager.executeQuery(query.using(dataService.staffManagementService))
                .then(function (data) {
                    return data.results;
                })
               .catch(function (err) {
                   logError('Restrieving resources days off failed', err, true);
               });
        }

I'm not sure what this means. Should it not work out-of-the-box since I've specifically asked breeze to use the custom dataAdapter ?


Solution

  • It's important to different between resource names and entity type names. Resource names are usually part of an endpoint and in plural (eg orders). Type names are typically singular (eg order).

    Locally breeze cannot do much with the resource name, since it won't call the endpoint. Instead you ask for a certain entity type name.

    You can map an entityType to a resourcename using the setEntityTypeForResourceName function:

    metadataStore.setEntityTypeForResourceName('Speakers', 'Person');
    

    See chapter "Resources names are not EntityType names" and the following chapters here: http://www.getbreezenow.com/documentation/querying-locally