Can't filter or order by using relational-pouch plugin

371 Views Asked by At

Since last two days I have been trying to sort and/or filter documents in pouchdb using a plugin relational-pouch but its not working for some reason

I have read the docs from Pouch DB's official documentation and followed the steps, i.e, I created an index and then called the find('type',options) method with document type and options as given in the code below, but the results I get is ordered by id's, the tricky part is I can pass the "limit" option in the options and it works but on the same place "sort" option doesen't seems to work, also it would be help full if someone can point me to a place where I can find how to filter in relational-pouch, the official GitHub docs won't help

get() {
this.db.createIndex({
  index: {
    fields: ['title']
  }
}).then((data)=>{
  this.db.rel.find('content', {
    limit : 1,
    sort: ['title']
  }).then((data)=>{console.log(data)});
}).catch(console.log)
}
// the schema applied
this.db.setSchema([
  {singular: 'user', plural: 'users', relations: { contents: {hasMany: 'content'}}},
  {singular: 'content', plural: 'contents', relations: { user: {belongsTo: 'user'}}}
]);

I am expecting results which are ordered by title(i even tried ordering by created), but I am getting the following:

{title: "Test title 5", description: "Test description 5", created: "2018-12-20T02:44:59.127Z", modified: "2018-12-20T02:44:59.127Z", id: "2A57FB9C-4E29-590C-AA4E-751FAA0F0AD9", …}

{title: "Test title 2", description: "Test description 2", created: "2018-12-19T03:40:56.302Z", modified: "2018-12-19T03:40:56.302Z", id: "2F568411-D955-42ED-9622-1C191AD6532D", …}

{title: "Test title 4", description: "Test description 4", created: "2018-12-20T02:44:51.654Z", modified: "2018-12-20T02:44:51.654Z", id: "319D2427-5862-477A-AAEA-CB2536C2430E", …}

{title: "Test title 1", description: "Test description 1", created: "2018-12-19T03:40:50.166Z", modified: "2018-12-19T03:40:50.166Z", id: "4D982FA6-99A9-4545-BC10-CBEF9530B3FA", …}

{title: "Test title 6", description: "Test description 6", created: "2018-12-20T02:45:06.927Z", modified: "2018-12-20T02:45:06.927Z", id: "7C4637AB-B626-E5FD-9353-5A7F926B98ED", …}
1

There are 1 best solutions below

4
Martin Bramwell On

If you expect to retrieve large numbers of records, there is only one good way to do it: allDocs.

relational-pouch is intended for work on a small collection of records related to one, or at most a small few, individual records.

The two tools are quite compatible and, with some experimentation and planning, work very well together. I work on large datasets with allDocs, render lists in the browser with live-find and, when the end-user picks a single item, retrieve it's related records for display/alteration with db.rel.find(). In such a case sorting is not an issue at all.

In other words ... if you NEED sorting and filtering you probably ought to get a short list of primary (master) records first, and only then use their key properties in db.rel.find() to retrieve their secondary (supporting, related, detail) records.

To emphasize ... allDocs should be your main tool and your design decisions should focus on it, not on relational-pouch.

Update: 2018/12/23 16:00 est

If all you need to do is ensure your records are in date order, just ensure that your IDs are in date order...

{id: "Test_2_20181219034050166", title: "Test title 1", description: "Test description 1", created: "2018-12-19T03:40:50.166Z", modified: "2018-12-19T03:40:50.166Z", …}

Check out the reference to sorting in the comand db.rel.find(type). You generate such specialized IDs with the class method db.rel.makeDocID(parsedID).