Search code examples
javascriptnode.jspouchdbrelational-pouch

Do one to many relational pouch instances always have to be updated on both ends?


If I add a book to a relational pouch instance (That already contains an author with id 1 that has books 6 and 7) like this:

then(function() {
  return db.rel.save('book', {
  title: 'Winny the Pooh',
  id: 8,
  author: 1
})

And subsequently log the author query like this:

}).then(function() {
  var result = db.rel.find('author', 1);    
  result.then(function(data) {
    console.log(data)
});

The result is this:

{ authors: 
   [ { name: 'George R. R. Martin',
       books: [Object],
       id: 1,
       rev: '1-f07514693adc67c175bb1919b979dfcd' } ],
  books: 
   [ { title: 'A Game of Thrones',
       author: 1,
       id: 6,
       rev: '1-a36627090c454eba8ded42464ecfd37a' },
     { title: 'The Hedge Knight',
       author: 1,
       id: 7,
       rev: '1-1b725f45b6c9db0798a49f713dfaa5c6' } ] }

It does not look like Winny the Pooh was added to author 1's books, even though the object specifies that Winny the Pooh belongs to author 1. Do I need to manually update author one with the id of the book Winny the Pooh?

I'm pasting the full test script below for reference:

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

var db = new PouchDB('mydb');
db.setSchema([
  {
    singular: 'author',
    plural: 'authors',
    relations: {
      books: {
        hasMany: 'book'
      }
    }
  },
  {
    singular: 'book',
    plural: 'books',
    relations: {
      author: {
        belongsTo: 'author'
      }
    }
  }
]);
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
  });
}).then(function() {
  return db.rel.save('book', {
    title: 'Winny the Pooh',
    id: 8,
    author: 1
  })
}).then(function() {
  var result = db.rel.find('author', 1);
  result.then(function(data) {
    console.log(data)
  });
}).catch(console.log.bind(console));

Solution

  • Short answer is no. This issue contains the answer details. I'll paste the final working script below.

        var PouchDB = require('pouchdb');
        PouchDB.plugin(require('relational-pouch'));
        PouchDB.plugin(require('pouchdb-find'));
    
        var db = new PouchDB('mydb');
        db.setSchema([
          {
            singular: 'author',
            plural: 'authors',
            relations: {
              books: {
                hasMany: {
                  type: 'book',
                  options: {
                    queryInverse: 'author'
                  }
                }
              }
            }
          }, {
            singular: 'book',
            plural: 'books',
            relations: {
              author: {
                belongsTo: 'author'
              }
            }
          }
        ]);
        db.rel.save('author', {
          name: 'George R. R. Martin',
          id: 1
        }).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
          });
        }).then(function() {
          return db.rel.save('book', {
            title: 'Winny the Pooh',
            id: 8,
            author: 1
          })
        }).then(function() {
          var result = db.rel.find('author', 1);
          return result.then(function(data) {
            console.log(data)
          });
        }).catch(console.log.bind(console));