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

Add feathers-hooks compatibility. #22

Merged
merged 3 commits into from
Nov 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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