环回:添加类型为 "hasMany" 的关系 table 中的数据
Loopback : adding data in relation table of type "hasMany"
我有一个 table 表示用户的个人资料。此配置文件可以跟随其他配置文件。
这是我的一部分 profil.json
"relations": {
"followers": {
"type": "hasMany",
"model": "Profil",
"foreignKey": "publisherId",
"keyThrough": "subscriberId",
"through": "Subscribing"
},
"subscribings": {
"type": "hasMany",
"model": "Profil",
"foreignKey": "subscriberId",
"keyThrough": "publisherId",
"through": "Subscribing"
},
这很好用,但现在我想知道个人资料的订阅日期。
所以我更新了 subscribing.json 关系 table 以添加日期
{
"name": "Subscribing",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"subscriptionDate": {
"type": "date",
"required": true,
"default": "$now"
}
},
"validations": [],
"relations": {
"subscriber": {
"type": "belongsTo",
"model": "Profil",
"as": "subscriber"
},
"publisher": {
"type": "belongsTo",
"model": "Profil",
"as": "publisher"
}
},
"acls": [],
"methods": {}
}
现在我希望能够查询此配置文件的数据,同时可以检索订阅日期。理想情况下,我会保持关系 table 非 public。
谢谢
您可以使用 "include" 过滤器将关系数据添加到响应中。
所以在你的情况下,这应该得到用户的 'subscribings':
Profil.find({include: 'subscribings'}, function() { /* ... */ });
尝试手动添加订阅者。
首先,在 subscribing.json
的旁边添加
"scope": {
"include": [
"subscriber"
]
}
接下来,在 profil.js
中创建钩子观察器(我使用 lodash 进行映射,但您可以使用您喜欢的库)
Profil.observe('loaded', function(ctx, next){
const Subscribing = Profil.app.models.Subscribing;
Subscribing.find({
where: {
publisherId: ctx.data.id
}
}, function (err, subscribings) {
if(err) return next(err)
ctx.data.subscribings = _.map(subscribings, function(subscribing){
return {
date: subscribing.subscriptionDate,
subscriber: subscribing.subscriber
}
});
next();
});
})
这就是全部!试试这个,如果有问题请评论。
@rigobcastro 在他的回答中提到了一种方法。
或者,您需要创建一个新关系。
假设我们称它为SubscribingMetaData
在您的 profile.json 中,添加以下内容
"subscribingMetaData": {
"type": "hasMany",
"model": "Subscribing",
"foreignKey": "subscriberId"
}
现在您可以将其包含在您的查询中,
Profil.find({include: ['subscribings', 'subscribingMetaData']}, function() { /* ... */ });
// process subscribingMetaData and map subscriptionDate with subscribings to get desired output.
我有一个 table 表示用户的个人资料。此配置文件可以跟随其他配置文件。
这是我的一部分 profil.json
"relations": {
"followers": {
"type": "hasMany",
"model": "Profil",
"foreignKey": "publisherId",
"keyThrough": "subscriberId",
"through": "Subscribing"
},
"subscribings": {
"type": "hasMany",
"model": "Profil",
"foreignKey": "subscriberId",
"keyThrough": "publisherId",
"through": "Subscribing"
},
这很好用,但现在我想知道个人资料的订阅日期。
所以我更新了 subscribing.json 关系 table 以添加日期
{
"name": "Subscribing",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"subscriptionDate": {
"type": "date",
"required": true,
"default": "$now"
}
},
"validations": [],
"relations": {
"subscriber": {
"type": "belongsTo",
"model": "Profil",
"as": "subscriber"
},
"publisher": {
"type": "belongsTo",
"model": "Profil",
"as": "publisher"
}
},
"acls": [],
"methods": {}
}
现在我希望能够查询此配置文件的数据,同时可以检索订阅日期。理想情况下,我会保持关系 table 非 public。 谢谢
您可以使用 "include" 过滤器将关系数据添加到响应中。 所以在你的情况下,这应该得到用户的 'subscribings':
Profil.find({include: 'subscribings'}, function() { /* ... */ });
尝试手动添加订阅者。
首先,在 subscribing.json
"scope": {
"include": [
"subscriber"
]
}
接下来,在 profil.js
中创建钩子观察器(我使用 lodash 进行映射,但您可以使用您喜欢的库)
Profil.observe('loaded', function(ctx, next){
const Subscribing = Profil.app.models.Subscribing;
Subscribing.find({
where: {
publisherId: ctx.data.id
}
}, function (err, subscribings) {
if(err) return next(err)
ctx.data.subscribings = _.map(subscribings, function(subscribing){
return {
date: subscribing.subscriptionDate,
subscriber: subscribing.subscriber
}
});
next();
});
})
这就是全部!试试这个,如果有问题请评论。
@rigobcastro 在他的回答中提到了一种方法。
或者,您需要创建一个新关系。
假设我们称它为SubscribingMetaData
在您的 profile.json 中,添加以下内容
"subscribingMetaData": {
"type": "hasMany",
"model": "Subscribing",
"foreignKey": "subscriberId"
}
现在您可以将其包含在您的查询中,
Profil.find({include: ['subscribings', 'subscribingMetaData']}, function() { /* ... */ });
// process subscribingMetaData and map subscriptionDate with subscribings to get desired output.