dinegg
TypeScript icon, indicating that this package has built-in type declarations

3.7.0 • Public • Published

dinegg

dinegg framework is base eggjs, use typescript develop.

QuickStart

$ npm install
$ npm test

publish your framework to npm, then change app's framework config:

// {app_root}/index.js
{
  "name": "my-project",
  "egg": {
    "framework": "dinegg"
  }
}

v3.3.0

添加多进程直连socket通讯机制逻辑

v3 breaking change

  • dinegg v3 使用最新 3.0 的 egg,由于 egg3 最低支持 node 14.20,因此使用 dinegg v3 也不再兼容低版本 node,当前推荐使用 16/18
  • sequelize 使用最新版的 6.0 版本
  • dinegg v3 强烈不建议在 service 中访问 ctx 上的属性,所有的 http 入参(body,params,query,header)等,都建议只在 controller 中获取。响应也只能在 controller 中返回统一对象。然后显示传递参数给到service,也就是说将service作为业务逻辑方法库,提供明确的入参出参。不直接访问ctx
  • controller和service等在modules和原egg中可同时声明,但二者最终会合并,如果出现相同目录,将被覆盖,覆盖方式:modules优先

使用 modules 封装业务逻辑代码在同一个目录下

  • 将 service、controller、middleware 等文件放在同一个目录,类似 nest 的模块组织程序结构。
  • 文件名以这些单词结尾,如 a.service.ts

使用指南:

  • 文件放置在 app/modules 目录下,以模块划分目录。如 app/modules/user/login.service.ts
  • 配置 config.modules 可以设置哪些模块导出(可控制哪些用哪些不用)。注意这里同时最好同步设置 ets 相关配置,这样代码提示也就不会有未导出的模块功能。
  • breking: 控制器定义必须以.controller.ts 结尾,在原 egg 定义的,第一层目录就必须与 module 的配置导出名一致才能引用到。
  • 注意,由于使用的.controller.ts 这种后缀,对原 egg 的控制器加载会有影响,原 egg 需同样后缀,且,如果使用了模块导出,原 egg 也必须外部套一层模块名目录。
//支持的文件
**/*.service.ts
**/*.controller.ts
**/router.ts
task/*.ts
schedule/*.ts
app.ts
agent.ts
// 配置文件
config.modules = {
  imports: ["mod_user","cat"],//配置需要使用的模块
  enable:true,//此属性配置用于开启module。开启后必须遵从本文档指南撰写代码,否则将和egg原来的方式造成冲突。
};

// tshelper.js
require('dinegg/lib/tshelper')([moduleName1,mod2])
  • 使用
//service
this.ctx.service.
//controller
app.controller.
//schedule 定时任务,从模块目录下的task/或者schedule/目录读取任务文件。只匹配模块下的第一层目录。*/task/task1.ts 和 */schedule/task2.ts

permission 权限控制

  • 装饰器@permission

装饰器可以从 controllerDecorator 和 serviceDecorator 导入,效果一致!

// controller.ts
import { permission, get } from "dinegg/decorator";

class Controller {
	@get("/api/abc")
	@permission(["abc:bcd"])
	async method() {}
}
  • 权限控制类实现及使用
// 自定义权限控制实现类
import { AbstractPermissionAccessControl } from "egg";

export default class RoleAccessControl2 extends AbstractPermissionAccessControl {
	async main() {
		const userRole = this.ctx.request.query.myRole;
		console.log("userRole", userRole);
		if (this.permId.includes(userRole)) {
			return true;
		} else {
			throw new Error(`抱歉,您没有权限!需要的权限:${this.permId.toString()}`);
		}
	}
}

// app.ts 在app.ts中绑定后即可使用。在控制器或service的方法上绑定装饰器,如果验证权限失败会抛出错误。
import { Application, IBoot } from "egg";
import PermissionAccessControl from "./PermissionAccessControl";
// import * as path from "path"

export default class AppInit implements IBoot {
	private readonly app: Application;

	constructor(_app: Application) {
		this.app = _app;
		this.app.PermissionAccessControl = PermissionAccessControl;
	}

	async willReady() {}
}

cache 内存缓存简单实现 map 接口

  • this.app.cache.set()

  • this.app.cache.get()

  • this.app.cache.remove()

  • this.app.cache.clear()

  • 使用方式 dts 声明

/**
 * 添加一个缓存
 *
 * @param {string} key 字段key,注意不能重复
 * @param {*} value
 * @param {number} seconds 缓存时间,按s为单位 默认20s
 * @memberof AppCache
 */
set(key: string, value: any, expire: number = 20): boolean;
/**
 * 获取缓存
 *
 * @template T
 * @param {string} key
 * @return {*}  {(T | null)}
 * @memberof AppCache
 */
get<T extends any>(key: string): T | null;
remove(key: string): boolean;
delete(key: string): boolean;
/** 移除已过期的key value */
removeExpired(): boolean;
/** 重置整个map为新的,原来的放弃,被垃圾回收 */
reset(): void;
/** 重置整个map为新的,原来的放弃,被垃圾回收 */
clear(): void;

dineggApiClient 进程直连通讯

egg多进程默认使用ipc在agent与app之间通讯,这是需要通过node默认的cluster机制,由master中转来实现的。 dinegg参考egg官方文档,实现了一套直连通讯机制接口

Questions & Suggestions

Please open an issue here.

Package Sidebar

Install

npm i dinegg

Weekly Downloads

15

Version

3.7.0

License

MIT

Unpacked Size

200 kB

Total Files

72

Last publish

Collaborators

  • dingiyan