@domain.js/main 0.3.3 → 0.3.6

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.
@@ -5,12 +5,14 @@ interface Cnf {
5
5
  };
6
6
  }
7
7
  interface Deps {
8
- redis: Pick<Redis, "hget" | "hset" | "hincrby">;
8
+ redis: Pick<Redis, "hget" | "hset" | "hincrby" | "hmget">;
9
9
  }
10
10
  export declare function Main(cnf: Cnf, deps: Deps): {
11
+ mget: (keys: string[]) => Promise<number[]>;
11
12
  get: (key: string) => Promise<number>;
12
13
  set: (key: string, val: number) => Promise<number>;
13
14
  incr: (key: string) => Promise<number>;
15
+ decr: (key: string) => Promise<number>;
14
16
  };
15
17
  export declare const Deps: string[];
16
18
  export {};
@@ -4,13 +4,39 @@ exports.Deps = exports.Main = void 0;
4
4
  function Main(cnf, deps) {
5
5
  const { counter: { key: REDIS_KEY }, } = cnf;
6
6
  const { redis } = deps;
7
+ /**
8
+ * 获取指定 key 的统计数
9
+ * @param key 要获取的 key
10
+ */
7
11
  const get = async (key) => {
8
12
  const num = await redis.hget(REDIS_KEY, key);
9
13
  return Number(num) | 0;
10
14
  };
15
+ /**
16
+ * 主动设置某个key为一个数字
17
+ * @param key 要设置的key
18
+ * @param val 要设置的值
19
+ */
11
20
  const set = (key, val) => redis.hset(REDIS_KEY, key, Math.max(0, val | 0));
21
+ /**
22
+ * 某个key自增长1
23
+ * @param key 要自增长的key
24
+ */
12
25
  const incr = (key) => redis.hincrby(REDIS_KEY, key, 1);
13
- return { get, set, incr };
26
+ /**
27
+ * 某个key自减少1
28
+ * @param key 要自减少的key
29
+ */
30
+ const decr = (key) => redis.hincrby(REDIS_KEY, key, -1);
31
+ /**
32
+ * 一次获取多个key的统计值
33
+ * @param keys 多个key按序输入, 返回的统计数据为数组,和keys数组保持对应关系
34
+ */
35
+ const mget = async (keys) => {
36
+ const res = await redis.hmget(REDIS_KEY, ...keys);
37
+ return res.map((x) => Number(x) | 0);
38
+ };
39
+ return { mget, get, set, incr, decr };
14
40
  }
15
41
  exports.Main = Main;
16
42
  exports.Deps = ["redis"];
