I'm trying to write a KeystoneJS (Mongoose) model for a Contest
collection. These are simplified versions of my actual models:
Contest
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
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',
}
}
});
As you can see I'm trying to filter the winner
relationship to display only entries that are participating on this contest. But I haven't been able to do it with the contest's id
or _id
so I just created a new dummy field awards.idContest
that is filled with:
Contest.schema.pre('save', function(next) {
this.awards.idContest = (this.id || this._id);
next();
});
How could I accomplish the same but without extra fields? Something like:
jury: {
winner: {
type: Types.Relationship,
ref: 'Entry',
many: false,
filters: {
'contest.id': ':id',
'contest.state': 'admited'
}
}
}