jj.js 0.9.0 → 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/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # v0.10.0 / 2024-02-26
2
+ 1. 类型文件位置调整及优化
3
+ 2. 自动生成应用端类型文件(开启app_debug,并且根目录下存在jsconfig.json文件)
4
+ 3. utils工具fs库方法优化,保留部分,其他方法建议使用fs.promises库
5
+ 4. 系统级别调整,app类改为system,默认输出['system', 'error']级别的日志
6
+
1
7
  # v0.9.0 / 2024-02-06
2
8
  1. 支持jsdoc,完善vscode代码提示
3
9
  2. 系统核心库app由对象改为class,使用const app = new App()
package/README.md CHANGED
@@ -14,7 +14,7 @@ A super simple lightweight NodeJS MVC framework(一个超级简单轻量的Nod
14
14
  2. 系统类库、用户类库都支持自动加载、懒加载、自动生成单实例
15
15
  3. 支持应用级、路由级、控制器级三级中间件,方便插件及二次开发
16
16
  4. 支持单应用和多应用两种运行模式
17
- 5. jsdoc支持,完善vscode代码提示
17
+ 5. 基于jsdoc,提供完整的代码提示。支持自动生成应用端jsdoc文件
18
18
 
19
19
  ### 项目地址
20
20
 
package/jj.js CHANGED
@@ -1,12 +1,12 @@
1
1
  /**
2
2
  * jj.js核心库<br/>
3
- * {App, Controller, Db, Model, Pagination, View, Logger, Cookie, Response, Upload, Url, Middleware, Cache, Context, View, utils}
3
+ * {App, Cache, Context, Controller, Cookie, Ctx, Db, Logger, Middleware, Model, Pagination, Response, Upload, Url, View, utils}
4
4
  * @module core
5
- * @type {import('./lib/types').Core}
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' || prop == 'inspect'){
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
@@ -13,6 +13,7 @@ class App extends Koa
13
13
  {
14
14
  /**
15
15
  * @override
16
+ * @param {...any} args - listen(port, ip, callback(err){})
16
17
  */
17
18
  listen(...args) {
18
19
  // exception
@@ -45,6 +46,13 @@ class App extends Koa
45
46
  // router
46
47
  this.use(router.routes()).use(router.allowedMethods());
47
48
 
49
+ // types
50
+ if(cfg_app.app_debug) {
51
+ const {isFileSync} = require('./utils/fs');
52
+ const jsconfigFile = path.join(cfg_app.base_dir, 'jsconfig.json');
53
+ isFileSync(jsconfigFile) && require('./types')();
54
+ }
55
+
48
56
  // server
49
57
  return super.listen(...args);
50
58
  }
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: '', // 静态文件目录,相对于应用根目录,为空或false时,关闭静态访问
16
+ static_dir: '', // 静态文件目录,相对于应用根目录,为空时,关闭静态访问
17
17
 
18
- koa_body: null // koa-body配置参数,为''、null、false时,关闭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: ['app', 'error'], // [app, error, warning, info, debug, http, sql]
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('./types').Config}
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('./types').Context} ctx
15
+ * @param {import('../types').KoaCtx} [ctx]
12
16
  */
