@less-is-more/less-js 1.2.10 → 1.2.14
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/.vscode/settings.json +3 -0
- package/package.json +3 -2
- package/src/db.js +21 -0
- package/src/router.js +23 -5
- package/test/test-db.js +8 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@less-is-more/less-js",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.14",
|
|
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
|
|
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 = {},
|
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 {
|
|
@@ -52,8 +53,7 @@ module.exports = class Router {
|
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
static _getPath(req) {
|
|
55
|
-
console.log("
|
|
56
|
-
console.log("path: " + req.path);
|
|
56
|
+
console.log("path:", req.method, req.path);
|
|
57
57
|
const paths = req.path.split("/");
|
|
58
58
|
const className = paths.length > 2 ? "/" + paths[paths.length - 2] : "/";
|
|
59
59
|
const methodName = paths[paths.length - 1];
|
|
@@ -70,15 +70,17 @@ module.exports = class Router {
|
|
|
70
70
|
JSON.stringify(req.headers).includes("multipart/form-data");
|
|
71
71
|
console.log("multipart:", isFile);
|
|
72
72
|
if (req.method == "POST") {
|
|
73
|
-
let data = await promisify(body)(req);
|
|
74
73
|
if (isFile) {
|
|
75
|
-
|
|
74
|
+
await this._getFile(req);
|
|
75
|
+
console.log("params:", req.params);
|
|
76
|
+
console.log("files:", req.files);
|
|
76
77
|
} else {
|
|
78
|
+
const data = await promisify(body)(req);
|
|
77
79
|
console.log("body: " + decodeURIComponent(data));
|
|
78
80
|
req.body = querystring.parse(decodeURIComponent(data));
|
|
79
81
|
}
|
|
80
82
|
}
|
|
81
|
-
req.params = {};
|
|
83
|
+
if (req.params == undefined) req.params = {};
|
|
82
84
|
if (!Param.isBlank(req.queries)) {
|
|
83
85
|
Object.keys(req.queries).forEach((n) => (req.params[n] = req.queries[n]));
|
|
84
86
|
}
|
|
@@ -87,14 +89,30 @@ module.exports = class Router {
|
|
|
87
89
|
}
|
|
88
90
|
}
|
|
89
91
|
|
|
92
|
+
static _getFile(req) {
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
const form = new multiparty.Form();
|
|
95
|
+
|
|
96
|
+
form.parse(req, function (err, fields, files) {
|
|
97
|
+
if (err) reject(err);
|
|
98
|
+
req.params = fields;
|
|
99
|
+
req.files = files;
|
|
100
|
+
resolve();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
90
105
|
static _handleReturn(result, res) {
|
|
91
106
|
if (!Param.isBlank(result)) {
|
|
92
107
|
if (result instanceof Ret) {
|
|
108
|
+
console.log("return:", result.toString());
|
|
93
109
|
res.send(result.toString());
|
|
94
110
|
} else {
|
|
95
111
|
if (typeof result === "string") {
|
|
112
|
+
console.log("return:", result);
|
|
96
113
|
res.send(result);
|
|
97
114
|
} else {
|
|
115
|
+
console.log("return:", JSON.stringify(result));
|
|
98
116
|
res.send(JSON.stringify(result));
|
|
99
117
|
}
|
|
100
118
|
}
|
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
|
});
|