FredericHeem/starhackit

View on GitHub
server/src/plugins/dbSchema/index.js

Summary

Maintainability
A
1 hr
Test Coverage
const Router = require('koa-66');
const PgSchema = require('./PgSchema');
let log = require('logfilename')(__filename);

function DbSchemaPlugin(app){
  log.debug("DbSchemaPlugin ");

  function DbSchemaHttpController(){
    return {
      async getSchema(context) {
        //let schema = {};
        //let transactions = await pipeApi(context, '/transactions');
        try {
          const schema = await PgSchema.toJSON(app.data.sequelize, 'public');
          context.status = 200;
          context.body = schema;
        } catch(error){
          log.error("getSchema ", error);
          context.body = error;
        }

      }
    };
  }

  function DbSchemaRouter(){
    let router = new Router();
    let dbSchemaHttpController = DbSchemaHttpController(app);
    router.get('/schema', dbSchemaHttpController.getSchema);
    app.server.baseRouter().mount("/db", router);
    return router;
  }

  DbSchemaRouter();

  return {
    async start(){
    },
    async stop(){
    }
  };
}

module.exports = DbSchemaPlugin;