2017-04-18 54 views
1

如果我添加一本書到一個關係小袋實例(已包含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的編號?

我粘貼下面的完整測試腳本供參考:

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

回答

1

簡短的回答是否定的。這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)); 
相關問題