Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to override service methods #26

Closed
sgireddy opened this issue Nov 5, 2015 · 2 comments
Closed

How to override service methods #26

sgireddy opened this issue Nov 5, 2015 · 2 comments

Comments

@sgireddy
Copy link

sgireddy commented Nov 5, 2015

By default feathers broadcast events to all connected clients when using socket.io mode, this could be fatal and feathers service provides a way to override default methods (created, find etc). How do I do the same with MongooseService?

@sgireddy
Copy link
Author

sgireddy commented Nov 5, 2015

found solution @ feathers-mongodb git, just needed to extend the serivce.

var mongoose = require('feathers-mongoose');
var Proto = require('uberproto');

var MessageService = mongoose.Service.extend({
    created: function (data, params, callback) {
        //TODO Write logic
        return callback(null, data);
    },
    updated: function (data, params, callback) {                
        //TODO Write logic
        return callback(null, false);
    }
});

module.exports = function (modelName, schema, options) {
    return Proto.create.call(MessageService, modelName, schema, options);
};

module.exports.Service = MessageService;

@sgireddy sgireddy closed this as completed Nov 5, 2015
@daffl
Copy link
Member

daffl commented Nov 5, 2015

That is correct. The newer versions will use ES6 classes instead of Uberproto but that is still in the works.

Our preferred method though is through Hooks (https://github.com/feathersjs/feathers-hooks). For example, you could add a created_at and updated_at with extension like this:

var mongoose = require('feathers-mongoose');
var Proto = require('uberproto');

var MessageService = mongoose.Service.extend({
    created: function (data, params, callback) {
        data.created_at = new Date();
        this._super(data, params, callback);
    },
    update: function (id, data, params, callback) {                
        data.created_at = new Date();
        this._super(data, params, callback);
    }
});

With hooks that would look like this:

function timestamp(name) {
  return function(hook, next) {
    hook.data[name] = new Date();
    next();
  }
}

app.service('users')
  // Add updated_at and created_at
  .before({
    create: timestamp('created_at'),
    update: timestamp('updated_at'),
    patch: timestamp('updated_at')
  });

The difference is that you can attach the timestamp hook to any service you'd like. I wrote about it in more detail at https://medium.com/all-about-feathersjs/api-service-composition-with-hooks-47af13aa6c01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants