genkgo / ember-localforage-adapter

Offline usage for Ember Data, based on localstorage adapter, but now uses Mozilla's localforage as data source
Other
133 stars 26 forks source link

querying records #45

Closed acerov closed 8 years ago

acerov commented 9 years ago

Hi,

this is my environment: ember-data 1.13.13, ember-localforage-adapter 1.13.1 I am unable to query records, it returns empty array, my code is bellow:

self.get('store').query('localDocumentDetail', { filter: { documentId: localDocument.get("id") } }).then(function(localDocumentDetails) {
    console.log("Document details bellow:");
    console.log(localDocumentDetails);
    localDocumentDetails.forEach(function(localDocumentDetail){
        console.log(localDocumentDetail);
    });
}

Thanks

sebweaver commented 9 years ago

I don't think query() accepts a filter.

Try this:

self.get('store').query('localDocumentDetail', { documentId: localDocument.get("id") }).then(function(localDocumentDetails) {
    console.log("Document details bellow:");
    console.log(localDocumentDetails);
    localDocumentDetails.forEach(function(localDocumentDetail){
        console.log(localDocumentDetail);
    });
}
acerov commented 9 years ago

Tried:

self.get('store').query('localDocumentDetail', { documentId: localDocument.get("id") }).then(function(localDocumentDetails) {
    console.log("Document details bellow:");
    console.log(localDocumentDetails);
    localDocumentDetails.forEach(function(localDocumentDetail){
        console.log(localDocumentDetail);
    });
}, function(reason) {
    console.log("localDocumentDetail query failed");
    console.log(reason);
});

strange but now it does not hit

console.log("Document details bellow:");

nor

console.log("localDocumentDetail query failed");

before it was hiting

console.log("Document details bellow:");
acerov commented 9 years ago

if I comment this lines:

//if (results.get('length')) {
//  results = this.loadRelationshipsForMany(store, type, results);
//}

in addon/adapters/localforage.js it works. I did not analyze much loadRelationshipsForMany function but I saw that in version 2.0.0 these lines does not exist.

sebweaver commented 9 years ago

There is a blocking promise in loadRelationships() which leave the query() promise pending when non embedded relationships can't be satisfied, instead of rejecting it (or resolving it without the missing records).

I'll push a PR as soon as the branch 1.13 will be created by @frederikbosch.

In the meantime, you should check your data, you probably have at least one unsatisfied relationship in the localDocumentDetail you request (i.e. with an id pointing to nowhere).

frederikbosch commented 9 years ago

@sebweaver The branch is there, feel free to create the PR. What are you planning? Issue a warning message in console? Throw an exception? I do not know the behaviour of other adapter when referencing non-existing relationships.

acerov commented 9 years ago

@sebweaver Yes, unsatisfied relationship happens when I am offline and I don't have access to data on rest server, because offline data references online data, probably I should cache referenced data locally.

@frederikbosch What is good behaviour for adapter in these situation, probably warning because findAll returns all records and query without filter none, they should behave the same I think.

frederikbosch commented 9 years ago

@acerov I understand the behaviour now. So rather than a non-existing relationships, this problem is related to a non-fetched relationship.

If we act on this, there are two scenarios possible. First the completely offline scenario: in this case there is erroneous data. In the second case the data within localforage is incomplete. Maybe both scenarios require different solutions.

acerov commented 9 years ago

@frederikbosch I think it is ok to return records (with warnings) regardless of non-fetched relationships because these records actually exist (in IndexedDB in my case), findAll behaves this way.

What was confusing to me, previous than finding out what is going on, is that findAll returned records and query (with filter that I know have a match) did not.

frederikbosch commented 9 years ago

Alright, we will wait for @sebweaver with a PR. Nice to hear that things are already sorted out for you.

acerov commented 9 years ago

@sebweaver @frederikbosch thank you both for help :+1: , waiting @sebweaver for a PR

sebweaver commented 9 years ago

It's in progress and will be released ASAP. :sweat_smile:

sebweaver commented 9 years ago

Done!

frederikbosch commented 8 years ago

Tagged and published!