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

Allow overriding default CRUD methods #12

Closed
agonbina opened this issue Apr 22, 2014 · 4 comments
Closed

Allow overriding default CRUD methods #12

agonbina opened this issue Apr 22, 2014 · 4 comments

Comments

@agonbina
Copy link
Contributor

There might be a time when there is a need to override one of the methods. I'm thinking:

mongooseService(modelName, schema, {
  find: function(...) {
    // override default logic
  }
}, mongoose);

Of course we can just override the method directly but if it's passed in as an option we could avoid setting the default on Store

@Glavin001
Copy link
Contributor

+1
Also to support Feathers Hooks and event dispatching and other future service features.

@daffl
Copy link
Member

daffl commented Apr 22, 2014

It could also use Uberproto which is what e.g. feathers-memory and feathers-mongodb use. That way you'd get a common inheritance pattern like:

var Memory = require('feathers-memory');

var TodoService = Memory.extend({
    get: function(id, params, callback) {
        this._super(id, params, function(error, data) {
            if(error) {
                return callback(error);
            }

            if(data.user !== params.user.id) {
                return callback(new Error('You can not access this todo'));
            }

            callback(null, data);
        });
    }
});

// Add a mixin that adds createdAt and updateAt timestamps
TodoService.mixin(timeStampMixin);

@daffl
Copy link
Member

daffl commented Jun 26, 2014

Easy extension should be possible with feathers-hooks no. With a boilerplate like:

// Get Feathers
var feathers = require('feathers');
// Get Hooks
var hooks = require('feathers-hooks');
// Get Mongoose
var mongoose = require('mongoose');
// Get Mongoose-Service
var mongooseService = require('feathers-mongoose');
// Connect to your MongoDB
mongoose.connect('mongodb://localhost/test');

// Create your Mongoose-Service, for a `User`
var userService = mongooseService('user', { 
        email: {type : String, required : true, index: {unique: true, dropDups: true}},
        firstName: {type : String, required : true},
        lastName: {type : String, required : true},
        age: {type : Number, required : true},
        password: {type : String, required : true, select: false},
        skills: {type : Array, required : true}
    }, mongoose);

// Setup Feathers
var app = feathers();
// Configure Feathers
app.use(feathers.logger('dev')); // For debugging purposes.
// ................
var port = 8080;
// ................
app.configure(hooks())
  .configure(feathers.socketio())
  .use('/user', userService); // <-- Register your custom Mongoose-Service with Feathers

You can e.g. add a createdAt to create like this:

// Add a before create hook
app.lookup('/user').before({
  create: function(hook, next) {
    // override default logic
    hook.data.createdAt = new Date();
    next();
  }
});

app.listen(port, function() {
    console.log('Express server listening on port ' + port);
});

Should we add that to the documentation?

@ekryski ekryski mentioned this issue Jul 29, 2015
@ekryski
Copy link
Member

ekryski commented Jul 29, 2015

Once PR #18 lands you'll be able to extend services explicitly or use hooks. feathers-hooks has a bunch of documentation on how they can be used, and we also have a more comprehensive guide on using hooks for validation.

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

4 participants