chanjs 1.0.30 → 1.0.33
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 +74 -73
- package/package.json +6 -5
- package/README.en.md +0 -100
- package/core/lib/config/config.js +0 -45
- package/core/lib/controller/controller.js +0 -25
- package/core/lib/extend/helper.js +0 -78
- package/core/lib/index.js +0 -41
- package/core/lib/middleware/template.js +0 -38
- package/core/lib/service/service.js +0 -190
- package/publish.bat +0 -4
package/README.md
CHANGED
@@ -1,98 +1,99 @@
|
|
1
|
-
# 中文 | [English](./README.en.md)
|
2
1
|
|
3
|
-
#
|
2
|
+
# Chan.js
|
4
3
|
|
5
|
-
Chan.js
|
4
|
+
Chan.js is a lightweight MVC framework developed in pure JavaScript based on Express. It embodies functional programming concepts, offering superior performance, clear code, and easy-to-follow processes that ensure high maintainability.
|
6
5
|
|
7
|
-
##
|
6
|
+
## features
|
8
7
|
|
9
|
-
-
|
10
|
-
-
|
11
|
-
-
|
12
|
-
-
|
13
|
-
-
|
14
|
-
-
|
15
|
-
-
|
16
|
-
-
|
17
|
-
-
|
18
|
-
-
|
8
|
+
- Built on top of Express
|
9
|
+
- Supports ES6 syntax
|
10
|
+
- Modular Design
|
11
|
+
- Supports multiple module routes
|
12
|
+
- Supports multiple module views
|
13
|
+
- Supports multiple module controllers
|
14
|
+
- Supports multiple module services
|
15
|
+
- Plugin-based architecture
|
16
|
+
- Lightweight (core code within 300 lines)
|
17
|
+
- Long-term maintenance
|
19
18
|
|
20
|
-
##
|
19
|
+
## Conventions Over Configuration
|
21
20
|
|
22
21
|
```code
|
23
22
|
|- app
|
24
|
-
|- config
|
25
|
-
|- module
|
26
|
-
|- module1
|
27
|
-
|- controller
|
28
|
-
|- service
|
29
|
-
|- view
|
30
|
-
|- router.js
|
31
|
-
|- module2
|
32
|
-
|- controller
|
33
|
-
|- service
|
34
|
-
|- view
|
35
|
-
|- router.js
|
36
|
-
|- extend
|
37
|
-
|- middleware
|
38
|
-
|- plugin
|
39
|
-
|- plus-module1
|
40
|
-
|- controller
|
41
|
-
|- service
|
42
|
-
|- view
|
43
|
-
|- router.js
|
44
|
-
|- module2
|
45
|
-
|- controller
|
46
|
-
|- service
|
47
|
-
|- view
|
48
|
-
|- router.js
|
49
|
-
|- public
|
23
|
+
|- config
|
24
|
+
|- module
|
25
|
+
|- module1
|
26
|
+
|- controller
|
27
|
+
|- service
|
28
|
+
|- view
|
29
|
+
|- router.js
|
30
|
+
|- module2
|
31
|
+
|- controller
|
32
|
+
|- service
|
33
|
+
|- view
|
34
|
+
|- router.js
|
35
|
+
|- extend
|
36
|
+
|- middleware
|
37
|
+
|- plugin
|
38
|
+
|- plus-module1
|
39
|
+
|- controller
|
40
|
+
|- service
|
41
|
+
|- view
|
42
|
+
|- router.js
|
43
|
+
|- module2
|
44
|
+
|- controller
|
45
|
+
|- service
|
46
|
+
|- view
|
47
|
+
|- router.js
|
48
|
+
|- public
|
50
49
|
|- index.js
|
51
50
|
```
|
52
51
|
|
53
|
-
###
|
52
|
+
### Initialization Process
|
54
53
|
|
55
|
-
-
|
56
|
-
-
|
57
|
-
-
|
58
|
-
-
|
59
|
-
-
|
60
|
-
-
|
61
|
-
-
|
62
|
-
-
|
63
|
-
-
|
64
|
-
-
|
65
|
-
-
|
66
|
-
- beforeStart()
|
67
|
-
- run()
|
54
|
+
- Initialization
|
55
|
+
- Load configuration
|
56
|
+
- Load modules
|
57
|
+
- Load services
|
58
|
+
- Load controllers
|
59
|
+
- Load routers
|
60
|
+
- Load extensions
|
61
|
+
- Load plugins
|
62
|
+
- Load services
|
63
|
+
- Load controllers
|
64
|
+
- Load routers
|
65
|
+
- beforeStart() Hook to merge configurations from the database into the configuration
|
66
|
+
- run() Start the server
|
68
67
|
|
69
|
-
###
|
68
|
+
### Official Website
|
70
69
|
|
71
|
-
|
72
|
-
|
70
|
+
A CMS system developed with Chan.js
|
71
|
+
Website:<https://www.chancms.top>
|
73
72
|
|
74
|
-
###
|
73
|
+
### Highlights
|
75
74
|
|
76
|
-
-
|
77
|
-
-
|
78
|
-
-
|
79
|
-
-
|
80
|
-
-
|
81
|
-
-
|
82
|
-
-
|
83
|
-
-
|
84
|
-
-
|
85
|
-
-
|
75
|
+
- Configuration files
|
76
|
+
- Multi-module MVC structure
|
77
|
+
- Plugin MVC support
|
78
|
+
- CORS cross-origin configuration support
|
79
|
+
- MySQL database support
|
80
|
+
- Route control
|
81
|
+
- Art-template template engine
|
82
|
+
- Static resources management
|
83
|
+
- Cookie handling
|
84
|
+
- Logging capabilities
|
86
85
|
|
87
|
-
###
|
86
|
+
### run
|
88
87
|
|
89
88
|
```javascript
|
90
89
|
const Chanjs = require("chanjs");
|
91
90
|
const chan = new Chanjs();
|
92
|
-
|
91
|
+
// Load middleware
|
93
92
|
chan.beforeStart(fn);
|
94
|
-
|
93
|
+
// Scan modules
|
95
94
|
chan.start();
|
96
|
-
|
95
|
+
// Start the server
|
97
96
|
chan.run();
|
98
97
|
```
|
98
|
+
|
99
|
+
This framework is designed for developers who seek a balance between simplicity and functionality, providing a robust foundation for building web applications with ease.
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "chanjs",
|
3
|
-
"version": "1.0.
|
4
|
-
"description": "
|
3
|
+
"version": "1.0.33",
|
4
|
+
"description": "Chanjs is a lightweight MVC framework developed based on express MySQL and pure JavaScript.",
|
5
5
|
"main": "core/chan.js",
|
6
6
|
"module": "core/chan.js",
|
7
7
|
"keywords": [
|
@@ -26,7 +26,8 @@
|
|
26
26
|
"mysql2": "^3.11.0",
|
27
27
|
"dayjs": "^1.11.12"
|
28
28
|
},
|
29
|
-
"
|
30
|
-
"
|
31
|
-
|
29
|
+
"homepage": "https://www.npmjs.com/package/chanjs",
|
30
|
+
"files": [
|
31
|
+
"README.md"
|
32
|
+
]
|
32
33
|
}
|
package/README.en.md
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
# [中文](./README.md) | English
|
2
|
-
|
3
|
-
# <center>Chan.js MVC Framework</center>
|
4
|
-
|
5
|
-
Chan.js is a lightweight MVC framework developed in pure JavaScript based on Express. It embodies functional programming concepts, offering superior performance, clear code, and easy-to-follow processes that ensure high maintainability.
|
6
|
-
|
7
|
-
## 特性
|
8
|
-
|
9
|
-
- Built on top of Express
|
10
|
-
- Supports ES6 syntax
|
11
|
-
- Modular Design
|
12
|
-
- Supports multiple module routes
|
13
|
-
- Supports multiple module views
|
14
|
-
- Supports multiple module controllers
|
15
|
-
- Supports multiple module services
|
16
|
-
- Plugin-based architecture
|
17
|
-
- Lightweight (core code within 300 lines)
|
18
|
-
- Long-term maintenance
|
19
|
-
|
20
|
-
## Conventions Over Configuration
|
21
|
-
|
22
|
-
```code
|
23
|
-
|- app
|
24
|
-
|- config 配置
|
25
|
-
|- module 模块1
|
26
|
-
|- module1 模块1
|
27
|
-
|- controller 控制器
|
28
|
-
|- service 服务模型
|
29
|
-
|- view 视图模板
|
30
|
-
|- router.js 路由
|
31
|
-
|- module2 模块2
|
32
|
-
|- controller 控制器
|
33
|
-
|- service 服务模型
|
34
|
-
|- view 视图模板
|
35
|
-
|- router.js路由
|
36
|
-
|- extend 扩展
|
37
|
-
|- middleware 中间件
|
38
|
-
|- plugin 插件
|
39
|
-
|- plus-module1 插件1
|
40
|
-
|- controller 控制器
|
41
|
-
|- service 服务模型
|
42
|
-
|- view 视图模板
|
43
|
-
|- router.js 路由
|
44
|
-
|- module2 插件2
|
45
|
-
|- controller 控制器
|
46
|
-
|- service 服务模型
|
47
|
-
|- view 视图模板
|
48
|
-
|- router.js路由
|
49
|
-
|- public 静态文件
|
50
|
-
|- index.js
|
51
|
-
```
|
52
|
-
|
53
|
-
### Initialization Process
|
54
|
-
|
55
|
-
- Initialization
|
56
|
-
- Load configuration
|
57
|
-
- Load modules
|
58
|
-
- Load services
|
59
|
-
- Load controllers
|
60
|
-
- Load routers
|
61
|
-
- Load extensions
|
62
|
-
- Load plugins
|
63
|
-
- Load services
|
64
|
-
- Load controllers
|
65
|
-
- Load routers
|
66
|
-
- beforeStart() Hook to merge configurations from the database into the configuration
|
67
|
-
- run() Start the server
|
68
|
-
|
69
|
-
### Official Website
|
70
|
-
|
71
|
-
A CMS system developed with Chan.js
|
72
|
-
Website:<https://www.chancms.top>
|
73
|
-
|
74
|
-
### Highlights
|
75
|
-
|
76
|
-
- Configuration files
|
77
|
-
- Multi-module MVC structure
|
78
|
-
- Plugin MVC support
|
79
|
-
- CORS cross-origin configuration support
|
80
|
-
- MySQL database support
|
81
|
-
- Route control
|
82
|
-
- Art-template template engine
|
83
|
-
- Static resources management
|
84
|
-
- Cookie handling
|
85
|
-
- Logging capabilities
|
86
|
-
|
87
|
-
### 运行
|
88
|
-
|
89
|
-
```javascript
|
90
|
-
const Chanjs = require("chanjs");
|
91
|
-
const chan = new Chanjs();
|
92
|
-
// Load middleware
|
93
|
-
chan.beforeStart(fn);
|
94
|
-
// Scan modules
|
95
|
-
chan.start();
|
96
|
-
// Start the server
|
97
|
-
chan.run();
|
98
|
-
```
|
99
|
-
|
100
|
-
This framework is designed for developers who seek a balance between simplicity and functionality, providing a robust foundation for building web applications with ease.
|
@@ -1,45 +0,0 @@
|
|
1
|
-
const path = require('path');
|
2
|
-
|
3
|
-
/**
|
4
|
-
* @description 根目录
|
5
|
-
*/
|
6
|
-
const ROOT_PATH = process.cwd();
|
7
|
-
// 读取package.json文件
|
8
|
-
const {version='1.0.0',author='明空'} = require(path.join(ROOT_PATH, 'package.json'));
|
9
|
-
|
10
|
-
/**
|
11
|
-
* @description 程序目录
|
12
|
-
*/
|
13
|
-
const APP_PATH = path.join(ROOT_PATH, 'app');
|
14
|
-
|
15
|
-
let config = {
|
16
|
-
JSON_LIMIT:"100kb",
|
17
|
-
logger : {
|
18
|
-
level: 'dev',
|
19
|
-
},
|
20
|
-
version,
|
21
|
-
author,
|
22
|
-
env:'dev',
|
23
|
-
template:'default',
|
24
|
-
views:[], //模板路径
|
25
|
-
static:[{ //静态文件路径
|
26
|
-
prefix: "/public/",
|
27
|
-
dir: ["app/public"],
|
28
|
-
maxAge: 0,
|
29
|
-
}],
|
30
|
-
database:{
|
31
|
-
client: "mysql2",
|
32
|
-
host: "localhost",
|
33
|
-
port: "3306",
|
34
|
-
user: "root",
|
35
|
-
password: "123456",
|
36
|
-
database: "chancms",
|
37
|
-
charset: "utf8mb4",
|
38
|
-
}
|
39
|
-
}
|
40
|
-
|
41
|
-
module.exports = {
|
42
|
-
ROOT_PATH,
|
43
|
-
APP_PATH,
|
44
|
-
...config
|
45
|
-
}
|
@@ -1,25 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* @description
|
3
|
-
* 控制器基类
|
4
|
-
*/
|
5
|
-
class Controller {
|
6
|
-
constructor() {}
|
7
|
-
|
8
|
-
download(res, file) {
|
9
|
-
res.download(file);
|
10
|
-
}
|
11
|
-
|
12
|
-
success(res, data = null) {
|
13
|
-
res.json({ code: 200, msg: "success", data: data });
|
14
|
-
}
|
15
|
-
|
16
|
-
fail(res, msg = "操作失败,请稍后再试") {
|
17
|
-
res.json({ code: 400, msg: msg });
|
18
|
-
}
|
19
|
-
|
20
|
-
error(res, msg) {
|
21
|
-
res.json({ code: 500, msg: msg });
|
22
|
-
}
|
23
|
-
}
|
24
|
-
|
25
|
-
module.exports = Controller;
|
@@ -1,78 +0,0 @@
|
|
1
|
-
const knex = require('knex');
|
2
|
-
|
3
|
-
/**
|
4
|
-
* @description 实例化一个类,并将该类的所有方法绑定到一个新的对象上。
|
5
|
-
* @param {Function} className - 需要实例化的类。
|
6
|
-
*@returns {Object} 包含绑定方法的对象。
|
7
|
-
*/
|
8
|
-
function bindClass(className) {
|
9
|
-
let obj = {};
|
10
|
-
const cls = new className();
|
11
|
-
Object.getOwnPropertyNames(cls.constructor.prototype).forEach(
|
12
|
-
(methodName) => {
|
13
|
-
if (
|
14
|
-
methodName !== "constructor" &&
|
15
|
-
typeof cls[methodName] === "function"
|
16
|
-
) {
|
17
|
-
obj[methodName] = cls[methodName].bind(cls);
|
18
|
-
}
|
19
|
-
}
|
20
|
-
);
|
21
|
-
return obj;
|
22
|
-
}
|
23
|
-
|
24
|
-
|
25
|
-
function createKnex(opt) {
|
26
|
-
let config = {
|
27
|
-
host:"localhost",
|
28
|
-
port:"3306",
|
29
|
-
client:"mysql2",
|
30
|
-
charset:"utf8mb4",
|
31
|
-
debug:false,
|
32
|
-
...opt
|
33
|
-
};
|
34
|
-
return knex({
|
35
|
-
client: config.client,
|
36
|
-
connection: {
|
37
|
-
host:config.host,
|
38
|
-
port:config.port,
|
39
|
-
user:config.user,
|
40
|
-
password:config.password,
|
41
|
-
database:config.database,
|
42
|
-
charset:config.charset,
|
43
|
-
},
|
44
|
-
debug: config.debug,
|
45
|
-
//默认为{min: 2, max: 10}
|
46
|
-
pool: {
|
47
|
-
min: 0,
|
48
|
-
max: 2,
|
49
|
-
},
|
50
|
-
log: {
|
51
|
-
warn(message) {
|
52
|
-
console.error("[knex warn]", message);
|
53
|
-
},
|
54
|
-
error(message) {
|
55
|
-
console.error("[knex error]", message);
|
56
|
-
},
|
57
|
-
},
|
58
|
-
});
|
59
|
-
}
|
60
|
-
/**
|
61
|
-
*
|
62
|
-
* @param {*} module 模块目录
|
63
|
-
* @returns Array
|
64
|
-
* @description 将web模块放到最后加载
|
65
|
-
*/
|
66
|
-
function loadWebToEnd(module=[]){
|
67
|
-
const index = module.indexOf('web');
|
68
|
-
if (index !== -1) {
|
69
|
-
const web = module.splice(index, 1);
|
70
|
-
module.push(web[0]);
|
71
|
-
}
|
72
|
-
return module;
|
73
|
-
}
|
74
|
-
module.exports = {
|
75
|
-
bindClass,
|
76
|
-
createKnex,
|
77
|
-
loadWebToEnd
|
78
|
-
};
|
package/core/lib/index.js
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
const express = require("express");
|
2
|
-
const cookieParser = require("cookie-parser");
|
3
|
-
const favicon = require("serve-favicon");
|
4
|
-
const morgan = require("morgan");
|
5
|
-
const path = require("path");
|
6
|
-
const template = require("./middleware/template.js");
|
7
|
-
module.exports = async function (app, config) {
|
8
|
-
const { logger, APP_PATH, cookieKey, static, JSON_LIMIT, appName, version } =
|
9
|
-
config;
|
10
|
-
//日志
|
11
|
-
app.use(morgan(logger.level));
|
12
|
-
|
13
|
-
// favicon 图标
|
14
|
-
app.use(favicon(path.join(APP_PATH, "public/favicon.ico")));
|
15
|
-
|
16
|
-
//cookie
|
17
|
-
app.use(cookieParser(cookieKey));
|
18
|
-
|
19
|
-
//解析接口 json & url
|
20
|
-
app.use(express.json({ limit: JSON_LIMIT }));
|
21
|
-
app.use(express.urlencoded({ extended: false }));
|
22
|
-
|
23
|
-
//配置模板引擎
|
24
|
-
template(app, config);
|
25
|
-
|
26
|
-
//使用静态资源 ,
|
27
|
-
if (static.length > 0) {
|
28
|
-
static.forEach((item) => {
|
29
|
-
const { prefix, dir, maxAge } = item;
|
30
|
-
app.use(prefix, express.static(dir, { maxAge: maxAge || 0 }));
|
31
|
-
});
|
32
|
-
}
|
33
|
-
|
34
|
-
//设置头信息
|
35
|
-
app.use((req, res, next) => {
|
36
|
-
res.setHeader("Create-By", "Chanjs");
|
37
|
-
res.setHeader("X-Powered-By", appName);
|
38
|
-
res.setHeader(appName, version);
|
39
|
-
next();
|
40
|
-
});
|
41
|
-
};
|
@@ -1,38 +0,0 @@
|
|
1
|
-
const path = require("path");
|
2
|
-
const template = require("art-template");
|
3
|
-
const dayjs = require("dayjs");
|
4
|
-
|
5
|
-
// 注册 dateFormat 函数
|
6
|
-
template.defaults.imports.dateFormat = function (date, format) {
|
7
|
-
if (!date) {
|
8
|
-
return "";
|
9
|
-
}
|
10
|
-
// 如果传入的是一个 Date 对象,转换为 dayjs 对象
|
11
|
-
if (
|
12
|
-
date instanceof Date ||
|
13
|
-
typeof date === "string" ||
|
14
|
-
typeof date === "number"
|
15
|
-
) {
|
16
|
-
date = dayjs(date);
|
17
|
-
} else {
|
18
|
-
return "";
|
19
|
-
}
|
20
|
-
return date.format(format);
|
21
|
-
};
|
22
|
-
|
23
|
-
module.exports = (app, config) => {
|
24
|
-
const { APP_PATH, views, env } = config;
|
25
|
-
//默认home view
|
26
|
-
const home = path.join(APP_PATH, `modules/web/view`);
|
27
|
-
//合并插件中的view
|
28
|
-
const all = [...views, home];
|
29
|
-
|
30
|
-
app.set("view options", {
|
31
|
-
debug: env === "dev",
|
32
|
-
cache: env === "prd",
|
33
|
-
minimize: true,
|
34
|
-
});
|
35
|
-
app.set("view engine", "html");
|
36
|
-
app.set("views", all);
|
37
|
-
app.engine(".html", require("express-art-template"));
|
38
|
-
};
|
@@ -1,190 +0,0 @@
|
|
1
|
-
class BaseService {
|
2
|
-
/**
|
3
|
-
* @description 构造函数
|
4
|
-
* @param {*} knex - knex实例
|
5
|
-
* @param {*} model - 模型名称
|
6
|
-
*/
|
7
|
-
constructor(knex, model = "") {
|
8
|
-
this.knex = knex;
|
9
|
-
this.model = model;
|
10
|
-
}
|
11
|
-
|
12
|
-
/**
|
13
|
-
* @description 查找所有符合条件的记录
|
14
|
-
* @param {*} query - 查询条件,例如:{name: 'test', age: 18}
|
15
|
-
* @returns {Promise} 返回符合条件的记录
|
16
|
-
*/
|
17
|
-
query(query) {
|
18
|
-
return this.knex(this.model).where(query);
|
19
|
-
}
|
20
|
-
|
21
|
-
/**
|
22
|
-
* @description 查询表所有记录,慎用
|
23
|
-
* @returns {Promise} 返回所有记录
|
24
|
-
*/
|
25
|
-
all() {
|
26
|
-
return this.knex(this.model).select();
|
27
|
-
}
|
28
|
-
|
29
|
-
/**
|
30
|
-
* @description 查询指定id的记录
|
31
|
-
* @param {*} id
|
32
|
-
* @returns {Promise} 返回指定id的记录
|
33
|
-
*/
|
34
|
-
findById(id) {
|
35
|
-
return this.knex(this.model).where("id", id).first();
|
36
|
-
}
|
37
|
-
|
38
|
-
/**
|
39
|
-
* @description 查询分页数据
|
40
|
-
* @param {*} current
|
41
|
-
* @param {*} pageSize
|
42
|
-
* @param {*} query
|
43
|
-
* @returns {Promise} 返回分页数据
|
44
|
-
*/
|
45
|
-
async findListAndQuery(current = 1, pageSize = 10, query = {}) {
|
46
|
-
const offset = (current - 1) * pageSize;
|
47
|
-
|
48
|
-
let countQuery = this.knex(this.model).count("* as total");
|
49
|
-
let dataQuery = this.knex(this.model);
|
50
|
-
|
51
|
-
// 应用查询条件
|
52
|
-
for (const key in query) {
|
53
|
-
dataQuery = dataQuery.where(key, query[key]);
|
54
|
-
countQuery = countQuery.where(key, query[key]);
|
55
|
-
}
|
56
|
-
|
57
|
-
// 获取总记录数
|
58
|
-
const total = await countQuery.first();
|
59
|
-
|
60
|
-
// 获取分页数据
|
61
|
-
const data = await dataQuery.offset(offset).limit(pageSize);
|
62
|
-
|
63
|
-
return { data, total: parseInt(total.total, 10) };
|
64
|
-
}
|
65
|
-
|
66
|
-
/**
|
67
|
-
* @description 查询指定id的记录
|
68
|
-
* @param {*} id - 记录id
|
69
|
-
* @returns {Promise} 返回指定id的记录
|
70
|
-
*/
|
71
|
-
findOneById(id) {
|
72
|
-
return this.knex(this.model).where("id", id).select();
|
73
|
-
}
|
74
|
-
|
75
|
-
/**
|
76
|
-
* @description 查询指定条件的记录
|
77
|
-
* @param {Object} query - {<key>:<value>}
|
78
|
-
* @returns {Promise} 返回指定条件的记录
|
79
|
-
*/
|
80
|
-
findOne(query) {
|
81
|
-
return this.knex(this.model).where(query).first();
|
82
|
-
}
|
83
|
-
|
84
|
-
/**
|
85
|
-
* @description 更新指定id的记录
|
86
|
-
* @param {*} id - 记录id
|
87
|
-
* @param {*} params - {<key>:<value>}
|
88
|
-
* @returns {Promise} 返回更新后的记录
|
89
|
-
*/
|
90
|
-
findOneAndUpdate(id, params) {
|
91
|
-
return this.knex(this.model).where("id", id).update(params).returning("*");
|
92
|
-
}
|
93
|
-
|
94
|
-
/**
|
95
|
-
* @description 查询指定条件的记录数量
|
96
|
-
* @param {*} query - {<key>:<value>}
|
97
|
-
* @returns {Promise} 返回指定条件的记录数量
|
98
|
-
*/
|
99
|
-
count(query) {
|
100
|
-
return this.knex(this.model).where(query).count("* as total").first();
|
101
|
-
}
|
102
|
-
|
103
|
-
/**
|
104
|
-
* @description 插入一条记录
|
105
|
-
* @param {*} params - {<key>:<value>}
|
106
|
-
* @returns {Promise} 返回插入后的记录
|
107
|
-
*/
|
108
|
-
insert(params) {
|
109
|
-
return this.knex(this.model).insert(params);
|
110
|
-
}
|
111
|
-
|
112
|
-
/**
|
113
|
-
* @description 插入多条记录
|
114
|
-
* @param {*} records - [{<key>:<value>}, {<key>:<value>}, ...]
|
115
|
-
* @returns {Promise} 返回插入后的记录
|
116
|
-
*/
|
117
|
-
insertMany(records) {
|
118
|
-
return this.knex(this.model).insert(records);
|
119
|
-
}
|
120
|
-
|
121
|
-
/**
|
122
|
-
* @description 更新指定id的记录
|
123
|
-
* @param {*} id - 记录id
|
124
|
-
* @param {*} params - {<key>:<value>}
|
125
|
-
* @returns {Promise} 返回更新后的记录
|
126
|
-
*/
|
127
|
-
updateById(id, params) {
|
128
|
-
return this.knex(this.model).where("id", id).update(params);
|
129
|
-
}
|
130
|
-
|
131
|
-
/**
|
132
|
-
* @description 更新数据
|
133
|
-
* @param {*} query - {<key>:<value>}
|
134
|
-
* @param {*} update - {<key>:<value>}
|
135
|
-
* @returns {Promise<*>} - 返回更新后的数据
|
136
|
-
*/
|
137
|
-
update(query, update) {
|
138
|
-
return this.knex(this.model).where(query).update(update);
|
139
|
-
}
|
140
|
-
|
141
|
-
/**
|
142
|
-
* @description 查询并更新
|
143
|
-
* @param {*} query - {<key>:<value>}
|
144
|
-
* @param {*} update - {<key>:<value>}
|
145
|
-
* @returns {Promise<*>} - 返回更新后的记录
|
146
|
-
*/
|
147
|
-
findAndModify(query, update) {
|
148
|
-
return this.knex(this.model).where(query).update(update);
|
149
|
-
}
|
150
|
-
|
151
|
-
/**
|
152
|
-
*
|
153
|
-
* @param {*} id - id
|
154
|
-
* @param {*} update - {<key>:<value>}
|
155
|
-
* @returns {Promise<*>} - 返回更新后的记录
|
156
|
-
*/
|
157
|
-
findByIdAndUpdate(id, update) {
|
158
|
-
return this.knex(this.model).where("id", id).update(update);
|
159
|
-
}
|
160
|
-
|
161
|
-
/**
|
162
|
-
* @description 根据条件更新记录
|
163
|
-
* @param {*} query - {<key>:<value>}
|
164
|
-
* @param {*} update - {<key>:<value>}
|
165
|
-
* @returns {Promise} - 返回更新后的记录
|
166
|
-
*/
|
167
|
-
findOneAndUpdate(query, update) {
|
168
|
-
return this.knex(this.model).where(query).update(update);
|
169
|
-
}
|
170
|
-
|
171
|
-
/**
|
172
|
-
* @description 根据id删除一条记录
|
173
|
-
* @param {*} id - 记录id
|
174
|
-
* @returns {Promise} - 返回删除的记录
|
175
|
-
*/
|
176
|
-
findByIdAndRemove(id) {
|
177
|
-
return this.knex(this.model).where("id", id).del();
|
178
|
-
}
|
179
|
-
|
180
|
-
/**
|
181
|
-
* @description 根据条件删除一条记录
|
182
|
-
* @param {*} query
|
183
|
-
* @returns {Promise} - 返回删除的记录
|
184
|
-
*/
|
185
|
-
findOneAndRemove(query) {
|
186
|
-
return this.knex(this.model).where(query).del();
|
187
|
-
}
|
188
|
-
}
|
189
|
-
|
190
|
-
module.exports = BaseService;
|
package/publish.bat
DELETED