13
17
  constructor(ctx) {
14
18
  super();
package/lib/ctx.js CHANGED
@@ -2,12 +2,8 @@
2
2
  const loader = require('./loader');
3
3
 
4
4
  /**
5
- * @typedef {typeof import('./types')} Ctx
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() {
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('./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
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 {(import('./types').Context|object)} ctx
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<(string|ListData)>}
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<(RowData|null)>}
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<(string|number|null)>}
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<(string|number|0)>}
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<(string|number|0)>}
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<(string|number|0)>}
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<(string|number|0)>}
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<(string|number|0)>}
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<(string|ListData|object)>}
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 = /** @type {ListData} */(await this.select());
678
+ const rows = await this.select();
679
679
  const result = key ? {} : [];
680
680
  rows.forEach(row => {
681
681
  if(key) {
@@ -704,9 +704,7 @@ class Db extends Context
704
704
  }
705
705
 
706
706
  const options = {...this._options}; // 暂存options
707
- // @ts-ignore
708
707
  const total = await this.count();
709
- // @ts-ignore
710
708
  pagination.total(total);
711
709
 
712
710
  // @ts-ignore
@@ -727,7 +725,7 @@ class Db extends Context
727
725
  * 插入一条数据
728
726
  * @public
729
727
  * @param {object} data - 待插入数据
730
- * @returns {Promise<(string|OkPacket)>}
728
+ * @returns {Promise<OkPacket>}
731
729
  */
732
730
  async insert(data) {
733
731
  data && (this._options.data = data);
@@ -753,7 +751,7 @@ class Db extends Context
753
751
  * @public
754
752
  * @param {object} data - 更新数据
755
753
  * @param {object} condition - 更新条件
756
- * @returns {Promise<(string|OkPacket)>}
754
+ * @returns {Promise<OkPacket>}
757
755
  */
758
756
  async update(data, condition) {
759
757
  data && (this._options.data = data);
@@ -788,7 +786,7 @@ class Db extends Context
788
786
  * @public
789
787
  * @param {string} field - 字段
790
788
  * @param {number} [step=1] - 增加值,默认1
791
- * @returns {Promise<(string|OkPacket)>}
789
+ * @returns {Promise<OkPacket>}
792
790
  */
793
791
  async inc(field, step) {
794
792
  return await this.update({[field]: ['inc', step]});
@@ -799,7 +797,7 @@ class Db extends Context
799
797
  * @public
800
798
  * @param {string} field - 字段
801
799
  * @param {number} [step=1] - 减少值,默认1
802
- * @returns {Promise<(string|OkPacket)>}
800
+ * @returns {Promise<OkPacket>}
803
801
  */
804
802
  async dec(field, step) {
805
803
  return await this.update({[field]: ['dec', step]});
@@ -810,7 +808,7 @@ class Db extends Context
810
808
  * @public
811
809
  * @param {string} field - 字段
812
810
  * @param {string} value - 自定义表达式
813
- * @returns {Promise<(string|OkPacket)>}
811
+ * @returns {Promise<OkPacket>}
814
812
  */
815
813
  async exp(field, value) {
816
814
  return await this.update({[field]: ['exp', value]});
@@ -820,7 +818,7 @@ class Db extends Context
820
818
  * 删除数据
821
819
  * @public
822
820
  * @param {object} condition - 山粗条件
823
- * @returns {Promise<(string|OkPacket)>}
821
+ * @returns {Promise<OkPacket>}
824
822
  */
825
823
  async delete(condition) {
826
824
  condition && (this._options.where = [], this._options.where.push([condition]));
@@ -849,7 +847,7 @@ class Db extends Context
849
847
  * @param {string} sql - sql语句或参数
850
848
  * @param {*} params - sql参数
851
849
  * @param {*} [reset=true] - 是否重置参数
852
- * @returns {Promise<(string|OkPacket|ListData|RowData)>}
850
+ * @returns {Promise<(OkPacket|ListData|RowData)>}
853
851
  */
854
852
  async execute(sql, params, reset=true) {
855
853
  this._queryStr = sql;
@@ -1062,7 +1060,7 @@ class Db extends Context
1062
1060
 
1063
1061
  /**
1064
1062
  * 获取sql缓存
1065
- * @param {string} key
1063
+ * @param {string} [key]
1066
1064
  * @returns {*}
1067
1065
  */
1068
1066
  getCache(key) {
@@ -1079,7 +1077,7 @@ class Db extends Context
1079
1077
 
1080
1078
  /**
1081
1079
  * 删除sql缓存
1082
- * @param {string} key
1080
+ * @param {string} [key]
1083
1081
  */
1084
1082
  deleteCache(key) {
1085
1083
  Db.cache.delete(key);
@@ -1088,7 +1086,7 @@ class Db extends Context
1088
1086
 
1089
1087
  /**
1090
1088
  * 设置数据库缓存实例
1091
- * @type {typeof import('./cache')}
1089
+ * @type {import('../types').Cache}
1092
1090
  */
1093
1091
  // @ts-ignore
1094
1092
  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('./types').LogHandle} LogHandle
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('./types').Context} ctx
12
- * @param {import('./types').Middleware} [next]
11
+ * @param {import('../types').KoaCtx} ctx
12
+ * @param {import('../types').AsyncNext} [next]
13
13
  */
14
14
  constructor(ctx, next) {
15
15
  super(ctx);
package/lib/model.js CHANGED
@@ -2,9 +2,9 @@ const Context = require('./context');
2
2
  const {toLine} = require('./utils/str');
3
3
 
4
4
  /**
5
- * @typedef {import('./types').OkPacket} OkPacket
6
- * @typedef {import('./types').RowData} RowData
7
- * @typedef {import('./types').ListData} ListData
5
+ * @typedef {import('../types').OkPacket} OkPacket
6
+ * @typedef {import('../types').RowData} RowData
7
+ * @typedef {import('../types').ListData} ListData
8
8
  */
9
9
 
10
10
  /**
@@ -15,7 +15,7 @@ class Model extends Context
15
15
  /**
16
16
  * Initialize a new `Model`
17
17
  * @public
18
- * @param {import('./types').Context} ctx
18
+ * @param {import('../types').KoaCtx} ctx
19
19
  */
20
20
  constructor(ctx) {
21
21
  super(ctx);
@@ -32,7 +32,6 @@ class Model extends Context
32
32
  * @returns {Promise<OkPacket>}
33
33
  */
34
34
  async add(data) {
35
- // @ts-ignore
36
35
  return await this.db.allowField().insert(data);
37
36
  }
38
37
 
@@ -49,7 +48,6 @@ class Model extends Context
49
48
  condition[this.pk] = data[this.pk];
50
49
  delete data[this.pk];
51
50
  }
52
- // @ts-ignore
53
51
  return await this.db.allowField().update(data, condition);
54
52
  }
55
53
 
@@ -64,7 +62,6 @@ class Model extends Context
64
62
  * @returns {Promise<OkPacket>}
65
63
  */
66
64
  async del(condition) {
67
- // @ts-ignore
68
65
  return await this.db.delete(condition);
69
66
  }
70
67
 
@@ -85,13 +82,12 @@ class Model extends Context
85
82
  * @returns {Promise<(ListData)>}
86
83
  */
87
84
  async all(condition) {
88
- // @ts-ignore
89
85
  return await this.db.select(condition);
90
86
  }
91
87
 
92
88
  /**
93
89
  * 数据库实例
94
- * @type {typeof import('./db').prototype}
90
+ * @type {import('../types').DbInstance}
95
91
  */
96
92
  get db() {
97
93
  if(!this._db){
package/lib/pagination.js CHANGED
@@ -9,7 +9,7 @@ class Pagination extends Context
9
9
  /**
10
10
  * Initialize a new `Pagination`
11
11
  * @public
12
- * @param {import('./types').Context} ctx
12
+ * @param {import('../types').KoaCtx} ctx
13
13
  */
14
14
  constructor(ctx) {
15
15
  super(ctx);
@@ -23,12 +23,12 @@ class Pagination extends Context
23
23
  /**
24
24
  * 分页配置
25
25
  * @public
26
- * @param {import('./types').PageConfig} [options]
26
+ * @param {import('../types').PageConfig} [options]
27
27
  * @returns {this}
28
28
  */
29
29
  init(options) {
30
30
  /**
31
- * @type {import('./types').PageConfig}
31
+ * @type {import('../types').PageConfig}
32
32
  * @private
33
33
  */
34
34
  this.options = {...cfg_page, ...options};
package/lib/response.js CHANGED
@@ -10,7 +10,7 @@ class Response extends Context
10
10
  /**
11
11
  * Initialize a new `Response`
12
12
  * @public
13
- * @param {import('./types').Context} ctx
13
+ * @param {import('../types').KoaCtx} ctx
14
14
  */
15
15
  constructor(ctx) {
16
16
  super(ctx);
package/lib/run.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const path = require('path');
2
2
  const loader = require('./loader');
3
- const {readFile} = require('./utils/fs');
3
+ const {readFile} = require('fs').promises;
4
4
  const {toHump} = require('./utils/str');
5
5
  const {app: cfg_app, view: cfg_view, db, page, log, cache, cookie, tpl} = require('./config');
6
6
  const compose = require('koa-compose');
@@ -8,11 +8,11 @@ const Logger = require('./logger');
8
8
 
9
9
  async function run(ctx, next, control_type=cfg_app.controller_folder) {
10
10
  if(cfg_app.app_debug) {
11
- Logger.debug(ctx.request);
12
- Logger.debug(`Router(${control_type}): {APP: '${ctx.APP}', CONTROLLER: '${ctx.CONTROLLER}', ACTION: '${ctx.ACTION}'}`);
13
- Object.keys(ctx.params).length && Logger.debug({Params: ctx.params});
14
- Object.keys(ctx.query).length && Logger.debug({Get: ctx.query});
15
- Object.keys(ctx.request.body || {}).length && Logger.debug({Post: ctx.request.body});
11
+ Logger.http('Request:', ctx.request);
12
+ Logger.http(`Router(${control_type}): {APP: '${ctx.APP}', CONTROLLER: '${ctx.CONTROLLER}', ACTION: '${ctx.ACTION}'}`);
13
+ Object.keys(ctx.params).length && Logger.http('Params:', ctx.params);
14
+ Object.keys(ctx.query).length && Logger.http('Get:', ctx.query);
15
+ Object.keys(ctx.request.body || {}).length && Logger.http('Post:', ctx.request.body);
16
16
  }
17
17
 
18
18
  // 设置根加载器
@@ -0,0 +1,22 @@
1
+ module.exports = `/**
2
+ * @type {typeof import('jj.js/types')}
3
+ */
4
+ const JJCtx = require('jj.js').Ctx;
5
+
6
+ /**
7
+ * @typedef {import('jj.js/types').Config} JJConfig
8
+ */
9
+
10
+ __TYPES__
11
+
12
+ /**
13
+ * @class Ctx
14
+ */
15
+ class Ctx extends JJCtx {
16
+ __PROPERTY__
17
+ }
18
+
19
+ /**
20
+ * @module types
21
+ */
22
+ module.exports = Ctx;`;