Gathering detailed insights and metrics for medigo-server-kit
Gathering detailed insights and metrics for medigo-server-kit
Gathering detailed insights and metrics for medigo-server-kit
Gathering detailed insights and metrics for medigo-server-kit
npm install medigo-server-kit
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
install via NPM Package Manager
npm install medigo-server-kit
1const ServerKit = require('medigo-server-kit') 2 3const server = new ServerKit({ 4 name: 'Medigo Service Name', 5 port: 8080 6}) 7 8server.run()
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:
1... 2const log = (req, res, next) => { 3 console.log('hello middleware.') 4 next() 5} 6 7server.middleware(log)
Or you can access directly to the express app
1server.app.use(log)
You can use express router express.Router
as example below:
1// router.js 2module.exports = router => { 3 router.get('/hello-medigo', (req, res, next) => { 4 res.json({ message: 'hello too.' }) 5 }) 6 return router 7} 8 9// server.js 10server.router(require('./router.js')) 11server.run()
This package comes with Mongoose and Sequelize ORM. Both databases are using URI Connection in configuration.
1const server = new ServerKit({ 2 name: 'Medigo Service Name', 3 port: 8080, 4 database: 'mongoose', 5 connection: 'mongodb://{username}:{password}@localhost:27017/{name}' 6}) 7 8// We recommend to setup database first before running the server 9server.setUpDatabase().then(() => { 10 server.run() 11})
Both database ORM should use our db instance
1// user.js model of user 2const mongoose = require('medigo-server-kit/database/mongoose').db 3const Schema = require('medigo-server-kit/database/MongooseSchema') 4 5// When using our schema, it will add timestamp, transform _id to id, 6// and force _id field type to string instead of ObjectId 7let userSchema = new Schema({ 8 username: { type: String, required: true }, 9 password: { type: String, required: true } 10}) 11 12let UserModel = mongoose.model('User', userSchema) 13 14UserModel.find({})
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.
This feature is currently under development, for a while, we provide simple Authorization like below:
1const c = require('controllers') 2const authorize = require('medigo-server-kit/security/authorize') 3 4module.exports = router => { 5 router.get('/doctor', authorize('adminGroupHC', 'adminHC'), c.doctor.getListDoctor) 6 return router 7} 8
To connect to another service around medigo services, this package also provide client connector for request. The service connectors available are:
Example of usage
1... 2const UserService = require('medigo-server-kit/services/User') 3 4async getUserList (req, res, next) { 5 // req param is important to know where the request comes from, 6 // who is the requesting user, and many more. 7 let userClient = new UserService(req) 8 let data = await userClient.get('/user') 9 res.json(data) 10} 11...
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:
object of id and name of medigo-client
x-medigo-client-id
which service that is requesting
x-medigo-client-name
object of current authenticated user
current healthcenter of user
x-medigo-user-id
x-medigo-healthcenter-id
Example of usage
1const ServiceConnection = require('medigo-server-kit/services/Connection') 2 3module.exports = { 4 getCurrentUser (req, res, next) { 5 let SC = new ServiceConnection(req) 6 // will return null if not exists 7 res.json({ data: SC.getUser() }) 8 } 9}
We also provide error Classes that are automatically handled by the app.
Example of usage
1const Error = require('medigo-server-kit/error') 2 3async getUserList (req, res, next) { 4 try { 5 if (false) { 6 throw new Error.BadRequest('Something\'s wrong in your request.') 7 } 8 } catch (error) { 9 next(error) // important to next for error handler 10 } 11}
tests
- Contains all the application teststests/__mocks__
- Subdirectory for mocks module are defined immediately adjacent to the moduletests/__tests__
- Contains all the scenario testsAvailable tests command:
npm run test
No vulnerabilities found.
No security vulnerabilities found.