medigo-server-kit v0.1.10
MEDIGO Server KIT
Getting started
Dependencies
- Express JS wrapper for Medigo service that using NodeJS environment.
- Mongoose
- Sequelize
- jest - For testing the application
Installation
install via NPM Package Manager
npm install medigo-server-kit
Features
- Default Middleware
- Router
- Database (Mongoose, Sequelize)
- Authentication And Authorization
- Service Connection
- Error And Handler
Usege
const ServerKit = require('medigo-server-kit')
const server = new ServerKit({
name: 'Medigo Service Name',
port: 8080
})
server.run()
Default Middleware Dependencies
This package comes with several middleware installed, but you can also add another middleware to the app. The default middlewares used are:
You can add middleware to the app with following:
...
const log = (req, res, next) => {
console.log('hello middleware.')
next()
}
server.middleware(log)
Or you can access directly to the express app
server.app.use(log)
Router
You can use express router express.Router
as example below:
// router.js
module.exports = router => {
router.get('/hello-medigo', (req, res, next) => {
res.json({ message: 'hello too.' })
})
return router
}
// server.js
server.router(require('./router.js'))
server.run()
Database (Mongoose, Sequelize)
This package comes with Mongoose and Sequelize ORM. Both databases are using URI Connection in configuration.
const server = new ServerKit({
name: 'Medigo Service Name',
port: 8080,
database: 'mongoose',
connection: 'mongodb://{username}:{password}@localhost:27017/{name}'
})
// We recommend to setup database first before running the server
server.setUpDatabase().then(() => {
server.run()
})
Both database ORM should use our db instance
- Mongoose
// user.js model of user
const mongoose = require('medigo-server-kit/database/mongoose').db
const Schema = require('medigo-server-kit/database/MongooseSchema')
// When using our schema, it will add timestamp, transform _id to id,
// and force _id field type to string instead of ObjectId
let userSchema = new Schema({
username: { type: String, required: true },
password: { type: String, required: true }
})
let UserModel = mongoose.model('User', userSchema)
UserModel.find({})
- Sequelize
If the service uses sequelize, you should install sequelize via npm to the app, because currently we don't provide schema for sequelize (will be provided later) and using this is still complicated. We will also update this documentation later.
Authentication And Authorization
This feature is currently under development, for a while, we provide simple Authorization like below:
const c = require('controllers')
const authorize = require('medigo-server-kit/security/authorize')
module.exports = router => {
router.get('/doctor', authorize('adminGroupHC', 'adminHC'), c.doctor.getListDoctor)
return router
}
Service Connection
To connect to another service around medigo services, this package also provide client connector for request. The service connectors available are:
- Auth
- User
- HealthCenter
- Doctor
- SuperAdmin
- Reservation
- Schedule
- Payment
- Notification
- Location
- Storage
- Middleware
- MasterData
- Service Client
Example of usage
...
const UserService = require('medigo-server-kit/services/User')
async getUserList (req, res, next) {
// req param is important to know where the request comes from,
// who is the requesting user, and many more.
let userClient = new UserService(req)
let data = await userClient.get('/user')
res.json(data)
}
...
- Service Connection
You can get access to current state of the request (such as authenticated user, healthCenter of the user, medigo-client and more by using Connection Class, Available informations are:
- getClient
object of id and name of medigo-client
- getClientId
x-medigo-client-id
- getServiceName
which service that is requesting
- getClientName
x-medigo-client-name
- getUser
object of current authenticated user
- getHealthCenter
current healthcenter of user
- getUserId
x-medigo-user-id
- getHealthCenterId
x-medigo-healthcenter-id
Example of usage
const ServiceConnection = require('medigo-server-kit/services/Connection')
module.exports = {
getCurrentUser (req, res, next) {
let SC = new ServiceConnection(req)
// will return null if not exists
res.json({ data: SC.getUser() })
}
}
Error and Handler
We also provide error Classes that are automatically handled by the app.
- Http
- Server
- Service
- BadRequest
- Forbidden
- NotFound
- Unauthentication
- Validation
- Validations
Example of usage
const Error = require('medigo-server-kit/error')
async getUserList (req, res, next) {
try {
if (false) {
throw new Error.BadRequest('Something\'s wrong in your request.')
}
} catch (error) {
next(error) // important to next for error handler
}
}
Testing Directory
tests
- Contains all the application teststests/__mocks__
- Subdirectory for mocks module are defined immediately adjacent to the moduletests/__tests__
- Contains all the scenario tests
Testing Application
Available tests command:
npm run test
10 months ago
12 months ago
1 year ago
3 years ago
3 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago