var keystone = require('keystone'),
Types = keystone.Field.Types;
var Country = new keystone.List('Country');
Country.add({
name: { type: String, required: true, initial:true, index: true },
primeMinister: { type: Types.Relationship, ref: 'User', required: true, index: {unique: true}, initial: true },
});
Country.defaultColumns = 'name, primeMinister';
Country.register();
Country.add({
name: { type: String, required: true, initial:true, index: true },
primeMinister: { type: Types.Relationship, ref: 'User', required: true, index: true, unique: true, initial: true },
});
var keystone = require('keystone'),
Types = keystone.Field.Types;
var Country = new keystone.List('Country');
Country.add({
name: { type: String, required: true, initial:true, index: true },
primeMinister: { type: Types.Relationship, ref: 'User', required: true, index: true, unique: true, initial: true },
});
var keystone = require('keystone'),
Types = keystone.Field.Types;
var Country = new keystone.List('Country');
Country.add({
name: { type: String, required: true, initial:true, index: true },
primeMinister: { type: Types.Relationship, ref: 'User', required: true, index: true, initial: true },
});
Country.defaultColumns = 'name, primeMinister';
Country.schema.pre('save', function(next) {
Country.model.find()
.where('primeMinister', this.primeMinister)
.exec()
.then(function (dupe) {
if(dupe && dupe[0]) {
next(new Error('User is already the Prime Minister of ' + dupe[0].name));
}
else {
next();
}
}, function (err) {
next(err);
});
});
Country.register();
var keystone = require('keystone'),
Types = keystone.Field.Types;
var Country = new keystone.List('Country');
Country.add({
name: { type: String, required: true, initial:true, index: true },
primeMinister: { type: Types.Relationship, ref: 'User', required: true, index: true, initial: true },
});
Country.defaultColumns = 'name, primeMinister';
Country.schema.pre('save', function(next) {
var detectDupe = function (err, dupe) {
if (err)
next(err);
if( (dupe && dupe[0]) && (this.id !== dupe[0].id) ) {
next(new Error('User is already the Prime Minister of ' + dupe[0].name));
}
else {
next();
}
}
var preventDupe = detectDupe.bind(this);
Country.model.find()
.where('primeMinister', this.primeMinister)
.exec(preventDupe);
});
Country.register();
unique: true,
Country.schema.pre('save',function(next) { this.primeMinister2 = '54997a0dc474c1ef1b126414'; next();});
Country.schema.add({primeMinister2: { type: keystone.mongoose.Schema.Types.ObjectId, unique:true, index: true, initial: true }});