jj.js 0.8.8 → 0.10.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/lib/logger.js CHANGED
@@ -1,48 +1,127 @@
1
- const {app: cfg_app, log: cfg_log} = require('./config');
2
-
3
- class Logger
4
- {
5
- constructor(handle) {
6
- if(new.target) {
7
- class ClildLogger extends this.constructor {}
8
- ClildLogger.setHandle(handle);
9
- return ClildLogger;
10
- }
11
- }
12
-
13
- static error(...args) {
14
- args.forEach(msg => this.log(msg, 'error'));
15
- }
16
-
17
- static warning(...args) {
18
- args.forEach(msg => this.log(msg, 'warning'));
19
- }
20
-
21
- static info(...args) {
22
- args.forEach(msg => this.log(msg, 'info'));
23
- }
24
-
25
- static debug(...args) {
26
- args.forEach(msg => this.log(msg, 'debug'));
27
- }
28
-
29
- static sql(...args) {
30
- args.forEach(msg => this.log(msg, 'sql'));
31
- }
32
-
33
- static http(...args) {
34
- args.forEach(msg => this.log(msg, 'http'));
35
- }
36
-
37
- static log(msg, level='info') {
38
- (cfg_app.app_debug || ~cfg_log.log_level.indexOf(level)) && this.handle(msg, level);
39
- }
40
-
41
- static setHandle(handle) {
42
- this.handle = typeof(handle) == 'function' ? handle : cfg_log.log_handle;
43
- }
44
- }
45
-
46
- Logger.setHandle(cfg_log.log_handle);
47
-
1
+ const {app: cfg_app, log: cfg_log} = require('./config');
2
+
3
+ /**
4
+ * @typedef {import('../types').LogHandle} LogHandle
5
+ */
6
+
7
+ class Logger
8
+ {
9
+ /**
10
+ * Creat a new `Logger` class
11
+ * @public
12
+ * @static
13
+ * @param {LogHandle} [handle]
14
+ */
15
+ constructor(handle) {
16
+ if(new.target) {
17
+ // @ts-ignore
18
+ class ClildLogger extends this.constructor {}
19
+ // @ts-ignore
20
+ ClildLogger.setHandle(handle);
21
+ return ClildLogger;
22
+ }
23
+ }
24
+
25
+ /**
26
+ * 输出system日志
27
+ * @public
28
+ * @static
29
+ * @param {...any} args
30
+ */
31
+ static system(...args) {
32
+ this.log('system', ...args);
33
+ }
34
+
35
+ /**
36
+ * 输出error日志
37
+ * @public
38
+ * @static
39
+ * @param {...any} args
40
+ */
41
+ static error(...args) {
42
+ this.log('error', ...args);
43
+ }
44
+
45
+ /**
46
+ * 输出warning日志
47
+ * @public
48
+ * @static
49
+ * @param {...any} args
50
+ */
51
+ static warning(...args) {
52
+ this.log('warning', ...args);
53
+ }
54
+
55
+ /**
56
+ * 输出info日志
57
+ * @public
58
+ * @static
59
+ * @param {...any} args
60
+ */
61
+ static info(...args) {
62
+ this.log('info', ...args);
63
+ }
64
+
65
+ /**
66
+ * 输出debug日志
67
+ * @public
68
+ * @static
69
+ * @param {...any} args
70
+ */
71
+ static debug(...args) {
72
+ this.log('debug', ...args);
73
+ }
74
+
75
+ /**
76
+ * 输出sql日志
77
+ * @public
78
+ * @static
79
+ * @param {...any} args
80
+ */
81
+ static sql(...args) {
82
+ this.log('sql', ...args);
83
+ }
84
+
85
+ /**
86
+ * 输出http日志
87
+ * @public
88
+ * @static
89
+ * @param {...any} args
90
+ */
91
+ static http(...args) {
92
+ this.log('http', ...args);
93
+ }
94
+
95
+ /**
96
+ * 输出error日志
97
+ * @public
98
+ * @static
99
+ * @param {string} level - 日志级别、标识
100
+ * @param {...any} args - 日志内容
101
+ */
102
+ static log(level='info', ...args) {
103
+ (cfg_app.app_debug || ~cfg_log.log_level.indexOf(level)) && this.handle(level, ...args);
104
+ }
105
+
106
+ /**
107
+ * 设置日志handle
108
+ * @public
109
+ * @static
110
+ * @param {LogHandle} handle
111
+ * @returns {Logger}
112
+ */
113
+ static setHandle(handle) {
114
+ /**
115
+ * @type {LogHandle}
116
+ */
117
+ this.handle = typeof handle == 'function' ? handle : cfg_log.log_handle;
118
+ return this;
119
+ }
120
+ }
121
+
122
+ /**
123
+ * 设置日志handle
124
+ */
125
+ Logger.setHandle(cfg_log.log_handle);
126
+
48
127
  module.exports = Logger;
