Database 与水线的链接关联
我有3个模型:提要、链接和源 每个提要都有一对多关联的链接 Feed.js:Database 与水线的链接关联,database,orm,sails.js,waterline,Database,Orm,Sails.js,Waterline,我有3个模型:提要、链接和源 每个提要都有一对多关联的链接 Feed.js: links: { collection: 'link', via: 'feed' } source: { model: 'source' } { links: [ { title: 'News link', url: 'http://href.com/sfs', feed: 1, source: 1, id: 1, createdAt:
links: {
collection: 'link',
via: 'feed'
}
source: {
model: 'source'
}
{
links:
[
{
title: 'News link',
url: 'http://href.com/sfs',
feed: 1,
source: 1,
id: 1,
createdAt: '2014-08-23T13:01:42.989Z',
updatedAt: '2014-08-23T13:01:42.989Z'
}
],
title: 'Feed Title',
photo: 'img/img.png',
yesNo: false,
id: 1,
createdAt: '2014-08-23T13:00:43.317Z',
updatedAt: '2014-08-23T13:00:43.317Z'
}
以及连接到具有一对一关联的源模型的链接模型
Link.js:
links: {
collection: 'link',
via: 'feed'
}
source: {
model: 'source'
}
{
links:
[
{
title: 'News link',
url: 'http://href.com/sfs',
feed: 1,
source: 1,
id: 1,
createdAt: '2014-08-23T13:01:42.989Z',
updatedAt: '2014-08-23T13:01:42.989Z'
}
],
title: 'Feed Title',
photo: 'img/img.png',
yesNo: false,
id: 1,
createdAt: '2014-08-23T13:00:43.317Z',
updatedAt: '2014-08-23T13:00:43.317Z'
}
源模型中没有什么特别的东西
Feed.find({}).populate('links').exec(console.log);
当我执行下面的查询时,我无法获得源模型中定义的链接的详细信息
Feed.find({}).populate('links').exec(console.log);
结果:
links: {
collection: 'link',
via: 'feed'
}
source: {
model: 'source'
}
{
links:
[
{
title: 'News link',
url: 'http://href.com/sfs',
feed: 1,
source: 1,
id: 1,
createdAt: '2014-08-23T13:01:42.989Z',
updatedAt: '2014-08-23T13:01:42.989Z'
}
],
title: 'Feed Title',
photo: 'img/img.png',
yesNo: false,
id: 1,
createdAt: '2014-08-23T13:00:43.317Z',
updatedAt: '2014-08-23T13:00:43.317Z'
}
我想用源模型填充“链接”集合。水线可以吗?还是我必须手动操作
谢谢,水线人口目前只有一级深;多层次的人口在路线图上。目前最好的办法是在填充链接的情况下执行初始的
提要
查询,从链接
记录中收集所有源
外键,一次找到它们,然后进行映射。比如:
async.auto({
// Get all feeds
feeds: function(cb) {
Feed.find({}).populate('links').exec(cb);
},
// Get all sources of feed links
sources: ['feeds', function(cb, results) {
var sourceIds = results.feeds.reduce(function(memo, feed) {
return memo.concat(_.pluck(feed.links, 'source'));
}, []);
Source.find(sourceIds).exec(cb);
}],
// Map source objects onto links
mapping: ['feeds', 'sources', function(cb, results) {
// Index sources by ID
var sources = _.indexBy(results.sources, 'id');
var feeds = results.feeds.map(function(feed) {
feed.links = feeds.links.map(function(link) {
link.source = sources[link.source];
return link;
});
return feed;
});
return cb(null, feeds);
}]
}, function done(err, results) {
// Handle err or do something with results.mapping
});
谢谢你的回答。我对node/sails太陌生了,需要做一些工作才能理解。但这似乎是目前最好的办法