I have overwritten the default collection that calls super insert/update/delete Collection calls.
However before calling i want to implement some security level that would automatically modify seloctor based on some logic.
export class CustomCollection extends Mongo.Collection {
constructor(collectionName, useCustomModifier = false, omitLog = false) {
super(collectionName);
this.useCustomModifier = useCustomModifier;
this.omitLog = omitLog;
}
find<T, U>(selector?: Selector<T> | ObjectID | string): Cursor<T, U> {
const userId = this.userId(); //don't have access to Meteor context
const sessionId = this.connection.id; //don't have access to Meteor context
const companyId = extraLogic(userId, sessionId); //some custom logic to extract companyId, etc. that would be automatically inserted to the query
return super.find( {...selector, companyId });
}
//...
}
And then on the Meteor methods and publications i would just these CustomCollection.find({}).
The reason why i don’t want to pass the the context using “apply(this, …)” is because it would require alot of refactoring of the existing code and some parts could be missed out by accident or some developers would make error by directly calling leaving it a as possible error on human side.
export const myCollection = new CustomCollection('myCollection ', false, true);
Meteor.methods({
'test': function () {
//can access this.userId(), this.connection.id here but would like that inside find() and automatically
myCollection.find({});
},
})