package/lib/middleware.js CHANGED
@@ -1,27 +1,59 @@
1
- const Context = require('./context');
2
-
3
- class Middleware extends Context
4
- {
5
- constructor(ctx, next) {
6
- super(ctx);
7
- this.$next = next;
8
- }
9
-
10
- $show(content) {
11
- this.$response.show(content);
12
- }
13
-
14
- $redirect(url, status) {
15
- this.$response.redirect(url, status);
16
- }
17
-
18
- $success(msg, url) {
19
- this.$response.success(msg, url);
20
- }
21
-
22
- $error(msg, url) {
23
- this.$response.error(msg, url);
24
- }
25
- }
26
-
1
+ const Context = require('./context');
2
+
3
+ /**
4
+ * @extends Context
5
+ */
6
+ class Middleware extends Context
7
+ {
8
+ /**
9
+ * Initialize a new `Middleware`
10
+ * @public
11
+ * @param {import('../types').KoaCtx} ctx
12
+ * @param {import('../types').AsyncNext} [next]
13
+ */
14
+ constructor(ctx, next) {
15
+ super(ctx);
16
+ this.$next = next;
17
+ }
18
+
19
+ /**
20
+ * 直接输出内容
21
+ * @public
22
+ * @param {*} data
23
+ */
24
+ $show(data) {
25
+ this.$response.show(data);
26
+ }
27
+
28
+ /**
29
+ * 跳转重定向
30
+ * @public
31
+ * @param {(string|object)} url
32
+ * @param {number} [status]
33
+ */
34
+ $redirect(url, status) {
35
+ this.$response.redirect(url, status);
36
+ }
37
+
38
+ /**
39
+ * 输出成功提示(html|json)
40
+ * @public
41
+ * @param {(string|object)} [msg]
42
+ * @param {(string|object)} [url]
43
+ */
44
+ $success(msg, url) {
45
+ this.$response.success(msg, url);
46
+ }
47
+
48
+ /**
49
+ * 输出错误提示(html|json)
50
+ * @public
51
+ * @param {(string|object)} [msg]
52
+ * @param {(string|object)} [url]
53
+ */
54
+ $error(msg, url) {
55
+ this.$response.error(msg, url);
56
+ }
57
+ }
58
+
27
59
  module.exports = Middleware;
package/lib/model.js CHANGED
@@ -1,19 +1,47 @@
1
1
  const Context = require('./context');
2
2
  const {toLine} = require('./utils/str');
3
3
 
4
+ /**
5
+ * @typedef {import('../types').OkPacket} OkPacket
6
+ * @typedef {import('../types').RowData} RowData
7
+ * @typedef {import('../types').ListData} ListData
8
+ */
9
+
10
+ /**
11
+ * @extends Context
12
+ */
4
13
  class Model extends Context
5
14
  {
15
+ /**
16
+ * Initialize a new `Model`
17
+ * @public
18
+ * @param {import('../types').KoaCtx} ctx
19
+ */
6
20
  constructor(ctx) {
7
21
  super(ctx);
8
22
  this.connection = null;
9
23
  this.table = toLine(this.constructor.name);
10
24
  this.pk = 'id';
25
+ this._db;
11
26
  }
12
27
 
28
+ /**
29
+ * 插入一条数据
30
+ * @public
31
+ * @param {object} data - 待插入数据
32
+ * @returns {Promise<OkPacket>}
33
+ */
13
34
  async add(data) {
14
35
  return await this.db.allowField().insert(data);
15
36
  }
16
37
 
38
+ /**
39
+ * 更新或插入一条数据
40
+ * @public
41
+ * @param {object} data - 更新或插入数据
42
+ * @param {object} [condition] - 更新条件,不为空或data包含主键则更新数据,否则插入数据
43
+ * @returns {Promise<OkPacket>}
44
+ */
17
45
  async save(data, condition = {}) {
18
46
  if(data[this.pk] || Object.keys(condition).length) {
19
47
  if(data[this.pk]) {
@@ -27,19 +55,40 @@ class Model extends Context
27
55
  return await this.add(data);
28
56
  }
29
57
 
58
+ /**
59
+ * 删除数据
60
+ * @public
61
+ * @param {object} condition - 删除条件
62
+ * @returns {Promise<OkPacket>}
63
+ */
30
64
  async del(condition) {
31
65
  return await this.db.delete(condition);
32
66
  }
33
67
 
68
+ /**
69
+ * 查询一条数据
70
+ * @public
71
+ * @param {object} condition - 查询条件
72
+ * @returns {Promise<(RowData|null)>}
73
+ */
34
74
  async get(condition) {
35
75
  return await this.db.find(condition);
36
76
  }
37
77
 
78
+ /**
79
+ * 查询多条数据
80
+ * @public
81
+ * @param {object} condition - 查询条件
82
+ * @returns {Promise<(ListData)>}
83
+ */
38
84
  async all(condition) {
39
85
  return await this.db.select(condition);
40
86
  }
41
87
 
42
- //数据库实例
88
+ /**
89
+ * 数据库实例
90
+ * @type {import('../types').DbInstance}
91
+ */
43
92
  get db() {
44
93
  if(!this._db){
45
94
  this._db = new (require('./db'))(this.ctx);