Closed Floriferous closed 4 years ago
So using attachSchema
multiple times is the right trick, the only annoying part was that you have to define the type
multiple times. If instead of passing a SimpleSchema you just pass an object, it works:
So, here's a scaleable solution to do this across collections:
const addServerSideSchema = (collection, extendedSchema) => {
const schema = collection.simpleSchema();
collection.attachSchema(schema.extend(extendedSchema));
return collection;
}
// And then:
const Todos = new Mongo.Collection('todos');
// On both environments
Todos.attachSchema( ... )
// On the server, for example:
addServerSideSchema(Todos, {
userId: {
autoValue() {
if (this.isSet) {
notifySlackOfUserChange(this.value);
}
}
}
})
I'd like to attach a autoValue function to one field of my schema, but only on the server, as it is pure server-side logic. What is the cleanest way to do this?
Something like this: