【发布时间】:2014-10-27 18:43:30
【问题描述】:
我正在尝试为 Contest 集合编写 KeystoneJS (Mongoose) 模型。这些是我实际模型的简化版本:
比赛
Contest.add({
title: {
type: Types.Text,
initial: true,
required: true,
index: true
},
state: {
type: Types.Select,
options: ['draft', 'started', 'finished', 'resolved'],
default: 'draft'
},
awards: {
idContest: { /* <--- copy of _id */
type: Types.Text,
hidden: true
},
jury: {
winner: {
type: Types.Relationship,
ref: 'Entry',
many: false,
filters: {
'contest.id': ':idContest', /* <--- filter */
'contest.state': 'admited'
}
}
}
}
});
参赛作品
Entry.add({
title: {
type: Types.Text,
initial: true,
required: true,
index: true
},
author: {
type: Types.Relationship,
ref: 'User',
initial: true,
index: true
},
contest: {
id: {
type: Types.Relationship,
ref: 'Contest',
index: true
},
state: {
type: Types.Select,
options: ['none', 'review', 'admited', 'rejected'],
default: 'none',
}
}
});
如您所见,我正在尝试过滤 winner 关系以仅显示参与此比赛的条目。但是我无法使用比赛的id 或_id 来做到这一点,所以我刚刚创建了一个新的虚拟字段awards.idContest,其中填充了:
Contest.schema.pre('save', function(next) {
this.awards.idContest = (this.id || this._id);
next();
});
如果没有额外的字段,我怎样才能完成相同的操作?比如:
jury: {
winner: {
type: Types.Relationship,
ref: 'Entry',
many: false,
filters: {
'contest.id': ':id',
'contest.state': 'admited'
}
}
}
【问题讨论】:
-
您能否提供您在
jury字段中引用的Entry列表的定义?没有它,就很难给你一个决定性的答案。 -
@JohnnyEstilles 我添加了
Entry模型。谢谢。 -
Ignacio,这是一个非常有趣的用例。 Keystone 的关系
filters选项目前不具备执行此类过滤器的能力。今天晚些时候,我将提交 PR 以添加和记录您正在寻找的功能。 PR 合并后我会在这里发布答案。
标签: node.js mongoose keystonejs