Koa Process Engine
它是一套自动处理engine开关的中间件。主要用来处理比如类似mysql
redis
mssql
之类的数据库自动关闭问题。同时进行一些最佳实践的部署。
它暴露出的一些api,能够让你在编写nodejs程序的时候尽可能快速简单。目前对51信用卡管家
公司内部具有高度自由的支持。
Dictionary
PROCESS
核心模块提供一个中间件,让之后的路由具有自动管理引擎开关的功能。
我们时常会为一个mysql
数据库什么时候开启与关闭的问题而烦恼,目前此类能够自动帮你管理这些引擎的开关,无需让用户考虑关闭时机的问题。大大提高了代码的效率。
import { PROCESS } from 'koa-process-engine';
import Router from 'koa-router';
const route = new Router();
export default route;
route.use('/api', PROCESS.connect({ ... configs }));
只要经过/api
的路由都具有自动管理的功能。
configs 参数配置如下:
属性名 |
描述 |
类型 |
默认值 |
name |
命名空间。比如name=process 那么方法被放置在 ctx.process |
String |
process |
error |
错误接收方法,用于自定义输出错误 |
Function |
undefined |
plugins |
插件列表集合 |
Array |
undefined |
Plugins
插件制作非常方便:
- 必须抛出的是一个
class
constructor
具有一个context对象,就是类似上面的 ctx.process
- 存在一个异步的
install
方法用于安装这个插件,具有一个options
参数,该参数是在plugins中用户定义的
- 存在一个异步的
destroy
方法,用来销毁。
class NewComponent {
constructor(options) {
...
}
post() {
}
...
}
export default class NewComponentInstall {
constructor(context) {
this.context = context;
}
async install(options) {
this.context.component = new NewComponent(options);
}
async destroy() {
...
}
}
我们来调用这个插件
import { PROCESS } from 'koa-process-engine';
import Router from 'koa-router';
import A from './a';
const route = new Router();
export default route;
route.use('/api', PROCESS.connect({
plugins: [
{
loader: A,
options: { a:1, b: 2 }
}
]
}));
route.use('/api', PROCESS.connect({
plugins: [ A ]
}));
Define Engine
我们需要使用插件模式来定义一些引擎
import { MYSQL } from 'koa-process-engine';
export default class NewComponentInstall {
constructor(context) {
this.context = context;
}
async install() {
this.mysql();
}
mysql() {
const object = this.context.set('mysql', MYSQL);
object.set('fanding', {
host: '',
database: '',
port: '',
user: ''
});
}
...
}
然后我们注册这个插件
import { PROCESS } from 'koa-process-engine';
import Router from 'koa-router';
import ENGINE from './engine';
const route = new Router();
export default route;
route.use('/api', PROCESS.connect({
plugins: [ ENGINE ]
}));
Create an Engine
创建一个mysql的数据库链接对象。
route.get('/a', async ctx => {
const mysql = await ctx.process.create('mysql:fanding');
...
})
URI
路由设置模块,用来统一设置路由,简化路由操作。
domain
设置一个域:
import { URI } from 'koa-process-engine';
URI.domain('a', 'http://api.u51.com');
观察一个域
import { URI } from 'koa-process-engine';
URI.domain('a', 'http://api.u51.com').watch(pather => {
return pather.replace(/\//g, ':');
});
获取一个域
import { URI } from 'koa-process-engine';
console.log(URI.domain('a'));
path
设置一个层
import { URI } from 'koa-process-engine';
URI.path('b', '/a/b/c/:id(\\d+)');
获取一个完整链接(不带query)
import { URI } from 'koa-process-engine';
URI.path('a:b', { id: 3 }).toString();
带上query参数
import { URI } from 'koa-process-engine';
URI.path('a:b', { id: 3 }).parse({a:1});
完整demo
import { URI } from 'koa-process-engine';
URI.domain('Redis', '').watch(pather => {
return pather.replace(/\//g, ':');
});
URI.path('Get', '/user/:id(\\d+)');
const result = URI.path('Redis:Get', { id:100 }).parse({ name: 'evio' });
SENDCAST
import { PROCESS, SENDCAST } from 'koa-process-engine';
import Router from 'koa-router';
const route = new Router();
export default route;
route.use('/api', PROCESS.connect({
plugins: [ SENDCAST ],
plugins: [
{
loader: SENDCAST,
options: {
name: 'cast',
cache: true
}
}
]
}));
route.get('/', async ctx => {
ctx.process.send({ a:1 });
})
SECRET
import { PROCESS, SECRET } from 'koa-process-engine';
import Router from 'koa-router';
const route = new Router();
export default route;
route.use('/api', PROCESS.connect({
plugins: [ SECRET ],
plugins: [
{
loader: SECRET,
options: {
key: 'test'
}
}
]
}));
route.get('/', async ctx => {
const {send, secret} = ctx.process;
send(secret('ddddsaffasfdsfas'));
})
TASKER
当我们需要使用任务调度,必须用到此模块
import { PROCESS, TASKER } from 'koa-process-engine';
import Router from 'koa-router';
const route = new Router();
export default route;
route.use('/api', PROCESS.connect({
plugins: [ TASKER ]
}));
route.get('/', async ctx => {
const { task, send } = ctx.process;
task.on('done', () => send('ok'));
task.on('reject', () => send('error done', 500));
task.on('error', err => console.log(err));
task.pipe(fn1).pipe(fn2);
})
CACHE
内置的一个redis缓存类设计
User Cache Plugin
import { PROCESS, CACHE } from 'koa-process-engine';
import Router from 'koa-router';
const route = new Router();
export default route;
route.use('/api', PROCESS.connect({
plugins: [ {
loader: CACHE,
options: 'redis:local'
} ]
}));
route.get('/', async ctx => {
ctx.process.cache.load(name, args);
ctx.process.cache.build(name, args);
ctx.process.cache.delete(name, args);
})
Use to define cache
import { uri, cache, URI } from 'koa-process-engine';
URI.domain('redis', '/').watch(pather => {
return pather.replace(/\//g, ':');
});;
export default class User {
@uri static getuser = '/a/b/c';
@cache('redis:getuser') cacheUserInfo(process, args) {
const mysql = await process.create('mysql:local');
return process.methods.getuser(mysql, args);
}
}
使用
import User from './x';
router.get('/x', async ctx => {
ctx.process.cache.load('redis:getuser');
ctx.process.cache.build('redis:getuser');
ctx.process.cache.delete('redis:getuser');
})