0.1.10 • Published 10 months ago

medigo-server-kit v0.1.10

Weekly downloads
2
License
ISC
Repository
bitbucket
Last release
10 months ago

MEDIGO Server KIT

tested with jest jest


Getting started


Dependencies

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 tests
  • tests/__mocks__ - Subdirectory for mocks module are defined immediately adjacent to the module
  • tests/__tests__ - Contains all the scenario tests

Testing Application

Available tests command:

npm run test

0.1.10

10 months ago

1.0.0

12 months ago

0.1.9

1 year ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.1-alpha.2

5 years ago

0.0.1-alpha.1

5 years ago

0.0.1-alpha

5 years ago

0.0.1-rc.65

5 years ago

0.0.1-rc.64

5 years ago

0.0.1-rc.62

5 years ago

0.0.1-rc.61

5 years ago

0.0.1-rc.60

5 years ago

0.0.1-rc.59

5 years ago

0.0.1-rc.58

5 years ago

0.0.1-rc.57

5 years ago

0.0.1-rc.56

5 years ago

0.0.1-rc.55

5 years ago

0.0.1-rc.54

5 years ago

0.0.1-rc.53

5 years ago

0.0.1-rc.52

5 years ago

0.0.1-rc.51

5 years ago

0.0.1-rc.50

5 years ago

0.0.1-rc.49

5 years ago

0.0.1-rc.48

5 years ago

0.0.1-rc.47

5 years ago

0.0.1-rc.46

5 years ago

0.0.1-rc.45

5 years ago

0.0.1-rc.44

5 years ago

0.0.1-rc.43

5 years ago

0.0.1-rc.42

5 years ago

0.0.1-rc.41

5 years ago

0.0.1-rc.40

5 years ago

0.0.1-rc.39

5 years ago

0.0.1-rc.38

5 years ago

0.0.1-rc.37

5 years ago

0.0.1-rc.36

5 years ago

0.0.1-rc.35

5 years ago

0.0.1-rc.34

5 years ago

0.0.1-rc.33

5 years ago

0.0.1-rc.32

5 years ago

0.0.1-rc.31

5 years ago

0.0.1-rc.30

5 years ago

0.0.1-rc.29

5 years ago

0.0.1-rc.28

5 years ago

0.0.1-rc.27

5 years ago

0.0.1-rc.26

5 years ago

0.0.1-rc.25

5 years ago

0.0.1-rc.24

5 years ago

0.0.1-rc.23

5 years ago

0.0.1-rc.22

5 years ago

0.0.1-rc.21

5 years ago

0.0.1-rc.20

5 years ago

0.0.1-rc.19

5 years ago

0.0.1-rc.18

5 years ago

0.0.1-rc.17

5 years ago

0.0.1-rc.16

5 years ago

0.0.1-rc.15

5 years ago

0.0.1-rc.14

5 years ago

0.0.1-rc.12

5 years ago

0.0.1-rc.11

5 years ago

0.0.1-rc.10

5 years ago

0.0.1-rc.9

5 years ago

0.0.1-rc.8

5 years ago

0.0.1-rc.7

5 years ago

0.0.1-rc.6

5 years ago

0.0.1-rc.5

5 years ago

0.0.1-rc.4

5 years ago

0.0.1-rc.3

5 years ago

0.0.1-rc.2

5 years ago

0.0.2

6 years ago

0.0.1-rc.1

6 years ago

0.0.1

6 years ago