@domain.js/main 0.3.25 → 0.4.0

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,2 +1,2 @@
1
- import { CnfDef, DepsDef, PubSubDef, Cache } from "./Define";
1
+ import { Cache, CnfDef, DepsDef, PubSubDef } from "./Define";
2
2
  export declare const After: (lru: Pick<Cache, "del">, cnf: CnfDef, deps: Pick<DepsDef, "logger">, pubsub?: PubSubDef | undefined) => void;
@@ -18,6 +18,7 @@ const After = (lru, cnf, deps, pubsub) => {
18
18
  });
19
19
  const del = lru.del.bind(lru);
20
20
  lru.del = (key) => {
21
+ del(key);
21
22
  pub.publish(delSignalChannel, key);
22
23
  };
23
24
  sub.on("message", async (channel, key) => {
@@ -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; // 未击中次数
@@ -8,7 +8,7 @@ interface Deps {
8
8
  redis: Pick<Redis, "hget" | "hset" | "hdel" | "hincrby">;
9
9
  }
10
10
  export declare function Main(cnf: Cnf, deps: Deps): {
11
- get: (key: string) => Promise<number>;
11
+ get: (key: string) => Promise<string | null>;
12
12
  set: (key: string, value: string) => Promise<number>;
13
13
  del: (key: string) => Promise<number>;
14
14
  incr: (key: string, step?: number) => Promise<number>;
@@ -5,8 +5,7 @@ function Main(cnf, deps) {
5
5
  const { hash: { key: REDIS_KEY }, } = cnf;
6
6
  const { redis } = deps;
7
7
  const get = async (key) => {
8
- const num = await redis.hget(REDIS_KEY, key);
9
- return Number(num) | 0;
8
+ return redis.hget(REDIS_KEY, key);
10
9
  };
11
10
  const set = (key, value) => redis.hset(REDIS_KEY, key, value);
12
11
  const del = (key) => redis.hdel(REDIS_KEY, key);
@@ -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;
@@ -9,6 +9,10 @@ export declare type Client = Socket<DefaultEventsMap, DefaultEventsMap, DefaultE
9
9
  profile?: ReturnType<typeof makeProfile>;
10
10
  inited?: boolean;
11
11
  roomId?: string;
12
+ methods?: Record<string, Function>;
13
+ operator?: any;
14
+ /** 退出房间回调函数 */
15
+ quit?: Function;
12
16
  };
13
17
  declare const makeProfile: (client: Client, type: string | undefined, auth: string | Signature, extra?: Profile["extra"]) => Profile;
14
18
  export declare function BridgeSocket(io: Server, domain: Domain): void;
@@ -105,10 +105,7 @@ function BridgeSocket(io, domain) {
105
105
  }
106
106
  catch (e) {
107
107
  client.inited = false;
108
- if (e instanceof MyError) {
109
- client.emit("internalError", e.message, e.code || "unknown");
110
- return;
111
- }
108
+ client.emit("internalError", e.code || "unknown", e.message, e.data);
112
109
  console.error(e);
113
110
  }
114
111
  });
@@ -116,44 +113,39 @@ function BridgeSocket(io, domain) {
116
113
  try {
117
114
  if (!client.profile || !client.inited)
118
115
  return;
119
- const res = await entrance({ ...client.profile, roomId }, client);
116
+ const ret = await entrance({ ...client.profile, roomId }, client);
120
117
  client.profile.roomId = roomId;
121
118
  client.roomId = roomId;
122
- client.emit("entranced", res);
119
+ Object.assign(client, ret);
120
+ client.emit("entranced", { ...ret, methods: Object.keys(ret.methods) });
123
121
  }
124
122
  catch (e) {
125
- if (e instanceof MyError) {
126
- client.emit("internalError", e.message, e.code || "unknown");
127
- return;
128
- }
123
+ client.emit("internalError", e.code || "unknown", e.message, e.data);
129
124
  console.error(e);
130
125
  }
131
126
  });
132
127
  client.use(async ([name, params, responseId], next) => {
133
128
  if (name === "init" || name === "entrance")
134
129
  return next();
135
- const { method } = domain[name];
130
+ if (!client.methods)
131
+ throw new MyError("没有允许执行的方法", "请先进入房间");
132
+ const method = client.methods[name];
136
133
  try {
137
134
  if (!method)
138
135
  throw new MyError("notFound", "不存在该领域方法");
139
136
  if (!client.profile)
140
137
  throw new MyError("noAuth", "请先执行 init");
141
- const res = await method({ ...client.profile, method: name }, params);
138
+ const res = await method(...params);
142
139
  if (responseId) {
143
140
  client.emit("response", responseId, res);
144
141
  }
145
142
  }
146
143
  catch (e) {
147
- if (e instanceof Error) {
148
- if (responseId) {
149
- client.emit("responseError", responseId, e.code, e.message, e.data);
150
- }
151
- else {
152
- client.emit(`${name}Error`, e.code, e.message, e.data);
153
- }
144
+ if (responseId) {
145
+ client.emit("responseError", responseId, e.code, e.message, e.data);
154
146
  }
155
147
  else {
156
- console.error(e);
148
+ client.emit(`${name}Error`, e.code, e.message, e.data);
157
149
  }
158
150
  }
159
151
  return next();
@@ -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.25",
3
+ "version": "0.4.0",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {