Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collection.load() doesn't seem to work as described in documentation #49

Closed
robhicks opened this issue Jun 24, 2015 · 10 comments
Closed

Comments

@robhicks
Copy link

I'm attempting to use ForunnerDB in a single page app. I'm using one collection. It is loaded the first time the app runs. After that, on relevant views I'm expecting to check to see if it's loaded. If it isn't, I need to reload it.
Whenever I use load, it always returns err as false, which means it never loads the data initially. Here's some code. Please let me know what I'm doing wrong.

loadTemplates() {
    var ts = this;
    return new Promise(function (resolve, reject) {
      var templateEntries = [];
      var countOfTemplates = 0;
      var returnedPromises = 0;
      ts.templatesCollection.load(function(err) {
        if (!err) {
          ts.templateService.getMasterTemplates()
            .then(function(data) {
              console.log('data',data);
              if (data && data.feed) {
                var feedEntries = data.getEntriesByType('relations/template');
                feedEntries.forEach(function(templateEntry) {
                  console.log('templateEntry',templateEntry);
                  var summary = templateEntry.summary;
                  ts.templatesCollection.insert({
                    id: templateEntry.id,
                    selected: false,
                    url: templateEntry.getContentUrl(),
                    name: summary.templateName,
                    description: summary.description,
                    state: summary.state,
                    type: summary.recordType,
                    creator: summary.creator,
                    modifier: summary.modifier,
                    updated: new Date(summary.modifyDate)
                  });
                });
                resolve();
              } else {
                resolve();
              }
            });
        } else {
          resolve();
        }
      });
    });
  }
@Irrelon
Copy link
Owner

Irrelon commented Jun 25, 2015

Hey ya,

I'm a bit confused about your question, specifically:

Whenever I use load, it always returns err as false, which means it never loads the data initially

Any stored data is loaded into the collection and the call to load() then tells the callback if an error occurred (which almost always will be false - which is expected behaviour).

which means it never loads the data initially

Returning err = false doesn't mean the data isn't loaded.

@robhicks
Copy link
Author

Okay, so it appears that collection.load() just check to see if the collection exists and if it does, it loads it. However, how would you recommend handling my scenario? I want to load the local collection not only if it exist but it contains data (particularly the case when a user changes the route (SPA)). However, if the collection is empty, it needs to be loaded.

@Irrelon
Copy link
Owner

Irrelon commented Jun 26, 2015

The .load() method will only return an error if it actually fails, not if persistent storage data exists or not. I can add an operation summary as data in the second returned parameter in the callback if you like that will describe if data was found and if so, how many records were found.

I think that would solve your problem.

It would be something like this:

db.collection('test').load(function (err, response, op) {
    if (!op.foundData) {
        // Load our data from the API
    } else if (op.rowCount < someExpectedNumberOfDocs) {
        // Load data again as we are missing some since last time the app was loaded
    }
});

If that would solve your problem let me know!

@Irrelon
Copy link
Owner

Irrelon commented Jun 26, 2015

Please clone the dev branch and let me know if that solves your issue as usage is described above.

@robhicks
Copy link
Author

Thanks for working on this. I grabbed dist/fdb-core+persist.js from your dev branch and attempted to use it. It doesn't appear to have the new code. Which one should I use?

@Irrelon
Copy link
Owner

Irrelon commented Jul 1, 2015

Hmm, it should do! But try using fdb-all.js first. That definitely has it as it is an "all modules enabled" build.

core+persist should be ok though...

@Irrelon
Copy link
Owner

Irrelon commented Jul 3, 2015

Oops, looks like my example above was incorrect, the operation data comes back in the third argument of the callback, not the second. I've updated the example above.

@telekosmos
Copy link

Just to say I came across this issue as I wanted to load the data at app startup from persistent storage, falling to network request if data not found. The callback params are perfect to check this out. As this could be very useful (IMHO), I suggest you to include in the documentation for the collection.load method.

I found response and op to have the same content (not the same == object), BTW.

cheers

@Irrelon
Copy link
Owner

Irrelon commented Mar 7, 2016

I believe the API has been updated since this issue was raised. The callback now contains:

callback(err, tableStats, metaStats);

The err is obvious, either falsy for no error or something non-falsy for an error.

The other two are:

tableStats - The details about the data loaded for the collection.
metaStats - The details about the data loaded for the collection's meta data (meta data includes stuff like last change timestamp for the collection etc).

I will update the load method docs to reflect this. :)

Irrelon added a commit that referenced this issue Mar 7, 2016
@Irrelon
Copy link
Owner

Irrelon commented Mar 7, 2016

OK, I've updated the docs on the edge branch as per the commit shown above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants