@domain.js/main 0.3.5 → 0.3.8

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"];
@@ -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, {
@@ -17,6 +17,6 @@ export interface ParamsSchema<Params extends {}, Keys extends string | number |
17
17
  /** 当前参数的描述信息 */
18
18
  description: string;
19
19
  /** 当前参数的类型 */
20
- type: "string" | "number" | "integer" | "object" | "array";
20
+ type: "string" | "number" | "integer" | "object" | "array" | "boolean";
21
21
  }>;
22
22
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.5",
3
+ "version": "0.3.8",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {