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

Is db.rel.find() have an option for paging? #106

Open
Murugan1778 opened this issue Aug 31, 2018 · 12 comments
Open

Is db.rel.find() have an option for paging? #106

Murugan1778 opened this issue Aug 31, 2018 · 12 comments

Comments

@Murugan1778
Copy link

Hi Guys,
I am using relational pouch for data fetching. How can i implement the data fetch by paging using db.rel.find()

@broerse
Copy link
Collaborator

broerse commented Aug 31, 2018

@Murugan1778
Copy link
Author

I have used auto generated id(UUID). So how can I use start key?

@broerse
Copy link
Collaborator

broerse commented Aug 31, 2018

In Ember Pouch we use startkey 'post_2_' and endkey 'post_3_' because we know our ID's always start with post_2_ for the post model. The startkey does not have to exist so you need to write something you know will come before your first real ID.

@Murugan1778
Copy link
Author

Consider like this, I have 10 records with following id structure post_2_randomUUID. If I want to fetch the records by 2 batch, for each batch I need 5 records. How can I implement this?

@broerse
Copy link
Collaborator

broerse commented Aug 31, 2018

Start with startkey post_2_ and limit 6. This way you know what your next startkey is.

@Murugan1778
Copy link
Author

Murugan1778 commented Aug 31, 2018

In this way I don't know the next start key because post_2 is randomUDID. Then how can I call next batch?

@broerse
Copy link
Collaborator

broerse commented Aug 31, 2018

? I don't seem to understand you. You know the next start key because it is the same as the ID in the last record.

@Murugan1778
Copy link
Author

Ok. This is my sample code

this.db.rel.save('post', { name: 'prabu', email: '[email protected]'})
this.db.rel.save('post', { name: 'murugan', email: '[email protected]'})

It will store in following format in pouchdb

{
"_id": "post_2_047E96BB-39BC-582F-A9B1-53385A9A4B93",
"_rev": "1-27f0ff931f544624863db976ce7785c8",
"data": {
"name": "parbu",
"email": "[email protected]"
}
}

{
"_id": "post_2_0f3076f044e8949b970e68a12d25a505",
"_rev": "1-ebdc46f142927ba11b4fcb49318c82cd",
"data": {
"name": "murugan",
"email": "[email protected]"
}
}

If I try to fetch like below format not working. Give empty response.

return this.db.rel.find('post',{startkey: 'post', limit: 2}).then(res => {
            console.log(res)
        })

If I change like this it will working. Give two docs in response.

return this.db.rel.find('post',{startkey: '047E96BB-39BC-582F-A9B1-53385A9A4B93', limit: 2}).then(res =>{
            console.log(res)
        })

So how I pass start key? I don't know the id first time. Only I know post.

@broerse
Copy link
Collaborator

broerse commented Sep 1, 2018

Use 'post_2_' the first time. As I said "The startkey does not have to exist" it just has to come before the first posible key.

@Murugan1778
Copy link
Author

thank you. I tried this also but it returns empty response only.

return this.db.rel.find('post',{startkey: 'post_2_', limit: 2}).then(res => {
           console.log(res)
       })

@broerse
Copy link
Collaborator

broerse commented Sep 1, 2018

Sorry I am working to much with pouchdb-find I forgot you had to just write null

See:

var PouchDB = require('pouchdb');
PouchDB.plugin(require('relational-pouch'));

var db = new PouchDB('https://martinic.couchcluster.com/bloggr');

db.setSchema([
  {
    singular: 'post',
    plural: 'posts',
    relations: {
      author: {belongsTo: 'author'}
    }
  },
  {
    singular: 'author',
    plural: 'authors',
    relations: {
      posts: {hasMany: 'post'}
    }
  }
]);

db.rel.find('post',{startkey: null, limit: 6}).then(res => {
  console.log(res)
}).catch(err => {
  console.log ('error', err);
});

@Murugan1778
Copy link
Author

Thanks it's working now. How to pass selector and sorting in this way?

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

No branches or pull requests

2 participants