1
votes

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
I STRONGLY recommend you start with the very first example in GitHub ( db.rel.save('author', { name: 'George R. R. Martin', id: 1, books: [6, 7] } etc., etc, ), and examine the data structure that relational-pouch writes to your database. Without a solid understanding of the structure you're going to go round and round in circles. If that does not help then, please edit your question to include examples of your raw data and the schema you applied to your database (using this.db.setSchema([{def},{def}, ...]);). - Martin Bramwell
@MartinBramwell after your recommendation i have edited the question and added the schema applied, also i have gone through documentation and tried following their step by step guide but, at the point where i need to sort with any other field other than id it just does not works, although i would like to get one thing in your notice that there is no mention of sort functionality in the docs of Relational-pouch - Budhana Tech
I am concerned that your output results do not resemble the typical output of a relational pouch find operation. This would happen if you are trying use relational pouch to retrieve data that was not previously written by relational pouch. As I said before, relational pouch has a particular storage structure that you must respect if you want to use it. I really recommend that, with an empty database, you experiment with all the various documented examples before trying to work on your own data. - Martin Bramwell
@MartinBramwell first of all thank you for your fast responses, as i mentioned in the comments after your recommendation i have followed the official docs to create a sample database (in relational pouch npm) but even there when i come to a point of sorting i can't, i tried their "post" example - Budhana Tech
It maybe that relational-pouch does not support explicit sorting, only implicit as I note in my update to my answer. - Martin Bramwell

1 Answers

0
votes

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).