Skip to content

Rayls/feathers-mongoose-service

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

feathers-mongoose-service

NPM version Build Status authors Total views Views in the last 24 hours

NPM

Easily create a Mongoose Service for Featherjs.

Installation

npm install feathers-mongoose-service --save

Usage

Important: Read over the Feathers documentation, specifically the section on Services. All of the Feathers REST and Socket API requests are supported.

Run example:

Run the following:

node example/index.js

Open your browser to http://localhost:8080/user. There will likely be an empty array in response. This is because you have no Users, yet.

I recommend the Postman extension for Chrome if you would like an easy to use REST Client.

How To Create a Mongoose Service

var customService = new mongooseService(modelName, mongooseSchema, mongooseConnection);

See Mongoose Schema Guide for more information on defining your schema.

Skip to Step 3 for the important part.

1) Get your dependencies.

// Get Feathers
var feathers = require('feathers');
// Get Mongoose
var mongoose = require('mongoose');
// Get Mongoose-Service
var mongooseService = require('feathers-mongoose-service'); 

2) Create your Mongoose connection to Mongodb.

// Get Mongoose
var mongoose = require('mongoose');
// Create your connection to Mongo
var connection = mongoose.connect('mongodb://localhost/test');

3) Create your custom Mongoose Service

// 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}
    }, connection);

4) Use your service with Feathers.

// Setup Feathers
var app = feathers();
// Configure Feathers
app.use(feathers.logger('dev')); // For debugging purposes.
// ................
var port = 8080;
// ................
app.configure(feathers.socketio())
  .use('/user', userService) // <-- Register your custom Mongoose-Service with Feathers
  .listen(port, function() {
        console.log('Express server listening on port ' + port);
    });

Complete Working Example code:

// Get Feathers
var feathers = require('feathers');
// Get Mongoose
var mongoose = require('mongoose');
// Get Mongoose-Service
var mongooseService = require('feathers-mongoose-service');
// Create your connection to Mongo
var connection = 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}
    }, connection);

// Setup Feathers
var app = feathers();

// Configure Feathers
app.use(feathers.logger('dev')); // For debugging purposes.
// ................
var port = 8080;
// ................
app.configure(feathers.socketio())
  .use('/user', userService) // <-- Register your custom Mongoose-Service with Feathers
  .listen(port, function() {
        console.log('Express server listening on port ' + port);
    });

License

MIT

Author

Glavin Wiechert

About

Easily create a Mongoose Service for Featherjs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published