@@ -27,11 +27,17 @@ export declare function Main(cnf: Cnf, deps: Deps, utils: ReturnType<typeof Util
27
27
  modify: <T extends ModelBase<any, any>>(Model: ModelStatic<T>, model: T, params: Record<string, any>, isAdmin?: boolean, _cols?: string[] | undefined) => Promise<T>;
28
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: <T_2 extends ModelBase<any, any>>(Model: ModelStatic<T_2>, params: Record<string, any>, allowAttrs?: string[] | undefined, toJSON?: boolean | undefined) => Promise<{
31
- count: number;
32
- rows: T_2[];
33
- }>;
34
- stats: <T_3 extends ModelBase<any, any>>(Model: ModelStatic<T_3>, params: Record<string, any>, where?: any, conf?: {
30
+ list: {
31
+ <T_2 extends ModelBase<any, any>, J = ReturnType<T_2["toJSON"]>>(Model: ModelStatic<T_2>, params: Record<string, any>, allowAttrs?: string[] | undefined, toJSON?: false | undefined): Promise<{
32
+ rows: T_2[];
33
+ count: number;
34
+ }>;
35
+ <T_3 extends ModelBase<any, any>, J_1 = ReturnType<T_3["toJSON"]>>(Model: ModelStatic<T_3>, params: Record<string, any>, allowAttrs?: string[] | undefined, toJSON?: true | undefined): Promise<{
36
+ rows: J_1[];
37
+ count: number;
38
+ }>;
39
+ };
40
+ stats: <T_4 extends ModelBase<any, any>>(Model: ModelStatic<T_4>, params: Record<string, any>, where?: any, conf?: {
35
41
  dimensions?: Record<string, string> | undefined;
36
42
  metrics: Record<string, string>;
37
43
  pagination?: {
@@ -99,15 +99,7 @@ function Main(cnf, deps, utils) {
99
99
  };
100
100
  // count条件所需属性
101
101
  const COUNT_OPT = Object.freeze(["where", "include"]);
102
- /**
103
- * Restful list (R of CRUD) for list resource
104
- * @param Model Model definition of resources
105
- * @param params parameters for updating
106
- * @param allowAttrs Allow columns to be returned
107
- * @param toJSON Whether to directly return JSON formatted objects
108
- * @returns findAll resource result, object propoties has count, rows
109
- */
110
- const list = async (Model, params, allowAttrs, toJSON) => {
102
+ async function list(Model, params, allowAttrs, toJSON) {
111
103
  const opt = findAllOpts(Model, params);
112
104
  const { _ignoreTotal } = params;
113
105
  // 提高查询速度
@@ -117,13 +109,10 @@ function Main(cnf, deps, utils) {
117
109
  if (Array.isArray(allowAttrs) && allowAttrs.length)
118
110
  opt.attributes = allowAttrs;
119
111
  const rows = await Model.findAll(opt);
120
- if (toJSON) {
121
- for (let i = 0; i < rows.length; i += 1) {
122
- rows[i] = rows[i].toJSON();
123
- }
124
- }
125
- return { count, rows };
126
- };
112
+ if (!toJSON)
113
+ return { rows, count };
114
+ return { count, rows: rows.map((x) => x.toJSON()) };
115
+ }
127
116
  return { modify, add, remove, list, stats: (0, stats_1.Stats)(cnf, deps, utils) };
128
117
  }
129
118
  exports.Main = Main;
@@ -20,7 +20,7 @@ export declare function Router(deps: Deps): {
20
20
  put: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
21
21
  del: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
22
22
  } & {
23
- collection: (res: string, _routePath?: string | undefined, controller?: string | undefined) => void;
23
+ collection: (res: string, _routePath?: string | undefined) => void;
24
24
  model: (res: string, routePath?: string) => void;
25
25
  resource: (res: string, routePath?: string) => void;
26
26
  };
@@ -43,8 +43,8 @@ declare type PickResources<paths extends string, Keys extends string = PickFirst
43
43
  * type t2 = PickCollection<"user", "user.addFile" | "user.Files"> // File
44
44
  */
45
45
  declare type PickCollect<Keys extends string, paths extends string> = paths extends `${Keys}.add${infer A}` ? A : never;
46
- declare type PickCollection<Keys extends string, paths extends string, Collects extends string = PickCollect<Keys, paths>> = Collects extends any ? `${Keys}.add${Collects}` | `${Keys}.${Lowercase<Collects>}s` extends paths ? [Lowercase<Collects>, Keys] : never : never;
47
- export declare type PickCollections<paths extends string, Keys extends string = PickFirst<paths>> = Keys extends any ? PickCollection<Keys, paths> : never;
46
+ declare type PickCollection<Keys extends string, paths extends string, Collects extends string = PickCollect<Keys, paths>> = Collects extends any ? `${Keys}.add${Collects}` | `${Keys}.${Lowercase<Collects>}s` extends paths ? `${Keys}::${Lowercase<Collects>}` : never : never;
47
+ export declare type PickCollections<paths extends string, Keys extends string = PickFirst<paths>> = Keys extends any ? PickCollection<Keys, paths> | (`${Keys}.add` | `${Keys}.list` extends paths ? Keys : never) : never;
48
48
  /**
49
49
  * 利用领域方法路径类型集合,收窄 methodPath, 同时可以自动提示
50
50
  */
@@ -52,7 +52,7 @@ export declare type NarrowDomainPaths<Paths extends string> = {
52
52
  [k in normalVerb]: (routePath: NoramVerbArguments[0], ctlAct: Paths, code?: NoramVerbArguments[2], isList?: NoramVerbArguments[3], handler?: NoramVerbArguments[4], resHandler?: NoramVerbArguments[5]) => ReturnType<TRouter["get"]>;
53
53
  } & {
54
54
  model: (res: PickModels<Paths>, routePath?: string) => ReturnType<TRouter["model"]>;
55
- collection: PickCollections<Paths> extends [infer R, infer C] ? (res: R, _routePath?: string, controller?: C) => ReturnType<TRouter["collection"]> : never;
55
+ collection: (res: PickCollections<Paths>, routePath?: string) => ReturnType<TRouter["collection"]>;
56
56
  resource: (res: PickResources<Paths>, routePath?: string) => ReturnType<TRouter["resource"]>;
57
57
  };
58
58
  export {};
@@ -153,29 +153,33 @@ function Router(deps) {
153
153
  del: RouterVerbFn("del"),
154
154
  };
155
155
  /**
156
- * controller 为可选参数,如果不填写则控制器名称直接就是 res ,方法为 list,add
157
- * 如果设置了controller 则控制器为 controller,方法为 #{res}s, add{Res}
156
+ * 集合方法,集合方法包含了向集合添加元素,以及查看集合(列表)
157
+ * @param res 资源名称,如果有父级资源用双分号隔开 eg user, user::file
158
+ * @param _routePath 路径地址,可选,默认按照既定规则拼接
158
159
  */
159
- const collection = (res, _routePath, controller) => {
160
+ const collection = (res, _routePath) => {
161
+ const arr = res.split("::");
162
+ const name = arr[1] ? arr[1] : arr[0];
163
+ const controller = arr[1] ? arr[0] : null;
160
164
  let routePath;
161
165
  if (typeof _routePath !== "string") {
162
166
  if (controller) {
163
- routePath = `/${controller}s/:${controller}Id/${res}s`;
167
+ routePath = `/${controller}s/:${controller}Id/${name}s`;
164
168
  }
165
169
  else {
166
- routePath = `/${res}s`;
170
+ routePath = `/${name}s`;
167
171
  }
168
172
  }
169
173
  else {
170
174
  routePath = _routePath;
171
175
  }
172
176
  if (controller) {
173
- register("get", routePath, `${controller}.${res}s`, 200, true);
174
- register("post", routePath, `${controller}.add${ucwords(res)}`, 201);
177
+ register("get", routePath, `${controller}.${name}s`, 200, true);
178
+ register("post", routePath, `${controller}.add${ucwords(name)}`, 201);
175
179
  }
176
180
  else {
177
- register("get", routePath, `${res}.list`, 200, true);
178
- register("post", routePath, `${res}.add`, 201);
181
+ register("get", routePath, `${name}.list`, 200, true);
182
+ register("post", routePath, `${name}.add`, 201);
179
183
  }
180
184
  };
181
185
  const model = (res, routePath = `/${res}s/:id`) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.3",
3
+ "version": "0.3.6",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {