1.0.14 • Published 7 years ago

koa-process-engine v1.0.14

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

Koa Process Engine

它是一套自动处理engine开关的中间件。主要用来处理比如类似mysql redis mssql之类的数据库自动关闭问题。同时进行一些最佳实践的部署。

它暴露出的一些api,能够让你在编写nodejs程序的时候尽可能快速简单。目前对51信用卡管家公司内部具有高度自由的支持。

Dictionary

  • URI 路径编辑模块
  • MYSQL mysql模块类
  • FTP ftp模块类
  • LDAP ldap模块类
  • PROCESS 核心process模块
  • REDIS redis模块类
  • SENDCAST I/O输出方法插件
  • SECRET 加密模块类
  • TASKER 任务调度模块类
  • CACHE 缓存模块类

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.processStringprocess
error错误接收方法,用于自定义输出错误Functionundefined
plugins插件列表集合Arrayundefined

Plugins

插件制作非常方便:

  • 必须抛出的是一个class
  • constructor 具有一个context对象,就是类似上面的 ctx.process
  • 存在一个异步的install方法用于安装这个插件,具有一个options参数,该参数是在plugins中用户定义的
  • 存在一个异步的destroy方法,用来销毁。
// a.js
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

我们需要使用插件模式来定义一些引擎

// engine.js
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 => {
    // 将这种 a/b/c 转化成 a:b:c
    return pather.replace(/\//g, ':');
});

获取一个域

import { URI } from 'koa-process-engine';
console.log(URI.domain('a')); // http://api.u51.com

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(); // http://api.u51.com/a/b/c/3

带上query参数

import { URI } from 'koa-process-engine';
URI.path('a:b', { id: 3 }).parse({a:1}); // http://api.u51.com/a/b/c/3?a=1

完整demo

import { URI } from 'koa-process-engine';
URI.domain('Redis', '').watch(pather => {
   // 将这种 a/b/c 转化成 a:b:c
   return pather.replace(/\//g, ':');
});
URI.path('Get', '/user/:id(\\d+)');

const result = URI.path('Redis:Get', { id:100 }).parse({ name: 'evio' });
// user:100?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', // 命名为cast 默认 send
                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' // 之前设置过的引擎名 必须是redis引擎名
    } ]
}));

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

// x.js
import { uri, cache, URI } from 'koa-process-engine';
URI.domain('redis', '/').watch(pather => {
   // 将这种 a/b/c 转化成 a:b:c
   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'); // 删除一个缓存
})
1.0.14

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.1

7 years ago