@domain.js/main 0.3.2 → 0.3.5

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,3 +1,11 @@
1
1
  {
2
- "typescript.tsdk": "node_modules/typescript/lib"
3
- }
2
+ "typescript.tsdk": "node_modules/typescript/lib",
3
+ "[markdown]": {
4
+ "editor.quickSuggestions": true
5
+ },
6
+ "editor.quickSuggestions": {
7
+ "other": true,
8
+ "comments": true,
9
+ "strings": true
10
+ }
11
+ }
@@ -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,26 +20,39 @@ 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
  };
27
27
  declare type TRouter = ReturnType<typeof Router>;
28
- export declare type ReplaceArrayItem<T extends any[], index extends number, R, S extends any[] = [], L extends number = S["length"]> = T extends [infer A, ...infer rest] ? L extends index ? [...S, R, ...rest] : ReplaceArrayItem<rest, index, R, [...S, A]> : never;
29
- /** 普通 route 方法名称 */
30
- declare type Keys = "get" | "post" | "put" | "patch" | "del";
31
- /** 从servers 路径字符串中提取可用作model的名称,目前还不严谨,聊胜于无 */
32
- declare type PickModelNames<paths extends string> = paths extends string ? paths extends `${infer F}.${string}` ? F : never : never;
33
- /** 替换函数的某个参数类型定义 */
34
- export declare type ParameterReplace<T extends (...args: any[]) => any, Index extends number, TR> = (...args: ReplaceArrayItem<Parameters<T>, Index, TR>) => ReturnType<T>;
28
+ /** 普通路径动作类型集合 */
29
+ declare type normalVerb = "get" | "post" | "put" | "del";
30
+ declare type NoramVerbArguments = Parameters<TRouter["get"]>;
31
+ /** 从用点分隔的字符串中提取第一部分 */
32
+ declare type PickFirst<paths extends string> = paths extends string ? paths extends `${infer F}.${string}` ? F : never : never;
33
+ /**
34
+ * services 路径中题可能的 model 名称的联合类型
35
+ */
36
+ declare type PickModels<paths extends string, Keys extends string = PickFirst<paths>> = Keys extends any ? `${Keys}.detail` | `${Keys}.modify` | `${Keys}.remove` extends paths ? Keys : never : never;
37
+ /**
38
+ * 从 services 路径中题可能的 resource 名称的联合类型
39
+ */
40
+ declare type PickResources<paths extends string, Keys extends string = PickFirst<paths>> = Keys extends any ? `${Keys}.add` | `${Keys}.list` | `${Keys}.detail` | `${Keys}.modify` | `${Keys}.remove` extends paths ? Keys : never : never;
41
+ /**
42
+ * 根据指定的 controller 尝试提取存在的 collectname
43
+ * type t2 = PickCollection<"user", "user.addFile" | "user.Files"> // File
44
+ */
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 ? `${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;
35
48
  /**
36
49
  * 利用领域方法路径类型集合,收窄 methodPath, 同时可以自动提示
37
50
  */
38
- export declare type NarrowDomainPaths<Paths extends string, ModelNames = PickModelNames<Paths>> = Omit<TRouter, Keys> & {
39
- [k in Keys]: ParameterReplace<TRouter["get"], 1, Paths>;
51
+ export declare type NarrowDomainPaths<Paths extends string> = {
52
+ [k in normalVerb]: (routePath: NoramVerbArguments[0], ctlAct: Paths, code?: NoramVerbArguments[2], isList?: NoramVerbArguments[3], handler?: NoramVerbArguments[4], resHandler?: NoramVerbArguments[5]) => ReturnType<TRouter["get"]>;
40
53
  } & {
41
- model: ParameterReplace<TRouter["model"], 0, ModelNames>;
42
- collection: ParameterReplace<ParameterReplace<TRouter["collection"], 0, ModelNames>, 2, ModelNames>;
43
- resource: ParameterReplace<TRouter["resource"], 0, ModelNames>;
54
+ model: (res: PickModels<Paths>, routePath?: string) => ReturnType<TRouter["model"]>;
55
+ collection: (res: PickCollections<Paths>, routePath?: string) => ReturnType<TRouter["collection"]>;
56
+ resource: (res: PickResources<Paths>, routePath?: string) => ReturnType<TRouter["resource"]>;
44
57
  };
45
58
  export {};
@@ -18,9 +18,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
18
18
  __setModuleDefault(result, mod);
19
19
  return result;
20
20
  };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
21
24
  Object.defineProperty(exports, "__esModule", { value: true });
22
25
  exports.Router = void 0;
23
- const _ = __importStar(require("lodash"));
26
+ const lodash_1 = __importDefault(require("lodash"));
24
27
  const errors = __importStar(require("restify-errors"));
25
28
  function Router(deps) {
26
29
  const { domain, apisRoute, utils, server, httpCodes = {}, makeProfileHook } = deps;
@@ -84,7 +87,7 @@ function Router(deps) {
84
87
  throw Error(`Missing domain method: ${methodPath}`);
85
88
  const { method } = domain[methodPath];
86
89
  /** 如果都没有则抛出异常 */
87
- if (!method || !_.isFunction(method)) {
90
+ if (!method || !lodash_1.default.isFunction(method)) {
88
91
  throw Error(`Missing domain method: ${methodPath}`);
89
92
  }
90
93
  server[verb](route, async (req, res, next) => {
@@ -115,7 +118,7 @@ function Router(deps) {
115
118
  if (!ok)
116
119
  res.send(code, results.rows);
117
120
  }
118
- else if (!_.isObject(results)) {
121
+ else if (!lodash_1.default.isObject(results)) {
119
122
  if (code === 204) {
120
123
  res.send(code);
121
124
  }
@@ -150,29 +153,33 @@ function Router(deps) {
150
153
  del: RouterVerbFn("del"),
151
154
  };
152
155
  /**
153
- * controller 为可选参数,如果不填写则控制器名称直接就是 res ,方法为 list,add
154
- * 如果设置了controller 则控制器为 controller,方法为 #{res}s, add{Res}
156
+ * 集合方法,集合方法包含了向集合添加元素,以及查看集合(列表)
157
+ * @param res 资源名称,如果有父级资源用双分号隔开 eg user, user::file
158
+ * @param _routePath 路径地址,可选,默认按照既定规则拼接
155
159
  */
156
- 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;
157
164
  let routePath;
158
165
  if (typeof _routePath !== "string") {
159
166
  if (controller) {
160
- routePath = `/${controller}s/:${controller}Id/${res}s`;
167
+ routePath = `/${controller}s/:${controller}Id/${name}s`;
161
168
  }
162
169
  else {
163
- routePath = `/${res}s`;
170
+ routePath = `/${name}s`;
164
171
  }
165
172
  }
166
173
  else {
167
174
  routePath = _routePath;
168
175
  }
169
176
  if (controller) {
170
- register("get", routePath, `${controller}.${res}s`, 200, true);
171
- 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);
172
179
  }
173
180
  else {
174
- register("get", routePath, `${res}.list`, 200, true);
175
- register("post", routePath, `${res}.add`, 201);
181
+ register("get", routePath, `${name}.list`, 200, true);
182
+ register("post", routePath, `${name}.add`, 201);
176
183
  }
177
184
  };
178
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.2",
3
+ "version": "0.3.5",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {