@domain.js/main 0.1.7 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.husky/pre-commit CHANGED
@@ -5,3 +5,4 @@ fi
5
5
  . "$(dirname "$0")/_/husky.sh"
6
6
 
7
7
  ts-node src/cli/index.ts loadDeps ./src/deps ts && npm run lint-staged && npm run build
8
+ npm tst
@@ -16,7 +16,7 @@ interface callbackArg {
16
16
  interface Deps {
17
17
  cronParser: typeof cronParser;
18
18
  humanInterval: typeof human;
19
- cia: {
19
+ myCia: {
20
20
  regist: (name: string, validator: any, waiters: waiter[]) => void;
21
21
  submit: (name: string, times: number, callback: (arg: callbackArg) => void) => void;
22
22
  };
@@ -4,7 +4,7 @@ exports.Deps = exports.Main = void 0;
4
4
  function Main(cnf, deps) {
5
5
  const { cron = {} } = cnf;
6
6
  const ciaTaskType = "cronJob";
7
- const { cia, humanInterval: human, cronParser: parser } = deps;
7
+ const { myCia, humanInterval: human, cronParser: parser } = deps;
8
8
  const { tz = "Asia/Shanghai" } = cron;
9
9
  // 注册信息
10
10
  const registed = {};
@@ -40,7 +40,7 @@ function Main(cnf, deps) {
40
40
  setTimeout(() => {
41
41
  opt.times += 1;
42
42
  opt.triggeredAt = Date.now();
43
- cia.submit(`Cron::${name}`, opt.times, ({ cronJob: [err, , totalMS] }) => {
43
+ myCia.submit(`Cron::${name}`, opt.times, ({ cronJob: [err, , totalMS] }) => {
44
44
  if (err) {
45
45
  opt.failds += 1;
46
46
  }
@@ -72,7 +72,7 @@ function Main(cnf, deps) {
72
72
  };
73
73
  // 注册到cia上, 为了借助cia的能力自动下发任务
74
74
  // 增加 Cron:: 前缀是为了避免和其他任务名称冲突
75
- cia.regist(`Cron::${name}`, null, [{ type: ciaTaskType }]);
75
+ myCia.regist(`Cron::${name}`, null, [{ type: ciaTaskType }]);
76
76
  };
77
77
  const start = () => {
78
78
  if (startedAt)
@@ -24,14 +24,23 @@ function Main(cnf, deps, utils) {
24
24
  * @param _cols Allow columns to be updated
25
25
  * @returns The resource that has been updated
26
26
  */
27
- const modify = (Model, model, params, isAdmin = false, _cols) => {
27
+ const modify = async (Model, model, params, isAdmin = false, _cols) => {
28
28
  const cols = _cols || Model.editableCols || Model.writableCols || [];
29
29
  const attr = pickParams(params, cols, Model, isAdmin);
30
30
  // 避免id 被篡改,强制删除id属性
31
31
  if (attr.id)
32
32
  delete attr.id;
33
33
  Object.assign(model, attr);
34
- return model.save();
34
+ const fields = model.changed();
35
+ if (!Array.isArray(fields) || !fields.length)
36
+ return model;
37
+ try {
38
+ return await model.save();
39
+ }
40
+ catch (e) {
41
+ Object.assign(model, model.previous());
42
+ throw e;
43
+ }
35
44
  };
36
45
  /**
37
46
  * Restful add(C of CRUD) for create a resource
@@ -0,0 +1,14 @@
1
+ import * as TypeORM from "typeorm";
2
+ interface Cnf {
3
+ typeorm: {
4
+ [propName: string]: Parameters<typeof TypeORM.createConnection>[0];
5
+ };
6
+ }
7
+ interface Deps {
8
+ TypeORM: typeof TypeORM;
9
+ }
10
+ export declare function Main(cnf: Cnf, deps: Deps): Promise<{
11
+ [propName: string]: TypeORM.Connection;
12
+ }>;
13
+ export declare const Deps: string[];
14
+ export {};
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Deps = exports.Main = void 0;
4
+ async function Main(cnf, deps) {
5
+ // 这里之所以要注入 Sequelize 是为了保证项目自身可以灵活选择自己的 Sequelize 版本, 这样改公共模块就会更加稳定, 避免频繁升级
6
+ const { typeorm: dbs } = cnf;
7
+ const { TypeORM } = deps;
8
+ const links = {};
9
+ for await (const k of Object.keys(dbs)) {
10
+ const db = dbs[k];
11
+ links[k] = await TypeORM.createConnection(db);
12
+ }
13
+ return links;
14
+ }
15
+ exports.Main = Main;
16
+ exports.Deps = ["TypeORM"];
@@ -22,6 +22,8 @@ export interface Profile {
22
22
  userAgent: string;
23
23
  startedAt: Date;
24
24
  requestId: string;
25
+ /** 自由挂载信息的节点 */
26
+ extra: Record<string, any>;
25
27
  revision?: string;
26
28
  uuid?: string;
27
29
  token?: string;
@@ -11,11 +11,15 @@ interface Deps {
11
11
  apisRoute?: string;
12
12
  swagger?: [any, any];
13
13
  }
14
+ /** 对 params 的处理函数 */
15
+ declare type Handler = (params: any) => void;
16
+ /** 对执行结构的处理 */
17
+ declare type ResHandler = (results: any, res: restify.Response) => void;
14
18
  export declare function Router(deps: Deps): {
15
- get: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Function | undefined, resHandler?: Function | undefined) => void;
16
- post: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Function | undefined, resHandler?: Function | undefined) => void;
17
- put: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Function | undefined, resHandler?: Function | undefined) => void;
18
- del: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Function | undefined, resHandler?: Function | undefined) => void;
19
+ get: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
20
+ post: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
21
+ put: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
22
+ del: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
19
23
  } & {
20
24
  collection: (res: string, _routePath?: string | undefined, controller?: string | undefined) => void;
21
25
  model: (res: string, routePath?: string) => void;
@@ -49,6 +49,7 @@ function Utils(cnf) {
49
49
  userAgent: req.userAgent(),
50
50
  startedAt: new Date(),
51
51
  requestId: req.id(),
52
+ extra: {},
52
53
  };
53
54
  const token = req.headers["x-auth-token"] || req.query.access_token || req.query.accessToken;
54
55
  // token 和签名认证只能二选一
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.1.7",
3
+ "version": "0.1.11",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {