node-service-utils
v0.1.2
Published
Generate services for your service oriented Node.js architecture
Downloads
83
Readme
Node Service Utils
Generate simple CRUD service modules for your service oriented Node.js architecture (user, auth, todo etc.)
Introduction
- MongoDB based
- ES2015 (Node 6 or above is required)
- Fully tested
Installation
Requirements
- MongoDB (with Mongoose)
- Node 6
npm i node-service-utils --save
Usage
Note: Look at /src/test
folder for more detailed examples.
mongoose = require('mongoose')
const { BaseService, ModelFactory } = require('node-service-utils')
// Your custom service class
class UserService extends BaseService {
// ... custom CRUD methods
}
// Your custom model for your service class
function userModel(db) {
const schema = {
firstName: { type: String, required: true },
lastName: { type: String, required: true },
password: { type: String, required: true },
}
const modelFactory = new ModelFactory(db)
const model = modelFactory.create('User', schema)
return model
}
// Use model for new instances of your custom service class
mongoose.connect(config.mongo, err => {
const userService = new UserService('User', userModel(mongoose))
// userService.create({ firstName: 'Homer', ... }).then(...)
// userService.find({ firstName: 'Simpson', ... }).then(...)
})