@domain.js/main 0.3.4 → 0.3.7

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;
@@ -68,15 +68,15 @@ const makeProfile = (client, type = "user", auth, extra = {}) => {
68
68
  return obj;
69
69
  };
70
70
  function BridgeSocket(io, domain) {
71
- const { method: subscribe } = domain["message.subscribe"];
72
- const { method: unsubscribe } = domain["message.unsubscribe"];
73
- const { method: entrance } = domain["message.entrance"];
71
+ const { method: subscribe } = domain["socket.subscribe"];
72
+ const { method: unsubscribe } = domain["socket.unsubscribe"];
73
+ const { method: entrance } = domain["socket.entrance"];
74
74
  if (!subscribe)
75
- throw Error("要启用 socket 服务,必须要要有 message.subscribe 方法,用来处理 socket 订阅");
75
+ throw Error("要启用 socket 服务,必须要要有 socket.subscribe 方法,用来处理 socket 订阅");
76
76
  if (!unsubscribe)
77
- throw Error("要启用 socket 服务,必须要要有 message.unsubscribe 方法,用来处理 socket 退订");
77
+ throw Error("要启用 socket 服务,必须要要有 socket.unsubscribe 方法,用来处理 socket 退订");
78
78
  if (!entrance)
79
- throw Error("要启用 socket 服务,必须要要有 message.entrance 方法,用来处理 加入某个房间");
79
+ throw Error("要启用 socket 服务,必须要要有 socket.entrance 方法,用来处理 加入某个房间");
80
80
  io.on("connection", (client) => {
81
81
  // 定义toJSON 避免 schema 验证报错
82
82
  Object.assign(client, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.4",
3
+ "version": "0.3.7",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {