jj.js 0.9.0 → 0.11.0
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/CHANGELOG.md +10 -0
- package/README.md +17 -92
- package/jj.js +3 -3
- package/lib/app.js +16 -0
- package/lib/config.js +4 -4
- package/lib/context.js +5 -1
- package/lib/ctx.js +39 -41
- package/lib/db.js +33 -37
- package/lib/logger.js +11 -1
- package/lib/middleware.js +2 -2
- package/lib/model.js +5 -9
- package/lib/pagination.js +3 -3
- package/lib/response.js +1 -1
- package/lib/run.js +30 -26
- package/lib/storage.js +5 -0
- package/lib/tpl/types.js +22 -0
- package/lib/types.js +215 -265
- package/lib/upload.js +2 -2
- package/lib/utils/fs.js +53 -62
- package/lib/utils/utils.js +1 -1
- package/lib/view.js +4 -3
- package/package.json +4 -1
- package/types.js +308 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
# v0.11.0 / 2024-02-26
|
|
2
|
+
1. 基于`AsyncLocalStorage`重构核心类
|
|
3
|
+
2. 空控制器和空方法名改为`_empty`
|
|
4
|
+
|
|
5
|
+
# v0.10.0 / 2024-02-26
|
|
6
|
+
1. 类型文件位置调整及优化
|
|
7
|
+
2. 自动生成应用端类型文件(开启app_debug,并且根目录下存在jsconfig.json文件)
|
|
8
|
+
3. utils工具fs库方法优化,保留部分,其他方法建议使用fs.promises库
|
|
9
|
+
4. 系统级别调整,app类改为system,默认输出['system', 'error']级别的日志
|
|
10
|
+
|
|
1
11
|
# v0.9.0 / 2024-02-06
|
|
2
12
|
1. 支持jsdoc,完善vscode代码提示
|
|
3
13
|
2. 系统核心库app由对象改为class,使用const app = new App()
|
package/README.md
CHANGED
|
@@ -6,15 +6,15 @@ A super simple lightweight NodeJS MVC framework(一个超级简单轻量的Nod
|
|
|
6
6
|
|
|
7
7
|
## 项目介绍
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
无论什么语言什么框架的程序,类库的使用都离不开require、import、include等导入模块,不用时,还得删除。而本框架基于proxy实现了类库自动加载、懒加载和Class自动实例化及单例化技术,所有的类库,想用直接调用,系统会自动导入。
|
|
10
10
|
|
|
11
11
|
### 项目特性
|
|
12
12
|
|
|
13
|
-
1.
|
|
14
|
-
2.
|
|
13
|
+
1. 系统架构为经典的MVC模式,模仿Thinkphp5,很容易上手
|
|
14
|
+
2. 系统类库、用户类库都支持自动加载、懒加载、Class自动生成单实例
|
|
15
15
|
3. 支持应用级、路由级、控制器级三级中间件,方便插件及二次开发
|
|
16
16
|
4. 支持单应用和多应用两种运行模式
|
|
17
|
-
5. jsdoc
|
|
17
|
+
5. 基于jsdoc,提供完整的代码提示,自动生成应用端types类型文件
|
|
18
18
|
|
|
19
19
|
### 项目地址
|
|
20
20
|
|
|
@@ -230,17 +230,6 @@ const app = {
|
|
|
230
230
|
}
|
|
231
231
|
module.exports = app;
|
|
232
232
|
```
|
|
233
|
-
- 模板配置`./config/view.js`:
|
|
234
|
-
```javascript
|
|
235
|
-
const view = {
|
|
236
|
-
view_folder: 'view', // 模板目录名
|
|
237
|
-
view_depr: '/', // 模版文件名分割符,'/'代表二级目录
|
|
238
|
-
view_ext: '.htm', // 模版文件后缀
|
|
239
|
-
view_engine: 'art-template', // 默认模版引擎,字符串或引擎类,咱不支持更换
|
|
240
|
-
view_filter: {}, // 模版函数,配置注入模板的函数,默认会自动注入url函数
|
|
241
|
-
}
|
|
242
|
-
module.exports = view;
|
|
243
|
-
```
|
|
244
233
|
- 数据库配置`./config/db.js`:可以配置多个,方便程序里切换使用。
|
|
245
234
|
```javascript
|
|
246
235
|
const db = {
|
|
@@ -257,67 +246,7 @@ const db = {
|
|
|
257
246
|
}
|
|
258
247
|
module.exports = db;
|
|
259
248
|
```
|
|
260
|
-
|
|
261
|
-
```javascript
|
|
262
|
-
const log = {
|
|
263
|
-
log_level: [], // [error, warning, info, debug, http, sql]
|
|
264
|
-
log_handle: function(msg, level) {console.log(`[${format('YY-mm-dd HH:ii:ss')}] [${level}] ${typeof msg == 'String' ? msg : JSON.stringify(msg)}`);} //function(msg, level) {}
|
|
265
|
-
}
|
|
266
|
-
module.exports = log;
|
|
267
|
-
```
|
|
268
|
-
- 缓存配置`./config/cache.js`:
|
|
269
|
-
```javascript
|
|
270
|
-
const cache = {
|
|
271
|
-
cache_time: 60 * 60 * 24, // 默认缓存时间(1天),为空或false则为10年
|
|
272
|
-
clear_time: undefined // (undefined: 一天清理一次, 0: 关闭自动清理, >0: 为自动清理周期)
|
|
273
|
-
}
|
|
274
|
-
module.exports = cache;
|
|
275
|
-
```
|
|
276
|
-
- 分页配置`./config/page.js`:
|
|
277
|
-
```javascript
|
|
278
|
-
const page = {
|
|
279
|
-
page_key : 'page', // 默认分页标识
|
|
280
|
-
key_origin : 'query', // query 或 params
|
|
281
|
-
page_size : 10, // 默认分页大小
|
|
282
|
-
page_length : 5, // 默认分页长度,数字页码链接数量
|
|
283
|
-
|
|
284
|
-
//网址规则,可为空,可为路由名字,可用参数:页码${page}
|
|
285
|
-
//样例:':name'
|
|
286
|
-
//样例:'/list_${page}.html'
|
|
287
|
-
url_page : '',
|
|
288
|
-
url_index : '',
|
|
289
|
-
|
|
290
|
-
//模块样式 可用参数:网址${url},页码${page},总数${total_page},总页数${total_page}
|
|
291
|
-
index_tpl : '<li class="index"><a href="${url}">首页</a></li>',
|
|
292
|
-
end_tpl : '<li class="end"><a href="${url}">末页</a></li>',
|
|
293
|
-
prev_tpl : '<li class="prev"><a href="${url}">上一页</a></li>',
|
|
294
|
-
next_tpl : '<li class="next"><a href="${url}">下一页</a></li>',
|
|
295
|
-
list_tpl : '<li><a href="${url}">${page}</a></li>',
|
|
296
|
-
active_tpl : '<li class="active"><a href="${url}">${page}</a></li>',
|
|
297
|
-
info_tpl : '<span class="info">共${total_page}页,${total}条记录</span>',
|
|
298
|
-
|
|
299
|
-
//渲染模版
|
|
300
|
-
template : '<div class="pagination"><ul class="page">${index}${prev}${list}${next}${end}</ul>${info}</div>'
|
|
301
|
-
}
|
|
302
|
-
module.exports = page;
|
|
303
|
-
```
|
|
304
|
-
- 跳转模板配置`./config/tpl.js`:模板可以配置为自定义的
|
|
305
|
-
```javascript
|
|
306
|
-
const tpl = {
|
|
307
|
-
jump: require('./tpl/jump'), // 跳转模板
|
|
308
|
-
exception: require('./tpl/exception') // 异常页面模板
|
|
309
|
-
}
|
|
310
|
-
module.exports = tpl;
|
|
311
|
-
```
|
|
312
|
-
- 自定义配置`./config/self.js`:自定义配置同样可以直接通过`this.$config.self`使用。
|
|
313
|
-
```javascript
|
|
314
|
-
const self = {
|
|
315
|
-
option1: ''
|
|
316
|
-
option2: ''
|
|
317
|
-
...
|
|
318
|
-
}
|
|
319
|
-
module.exports = self;
|
|
320
|
-
```
|
|
249
|
+
|
|
321
250
|
- 路由配置`./config/routes.js`:
|
|
322
251
|
路由功能基于`@koa/router`开发,关于url匹配规则可以参考官方文档:[文档地址](https://www.npmjs.com/package/@koa/router)
|
|
323
252
|
|
|
@@ -338,26 +267,22 @@ module.exports = routes;
|
|
|
338
267
|
```
|
|
339
268
|
> 注意:本路由配置示例前两条规则为常规用法,后面的非常规用法,不建议使用。如果是单应用模式,可以去掉path参数里的app。
|
|
340
269
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
应用入口文件放在项目的根目录,名字可以任意改。
|
|
344
|
-
|
|
345
|
-
示例文件 `./server.js`
|
|
270
|
+
- 自定义配置`./config/self.js`:自定义配置同样可以直接通过`this.$config.self`使用。
|
|
346
271
|
```javascript
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
}
|
|
272
|
+
const self = {
|
|
273
|
+
option1: ''
|
|
274
|
+
option2: ''
|
|
275
|
+
...
|
|
276
|
+
}
|
|
277
|
+
module.exports = self;
|
|
352
278
|
```
|
|
353
|
-
|
|
279
|
+
- 其他配置项,请参考系统配置文件`jj.js/lib/config.js`
|
|
280
|
+
|
|
281
|
+
### 编码命名规范
|
|
354
282
|
|
|
355
|
-
|
|
283
|
+
类名使用大驼峰,方法名使用小驼峰,私有方法使用下划线前缀。
|
|
284
|
+
控制器文件名使用小写下划线。
|
|
356
285
|
|
|
357
|
-
通过以上文档,可以看到:
|
|
358
|
-
1. jj.js是一个轻量的mvc框架,几乎所有文件不使用就不会调用。
|
|
359
|
-
2. 同时也是个功能强大的框架,支持应用级、路由级、控制器级三级中间件。
|
|
360
|
-
3. 类库自动加载,只要是继承自`Context`的类库,都可以在方法内使用包含`$`前缀的属性自动加载其他资源。
|
|
361
286
|
|
|
362
287
|
## 应用案例
|
|
363
288
|
|
package/jj.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* jj.js核心库<br/>
|
|
3
|
-
* {App,
|
|
3
|
+
* {App, Cache, Context, Controller, Cookie, Ctx, Db, Logger, Middleware, Model, Pagination, Response, Upload, Url, View, utils, config}
|
|
4
4
|
* @module core
|
|
5
|
-
* @type {import('./
|
|
5
|
+
* @type {import('./types').Core}
|
|
6
6
|
*/
|
|
7
7
|
module.exports = new Proxy({}, {
|
|
8
8
|
get: (target, prop) => {
|
|
9
|
-
if(prop in target || typeof prop == 'symbol' ||
|
|
9
|
+
if(prop in target || typeof prop == 'symbol' || ['inspect', 'router', 'run', 'types'].includes(prop)) {
|
|
10
10
|
return target[prop];
|
|
11
11
|
}
|
|
12
12
|
prop = prop.toLowerCase();
|
package/lib/app.js
CHANGED
|
@@ -4,6 +4,7 @@ const {app: cfg_app} = require('./config');
|
|
|
4
4
|
const Logger = require('./logger');
|
|
5
5
|
const Response = require('./response');
|
|
6
6
|
const router = require('./router');
|
|
7
|
+
const storage = require('./storage');
|
|
7
8
|
const pjson = require('../package.json');
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -13,8 +14,16 @@ class App extends Koa
|
|
|
13
14
|
{
|
|
14
15
|
/**
|
|
15
16
|
* @override
|
|
17
|
+
* @param {...any} args - listen(port, ip, callback(err){})
|
|
16
18
|
*/
|
|
17
19
|
listen(...args) {
|
|
20
|
+
// storage
|
|
21
|
+
this.use(async (ctx, next) => {
|
|
22
|
+
await storage.run({}, async() => {
|
|
23
|
+
await next();
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
18
27
|
// exception
|
|
19
28
|
this.use(async (ctx, next) => {
|
|
20
29
|
ctx.APP_TIME = Date.now();
|
|
@@ -45,6 +54,13 @@ class App extends Koa
|
|
|
45
54
|
// router
|
|
46
55
|
this.use(router.routes()).use(router.allowedMethods());
|
|
47
56
|
|
|
57
|
+
// types
|
|
58
|
+
if(cfg_app.app_debug) {
|
|
59
|
+
const {isFileSync} = require('./utils/fs');
|
|
60
|
+
const jsconfigFile = path.join(cfg_app.base_dir, 'jsconfig.json');
|
|
61
|
+
isFileSync(jsconfigFile) && require('./types')();
|
|
62
|
+
}
|
|
63
|
+
|
|
48
64
|
// server
|
|
49
65
|
return super.listen(...args);
|
|
50
66
|
}
|
package/lib/config.js
CHANGED
|
@@ -13,9 +13,9 @@ const app = {
|
|
|
13
13
|
common_app: 'common', // 公共应用,存放公共模型及逻辑
|
|
14
14
|
controller_folder: 'controller', //控制器目录名
|
|
15
15
|
|
|
16
|
-
static_dir: '', //
|
|
16
|
+
static_dir: '', // 静态文件目录,相对于应用根目录,为空时,关闭静态访问
|
|
17
17
|
|
|
18
|
-
koa_body: null // koa-body配置参数,为
|
|
18
|
+
koa_body: null // koa-body配置参数,为null或空时,关闭koa-body
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
const view = {
|
|
@@ -40,7 +40,7 @@ const db = {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
const log = {
|
|
43
|
-
log_level: ['
|
|
43
|
+
log_level: ['system', 'error'], // [system, error, warning, info, debug, http, sql]
|
|
44
44
|
log_handle: function(level, ...args) {console.log(`[${format('YY-mm-dd HH:ii:ss')}] [${level}]`, ...args.map(msg => typeof msg == 'string' ? msg : JSON.stringify(msg)));}
|
|
45
45
|
}
|
|
46
46
|
|
|
@@ -84,7 +84,7 @@ const base_config = loader(path.join(base_dir, './config'));
|
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
86
|
* @module config
|
|
87
|
-
* @type {import('
|
|
87
|
+
* @type {import('../types').Config}
|
|
88
88
|
*/
|
|
89
89
|
module.exports = {
|
|
90
90
|
app: {...app, ...base_config.app, base_dir},
|
package/lib/context.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 开发jj.js库本身时,可以设置为import('../types')
|
|
3
|
+
* @type {typeof import('../../../types')}
|
|
4
|
+
*/
|
|
1
5
|
const Ctx = require('./ctx');
|
|
2
6
|
|
|
3
7
|
/**
|
|
@@ -8,7 +12,7 @@ class Context extends Ctx
|
|
|
8
12
|
/**
|
|
9
13
|
* Initialize a new `Context`
|
|
10
14
|
* @public
|
|
11
|
-
* @param {import('
|
|
15
|
+
* @param {import('../types').KoaCtx} [ctx]
|
|
12
16
|
*/
|
|
13
17
|
constructor(ctx) {
|
|
14
18
|
super();
|
package/lib/ctx.js
CHANGED
|
@@ -1,57 +1,55 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
|
-
const
|
|
2
|
+
const storage = require('./storage');
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @class Ctx
|
|
10
|
-
* @type {Ctx}
|
|
5
|
+
* 开发jj.js库本身时,可以设置为import('../types')
|
|
6
|
+
* @type {typeof import('../../../types')}
|
|
11
7
|
*/
|
|
12
8
|
const Ctx = new Proxy(class {}, {
|
|
13
9
|
construct() {
|
|
14
10
|
return new Proxy({__proto__: arguments[2].prototype}, {
|
|
15
|
-
get: (target, prop, receiver) => {
|
|
16
|
-
if(prop in target || typeof prop == 'symbol' || prop == 'inspect') {
|
|
17
|
-
return Reflect.get(target, prop, receiver);
|
|
11
|
+
get: (target, $prop, receiver) => {
|
|
12
|
+
if($prop in target || typeof $prop == 'symbol' || $prop == 'inspect') {
|
|
13
|
+
return Reflect.get(target, $prop, receiver);
|
|
18
14
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
|
|
16
|
+
const store = storage.getStore();
|
|
17
|
+
const $ = store.$;
|
|
18
|
+
const _ = store._;
|
|
19
|
+
if($prop == '$' || $prop == '_') {
|
|
20
|
+
return store[$prop];
|
|
23
21
|
}
|
|
24
|
-
if(prop.slice(0, 1) != '$') {
|
|
22
|
+
if($prop.slice(0, 1) != '$') {
|
|
25
23
|
return undefined;
|
|
26
24
|
}
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
25
|
+
|
|
26
|
+
const prop = $prop.slice(1);
|
|
27
|
+
const APP = store.APP;
|
|
28
|
+
const COMMON = store.COMMON;
|
|
29
|
+
|
|
30
|
+
// 缓存
|
|
31
|
+
if(_[APP][$prop] === undefined) {
|
|
32
|
+
if(_[APP][prop] && _[COMMON] && _[COMMON][prop]) {
|
|
33
|
+
_[APP][$prop] = new Proxy({}, {
|
|
34
|
+
get: (...args) => {
|
|
35
|
+
return _[APP][prop][args[1]] || _[COMMON][prop][args[1]];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
} else if(_[APP][prop]) {
|
|
39
|
+
_[APP][$prop] = _[APP][prop];
|
|
40
|
+
} else if(_[COMMON] && _[COMMON][prop]) {
|
|
41
|
+
_[APP][$prop] = _[COMMON][prop];
|
|
42
|
+
} else {
|
|
43
|
+
_[APP][$prop] = null;
|
|
44
|
+
}
|
|
45
|
+
// lib调用,防止覆盖
|
|
46
|
+
if(['logger', 'pagination', 'response', 'url', 'view'].includes(prop) && _[APP][$prop] && _[APP][$prop].__node && !_[APP][$prop].__node.isClass && $[prop]) {
|
|
47
|
+
_[APP][$prop] = $[prop];
|
|
50
48
|
}
|
|
51
|
-
return _[APP][prop] || _[pp] || (ctx.$ && ctx.$[pp]) || undefined;
|
|
52
|
-
} else {
|
|
53
|
-
return (target.$ || (target.$ = loader('../'))) && target.$[pp];
|
|
54
49
|
}
|
|
50
|
+
|
|
51
|
+
// 屏蔽部分lib
|
|
52
|
+
return _[APP][$prop] || _[prop] || (!['app', 'loader', 'router', 'run', 'storage', 'types'].includes(prop) && $[prop]) || undefined;
|
|
55
53
|
}
|
|
56
54
|
});
|
|
57
55
|
}
|
package/lib/db.js
CHANGED
|
@@ -3,17 +3,17 @@ const md5 = require('./utils/md5');
|
|
|
3
3
|
const Context = require('./context');
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* @typedef {import('
|
|
7
|
-
* @typedef {import('
|
|
8
|
-
* @typedef {import('
|
|
9
|
-
* @typedef {import('
|
|
10
|
-
* @typedef {import('
|
|
11
|
-
* @typedef {import('
|
|
12
|
-
* @typedef {import('
|
|
13
|
-
* @typedef {import('
|
|
14
|
-
* @typedef {import('
|
|
15
|
-
* @typedef {import('
|
|
16
|
-
* @typedef {import('
|
|
6
|
+
* @typedef {import('../types').Pagination} Pagination
|
|
7
|
+
* @typedef {import('../types').PaginationInstance} PaginationInstance
|
|
8
|
+
* @typedef {import('../types').Pool} Pool
|
|
9
|
+
* @typedef {import('../types').PoolConfig} PoolConfig
|
|
10
|
+
* @typedef {import('../types').PoolConnection} PoolConnection
|
|
11
|
+
* @typedef {import('../types').QueryOptions} QueryOptions
|
|
12
|
+
* @typedef {import('../types').OkPacket} OkPacket
|
|
13
|
+
* @typedef {import('../types').RowData} RowData
|
|
14
|
+
* @typedef {import('../types').ListData} ListData
|
|
15
|
+
* @typedef {import('../types').FieldInfo} FieldInfo
|
|
16
|
+
* @typedef {import('../types').PoolMap} PoolMap
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
//连接池
|
|
@@ -34,7 +34,7 @@ class Db extends Context
|
|
|
34
34
|
/**
|
|
35
35
|
* Initialize a new `Db`
|
|
36
36
|
* @public
|
|
37
|
-
* @param {
|
|
37
|
+
* @param {import('../types').KoaCtx} [ctx]
|
|
38
38
|
* @param {(string|PoolConfig)} [options] - 数据库配置标识或连接参数
|
|
39
39
|
*/
|
|
40
40
|
constructor(ctx, options) {
|
|
@@ -531,7 +531,7 @@ class Db extends Context
|
|
|
531
531
|
* 获取多条数据
|
|
532
532
|
* @public
|
|
533
533
|
* @param {object} condition - 查询条件
|
|
534
|
-
* @returns {Promise<
|
|
534
|
+
* @returns {Promise<ListData>}
|
|
535
535
|
*/
|
|
536
536
|
async select(condition) {
|
|
537
537
|
condition && (this._options.where = [], this._options.where.push([condition]));
|
|
@@ -577,7 +577,7 @@ class Db extends Context
|
|
|
577
577
|
* 获取一条数据
|
|
578
578
|
* @public
|
|
579
579
|
* @param {object} condition - 查询条件
|
|
580
|
-
* @returns {Promise
|
|
580
|
+
* @returns {Promise<?RowData>}
|
|
581
581
|
*/
|
|
582
582
|
async find(condition) {
|
|
583
583
|
condition && (this._options.where = [], this._options.where.push([condition]));
|
|
@@ -595,7 +595,7 @@ class Db extends Context
|
|
|
595
595
|
* 获取一个字段值
|
|
596
596
|
* @public
|
|
597
597
|
* @param {string} field - 字段
|
|
598
|
-
* @returns {Promise
|
|
598
|
+
* @returns {Promise<*>}
|
|
599
599
|
*/
|
|
600
600
|
async value(field) {
|
|
601
601
|
this._options.field = [];
|
|
@@ -613,7 +613,7 @@ class Db extends Context
|
|
|
613
613
|
* 获取总数
|
|
614
614
|
* @public
|
|
615
615
|
* @param {string} [field] - 字段
|
|
616
|
-
* @returns {Promise<
|
|
616
|
+
* @returns {Promise<number>}
|
|
617
617
|
*/
|
|
618
618
|
async count(field='*') {
|
|
619
619
|
return await this.value(`count(${field})`) || 0;
|
|
@@ -623,7 +623,7 @@ class Db extends Context
|
|
|
623
623
|
* 获取字段最大值
|
|
624
624
|
* @public
|
|
625
625
|
* @param {string} field - 字段
|
|
626
|
-
* @returns {Promise<
|
|
626
|
+
* @returns {Promise<number>}
|
|
627
627
|
*/
|
|
628
628
|
async max(field) {
|
|
629
629
|
return await this.value(`max(${field})`);
|
|
@@ -633,7 +633,7 @@ class Db extends Context
|
|
|
633
633
|
* 获取字段最小值
|
|
634
634
|
* @public
|
|
635
635
|
* @param {string} field - 字段
|
|
636
|
-
* @returns {Promise<
|
|
636
|
+
* @returns {Promise<number>}
|
|
637
637
|
*/
|
|
638
638
|
async min(field) {
|
|
639
639
|
return await this.value(`min(${field})`);
|
|
@@ -643,7 +643,7 @@ class Db extends Context
|
|
|
643
643
|
* 获取字段平均值
|
|
644
644
|
* @public
|
|
645
645
|
* @param {string} field - 字段
|
|
646
|
-
* @returns {Promise<
|
|
646
|
+
* @returns {Promise<number>}
|
|
647
647
|
*/
|
|
648
648
|
async avg(field) {
|
|
649
649
|
return await this.value(`avg(${field})`);
|
|
@@ -653,7 +653,7 @@ class Db extends Context
|
|
|
653
653
|
* 获取字段总和
|
|
654
654
|
* @public
|
|
655
655
|
* @param {string} field - 字段
|
|
656
|
-
* @returns {Promise<
|
|
656
|
+
* @returns {Promise<number>}
|
|
657
657
|
*/
|
|
658
658
|
async sum(field) {
|
|
659
659
|
return await this.value(`sum(${field})`);
|
|
@@ -664,7 +664,7 @@ class Db extends Context
|
|
|
664
664
|
* @public
|
|
665
665
|
* @param {string} field - 数据字段
|
|
666
666
|
* @param {string} [key] - key字段,不设置返回数据数组,设置则返回{key: field}对象数组
|
|
667
|
-
* @returns {Promise<(
|
|
667
|
+
* @returns {Promise<(ListData|object)>}
|
|
668
668
|
*/
|
|
669
669
|
async column(field, key) {
|
|
670
670
|
this._options.field = [];
|
|
@@ -675,7 +675,7 @@ class Db extends Context
|
|
|
675
675
|
return await this.select();
|
|
676
676
|
}
|
|
677
677
|
|
|
678
|
-
const rows =
|
|
678
|
+
const rows = await this.select();
|
|
679
679
|
const result = key ? {} : [];
|
|
680
680
|
rows.forEach(row => {
|
|
681
681
|
if(key) {
|
|
@@ -699,14 +699,10 @@ class Db extends Context
|
|
|
699
699
|
async pagination({page, page_size, pagination} = {}) {
|
|
700
700
|
!page && (page = this._options.page.page);
|
|
701
701
|
!page_size && (page_size = this._options.page.pageSize);
|
|
702
|
-
|
|
703
|
-
pagination = this.$pagination.__node && this.$pagination.__node.isClass ? this.$pagination : this.$.pagination;
|
|
704
|
-
}
|
|
702
|
+
!pagination && (pagination = this.$pagination);
|
|
705
703
|
|
|
706
704
|
const options = {...this._options}; // 暂存options
|
|
707
|
-
// @ts-ignore
|
|
708
705
|
const total = await this.count();
|
|
709
|
-
// @ts-ignore
|
|
710
706
|
pagination.total(total);
|
|
711
707
|
|
|
712
708
|
// @ts-ignore
|
|
@@ -727,7 +723,7 @@ class Db extends Context
|
|
|
727
723
|
* 插入一条数据
|
|
728
724
|
* @public
|
|
729
725
|
* @param {object} data - 待插入数据
|
|
730
|
-
* @returns {Promise<
|
|
726
|
+
* @returns {Promise<OkPacket>}
|
|
731
727
|
*/
|
|
732
728
|
async insert(data) {
|
|
733
729
|
data && (this._options.data = data);
|
|
@@ -753,7 +749,7 @@ class Db extends Context
|
|
|
753
749
|
* @public
|
|
754
750
|
* @param {object} data - 更新数据
|
|
755
751
|
* @param {object} condition - 更新条件
|
|
756
|
-
* @returns {Promise<
|
|
752
|
+
* @returns {Promise<OkPacket>}
|
|
757
753
|
*/
|
|
758
754
|
async update(data, condition) {
|
|
759
755
|
data && (this._options.data = data);
|
|
@@ -788,7 +784,7 @@ class Db extends Context
|
|
|
788
784
|
* @public
|
|
789
785
|
* @param {string} field - 字段
|
|
790
786
|
* @param {number} [step=1] - 增加值,默认1
|
|
791
|
-
* @returns {Promise<
|
|
787
|
+
* @returns {Promise<OkPacket>}
|
|
792
788
|
*/
|
|
793
789
|
async inc(field, step) {
|
|
794
790
|
return await this.update({[field]: ['inc', step]});
|
|
@@ -799,7 +795,7 @@ class Db extends Context
|
|
|
799
795
|
* @public
|
|
800
796
|
* @param {string} field - 字段
|
|
801
797
|
* @param {number} [step=1] - 减少值,默认1
|
|
802
|
-
* @returns {Promise<
|
|
798
|
+
* @returns {Promise<OkPacket>}
|
|
803
799
|
*/
|
|
804
800
|
async dec(field, step) {
|
|
805
801
|
return await this.update({[field]: ['dec', step]});
|
|
@@ -810,7 +806,7 @@ class Db extends Context
|
|
|
810
806
|
* @public
|
|
811
807
|
* @param {string} field - 字段
|
|
812
808
|
* @param {string} value - 自定义表达式
|
|
813
|
-
* @returns {Promise<
|
|
809
|
+
* @returns {Promise<OkPacket>}
|
|
814
810
|
*/
|
|
815
811
|
async exp(field, value) {
|
|
816
812
|
return await this.update({[field]: ['exp', value]});
|
|
@@ -820,7 +816,7 @@ class Db extends Context
|
|
|
820
816
|
* 删除数据
|
|
821
817
|
* @public
|
|
822
818
|
* @param {object} condition - 山粗条件
|
|
823
|
-
* @returns {Promise<
|
|
819
|
+
* @returns {Promise<OkPacket>}
|
|
824
820
|
*/
|
|
825
821
|
async delete(condition) {
|
|
826
822
|
condition && (this._options.where = [], this._options.where.push([condition]));
|
|
@@ -849,7 +845,7 @@ class Db extends Context
|
|
|
849
845
|
* @param {string} sql - sql语句或参数
|
|
850
846
|
* @param {*} params - sql参数
|
|
851
847
|
* @param {*} [reset=true] - 是否重置参数
|
|
852
|
-
* @returns {Promise<(
|
|
848
|
+
* @returns {Promise<(OkPacket|ListData|RowData)>}
|
|
853
849
|
*/
|
|
854
850
|
async execute(sql, params, reset=true) {
|
|
855
851
|
this._queryStr = sql;
|
|
@@ -1062,7 +1058,7 @@ class Db extends Context
|
|
|
1062
1058
|
|
|
1063
1059
|
/**
|
|
1064
1060
|
* 获取sql缓存
|
|
1065
|
-
* @param {string} key
|
|
1061
|
+
* @param {string} [key]
|
|
1066
1062
|
* @returns {*}
|
|
1067
1063
|
*/
|
|
1068
1064
|
getCache(key) {
|
|
@@ -1079,7 +1075,7 @@ class Db extends Context
|
|
|
1079
1075
|
|
|
1080
1076
|
/**
|
|
1081
1077
|
* 删除sql缓存
|
|
1082
|
-
* @param {string} key
|
|
1078
|
+
* @param {string} [key]
|
|
1083
1079
|
*/
|
|
1084
1080
|
deleteCache(key) {
|
|
1085
1081
|
Db.cache.delete(key);
|
|
@@ -1088,7 +1084,7 @@ class Db extends Context
|
|
|
1088
1084
|
|
|
1089
1085
|
/**
|
|
1090
1086
|
* 设置数据库缓存实例
|
|
1091
|
-
* @type {
|
|
1087
|
+
* @type {import('../types').Cache}
|
|
1092
1088
|
*/
|
|
1093
1089
|
// @ts-ignore
|
|
1094
1090
|
Db.cache = new (require('./cache'))();
|
package/lib/logger.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const {app: cfg_app, log: cfg_log} = require('./config');
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @typedef {import('
|
|
4
|
+
* @typedef {import('../types').LogHandle} LogHandle
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
class Logger
|
|
@@ -22,6 +22,16 @@ class Logger
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* 输出system日志
|
|
27
|
+
* @public
|
|
28
|
+
* @static
|
|
29
|
+
* @param {...any} args
|
|
30
|
+
*/
|
|
31
|
+
static system(...args) {
|
|
32
|
+
this.log('system', ...args);
|
|
33
|
+
}
|
|
34
|
+
|
|
25
35
|
/**
|
|
26
36
|
* 输出error日志
|
|
27
37
|
* @public
|
package/lib/middleware.js
CHANGED
|
@@ -8,8 +8,8 @@ class Middleware extends Context
|
|
|
8
8
|
/**
|
|
9
9
|
* Initialize a new `Middleware`
|
|
10
10
|
* @public
|
|
11
|
-
* @param {import('
|
|
12
|
-
* @param {import('
|
|
11
|
+
* @param {import('../types').KoaCtx} ctx
|
|
12
|
+
* @param {import('../types').AsyncNext} [next]
|
|
13
13
|
*/
|
|
14
14
|
constructor(ctx, next) {
|
|
15
15
|
super(ctx);
|