@domain.js/main 0.1.14 → 0.1.15

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,50 +1,5 @@
1
- import * as Sequelize from "sequelize";
1
+ import { ModelBase } from "../sequelize";
2
2
  export interface Params {
3
3
  [propName: string]: any;
4
4
  }
5
- export declare type TModel = typeof Sequelize.Model & {
6
- filterAttrs?: string[];
7
- writableCols?: string[];
8
- editableCols?: string[];
9
- allowIncludeCols?: string[];
10
- onlyAdminCols?: string[];
11
- pagination?: {
12
- maxResults: number;
13
- maxStartIndex: number;
14
- maxResultsLimit: number;
15
- };
16
- sort?: {
17
- default: string;
18
- allow: string[];
19
- defaultDirection?: "DESC" | "ASC";
20
- };
21
- includes?: {
22
- [k: string]: {
23
- as: string;
24
- required: boolean;
25
- model: TModel;
26
- };
27
- };
28
- searchCols?: {
29
- [k: string]: {
30
- op: "=" | "LIKE";
31
- match: string[];
32
- };
33
- };
34
- stats?: {
35
- dimensions?: Record<string, string>;
36
- metrics: Record<string, string>;
37
- pagination?: {
38
- maxResults: number;
39
- maxStartIndex: number;
40
- maxResultsLimit: number;
41
- };
42
- };
43
- unique?: string[];
44
- };
45
- export declare type ModelExtraAtts = Omit<TModel, keyof typeof Sequelize.Model>;
46
- export interface Include {
47
- as: string;
48
- required: boolean;
49
- model: TModel;
50
- }
5
+ export declare type TModel = typeof ModelBase;
@@ -1,6 +1,6 @@
1
1
  import _ from "lodash";
2
2
  import * as Sequelize from "sequelize";
3
- import { TModel, Params } from "./defines";
3
+ import { ModelBase, ModelStatic } from "../sequelize";
4
4
  import { Stats } from "./stats";
5
5
  import { Utils } from "./utils";
6
6
  export { Before } from "./Before";
