How to make an object with Meteor.call and server-only functions?


#1

Hi!
I can’t see how I have to modelize my application for server/client accessible object.

What I understand: Meteor.call is for the functions called from the client. For the functions only needed for the server, I can make a class object with all my methods inside.

But what happen to an object that have both?
For example I have a collection Notifications. I want the interface to be able to update a list of notification to a read state.

import { Mongo } from 'meteor/mongo';
export const Notifications = new Mongo.Collection('notifications');
Meteor.methods({
    'notification.setAsRead': function(doc){
      [...]
    }

But I want to have a function to send notifications for the server part. So I can make

Notifications = class Notifications {
  constructor(doc) {
    [...]
  }
  send(){
    [...]
  }

But the object Notification is already a Mongo Collection

:question: How can I deal with this problem? :question:
Thanks for your remarks. :thumbsup:


#2

You can either define some static functions on the Notifications-Collection (I see that quite often)
and call that in the method:

Notifications.setAsRead = function(notificationId) {
   this.update(notificationId, {$set: {read: true}});
}

//...

Meteor.methods({
    'notification.setAsRead': function(notificationId){
        Notifications.setAsRead(notificationId);
    }

Or you put these methods in another file like NotificationService or whatever, but it’s basically the same.


#3

Hi macrozone!
Well it’s true that it is an object after all…
So I can add functions to a mongo object! This resolve my question!
I can add all those server functions in a server/ directory under the imports/api/notifications

But I will not be able to have a constructor. I can have a NotificationService for that…

Thanks :wink: