angularjs-mongolab icon indicating copy to clipboard operation
angularjs-mongolab copied to clipboard

getByIds enhancement

Open hfinck opened this issue 12 years ago • 1 comments

Right now the getByIds function works with wrapping the provided ids in a MongoDB $oid object. Assuming you merge my last PR this won't work in all cases. We also have to search for the bare ids.

We could either make the getByIds query a bit more complicated in order to keep the API simple:

Resource.getByIds = function (ids, successcb, errorcb) {
      var qin = [];
      angular.forEach(ids, function (id) {
        qin.push({$oid:id});
      });
      return Resource.query({$or:[{id_:{$in:qin}},{id_:{$in:ids}}]}, successcb, errorcb);
};

or add another method, which queries using bare ids.

Or simply use the query method if one wants to query with bare ids.

Improving the current query would keep the API simple, but has drawback in performance.

What would you recommend?

hfinck avatar Oct 30 '12 15:10 hfinck

Yes, I see, So, I've added this getByIds method to the API since working with the $in + $oid syntax was really painful while using raw query. But I definitively see your point here, since the $oid was hard-coded and thus only usable only with auto-generated Ids. So, for now I've decided to simply rename this method to getByObjectIds.

Now, the longer-term strategy could be to introduce another parameter to the $mongolabResourceHttp that would specify if auto-generated ids are used or not. So one it could take a form of: MmongolabResourceFactory(collectionName, useNaturalKeys), second arg being false be default (so it would work as today).

Then, people would create their resources like this:

  • $mongolabResourceHttp('projects') for auto-generated ids and
  • $mongolabResourceHttp('projects, true') for natural keys

and we would be able to figure out the proper setup of the getByIds array.

How does it sound?

pkozlowski-opensource avatar Oct 30 '12 20:10 pkozlowski-opensource