@less-is-more/less-js 1.2.9 → 1.2.13

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@less-is-more/less-js",
3
- "version": "1.2.9",
3
+ "version": "1.2.13",
4
4
  "description": "Fast develop kit for nodejs",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
15
- "url": "https://gitee.com/lim/less-js.git"
15
+ "url": "https://gitee.com/shengapp/lim-js"
16
16
  },
17
17
  "author": "Less Is More",
18
18
  "license": "MIT",
@@ -21,6 +21,7 @@
21
21
  "ejs": "^3.1.6",
22
22
  "fs-extra": "^10.0.0",
23
23
  "inquirer": "^8.1.2",
24
+ "multiparty": "^4.2.2",
24
25
  "mysql2": "^2.2.5",
25
26
  "pg-hstore": "^2.3.3",
26
27
  "querystring": "^0.2.1",
package/src/db.js CHANGED
@@ -156,6 +156,27 @@ module.exports = class Db {
156
156
  });
157
157
  }
158
158
 
159
+ /**
160
+ * 查询所有数据,不分页
161
+ * @param {*} model 模型
162
+ * @param {*} params 查询参数,支持符号标记,如Db.Op.lt,具体参考https://sequelize.org/master/manual/model-querying-basics.html#operators
163
+ * @param {*} order 排序,参考默认值
164
+ * @param {*} attributes 只返回属性
165
+ * @returns
166
+ */
167
+ static async findAll(
168
+ model,
169
+ params = {},
170
+ order = [["id", "desc"]],
171
+ attributes = { exclude: [""] }
172
+ ) {
173
+ return await model(this.getInstance()).findAll({
174
+ where: params,
175
+ order: order,
176
+ attributes: attributes,
177
+ });
178
+ }
179
+
159
180
  static async findOne(
160
181
  model,
161
182
  params = {},
@@ -177,7 +198,7 @@ module.exports = class Db {
177
198
  if (data == null) {
178
199
  throw new Error("数据不存在");
179
200
  } else {
180
- data.destroy();
201
+ await data.destroy();
181
202
  }
182
203
  }
183
204
 
package/src/router.js CHANGED
@@ -2,6 +2,7 @@ const Ret = require("./ret.js");
2
2
  const Param = require("./param.js");
3
3
  const querystring = require("querystring");
4
4
  const body = require("body");
5
+ const multiparty = require("multiparty");
5
6
  const { promisify } = require("util");
6
7
 
7
8
  module.exports = class Router {
@@ -17,46 +18,13 @@ module.exports = class Router {
17
18
  */
18
19
  static async route(targets, req, res, context) {
19
20
  const startTime = new Date().getTime();
20
- console.log("method: " + req.method);
21
- console.log("path: " + req.path);
22
- const paths = req.path.split("/");
23
- const className = paths.length > 2 ? "/" + paths[paths.length - 2] : "/";
24
- const methodName = paths[paths.length - 1];
25
- let message;
21
+ const { methodName, className } = this._getPath(req);
26
22
  if (methodName) {
27
23
  const method = targets[className][methodName];
28
24
  if (method) {
29
25
  try {
30
26
  // 兼容非阿里云
31
- if (req.query) {
32
- req.queries = req.query;
33
- }
34
- console.log(
35
- "queries:",
36
- req.queries ? JSON.stringify(req.queries) : ""
37
- );
38
- const isFile =
39
- req.headers &&
40
- JSON.stringify(req.headers).includes("multipart/form-data");
41
- console.log("multipart:", isFile);
42
- if (req.method == "POST") {
43
- let data = await promisify(body)(req);
44
- if (isFile) {
45
- req.body = data.toString();
46
- } else {
47
- console.log("body: " + decodeURIComponent(data));
48
- req.body = querystring.parse(decodeURIComponent(data));
49
- }
50
- }
51
- req.params = {};
52
- if (!Param.isBlank(req.queries)) {
53
- Object.keys(req.queries).forEach(
54
- (n) => (req.params[n] = req.queries[n])
55
- );
56
- }
57
- if (!Param.isBlank(req.body) && !isFile) {
58
- Object.keys(req.body).forEach((n) => (req.params[n] = req.body[n]));
59
- }
27
+ await this._handleParams(req);
60
28
  // before
61
29
  if (targets.before) {
62
30
  console.log("start before action");
@@ -70,39 +38,91 @@ module.exports = class Router {
70
38
  await targets.after(req, res, context);
71
39
  }
72
40
  // controller方法有返回值的自动send
73
- if (!Param.isBlank(result)) {
74
- if (result instanceof Ret) {
75
- res.send(result.toString());
76
- } else {
77
- if (typeof result === "string") {
78
- res.send(result);
79
- } else {
80
- res.send(JSON.stringify(result));
81
- }
82
- }
83
- }
41
+ this._handleReturn(result, res);
84
42
  } catch (e) {
85
43
  console.log(e);
86
- message = e.message ? e.message : "出错啦";
44
+ return this._sendError(e.message ? e.message : "出错啦", res);
87
45
  }
88
46
  } else {
89
- message = "请输入正确方法";
47
+ return this._sendError("请输入正确方法", res);
90
48
  }
91
49
  } else {
92
- message = "请输入方法";
93
- }
94
- if (message) {
95
- console.log("error:", message);
96
- this.sendBack(message, res);
50
+ return this._sendError("请输入方法", res);
97
51
  }
98
52
  console.log("end", (new Date().getTime() - startTime) / 1000, "s");
99
- return message;
100
53
  }
101
54
 
102
- static sendBack(message, res) {
55
+ static _getPath(req) {
56
+ console.log("method: " + req.method);
57
+ console.log("path: " + req.path);
58
+ const paths = req.path.split("/");
59
+ const className = paths.length > 2 ? "/" + paths[paths.length - 2] : "/";
60
+ const methodName = paths[paths.length - 1];
61
+ return { methodName, className };
62
+ }
63
+
64
+ static async _handleParams(req) {
65
+ if (req.query) {
66
+ req.queries = req.query;
67
+ }
68
+ console.log("queries:", req.queries ? JSON.stringify(req.queries) : "");
69
+ const isFile =
70
+ req.headers &&
71
+ JSON.stringify(req.headers).includes("multipart/form-data");
72
+ console.log("multipart:", isFile);
73
+ if (req.method == "POST") {
74
+ if (isFile) {
75
+ await this._getFile(req);
76
+ console.log("params", req.params);
77
+ console.log("files", req.files);
78
+ } else {
79
+ const data = await promisify(body)(req);
80
+ console.log("body: " + decodeURIComponent(data));
81
+ req.body = querystring.parse(decodeURIComponent(data));
82
+ }
83
+ }
84
+ if (req.params == undefined) req.params = {};
85
+ if (!Param.isBlank(req.queries)) {
86
+ Object.keys(req.queries).forEach((n) => (req.params[n] = req.queries[n]));
87
+ }
88
+ if (!Param.isBlank(req.body) && !isFile) {
89
+ Object.keys(req.body).forEach((n) => (req.params[n] = req.body[n]));
90
+ }
91
+ }
92
+
93
+ static _getFile(req) {
94
+ return new Promise((resolve, reject) => {
95
+ const form = new multiparty.Form();
96
+
97
+ form.parse(req, function (err, fields, files) {
98
+ if (err) reject(err);
99
+ req.params = fields;
100
+ req.files = files;
101
+ resolve();
102
+ });
103
+ });
104
+ }
105
+
106
+ static _handleReturn(result, res) {
107
+ if (!Param.isBlank(result)) {
108
+ if (result instanceof Ret) {
109
+ res.send(result.toString());
110
+ } else {
111
+ if (typeof result === "string") {
112
+ res.send(result);
113
+ } else {
114
+ res.send(JSON.stringify(result));
115
+ }
116
+ }
117
+ }
118
+ }
119
+
120
+ static _sendError(message, res) {
121
+ console.log("error:", message);
103
122
  if (this.#defaultFormat) {
104
123
  res.send(new Ret(false, message, Ret.CODE_SYSTEM).toString());
105
124
  }
125
+ return message;
106
126
  }
107
127
 
108
128
  /**
package/test/test-db.js CHANGED
@@ -170,4 +170,12 @@ describe("db.js", () => {
170
170
  }
171
171
  });
172
172
  });
173
+
174
+ describe("findAll()", () => {
175
+ it("success", async () => {
176
+ let data = await Db.findAll(Menu, { ordering: "1" });
177
+ console.log(data);
178
+ assert(data.length > 0, "no data");
179
+ });
180
+ })
173
181
  });