@less-is-more/less-js 1.4.39-0 → 1.4.41
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/README.md +70 -76
- package/README_zh.md +147 -0
- package/package.json +1 -1
- package/src/router.js +5 -1
package/README.md
CHANGED
|
@@ -1,59 +1,58 @@
|
|
|
1
1
|
# lim-js
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## Introduction
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
ivanlin96@gmail.com
|
|
5
|
+
A quick utility library for Node.js and JavaScript, designed to simplify tasks with minimal code. Contact: ivanlin96@gmail.com
|
|
7
6
|
|
|
8
|
-
##
|
|
7
|
+
## Installation
|
|
9
8
|
|
|
10
|
-
```
|
|
11
|
-
npm install @less-is-more/less-js
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
## 开始
|
|
15
|
-
|
|
16
|
-
1. 生成数据库映射对象,可以使用 sequelize-automatic。具体请查看其官方 npm 说明。
|
|
17
|
-
2. src下建立index.js文件,可参考
|
|
18
|
-
```js
|
|
19
|
-
const { Router, Db, Redis } = require("@less-is-more/less-js");
|
|
20
|
-
const ApiController = require("./api/api-controller");
|
|
21
|
-
const process = require("process");
|
|
22
|
-
|
|
23
|
-
exports.init = (context, callback) => {
|
|
24
|
-
Db.init(
|
|
25
|
-
process.env.dbHost,
|
|
26
|
-
process.env.dbDatabase,
|
|
27
|
-
process.env.dbUser,
|
|
28
|
-
process.env.dbPassword
|
|
29
|
-
);
|
|
30
|
-
Redis.init(process.env.redisUrl, "default", process.env.redisPassword);
|
|
31
|
-
callback(null, "");
|
|
32
|
-
};
|
|
33
|
-
exports.handler = (req, res, context) => {
|
|
34
|
-
res.setHeader("content-type", "application/json");
|
|
35
|
-
const targets = {
|
|
36
|
-
"/api": new ApiController(),
|
|
37
|
-
};
|
|
38
|
-
Router.route(targets, req, res, context);
|
|
39
|
-
};
|
|
40
|
-
```
|
|
41
|
-
3. 自动生成增删查改。在项目根的命令行执行,然后选择数据库表
|
|
42
9
|
```shell
|
|
43
|
-
less-js
|
|
10
|
+
npm install @less-is-more/less-js
|
|
44
11
|
```
|
|
45
12
|
|
|
46
|
-
##
|
|
47
|
-
|
|
48
|
-
|
|
13
|
+
## Getting Started
|
|
14
|
+
|
|
15
|
+
1. Generate database mapping objects using `sequelize-automatic`. Please refer to its official npm documentation for details.
|
|
16
|
+
2. Create an `index.js` file under the `src` directory. You can refer to the following example:
|
|
17
|
+
```js
|
|
18
|
+
const { Router, Db, Redis } = require("@less-is-more/less-js");
|
|
19
|
+
const ApiController = require("./api/api-controller");
|
|
20
|
+
const process = require("process");
|
|
21
|
+
|
|
22
|
+
exports.init = (context, callback) => {
|
|
23
|
+
Db.init(
|
|
24
|
+
process.env.dbHost,
|
|
25
|
+
process.env.dbDatabase,
|
|
26
|
+
process.env.dbUser,
|
|
27
|
+
process.env.dbPassword
|
|
28
|
+
);
|
|
29
|
+
Redis.init(process.env.redisUrl, "default", process.env.redisPassword);
|
|
30
|
+
callback(null, "");
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
exports.handler = (req, res, context) => {
|
|
34
|
+
res.setHeader("content-type", "application/json");
|
|
35
|
+
const targets = {
|
|
36
|
+
"/api": new ApiController(),
|
|
37
|
+
};
|
|
38
|
+
Router.route(targets, req, res, context);
|
|
39
|
+
};
|
|
40
|
+
```
|
|
41
|
+
3. Automatically generate CRUD operations. Execute the following command in the root of the project and select the database table:
|
|
42
|
+
```shell
|
|
43
|
+
less-js-gen
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Features
|
|
47
|
+
|
|
48
|
+
Each function has specific documentation for its parameters, which will not be repeated here.
|
|
49
49
|
|
|
50
50
|
### Route
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
Controller 里面需要执行 send 或者直接 return 一个值(值会自动转换文本,来符合 send 的标准)。
|
|
52
|
+
Simple routing, usually placed in the entry function `index`. In the Controller, you need to execute `send` or directly return a value (the value will be automatically converted to text to meet the `send` standard).
|
|
54
53
|
|
|
55
54
|
```js
|
|
56
|
-
// ProductController
|
|
55
|
+
// ProductController is a class implementation
|
|
57
56
|
let targets = {
|
|
58
57
|
"/product": ProductController,
|
|
59
58
|
"/order": OrderController,
|
|
@@ -63,85 +62,80 @@ Router.route(targets, req, res);
|
|
|
63
62
|
|
|
64
63
|
### Cache
|
|
65
64
|
|
|
66
|
-
|
|
67
|
-
使用 redis 作为缓存,需要初始化
|
|
68
|
-
没有使用 annotation,在 mocha 测试快速。
|
|
65
|
+
Caching returns data if available, otherwise calls a function and saves the result. Uses Redis for caching and requires initialization. No annotations are used, allowing for quick testing with Mocha.
|
|
69
66
|
|
|
70
67
|
```js
|
|
71
|
-
const { Cache, Redis } = require("
|
|
72
|
-
//
|
|
73
|
-
Redis.init("
|
|
68
|
+
const { Cache, Redis } = require("@less-is-more/less-js");
|
|
69
|
+
// Initialize once, can be placed in a common area
|
|
70
|
+
Redis.init("address", "username", "password");
|
|
74
71
|
|
|
75
72
|
let testFunction = (a, b) => {
|
|
76
73
|
console.log(a + b);
|
|
77
74
|
return a + b + "";
|
|
78
75
|
};
|
|
79
|
-
//
|
|
76
|
+
// Call
|
|
80
77
|
const result = await Cache.get("test", 20, testFunction, testFunction, 1, 2);
|
|
81
78
|
```
|
|
82
79
|
|
|
83
80
|
### Db
|
|
84
81
|
|
|
85
|
-
|
|
86
|
-
使用到 sequelize,对象和查询方法可以参考官方文档
|
|
82
|
+
Currently supports MySQL. Uses Sequelize; refer to the official documentation for objects and query methods.
|
|
87
83
|
|
|
88
84
|
```js
|
|
89
|
-
const { Db } = require("
|
|
90
|
-
//
|
|
91
|
-
Db.init("
|
|
92
|
-
//
|
|
85
|
+
const { Db } = require("@less-is-more/less-js");
|
|
86
|
+
// Initialize once, can be placed in a common area
|
|
87
|
+
Db.init("address", "database", "user", "password");
|
|
88
|
+
// Simple query
|
|
93
89
|
let data = await Db.find(Menu, { ordering: "1" });
|
|
94
|
-
//
|
|
90
|
+
// With pagination
|
|
95
91
|
let data = await Db.find(Menu, { ordering: "2" }, [["id", "desc"]], 2, 3);
|
|
96
|
-
//
|
|
92
|
+
// Specify return columns
|
|
97
93
|
let data = await Db.find(Menu, { ordering: "0" }, [["id", "desc"]], 1, 10, [
|
|
98
94
|
"id",
|
|
99
95
|
]);
|
|
100
|
-
//
|
|
96
|
+
// Add, Product is a generated object, refer to sequelize-automate
|
|
101
97
|
Db.add(Product, {
|
|
102
98
|
code: "test",
|
|
103
|
-
name: "
|
|
99
|
+
name: "product",
|
|
104
100
|
});
|
|
105
|
-
//
|
|
101
|
+
// Update
|
|
106
102
|
Db.update(Menu, { name: "test name2", ordering: "2" }, { id: 7 });
|
|
107
|
-
//
|
|
103
|
+
// Delete, limited to one record
|
|
108
104
|
Db.delOne(Menu, { code: "abc" });
|
|
109
105
|
```
|
|
110
106
|
|
|
111
107
|
### Redis
|
|
112
108
|
|
|
113
|
-
|
|
109
|
+
Simple encapsulation. Commands are the same as the official ones.
|
|
114
110
|
|
|
115
111
|
```js
|
|
116
|
-
const { Redis } = require("
|
|
117
|
-
//
|
|
118
|
-
Redis.init("
|
|
119
|
-
//
|
|
112
|
+
const { Redis } = require("@less-is-more/less-js");
|
|
113
|
+
// Initialize once, can be placed in a common area
|
|
114
|
+
Redis.init("address", "username", "password");
|
|
115
|
+
// Execute command, call official command, parameter order is the same
|
|
120
116
|
let result = await Redis.exec("get", "test");
|
|
121
117
|
```
|
|
122
118
|
|
|
123
119
|
### Param
|
|
124
120
|
|
|
125
|
-
|
|
126
|
-
基本沿用 validator 的方法,https://www.npmjs.com/package/validator
|
|
127
|
-
check 方法新增 isNotEmpty, require。支持多个,空格隔开。默认 is 类的方法不含空
|
|
121
|
+
Parameter handling tool. Checks parameters, throws an Error on failure, returns a parameter set on success. Primarily uses methods from the `validator` package: https://www.npmjs.com/package/validator. The `check` method adds `isNotEmpty` and `require`. Supports multiple checks, separated by spaces. By default, `is` methods do not include empty values.
|
|
128
122
|
|
|
129
123
|
```js
|
|
130
124
|
let params = await Param.checkParams([
|
|
131
|
-
["
|
|
132
|
-
["
|
|
125
|
+
["return parameter key", value, "isNotEmpty", "error message"],
|
|
126
|
+
["return parameter key2", value2, "isNotEmpty", "error message2"],
|
|
133
127
|
]);
|
|
134
|
-
//
|
|
128
|
+
// Check if empty
|
|
135
129
|
Param.isBlank(value);
|
|
136
130
|
```
|
|
137
131
|
|
|
138
132
|
### Sms
|
|
139
133
|
|
|
140
|
-
|
|
134
|
+
Currently supports Alibaba Cloud's SMS service.
|
|
141
135
|
|
|
142
136
|
```js
|
|
143
|
-
//
|
|
137
|
+
// Initialize once, can be placed in a common area
|
|
144
138
|
Sms.init("key", "secret", "token");
|
|
145
139
|
Sms.debug(true);
|
|
146
|
-
Sms.send("
|
|
140
|
+
Sms.send("phone number", { info: "SMS parameters" }, "signature", "SMS code");
|
|
147
141
|
```
|
package/README_zh.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# lim-js
|
|
2
|
+
|
|
3
|
+
## 介绍
|
|
4
|
+
|
|
5
|
+
快捷处理函数 nodejs、js。基本实现一行搞定。
|
|
6
|
+
ivanlin96@gmail.com
|
|
7
|
+
|
|
8
|
+
## 安装使用
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
npm install @less-is-more/less-js
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## 开始
|
|
15
|
+
|
|
16
|
+
1. 生成数据库映射对象,可以使用 sequelize-automatic。具体请查看其官方 npm 说明。
|
|
17
|
+
2. src下建立index.js文件,可参考
|
|
18
|
+
```js
|
|
19
|
+
const { Router, Db, Redis } = require("@less-is-more/less-js");
|
|
20
|
+
const ApiController = require("./api/api-controller");
|
|
21
|
+
const process = require("process");
|
|
22
|
+
|
|
23
|
+
exports.init = (context, callback) => {
|
|
24
|
+
Db.init(
|
|
25
|
+
process.env.dbHost,
|
|
26
|
+
process.env.dbDatabase,
|
|
27
|
+
process.env.dbUser,
|
|
28
|
+
process.env.dbPassword
|
|
29
|
+
);
|
|
30
|
+
Redis.init(process.env.redisUrl, "default", process.env.redisPassword);
|
|
31
|
+
callback(null, "");
|
|
32
|
+
};
|
|
33
|
+
exports.handler = (req, res, context) => {
|
|
34
|
+
res.setHeader("content-type", "application/json");
|
|
35
|
+
const targets = {
|
|
36
|
+
"/api": new ApiController(),
|
|
37
|
+
};
|
|
38
|
+
Router.route(targets, req, res, context);
|
|
39
|
+
};
|
|
40
|
+
```
|
|
41
|
+
3. 自动生成增删查改。在项目根的命令行执行,然后选择数据库表
|
|
42
|
+
```shell
|
|
43
|
+
less-js-gen
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## 功能块
|
|
47
|
+
|
|
48
|
+
具体每个函数有具体说明每个参数的作用,以下不再重复。
|
|
49
|
+
|
|
50
|
+
### Route
|
|
51
|
+
|
|
52
|
+
简单路由。通常放在函数入口 index 中。
|
|
53
|
+
Controller 里面需要执行 send 或者直接 return 一个值(值会自动转换文本,来符合 send 的标准)。
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
// ProductController为类实现
|
|
57
|
+
let targets = {
|
|
58
|
+
"/product": ProductController,
|
|
59
|
+
"/order": OrderController,
|
|
60
|
+
};
|
|
61
|
+
Router.route(targets, req, res);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Cache
|
|
65
|
+
|
|
66
|
+
缓存,有数据就返回,没有就调用函数并保存结果。
|
|
67
|
+
使用 redis 作为缓存,需要初始化
|
|
68
|
+
没有使用 annotation,在 mocha 测试快速。
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
const { Cache, Redis } = require(" @less-is-more/less-js");
|
|
72
|
+
// 初始化一次就可以,可以放公用
|
|
73
|
+
Redis.init("地址", "用户名", "密码");
|
|
74
|
+
|
|
75
|
+
let testFunction = (a, b) => {
|
|
76
|
+
console.log(a + b);
|
|
77
|
+
return a + b + "";
|
|
78
|
+
};
|
|
79
|
+
// 调用
|
|
80
|
+
const result = await Cache.get("test", 20, testFunction, testFunction, 1, 2);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Db
|
|
84
|
+
|
|
85
|
+
数据库暂时支持 mysql。
|
|
86
|
+
使用到 sequelize,对象和查询方法可以参考官方文档
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
const { Db } = require(" @less-is-more/less-js");
|
|
90
|
+
// 初始化一次就可以,可以放公用
|
|
91
|
+
Db.init("地址", "数据库", "用户", "密码");
|
|
92
|
+
// 简单查询
|
|
93
|
+
let data = await Db.find(Menu, { ordering: "1" });
|
|
94
|
+
// 带分页
|
|
95
|
+
let data = await Db.find(Menu, { ordering: "2" }, [["id", "desc"]], 2, 3);
|
|
96
|
+
// 指定返回列
|
|
97
|
+
let data = await Db.find(Menu, { ordering: "0" }, [["id", "desc"]], 1, 10, [
|
|
98
|
+
"id",
|
|
99
|
+
]);
|
|
100
|
+
// 添加,Product是生成对象,可以参考sequelize-automate
|
|
101
|
+
Db.add(Product, {
|
|
102
|
+
code: "test",
|
|
103
|
+
name: "产品",
|
|
104
|
+
});
|
|
105
|
+
// 修改
|
|
106
|
+
Db.update(Menu, { name: "test name2", ordering: "2" }, { id: 7 });
|
|
107
|
+
// 删除。限制了一条记录
|
|
108
|
+
Db.delOne(Menu, { code: "abc" });
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Redis
|
|
112
|
+
|
|
113
|
+
简单封装。命令和官方一样。
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
const { Redis } = require(" @less-is-more/less-js");
|
|
117
|
+
// 初始化一次就可以,可以放公用
|
|
118
|
+
Redis.init("地址", "用户名", "密码");
|
|
119
|
+
// exec命令,调用官方命令,参数顺序一样
|
|
120
|
+
let result = await Redis.exec("get", "test");
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Param
|
|
124
|
+
|
|
125
|
+
参数处理工具。检查参数,错误抛 Error,成功返回参数集合
|
|
126
|
+
基本沿用 validator 的方法,https://www.npmjs.com/package/validator
|
|
127
|
+
check 方法新增 isNotEmpty, require。支持多个,空格隔开。默认 is 类的方法不含空
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
let params = await Param.checkParams([
|
|
131
|
+
["返回参数key", value, "isNotEmpty", "错误显示的内容"],
|
|
132
|
+
["返回参数key2", value2, "isNotEmpty", "错误显示的内容2"],
|
|
133
|
+
]);
|
|
134
|
+
// 检查是否空
|
|
135
|
+
Param.isBlank(value);
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Sms
|
|
139
|
+
|
|
140
|
+
暂时支持阿里云的短信服务
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
// 初始化一次就可以,可以放公用
|
|
144
|
+
Sms.init("key", "secret", "token");
|
|
145
|
+
Sms.debug(true);
|
|
146
|
+
Sms.send("手机号码", { info: "短信参数" }, "签名", "短信编号");
|
|
147
|
+
```
|
package/package.json
CHANGED
package/src/router.js
CHANGED
|
@@ -45,9 +45,13 @@ module.exports = class Router {
|
|
|
45
45
|
};
|
|
46
46
|
// 转换后调用原来的方法
|
|
47
47
|
await this.route(targets, req, res, context);
|
|
48
|
+
// 看看返回的headers中有没有content-type,没有的话默认为json
|
|
49
|
+
if (!res.headers["content-type"] && !res.headers["Content-Type"]) {
|
|
50
|
+
res.headers["content-type"] = "application/json";
|
|
51
|
+
}
|
|
48
52
|
return {
|
|
49
53
|
statusCode: res.statusCode || 200,
|
|
50
|
-
headers:
|
|
54
|
+
headers: res.headers,
|
|
51
55
|
body: res.content.toString("base64"),
|
|
52
56
|
isBase64Encoded: true,
|
|
53
57
|
};
|