javascript - 一对多关系袋实例是否始终需要在两端进行更新?

标签 javascript node.js pouchdb relational-pouch

如果我将一本书添加到关系袋实例(该实例已包含 id 为 1 且拥有书籍 6 和 7 的作者),如下所示:

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)
});

结果是这样的:

{ 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' } ] }

即使该对象指定 Winny the Pooh 属于作者 1,Winny the Pooh 似乎并未添加到作者 1 的书籍中。我是否需要使用图书 Winny the Pooh 的 ID 手动更新作者 1?

我粘贴下面的完整测试脚本以供引用:

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

最佳答案

简短的回答是否定的。这个issue contains the answer details 。我将在下面粘贴最终的工作脚本。

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

关于javascript - 一对多关系袋实例是否始终需要在两端进行更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43480474/

相关文章:

javascript - Highcharts 自定义导出在重置时隐藏

node.js - Elastic Search中unicode(emoji)字符的字符串查询

javascript 不工作但 IE 显示没有错误

node.js - 在Docker容器中找不到依赖项

javascript - 如何使用 Browserify 和 Gulp 启动多页面应用程序

couchdb - 当 revs_limit 大于 0 时,PouchDB 不同步删除

javascript - 将多个 PouchDB 数据库同步到一个 CouchDB 数据库

couchdb - 如何解决与连续复制的冲突

JavaScript 改变 HTML 元素的类

javascript - ng 类在值更改时未正确更新