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

Best practive / help #95

Open
fransyozef opened this issue Feb 16, 2018 · 2 comments
Open

Best practive / help #95

fransyozef opened this issue Feb 16, 2018 · 2 comments

Comments

@fransyozef
Copy link

Not really an issue but more help. I'm a bit confused.

db.rel.save('author', {
  name: 'George R. R. Martin', id: 1, books: [6, 7]
}).then(function () {
  return db.rel.save('book', { title: 'A Game of Thrones', id: 6, author: 1});
}).then(function () {
  return db.rel.save('book', {title: 'The Hedge Knight', id: 7, author: 1});
}).catch(console.log.bind(console));

what I don't understand in this code, you save a new author, and also add the new book foreignkeys. But in real life you would not know these keys before you created the book records. The author foreign key makes sense in the book records. So why the book id's in the 'books' array?

@luca147
Copy link

luca147 commented May 8, 2018

I can not find a way for this library to work properly, I am already thinking about making the relationships manually using pouch only.

@509dave16
Copy link

@fransyozef With the way that CouchDB/PouchDB, works there are no auto-incrementing IDs like a SQL database. In PouchDB, you can opt to have an _id generated for you(a uuid I believe). This is how you would have the ID ahead of time. And since uuid's are pretty much guaranteed to to be unique you shouldn't have to worry.

You can choose to have your HasMany relationships not represented by an array of IDs on the parent type. Take a look at this section of the README.md: https://github.com/pouchdb-community/relational-pouch#dont-save-hasmany. For this case you would first create the author and then create each book utilizing the id from the author you created.

If you would like I can come up with a code sample for you. The code sample you are referring to is following a pattern where child books that belong to the author are having their IDs stored on the author, whichh is not required. Just simply an example of how relational-pouch can be used.

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

3 participants