3.8.2 • Published 5 days ago

dinegg v3.8.2

Weekly downloads
224
License
MIT
Repository
-
Last release
5 days ago

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.

3.8.2

5 days ago

3.8.1

6 days ago

3.8.0

16 days ago

3.7.0

1 month ago

3.6.0

5 months ago

3.5.4

5 months ago

3.2.0

10 months ago

2.15.2

8 months ago

2.15.3

8 months ago

2.15.0

8 months ago

2.15.1

8 months ago

3.3.1

9 months ago

3.3.0

9 months ago

3.5.3

8 months ago

3.5.2

9 months ago

3.5.1

9 months ago

3.5.0

9 months ago

3.1.4

10 months ago

3.0.4

11 months ago

3.1.3

11 months ago

3.1.2

11 months ago

3.1.1

11 months ago

3.1.0

11 months ago

3.0.3

11 months ago

3.0.1

11 months ago

3.0.0

11 months ago

2.13.2

1 year ago

2.13.0

1 year ago

2.13.1

1 year ago

2.12.0

1 year ago

2.11.0

2 years ago

2.11.1

2 years ago

2.10.1

2 years ago

2.10.2

2 years ago

2.10.0

2 years ago

2.9.0

2 years ago

2.8.0

2 years ago

2.7.5

2 years ago

2.7.0

2 years ago

2.6.1

2 years ago

2.6.0

2 years ago

2.7.2

2 years ago

2.6.3

2 years ago

2.6.2

2 years ago

2.7.4

2 years ago

2.7.3

2 years ago

2.5.1

2 years ago

2.5.0

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.4.2

3 years ago

2.3.2

3 years ago

2.3.4

3 years ago

2.3.3

3 years ago

2.3.1

3 years ago

2.3.0

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.5.0

3 years ago

0.4.1

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.2

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago