Skip to content
/ kojo Public
forked from yentsun/kojo

A Node.js event-driven microservice framework. Kōjō (工場) means 'plant' in Japanese.

License

Notifications You must be signed in to change notification settings

trueshura/kojo

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kojo

A Node.js event-driven microservice framework. Kōjō (工場) means 'plant' in Japanese.

Installation

 npm i kojo

Usage

Create a plant:

const Plant = require('kojo');
const pg = require('pg'); 
const pack = require('./package.json');


async function main() {
   
   const plant = new Plant('plantName', options, pack);
   await plant.ready();
   const {username, password, host, name} = _config.pg;
   const pool = new pg.Pool({
       user: username,
       database: name,
       password,
       host
   });
   plant.set('pg', pool);
}

return main();

Create a module's method (modules/<moduleName>/<methodName>.js):

module.exports = async function (someData) {
   
   const {plant, logger} = this;  // plant instance and the logger
   logger.debug('creating new record', someData);
   const someModule = plant.module('someModule'); // load other module
   const pool = plant.get('pg');  // get pg instance from plant
   const query = `INSERT INTO ... RETURNING *`;
   const result = await pool.query(query);
   const newRecord = result ? result.rows[0] : null;
   if (newRecord) {
       logger.info('new record created', newRecord);
       someModule.emit('created', newRecord)
   }
   return newRecord;
}

Create a subscriber (subscribers/someEntity.created.js):

module.exports = (plant, logger) => {

   const someModule = plant.module('someModule');
   const someOtherModule = plant.module('someOtherModule');
   const nats = plant.get('nats');

   someModule.on('created', async (newRecord) => {
       const {id, ...} = newRecord;
       const data = await nats.request('some.subject', {...});
       const result = await someOtherModule.method(data);
       logger.debug('some log entry...');
       const updatedRecord = await someModule.update(id, result);
       if (updatedRecord) nats.publish('record.created', updatedRecord);
   });
}

Test

npm test

About

A Node.js event-driven microservice framework. Kōjō (工場) means 'plant' in Japanese.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%