@@ -24,14 +24,14 @@ export interface CreatorAndClientIp {
24
24
  * @returns modify, add, remove, list, stats five methods
25
25
  */
26
26
  export declare function Main(cnf: Cnf, deps: Deps, utils: ReturnType<typeof Utils>): {
27
- modify: (Model: TModel, model: Sequelize.Model<any, any>, params: Params, isAdmin?: boolean, _cols?: string[] | undefined) => Promise<Sequelize.Model<any, any>>;
28
- add: (Model: TModel, params: Params, isAdmin: boolean | undefined, _cols: string[] | undefined, { creatorId, clientIp }: CreatorAndClientIp) => Promise<any>;
27
+ modify: <T extends ModelBase<any, any>>(Model: ModelStatic<T>, model: T, params: Record<string, any>, isAdmin?: boolean, _cols?: string[] | undefined) => Promise<T>;
28
+ add: <T_1 extends ModelBase<any, any>>(Model: ModelStatic<T_1>, params: Record<string, any>, isAdmin: boolean | undefined, _cols: string[] | undefined, { creatorId, clientIp }: CreatorAndClientIp) => Promise<T_1>;
29
29
  remove: (model: Sequelize.Model, deletorId: UserId) => Promise<void | Sequelize.Model<any, any>>;
30
- list: (Model: TModel, params: Params, allowAttrs?: string[] | undefined, toJSON?: boolean | undefined) => Promise<{
30
+ list: <T_2 extends ModelBase<any, any>>(Model: ModelStatic<T_2>, params: Record<string, any>, allowAttrs?: string[] | undefined, toJSON?: boolean | undefined) => Promise<{
31
31
  count: number;
32
- rows: any[];
32
+ rows: T_2[];
33
33
  }>;
34
- stats: (Model: TModel, params: Params, where?: any, conf?: {
34
+ stats: <T_3 extends ModelBase<any, any>>(Model: ModelStatic<T_3>, params: Record<string, any>, where?: any, conf?: {
35
35
  dimensions?: Record<string, string> | undefined;
36
36
  metrics: Record<string, string>;
37
37
  pagination?: {
@@ -41,7 +41,7 @@ export declare function Main(cnf: Cnf, deps: Deps, utils: ReturnType<typeof Util
41
41
  } | undefined;
42
42
  } | undefined) => Promise<{
43
43
  count: number;
44
- rows: any;
44
+ rows: Record<string, string | number>[];
45
45
  }>;
46
46
  };
47
47
  export declare const Deps: string[];
@@ -116,7 +116,7 @@ function Main(cnf, deps, utils) {
116
116
  count = await Model.count(lodash_1.default.pick(opt, COUNT_OPT));
117
117
  if (Array.isArray(allowAttrs) && allowAttrs.length)
118
118
  opt.attributes = allowAttrs;
119
- const rows = (await Model.findAll(opt));
119
+ const rows = await Model.findAll(opt);
120
120
  if (toJSON) {
121
121
  for (let i = 0; i < rows.length; i += 1) {
122
122
  rows[i] = rows[i].toJSON();
@@ -1,13 +1,21 @@
1
1
  import _ from "lodash";
2
2
  import * as Sequelize from "sequelize";
3
- import { Params, ModelExtraAtts, TModel } from "./defines";
3
+ import { ModelBase, ModelStatic } from "../sequelize";
4
4
  import { Utils } from "./utils";
5
5
  interface Deps {
6
6
  _: typeof _;
7
7
  Sequelize: Pick<typeof Sequelize, "literal" | "and" | "fn">;
8
8
  }
9
- export declare function Stats(cnf: {}, deps: Deps, utils: ReturnType<typeof Utils>): (Model: TModel, params: Params, where?: any, conf?: ModelExtraAtts["stats"]) => Promise<{
9
+ export declare function Stats(cnf: {}, deps: Deps, utils: ReturnType<typeof Utils>): <T extends ModelBase<any, any>>(Model: ModelStatic<T>, params: Record<string, any>, where?: any, conf?: {
10
+ dimensions?: Record<string, string> | undefined;
11
+ metrics: Record<string, string>;
12
+ pagination?: {
13
+ maxResults: number;
14
+ maxStartIndex: number;
15
+ maxResultsLimit: number;
16
+ } | undefined;
17
+ } | undefined) => Promise<{
10
18
  count: number;
11
- rows: any;
19
+ rows: Record<string, string | number>[];
12
20
  }>;
13
21
  export {};
@@ -149,7 +149,7 @@ function Stats(cnf, deps, utils) {
149
149
  let count = 0;
150
150
  if (_ignoreTotal !== "yes")
151
151
  count = await statsCount(Model, opt, dims);
152
- const rows = await Model.findAll(opt);
152
+ const rows = (await Model.findAll(opt));
153
153
  for (const x of rows) {
154
154
  for (const met of metrics) {
155
155
  x[met] = x[met] ? Number(x[met]) : 0;
@@ -1,8 +1,8 @@
1
1
  import type { LoDashStatic } from "lodash";
2
+ import moment from "moment";
2
3
  import * as mysql from "mysql2";
3
4
  import * as Sequelize from "sequelize";
4
- import moment from "moment";
5
- import { ModelExtraAtts, TModel, Params } from "./defines";
5
+ import { ModelBase, ModelStatic } from "../sequelize";
6
6
  interface Cnf {
7
7
  rest: {
8
8
  relativeMaxRangeDays?: number;
@@ -19,11 +19,15 @@ interface Deps {
19
19
  };
20
20
  }
21
21
  export declare function Utils(cnf: Cnf, deps: Deps): Readonly<{
22
- pickParams: (params: any, cols: string[], Model: TModel, isAdmin: boolean) => {
22
+ pickParams: <T extends ModelBase<any, any>>(params: any, cols: string[], Model: ModelStatic<T>, isAdmin: boolean) => {
23
23
  [propName: string]: any;
24
24
  };
25
- findAllOpts: (Model: TModel, params: Params) => any;
26
- pageParams: (pagination: ModelExtraAtts["pagination"], params: Params) => {
25
+ findAllOpts: <T_1 extends ModelBase<any, any>>(Model: ModelStatic<T_1>, params: Record<string, any>) => any;
26
+ pageParams: (pagination: {
27
+ maxResults: number;
28
+ maxStartIndex: number;
29
+ maxResultsLimit: number;
30
+ } | undefined, params: Record<string, any>) => {
27
31
  offset: number;
28
32
  limit: number;
29
33
  };
@@ -199,6 +199,7 @@ function Utils(cnf, deps) {
199
199
  };
200
200
  const RELATIVE_RANGE_ERROR = errors.notAllowed(`相对时间跨度最多 ${RELATIVE_MAX_RANGE} 天`);
201
201
  // findOptFilter 的处理
202
+ // eslint-disable-next-line complexity
202
203
  const findOptFilter = (params, name, where, Op, col = name) => {
203
204
  let value;
204
205
  if (!params)
@@ -391,10 +392,12 @@ function Utils(cnf, deps) {
391
392
  const searchOptResII = searchOpt(x.model, params._searchs, params.q, x.as);
392
393
  if (searchOptResII)
393
394
  searchOrs.push(searchOptResII);
394
- x.where = includeWhere;
395
395
  // 以及关联资源允许返回的字段
396
- if (x.model.allowIncludeCols)
397
- x.attributes = x.model.allowIncludeCols;
396
+ let attributes;
397
+ if (x.model.allowIncludeCols && x.model.allowIncludeCols.length)
398
+ attributes = x.model.allowIncludeCols;
399
+ // 将过滤条件和查询的字段附加上去
400
+ Object.assign(x, { where: includeWhere, attributes });
398
401
  });
399
402
  }
400
403
  const ret = {
@@ -1,16 +1,85 @@
1
- import * as Sequelize from "sequelize";
1
+ import { Model, Options, Sequelize } from "sequelize";
2
2
  interface Cnf {
3
3
  sequelize: {
4
- [propName: string]: Sequelize.Options;
4
+ [propName: string]: Options;
5
5
  };
6
6
  }
7
7
  interface Deps {
8
8
  Sequelize: {
9
- Sequelize: typeof Sequelize.Sequelize;
9
+ Sequelize: typeof Sequelize;
10
10
  };
11
11
  }
12
+ declare type NonConstructorKeys<T> = {
13
+ [P in keyof T]: T[P] extends new () => any ? never : P;
14
+ }[keyof T];
15
+ declare type NonConstructor<T> = Pick<T, NonConstructorKeys<T>>;
16
+ export declare type ModelStatic<M extends ModelBase> = NonConstructor<typeof ModelBase> & (new () => M);
12
17
  export declare function Main(cnf: Cnf, deps: Deps): {
13
- [propName: string]: Sequelize.Sequelize;
18
+ [propName: string]: Sequelize;
14
19
  };
15
20
  export declare const Deps: string[];
21
+ /**
22
+ * Model 基类
23
+ */
24
+ export declare class ModelBase<Attrs extends {} = any, Attrs4Create extends {} = Attrs> extends Model<Attrs, Attrs4Create> {
25
+ /**
26
+ * 基于主键获取某条数据的Mode实例,自动维护内存级 cache
27
+ * @param pk 主键
28
+ */
29
+ static getByPk<M extends ModelBase>(this: ModelStatic<M>, pk: string | number): Promise<M | null>;
30
+ /**
31
+ * 基于主键获取某些数据的Mode实例列表,维持参数的顺序,自动维护内存级 cache
32
+ * @param pks 主键数组
33
+ */
34
+ static getByPks<M extends ModelBase>(this: ModelStatic<M>, pks: string[] | number[]): Promise<M[]>;
35
+ /** 允许过滤的字段, 对于某些隐私、敏感信息,应该禁止基于其过滤, 使用者反复尝试可以暴力破解敏感信息 */
36
+ static filterAttrs?: string[];
37
+ /** 新增资源的时候可以写入的列名称集合 */
38
+ static writableCols?: string[];
39
+ /** 编辑资源的时候可以写入的列名称集合 */
40
+ static editableCols?: string[];
41
+ /** 关联资源的时候允许被关联展示的列名称集合 */
42
+ static allowIncludeCols?: string[];
43
+ /** 编辑过程中,仅管理可以更改的列名称集合 */
44
+ static onlyAdminCols?: string[];
45
+ /** 列表查询时候分页控制参数 */
46
+ static pagination?: {
47
+ maxResults: number;
48
+ maxStartIndex: number;
49
+ maxResultsLimit: number;
50
+ };
51
+ /** 列表查询时候排序控制参数 */
52
+ static sort?: {
53
+ default: string;
54
+ allow: string[];
55
+ defaultDirection?: "DESC" | "ASC";
56
+ };
57
+ /** 关联资源设定, 除非要关联过滤,否则不要设置资源之间的关联关系 */
58
+ static includes?: {
59
+ [k: string]: {
60
+ as: string;
61
+ required: boolean;
62
+ model: typeof ModelBase;
63
+ };
64
+ };
65
+ /** 模糊搜索相关设定 */
66
+ static searchCols?: {
67
+ [k: string]: {
68
+ op: "=" | "LIKE";
69
+ match: string[];
70
+ };
71
+ };
72
+ /** 统计相关设定 */
73
+ static stats?: {
74
+ dimensions?: Record<string, string>;
75
+ metrics: Record<string, string>;
76
+ pagination?: {
77
+ maxResults: number;
78
+ maxStartIndex: number;
79
+ maxResultsLimit: number;
80
+ };
81
+ };
82
+ /** 联合唯一列名称集合,用来自动恢复软删除的资源 */
83
+ static unique?: string[];
84
+ }
16
85
  export {};
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Deps = exports.Main = void 0;
3
+ exports.ModelBase = exports.Deps = exports.Main = void 0;
4
+ const sequelize_1 = require("sequelize");
4
5
  function Main(cnf, deps) {
5
6
  // 这里之所以要注入 Sequelize 是为了保证项目自身可以灵活选择自己的 Sequelize 版本, 这样改公共模块就会更加稳定, 避免频繁升级
6
7
  const { sequelize: dbs } = cnf;
@@ -14,3 +15,49 @@ function Main(cnf, deps) {
14
15
  }
15
16
  exports.Main = Main;
16
17
  exports.Deps = ["Sequelize"];
18
+ /**
19
+ * Model 基类
20
+ */
21
+ class ModelBase extends sequelize_1.Model {
22
+ /**
23
+ * 基于主键获取某条数据的Mode实例,自动维护内存级 cache
24
+ * @param pk 主键
25
+ */
26
+ static getByPk(pk) {
27
+ return this.findByPk(pk);
28
+ }
29
+ /**
30
+ * 基于主键获取某些数据的Mode实例列表,维持参数的顺序,自动维护内存级 cache
31
+ * @param pks 主键数组
32
+ */
33
+ static async getByPks(pks) {
34
+ if (!Array.isArray(pks) || !pks.length)
35
+ return [];
36
+ //静态方法调用同一个类中的其他静态方法,可使用 this 关键字
37
+ // eslint-disable-next-line no-undef
38
+ const list = [];
39
+ for await (const x of pks) {
40
+ const item = await this.getByPk(x);
41
+ if (item)
42
+ list.push(item);
43
+ }
44
+ return list;
45
+ }
46
+ }
47
+ exports.ModelBase = ModelBase;
48
+ /** 新增资源的时候可以写入的列名称集合 */
49
+ ModelBase.writableCols = [];
50
+ /** 编辑资源的时候可以写入的列名称集合 */
51
+ ModelBase.editableCols = [];
52
+ /** 列表查询时候分页控制参数 */
53
+ ModelBase.pagination = {
54
+ maxResults: 10,
55
+ maxStartIndex: 50000,
56
+ maxResultsLimit: 5000,
57
+ };
58
+ /** 列表查询时候排序控制参数 */
59
+ ModelBase.sort = {
60
+ default: "id",
61
+ defaultDirection: "DESC",
62
+ allow: ["id"],
63
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -77,7 +77,7 @@
77
77
  "mysql2": "^2.3.3",
78
78
  "restify": "^8.6.0",
79
79
  "restify-errors": "^8.0.2",
80
- "sequelize": "6.12.1",
80
+ "sequelize": "6.16.1",
81
81
  "type-fest": "^2.8.0",
82
82
  "uuid": "^8.3.2",
83
83
  "xlsx": "^0.17.4"