一对多关系 pouch 实例是否总是需要在两端更新?
Do one to many relational pouch instances always have to be updated on both ends?
如果我将一本书添加到关系袋实例(已经包含 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。我是否需要手动更新作者 1 的 ID这本书 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));
简短的回答是否定的。这个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));
如果我将一本书添加到关系袋实例(已经包含 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。我是否需要手动更新作者 1 的 ID这本书 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));
简短的回答是否定的。这个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));