@domain.js/main 0.3.24 → 0.3.27

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.
@@ -1,5 +1,5 @@
1
1
  import { Cache, CnfDef, DepsDef, PubSubDef } from "./Define";
2
- export { Before } from "./Before";
3
2
  export { After } from "./After";
3
+ export { Before } from "./Before";
4
4
  export declare function Main(cnf: CnfDef, deps: DepsDef, pubsub?: PubSubDef): Cache;
5
5
  export declare const Deps: string[];
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Deps = exports.Main = exports.After = exports.Before = void 0;
4
- var Before_1 = require("./Before");
5
- Object.defineProperty(exports, "Before", { enumerable: true, get: function () { return Before_1.Before; } });
3
+ exports.Deps = exports.Main = exports.Before = exports.After = void 0;
6
4
  var After_1 = require("./After");
7
5
  Object.defineProperty(exports, "After", { enumerable: true, get: function () { return After_1.After; } });
6
+ var Before_1 = require("./Before");
7
+ Object.defineProperty(exports, "Before", { enumerable: true, get: function () { return Before_1.Before; } });
8
8
  function Main(cnf, deps, pubsub) {
9
9
  let hits = 0; // 击中次数
10
10
  let misseds = 0; // 未击中次数
@@ -2,6 +2,7 @@ import _ from "lodash";
2
2
  import { v4 } from "uuid";
3
3
  interface Cnf {
4
4
  logger: {
5
+ level?: "info" | "error" | "none";
5
6
  clientId: string;
6
7
  errorLogPath: string;
7
8
  infoLogPath: string;
@@ -4,19 +4,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Deps = exports.Main = void 0;
7
- const fs_1 = __importDefault(require("fs"));
7
+ const fs_1 = require("fs");
8
+ const promises_1 = __importDefault(require("fs/promises"));
8
9
  const path_1 = __importDefault(require("path"));
9
10
  const util_1 = require("util");
10
11
  const date = (offset = 0) => new Date(Date.now() + (offset | 0)).toISOString();
11
12
  function Main(cnf, deps) {
12
- const { logger: { errorLogPath, infoLogPath, ignoreErrors, clientId }, } = cnf;
13
+ const { logger: { level, errorLogPath, infoLogPath, ignoreErrors, clientId }, } = cnf;
13
14
  const { _, uuid: { v4: uuid }, } = deps;
14
15
  const makeDir = _.memoize((dir) => {
15
- if (!fs_1.default.existsSync(dir))
16
- fs_1.default.mkdirSync(dir);
16
+ if (!(0, fs_1.existsSync)(dir))
17
+ (0, fs_1.mkdirSync)(dir);
17
18
  });
18
19
  const ignores = new Set(ignoreErrors);
19
20
  const error = (e, extra) => {
21
+ if (level === "none")
22
+ return;
20
23
  if (!e) {
21
24
  console.trace("Logger.error but error is null or undefined");
22
25
  return;
@@ -49,14 +52,11 @@ function Main(cnf, deps) {
49
52
  }
50
53
  if (e.stack)
51
54
  content.push(JSON.stringify(e.stack));
52
- try {
53
- fs_1.default.appendFileSync(file, `${content.join("\t")}\n`);
54
- }
55
- catch (err) {
56
- console.error("Logger.error appendFileSync faid: %o", err);
57
- }
55
+ promises_1.default.appendFile(file, `${content.join("\t")}\n`).catch(console.error);
58
56
  };
59
57
  const info = (message, extra) => {
58
+ if (level === "none" || level === "error")
59
+ return;
60
60
  const time = date();
61
61
  const today = time.split("T")[0];
62
62
  const dir = path_1.default.resolve(infoLogPath, today);
@@ -72,14 +72,11 @@ function Main(cnf, deps) {
72
72
  content.push((0, util_1.format)(extra));
73
73
  }
74
74
  }
75
- try {
76
- fs_1.default.appendFileSync(file, `${content.join("\t")}\n`);
77
- }
78
- catch (e) {
79
- console.error("Logger.info appendFileSync faid: %o", e);
80
- }
75
+ promises_1.default.appendFile(file, `${content.join("\t")}\n`).catch(console.error);
81
76
  };
82
77
  const logger = (fn, name, isAsync = false, transform = (x) => x, errorHandler = (e) => (e instanceof Error ? e.message : ""), argsHandler = JSON.stringify) => {
78
+ if (level === "none")
79
+ return fn;
83
80
  if (isAsync) {
84
81
  const handler = {
85
82
  async apply(fn, me, args) {
@@ -9,6 +9,7 @@ interface Deps {
9
9
  Sequelize: typeof Sequelize;
10
10
  };
11
11
  }
12
+ export declare const Deps: string[];
12
13
  declare type NonConstructorKeys<T> = {
13
14
  [P in keyof T]: T[P] extends new () => any ? never : P;
14
15
  }[keyof T];
@@ -17,7 +18,6 @@ export declare type ModelStatic<M extends ModelBase> = NonConstructor<typeof Mod
17
18
  export declare function Main(cnf: Cnf, deps: Deps): {
18
19
  [propName: string]: Sequelize;
19
20
  };
20
- export declare const Deps: string[];
21
21
  /** Model 上的 sort 设定类型 */
22
22
  export interface ModelSort<Fields extends string> {
23
23
  default: Fields;
@@ -43,6 +43,10 @@ export declare class ModelBase<Attrs extends {} = any, Attrs4Create extends {} =
43
43
  * @param pk 主键
44
44
  */
45
45
  static getByPk<M extends ModelBase>(this: ModelStatic<M>, pk: string | number): Promise<M | null>;
46
+ /** cache 是否开启 */
47
+ static cache: boolean;
48
+ /** 删除cache */
49
+ static delCache(_pk: string | number): void;
46
50
  /**
47
51
  * 基于主键获取某些数据的Mode实例列表,维持参数的顺序,自动维护内存级 cache
48
52
  * @param pks 主键数组
@@ -93,7 +97,5 @@ export declare class ModelBase<Attrs extends {} = any, Attrs4Create extends {} =
93
97
  };
94
98
  /** 联合唯一列名称集合,用来自动恢复软删除的资源 */
95
99
  static unique?: string[];
96
- /** cache 是否开启 */
97
- static cache: boolean;
98
100
  }
99
101
  export {};
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ModelBase = exports.Deps = exports.Main = void 0;
3
+ exports.ModelBase = exports.Main = exports.Deps = void 0;
4
4
  const sequelize_1 = require("sequelize");
5
+ exports.Deps = ["Sequelize"];
5
6
  function Main(cnf, deps) {
6
7
  // 这里之所以要注入 Sequelize 是为了保证项目自身可以灵活选择自己的 Sequelize 版本, 这样改公共模块就会更加稳定, 避免频繁升级
7
8
  const { sequelize: dbs } = cnf;
@@ -14,7 +15,6 @@ function Main(cnf, deps) {
14
15
  return sequelizes;
15
16
  }
16
17
  exports.Main = Main;
17
- exports.Deps = ["Sequelize"];
18
18
  /**
19
19
  * Model 基类
20
20
  */
@@ -26,6 +26,8 @@ class ModelBase extends sequelize_1.Model {
26
26
  static getByPk(pk) {
27
27
  return this.findByPk(pk);
28
28
  }
29
+ /** 删除cache */
30
+ static delCache(_pk) { }
29
31
  /**
30
32
  * 基于主键获取某些数据的Mode实例列表,维持参数的顺序,自动维护内存级 cache
31
33
  * @param pks 主键数组
@@ -45,6 +47,8 @@ class ModelBase extends sequelize_1.Model {
45
47
  }
46
48
  }
47
49
  exports.ModelBase = ModelBase;
50
+ /** cache 是否开启 */
51
+ ModelBase.cache = true;
48
52
  /** 新增资源的时候可以写入的列名称集合 */
49
53
  ModelBase.writableCols = [];
50
54
  /** 编辑资源的时候可以写入的列名称集合 */
@@ -61,5 +65,3 @@ ModelBase.sort = {
61
65
  defaultDirection: "DESC",
62
66
  allow: ["id"],
63
67
  };
64
- /** cache 是否开启 */
65
- ModelBase.cache = true;
@@ -126,7 +126,8 @@ function Utils(cnf) {
126
126
  throw Error("Params.__files disallow assignment");
127
127
  if (lodash_1.default.size(req.files))
128
128
  params.__files = req.files;
129
- return params;
129
+ // 将上传文件附加到 params
130
+ return { ...params, ...req.files };
130
131
  },
131
132
  /**
132
133
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.24",
3
+ "version": "0.3.27",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {