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

Refactored the code to allow the use of the mongoose plugins #16

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 19 additions & 10 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
// Get Feathers
var feathers = require('feathers');
var bodyParser = require('body-parser');
// Get Mongoose
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var morgan = require('morgan');
// Connect to your MongoDB
mongoose.connect('mongodb://localhost/test');

// Get Mongoose-Service
var mongooseService = require('../lib'); // Mongoose-Service

var UserSchema = new Schema({
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}
});
var UserModel = mongoose.model('User', UserSchema);

// 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);
var userService = mongooseService(UserModel);

// Setup Feathers
var app = feathers();

// Configure Feathers
app.use(feathers.logger('dev')); // For debugging purposes.
// Enable logger (morgan)
app.use(morgan('dev')); // For debugging purposes.
// ................
var port = 8080;
// ................
app.configure(feathers.socketio())
app.configure(feathers.rest())
.configure(feathers.socketio())
.use(bodyParser({extend:true}))
.use('/user', userService) // <-- Register your custom Mongoose-Service with Feathers
.listen(port, function() {
console.log('Express server listening on port ' + port);
Expand Down
20 changes: 6 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
// Dependencies

// -----------------
module.exports = function(modelName, schema, mongoose) {
module.exports = function(model) {

// Constructor
function Store(modelName, schema, mongoose) {
// Extract
var Schema = mongoose.Schema;
var connection = mongoose.connection;
function Store(model) {

// Set model name
this.name = modelName;

// Create Schema
this.schema = new Schema(schema);

// Create Model
this.model = connection.model(this.name, this.schema);
this.name = model.modelName;
this.schema = model.Schema;
this.model = model;

return this;
}
Expand Down Expand Up @@ -99,6 +91,6 @@ module.exports = function(modelName, schema, mongoose) {
});
};

return new Store(modelName, schema, mongoose);
return new Store(model);

};
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@
"homepage": "https://github.com/feathersjs/feathers-mongoose-service",
"devDependencies": {
"mocha": "*",
"grunt-cli": "~0.1.7",
"grunt": "~0.4.1",
"grunt-cli": "~0.1.13",
"grunt": "~0.4.5",
"grunt-release": "~0.7.0",
"grunt-contrib-jshint": "~0.x",
"grunt-contrib-jshint": "~0.10.0",
"grunt-simple-mocha": "~0.4.0",
"grunt-contrib-watch": "~0.5.3",
"grunt-express-server": "~0.4.11",
"feathers": ">= 0.3.0 < 1",
"mongoose": ">= 3.8.4 < 4"
"grunt-contrib-watch": "~0.6.1",
"grunt-express-server": "~0.4.17",
"feathers": "~1.x",
"mongoose": ">= 3.8.4 < 4",
"morgan": "~1.1.0",
"body-parser": "^1.4.3"
},
"dependencies": {},
"peerDependencies": {
"feathers": ">= 0.3.0 < 1"
"feathers": "~1.x"
, "mongoose": ">= 3.8.4 < 4"
}
}
14 changes: 11 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var assert = require('assert');
var mongooseService = require('../lib');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var connection = mongoose.connect('mongodb://localhost/test');

Expand Down Expand Up @@ -33,9 +34,16 @@ describe('Usage', function() {

describe('#new(mongoose)', function() {
it('should return a new Mongoose Service', function() {
var service = new mongooseService('test', {
field: {type: String}
}, mongoose);
var UserSchema = new Schema({
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}
});
var UserModel = mongoose.model('User', UserSchema);
var service = new mongooseService(UserModel);
assert.equal(true, !!service);
assert.equal(true, !!service.find);
assert.equal(true, !!service.get);
Expand Down