Skip to content

Commit

Permalink
Merge pull request #22 from feathersjs/hooks-simple-setup
Browse files Browse the repository at this point in the history
Add feathers-hooks compatibility.
  • Loading branch information
marshallswain committed Nov 22, 2015
2 parents facae4a + 4746819 commit 756fb8f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,59 @@ The following options can be passed when creating a new Mongoose service:

**Note:** By default, each service creates its own database connection. If you don't want this you can pass an existing mongoose connection via the `connection` property.


## Using with `feathers-hooks`
If you're using the [feathers-hooks](https://github.com/feathersjs/feathers-hooks) plugin, you can pass the hooks directly on the service configuration object. The below is just an example. You are responsible to secure your own data.

```
var authHooks = require('feathers-authentication').hooks;
var hooks = require('../hooks');
var Property = {
schema: {
title: {type: String, required: true},
description: {type: String},
sold: {type: Boolean, 'default': false}
},
methods: {
isComplete: function(){
return this.complete;
}
},
statics: {
},
virtuals: {
'id': function(){
return this._id.toHexString();
}
},
indexes: [
],
// Hooks
before:{
all: [authHooks.requireAuth],
find: [authHooks.queryWithUserId],
get: [authHooks.queryWithUserId],
// These will be executed in the order listed
create: [authHooks.setUserId, hooks.log],
update: [authHooks.setUserId],
patch: [authHooks.setUserId],
remove: [authHooks.setUserId]
},
after:{
all: [],
find: [hooks.log],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
module.exports = Property;
```

## Mongoose Schemas

The recommended way of defining and passing a model to a `feathers-mongoose` service is shown above. Using object literal syntax makes things easily testable without having to mock out existing mongoose functionality.
Expand Down
6 changes: 5 additions & 1 deletion lib/feathers-mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ var MongooseService = Proto.extend({

this.options = _.extend({}, options);
this.type = 'mongoose';

// Add feathers-hooks compatibility.
this.before = entity.before || undefined;
this.after = entity.after || undefined;

// It is a mongoose model already
if (entity.modelName) {
Expand Down Expand Up @@ -264,4 +268,4 @@ module.exports = function(modelName, schema, options) {

module.exports.Service = MongooseService;

module.exports.mongoose = mongoose;
module.exports.mongoose = mongoose;
20 changes: 19 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,25 @@ var Post = {
},
indexes: [
{'published': 1, background: true}
]
],
before:{
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
after:{
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};

describe('Feathers Mongoose Service', function() {
Expand Down

0 comments on commit 756fb8f

Please sign in to comment.