mm_dbs 1.0.0 → 1.0.1
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/example/index.js +50 -0
- package/index.js +2 -2
- package/lib/{drive.js → db.js} +100 -167
- package/lib/manager.js +17 -30
- package/package.json +6 -1
package/example/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mm_dbs 数据库框架示例
|
|
3
|
+
* 演示 dbAdmin() 获取管理器、manager.add() 注册驱动(不连接真实数据库),
|
|
4
|
+
* 以及 Db.model() 字段模型生成与字段 SQL 模板生成。
|
|
5
|
+
* 运行:node example/index.js
|
|
6
|
+
*/
|
|
7
|
+
const { Db, Manager, dbAdmin } = require('../index.js');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 主流程
|
|
11
|
+
*/
|
|
12
|
+
function main() {
|
|
13
|
+
// 方式1:通过工厂函数获取数据库管理器实例
|
|
14
|
+
const manager = dbAdmin('example');
|
|
15
|
+
console.log('工厂实例是 Manager 类型:', manager instanceof Manager);
|
|
16
|
+
console.log('admin 缓存同作用域实例:', dbAdmin('example') === manager);
|
|
17
|
+
|
|
18
|
+
// 方式2:注册一个数据库驱动(仅注册配置,不建立连接)
|
|
19
|
+
const drive = manager.add({
|
|
20
|
+
name: 'user',
|
|
21
|
+
title: '用户表',
|
|
22
|
+
table: 'user'
|
|
23
|
+
});
|
|
24
|
+
console.log('驱动是 Db 类型:', drive instanceof Db);
|
|
25
|
+
console.log('驱动表名:', drive.config.table);
|
|
26
|
+
|
|
27
|
+
// 通过字段描述生成字段模型
|
|
28
|
+
const idModel = drive.model({
|
|
29
|
+
name: 'id',
|
|
30
|
+
type: 'int(11)',
|
|
31
|
+
note: '编号:唯一标识',
|
|
32
|
+
pk: true,
|
|
33
|
+
auto: true,
|
|
34
|
+
notnull: true
|
|
35
|
+
});
|
|
36
|
+
const nameModel = drive.model({
|
|
37
|
+
name: 'name',
|
|
38
|
+
type: 'varchar(50)',
|
|
39
|
+
note: '姓名:长度[2,50]',
|
|
40
|
+
notnull: true
|
|
41
|
+
});
|
|
42
|
+
console.log('主键字段模型:', JSON.stringify(idModel, null, 2));
|
|
43
|
+
console.log('姓名字段模型:', JSON.stringify(nameModel, null, 2));
|
|
44
|
+
|
|
45
|
+
// 生成字段 SQL 模板(纯逻辑,无需数据库连接)
|
|
46
|
+
console.log('主键字段 SQL:', drive._generateFieldSql(idModel));
|
|
47
|
+
console.log('姓名字段 SQL:', drive._generateFieldSql(nameModel));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
main();
|
package/index.js
CHANGED
|
@@ -5,7 +5,7 @@ const admin = new Admin();
|
|
|
5
5
|
admin.setClass(Manager);
|
|
6
6
|
|
|
7
7
|
module.exports = {
|
|
8
|
-
|
|
8
|
+
Db: require('./lib/db'),
|
|
9
9
|
Manager,
|
|
10
10
|
admin,
|
|
11
11
|
/**
|
|
@@ -19,6 +19,6 @@ module.exports = {
|
|
|
19
19
|
if (!scope) {
|
|
20
20
|
scope = 'os';
|
|
21
21
|
}
|
|
22
|
-
return admin.get(scope,
|
|
22
|
+
return admin.get(scope, config, os);
|
|
23
23
|
}
|
|
24
24
|
};
|
package/lib/{drive.js → db.js}
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const path = require('path');
|
|
3
|
-
const Item = require('
|
|
4
|
-
const { string, array, file } = require('mm_helper');
|
|
3
|
+
const Item = require('mm_mod').Base;
|
|
4
|
+
const { string, array, object, file } = require('mm_helper');
|
|
5
5
|
|
|
6
6
|
/** 关键词搜索默认SQL模板(用户表关联查询) */
|
|
7
7
|
const KEYWORD_DEFAULT =
|
|
@@ -10,73 +10,6 @@ const KEYWORD_DEFAULT =
|
|
|
10
10
|
/** 关键词搜索默认提示 */
|
|
11
11
|
const KEYWORD_DEFAULT_TIP = '昵称、手机号、钱包地址';
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
* 按目标类型转换源值(复刻 mm_expand 对象合并的类型转换规则)
|
|
15
|
-
* @param {*} target_val 目标值
|
|
16
|
-
* @param {*} source_val 源值
|
|
17
|
-
* @returns {*} 转换后的值
|
|
18
|
-
*/
|
|
19
|
-
function _convertValue(target_val, source_val) {
|
|
20
|
-
var target_type = typeof target_val;
|
|
21
|
-
if (target_type === 'number') {
|
|
22
|
-
var num = Number(source_val);
|
|
23
|
-
return isNaN(num) ? 0 : num;
|
|
24
|
-
} else if (target_type === 'boolean') {
|
|
25
|
-
if (source_val === '0' || source_val === 'false' || source_val === 'False') {
|
|
26
|
-
return false;
|
|
27
|
-
} else if (source_val === '1' || source_val === 'true' || source_val === 'True') {
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
return Boolean(source_val);
|
|
31
|
-
} else if (target_type === 'string') {
|
|
32
|
-
return String(source_val);
|
|
33
|
-
}
|
|
34
|
-
return source_val;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 深度合并对象属性(替代原全局 push 深合并)
|
|
39
|
-
* @param {object} target 被添加的对象
|
|
40
|
-
* @param {object} source 用作添加的对象
|
|
41
|
-
* @param {boolean} supplement 是否补充目标没有的属性
|
|
42
|
-
* @returns {object} 修改后的目标对象
|
|
43
|
-
*/
|
|
44
|
-
function _push(target, source, supplement) {
|
|
45
|
-
if (!target || !source || typeof target !== 'object' || typeof source !== 'object') {
|
|
46
|
-
return target;
|
|
47
|
-
}
|
|
48
|
-
for (var key in source) {
|
|
49
|
-
if (target.hasOwnProperty(key)) {
|
|
50
|
-
var target_val = target[key];
|
|
51
|
-
var source_val = source[key];
|
|
52
|
-
if (typeof target_val === 'object' && typeof source_val === 'object') {
|
|
53
|
-
_push(target_val, source_val, supplement);
|
|
54
|
-
} else if (typeof target_val === typeof source_val) {
|
|
55
|
-
target[key] = _convertValue(target_val, source_val);
|
|
56
|
-
}
|
|
57
|
-
} else if (supplement) {
|
|
58
|
-
target[key] = source[key];
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return target;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* 补全文件路径(相对路径基于当前工作目录或指定基准目录)
|
|
66
|
-
* @param {string} file 文件路径
|
|
67
|
-
* @param {string} dir 基准目录(可选)
|
|
68
|
-
* @returns {string} 完整路径
|
|
69
|
-
*/
|
|
70
|
-
function _resolvePath(file, dir) {
|
|
71
|
-
if (!file || typeof file !== 'string') {
|
|
72
|
-
return process.cwd();
|
|
73
|
-
}
|
|
74
|
-
if (path.isAbsolute(file)) {
|
|
75
|
-
return file;
|
|
76
|
-
}
|
|
77
|
-
return path.resolve(dir || process.cwd(), file);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
13
|
/**
|
|
81
14
|
* 对LIKE查询中的关键词进行转义, 防止通配符注入和性能攻击
|
|
82
15
|
* @param {string} keyword 原始关键词
|
|
@@ -97,7 +30,7 @@ var escapeLikeKeyword = function (keyword) {
|
|
|
97
30
|
* @augments {Item}
|
|
98
31
|
* @class
|
|
99
32
|
*/
|
|
100
|
-
class
|
|
33
|
+
class Db extends Item {
|
|
101
34
|
static config = {
|
|
102
35
|
// 库表名称(唯一标识)
|
|
103
36
|
name: '',
|
|
@@ -127,7 +60,7 @@ class Drive extends Item {
|
|
|
127
60
|
* @class
|
|
128
61
|
*/
|
|
129
62
|
constructor(config, parent) {
|
|
130
|
-
super({ ...
|
|
63
|
+
super({ ...Db.config, ...config });
|
|
131
64
|
|
|
132
65
|
// 父级管理器引用(用于配置回退与插件查找)
|
|
133
66
|
this.parent = parent || null;
|
|
@@ -149,7 +82,7 @@ class Drive extends Item {
|
|
|
149
82
|
/**
|
|
150
83
|
* 重置配置参数
|
|
151
84
|
*/
|
|
152
|
-
|
|
85
|
+
Db.prototype._preset = function () {
|
|
153
86
|
this.query_string = [
|
|
154
87
|
'name',
|
|
155
88
|
'title',
|
|
@@ -180,7 +113,7 @@ Drive.prototype._preset = function () {
|
|
|
180
113
|
* @param {string} type 字段类型
|
|
181
114
|
* @returns {object} 解析结果
|
|
182
115
|
*/
|
|
183
|
-
|
|
116
|
+
Db.prototype._parseFieldType = function (type) {
|
|
184
117
|
var tp = string.left(type, '(', true);
|
|
185
118
|
var len = string.between(type, '(', ')');
|
|
186
119
|
var max = 0;
|
|
@@ -202,7 +135,7 @@ Drive.prototype._parseFieldType = function (type) {
|
|
|
202
135
|
* @param {number} max_length 最大长度
|
|
203
136
|
* @returns {object} 解析结果
|
|
204
137
|
*/
|
|
205
|
-
|
|
138
|
+
Db.prototype._parseFieldNote = function (note, tp, max_length) {
|
|
206
139
|
var proc_note = note.replace(':', ':').replace('(', '(').replace(')', ')');
|
|
207
140
|
var title = string.left(proc_note, ':', true).trim();
|
|
208
141
|
var desc = string.right(proc_note, ':');
|
|
@@ -256,7 +189,7 @@ Drive.prototype._parseFieldNote = function (note, tp, max_length) {
|
|
|
256
189
|
* @param {number} max_length 最大长度
|
|
257
190
|
* @returns {number} 计算后的最大值
|
|
258
191
|
*/
|
|
259
|
-
|
|
192
|
+
Db.prototype._calcMaxValue = function (tp, max, max_length) {
|
|
260
193
|
var local_max = max;
|
|
261
194
|
if (local_max === 0) {
|
|
262
195
|
switch (tp) {
|
|
@@ -295,7 +228,7 @@ Drive.prototype._calcMaxValue = function (tp, max, max_length) {
|
|
|
295
228
|
* @param {*} default_val 默认值
|
|
296
229
|
* @returns {*} 处理后的默认值
|
|
297
230
|
*/
|
|
298
|
-
|
|
231
|
+
Db.prototype._handleDefaultValue = function (tp, not_null, default_val) {
|
|
299
232
|
if (not_null && !default_val) {
|
|
300
233
|
if (tp === 'varchar' || tp === 'text') {
|
|
301
234
|
return '';
|
|
@@ -327,7 +260,7 @@ Drive.prototype._handleDefaultValue = function (tp, not_null, default_val) {
|
|
|
327
260
|
* @returns {object} 字段模型对象
|
|
328
261
|
* @private
|
|
329
262
|
*/
|
|
330
|
-
|
|
263
|
+
Db.prototype._createFieldModel = function (
|
|
331
264
|
name,
|
|
332
265
|
title,
|
|
333
266
|
description,
|
|
@@ -391,7 +324,7 @@ Drive.prototype._createFieldModel = function (
|
|
|
391
324
|
* @param {object} fields.note 字段备注
|
|
392
325
|
* @returns {object} 返回字段模型
|
|
393
326
|
*/
|
|
394
|
-
|
|
327
|
+
Db.prototype.model = function (fields) {
|
|
395
328
|
var { name, notnull, type, pk, note, auto } = fields;
|
|
396
329
|
|
|
397
330
|
var { tp, max: max_length, decimal } = this._parseFieldType(type);
|
|
@@ -447,7 +380,7 @@ Drive.prototype.model = function (fields) {
|
|
|
447
380
|
* @property {string} o.Manager_comment 索引备注
|
|
448
381
|
* @returns {object} 返回索引模型
|
|
449
382
|
*/
|
|
450
|
-
|
|
383
|
+
Db.prototype.newIndexModel = function (o) {
|
|
451
384
|
return {
|
|
452
385
|
name: o.Key_name,
|
|
453
386
|
type: o.Non_unique ? 'unique' : 'index',
|
|
@@ -461,7 +394,7 @@ Drive.prototype.newIndexModel = function (o) {
|
|
|
461
394
|
* @param {object} db 数据库管理器
|
|
462
395
|
* @returns {Array} 索引列表
|
|
463
396
|
*/
|
|
464
|
-
|
|
397
|
+
Db.prototype._updateFile = async function (db) {
|
|
465
398
|
var sql = 'SHOW INDEX FROM `' + this.config.table + '`';
|
|
466
399
|
var rows = await db.run(sql);
|
|
467
400
|
var dict = {};
|
|
@@ -490,7 +423,7 @@ Drive.prototype._updateFile = async function (db) {
|
|
|
490
423
|
* @param {object} db 数据库管理器
|
|
491
424
|
* @param {boolean} cover 是否覆盖文件
|
|
492
425
|
*/
|
|
493
|
-
|
|
426
|
+
Db.prototype.updateFile = async function (db, cover) {
|
|
494
427
|
var cg = this.config;
|
|
495
428
|
var list = [];
|
|
496
429
|
|
|
@@ -534,7 +467,7 @@ Drive.prototype.updateFile = async function (db, cover) {
|
|
|
534
467
|
* 更新索引
|
|
535
468
|
* @param {object} db 数据库管理器
|
|
536
469
|
*/
|
|
537
|
-
|
|
470
|
+
Db.prototype._updateDb = async function (db) {
|
|
538
471
|
var cg = this.config;
|
|
539
472
|
let index = cg.index || [];
|
|
540
473
|
// 更新索引
|
|
@@ -562,7 +495,7 @@ Drive.prototype._updateDb = async function (db) {
|
|
|
562
495
|
* @param {Array} list 字段列表
|
|
563
496
|
* @returns {Array} 字段列表
|
|
564
497
|
*/
|
|
565
|
-
|
|
498
|
+
Db.prototype._initTable = async function (db, cg, list) {
|
|
566
499
|
var fields = await db.fields();
|
|
567
500
|
if (fields.length === 0) {
|
|
568
501
|
var k = cg.key;
|
|
@@ -599,7 +532,7 @@ Drive.prototype._initTable = async function (db, cg, list) {
|
|
|
599
532
|
* @param {Array} fields 数据库字段列表
|
|
600
533
|
* @param {Array} list 配置字段列表
|
|
601
534
|
*/
|
|
602
|
-
|
|
535
|
+
Db.prototype._deleteUnusedFields = async function (db, fields, list) {
|
|
603
536
|
for (var i = 0; i < fields.length; i++) {
|
|
604
537
|
var o = fields[i];
|
|
605
538
|
var obj = array.getObj(list, {
|
|
@@ -617,7 +550,7 @@ Drive.prototype._deleteUnusedFields = async function (db, fields, list) {
|
|
|
617
550
|
* @param {object} o 字段对象
|
|
618
551
|
* @returns {string} 字段SQL
|
|
619
552
|
*/
|
|
620
|
-
|
|
553
|
+
Db.prototype._generateFieldSql = function (o) {
|
|
621
554
|
var type = this._getTypeSql(o);
|
|
622
555
|
var notnull = this._getNotNullSql(o);
|
|
623
556
|
var value = this._getValueSql(o);
|
|
@@ -631,7 +564,7 @@ Drive.prototype._generateFieldSql = function (o) {
|
|
|
631
564
|
* @returns {string} 类型SQL
|
|
632
565
|
* @private
|
|
633
566
|
*/
|
|
634
|
-
|
|
567
|
+
Db.prototype._getTypeSql = function (o) {
|
|
635
568
|
var type = o.type;
|
|
636
569
|
var max_len = o.max_length || 0;
|
|
637
570
|
|
|
@@ -672,7 +605,7 @@ Drive.prototype._getTypeSql = function (o) {
|
|
|
672
605
|
* @returns {string} NOT NULL SQL
|
|
673
606
|
* @private
|
|
674
607
|
*/
|
|
675
|
-
|
|
608
|
+
Db.prototype._getNotNullSql = function (o) {
|
|
676
609
|
var notnull = '';
|
|
677
610
|
|
|
678
611
|
if (this._isNotNullRequired(o)) {
|
|
@@ -692,7 +625,7 @@ Drive.prototype._getNotNullSql = function (o) {
|
|
|
692
625
|
* @returns {boolean} 是否需要NOT NULL
|
|
693
626
|
* @private
|
|
694
627
|
*/
|
|
695
|
-
|
|
628
|
+
Db.prototype._isNotNullRequired = function (o) {
|
|
696
629
|
return (
|
|
697
630
|
o.not_null || this._isDateTimeType(o.type) || this._isNumericType(o.type)
|
|
698
631
|
);
|
|
@@ -704,7 +637,7 @@ Drive.prototype._isNotNullRequired = function (o) {
|
|
|
704
637
|
* @returns {boolean} 是否为日期时间类型
|
|
705
638
|
* @private
|
|
706
639
|
*/
|
|
707
|
-
|
|
640
|
+
Db.prototype._isDateTimeType = function (type) {
|
|
708
641
|
return (
|
|
709
642
|
type === 'date' ||
|
|
710
643
|
type === 'time' ||
|
|
@@ -719,7 +652,7 @@ Drive.prototype._isDateTimeType = function (type) {
|
|
|
719
652
|
* @returns {boolean} 是否为数字类型
|
|
720
653
|
* @private
|
|
721
654
|
*/
|
|
722
|
-
|
|
655
|
+
Db.prototype._isNumericType = function (type) {
|
|
723
656
|
return (
|
|
724
657
|
type !== 'varchar' &&
|
|
725
658
|
type !== 'longtext' &&
|
|
@@ -734,7 +667,7 @@ Drive.prototype._isNumericType = function (type) {
|
|
|
734
667
|
* @returns {boolean} 是否需要AUTO_INCREMENT
|
|
735
668
|
* @private
|
|
736
669
|
*/
|
|
737
|
-
|
|
670
|
+
Db.prototype._isAutoInc = function (o) {
|
|
738
671
|
return !!o.auto && !this._isDateTimeType(o.type);
|
|
739
672
|
};
|
|
740
673
|
|
|
@@ -744,7 +677,7 @@ Drive.prototype._isAutoInc = function (o) {
|
|
|
744
677
|
* @returns {string} 默认值SQL
|
|
745
678
|
* @private
|
|
746
679
|
*/
|
|
747
|
-
|
|
680
|
+
Db.prototype._getValueSql = function (o) {
|
|
748
681
|
if (this._isAutoTime(o)) {
|
|
749
682
|
return 'DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
|
|
750
683
|
}
|
|
@@ -771,7 +704,7 @@ Drive.prototype._getValueSql = function (o) {
|
|
|
771
704
|
* @returns {boolean} 是否为自动时间
|
|
772
705
|
* @private
|
|
773
706
|
*/
|
|
774
|
-
|
|
707
|
+
Db.prototype._isAutoTime = function (o) {
|
|
775
708
|
return !!o.auto && this._isDateTimeType(o.type);
|
|
776
709
|
};
|
|
777
710
|
|
|
@@ -781,7 +714,7 @@ Drive.prototype._isAutoTime = function (o) {
|
|
|
781
714
|
* @returns {string} 默认值SQL
|
|
782
715
|
* @private
|
|
783
716
|
*/
|
|
784
|
-
|
|
717
|
+
Db.prototype._getDefaultValueSql = function (o) {
|
|
785
718
|
if (this._isCurrentDefault(o)) {
|
|
786
719
|
return 'DEFAULT ' + o.default;
|
|
787
720
|
} else if (this._isQuotableType(o.type)) {
|
|
@@ -797,7 +730,7 @@ Drive.prototype._getDefaultValueSql = function (o) {
|
|
|
797
730
|
* @returns {boolean} 是否为当前时间默认值
|
|
798
731
|
* @private
|
|
799
732
|
*/
|
|
800
|
-
|
|
733
|
+
Db.prototype._isCurrentDefault = function (o) {
|
|
801
734
|
return (
|
|
802
735
|
this._isDateTimeType(o.type) &&
|
|
803
736
|
o.default &&
|
|
@@ -811,7 +744,7 @@ Drive.prototype._isCurrentDefault = function (o) {
|
|
|
811
744
|
* @returns {boolean} 需要引号返回true,否则返回false
|
|
812
745
|
* @private
|
|
813
746
|
*/
|
|
814
|
-
|
|
747
|
+
Db.prototype._isQuotableType = function (type) {
|
|
815
748
|
return (
|
|
816
749
|
type === 'varchar' ||
|
|
817
750
|
type === 'longtext' ||
|
|
@@ -829,7 +762,7 @@ Drive.prototype._isQuotableType = function (type) {
|
|
|
829
762
|
* @returns {string} 空默认值SQL
|
|
830
763
|
* @private
|
|
831
764
|
*/
|
|
832
|
-
|
|
765
|
+
Db.prototype._getDefaultEmptySql = function (o) {
|
|
833
766
|
if (this._isStringType(o.type)) {
|
|
834
767
|
return "DEFAULT ''";
|
|
835
768
|
} else {
|
|
@@ -843,7 +776,7 @@ Drive.prototype._getDefaultEmptySql = function (o) {
|
|
|
843
776
|
* @returns {string} 注释SQL
|
|
844
777
|
* @private
|
|
845
778
|
*/
|
|
846
|
-
|
|
779
|
+
Db.prototype._getNoteSql = function (o) {
|
|
847
780
|
var note = (o.title || '') + ':';
|
|
848
781
|
|
|
849
782
|
if (o.type === 'varchar' || o.type === 'text' || o.type === 'longtext') {
|
|
@@ -878,7 +811,7 @@ Drive.prototype._getNoteSql = function (o) {
|
|
|
878
811
|
* @returns {string} 字段SQL
|
|
879
812
|
* @private
|
|
880
813
|
*/
|
|
881
|
-
|
|
814
|
+
Db.prototype._buildFieldSql = function (name, type, notnull, value, note) {
|
|
882
815
|
var sql = "`{1}` {2} {3} {4} COMMENT '{5}'";
|
|
883
816
|
return sql
|
|
884
817
|
.replace('{1}', name)
|
|
@@ -895,7 +828,7 @@ Drive.prototype._buildFieldSql = function (name, type, notnull, value, note) {
|
|
|
895
828
|
* @param {object} o 字段对象
|
|
896
829
|
* @param {Array} fields 数据库字段列表
|
|
897
830
|
*/
|
|
898
|
-
|
|
831
|
+
Db.prototype._addOrUpdateField = async function (db, table, o, fields) {
|
|
899
832
|
var arr = fields.filter((f) => {
|
|
900
833
|
return f.name === o.name;
|
|
901
834
|
});
|
|
@@ -919,7 +852,7 @@ Drive.prototype._addOrUpdateField = async function (db, table, o, fields) {
|
|
|
919
852
|
* @param {object} db 数据库管理器
|
|
920
853
|
* @returns {string} 更新成功返回空,否则返回错误提示
|
|
921
854
|
*/
|
|
922
|
-
|
|
855
|
+
Db.prototype.updateDb = async function (db) {
|
|
923
856
|
var cg = this.config;
|
|
924
857
|
var table = cg.table + '';
|
|
925
858
|
var list = cg.fields;
|
|
@@ -947,14 +880,14 @@ Drive.prototype.updateDb = async function (db) {
|
|
|
947
880
|
* @returns {object} 目录信息
|
|
948
881
|
* @private
|
|
949
882
|
*/
|
|
950
|
-
|
|
883
|
+
Db.prototype._deteDir = function (cg) {
|
|
951
884
|
var arr = cg.table.split('_');
|
|
952
885
|
var scope = arr[0];
|
|
953
|
-
var p =
|
|
954
|
-
var dir =
|
|
955
|
-
var dir_api =
|
|
956
|
-
var db_dir =
|
|
957
|
-
var config_file =
|
|
886
|
+
var p = file.fullname('./app/');
|
|
887
|
+
var dir = file.fullname('./' + scope, p);
|
|
888
|
+
var dir_api = file.fullname('./plugin/server', dir);
|
|
889
|
+
var db_dir = file.fullname('./db', dir_api);
|
|
890
|
+
var config_file = file.fullname(
|
|
958
891
|
'./' + cg.table.replace(scope + '_', '') + '.db.json',
|
|
959
892
|
db_dir,
|
|
960
893
|
);
|
|
@@ -973,7 +906,7 @@ Drive.prototype._deteDir = function (cg) {
|
|
|
973
906
|
* @param {string} dir 目录路径
|
|
974
907
|
* @private
|
|
975
908
|
*/
|
|
976
|
-
|
|
909
|
+
Db.prototype._ensureDir = function (dir) {
|
|
977
910
|
if (!fs.existsSync(dir)) {
|
|
978
911
|
fs.mkdirSync(dir);
|
|
979
912
|
}
|
|
@@ -983,7 +916,7 @@ Drive.prototype._ensureDir = function (dir) {
|
|
|
983
916
|
* 保存当前配置到配置文件(替代旧基类 save)
|
|
984
917
|
* @returns {void}
|
|
985
918
|
*/
|
|
986
|
-
|
|
919
|
+
Db.prototype.save = function () {
|
|
987
920
|
if (!this.config_file) {
|
|
988
921
|
return;
|
|
989
922
|
}
|
|
@@ -1014,7 +947,7 @@ Drive.prototype.save = function () {
|
|
|
1014
947
|
* 获取当前驱动目录(替代旧基类 getDir)
|
|
1015
948
|
* @returns {string} 目录路径
|
|
1016
949
|
*/
|
|
1017
|
-
|
|
950
|
+
Db.prototype.getDir = function () {
|
|
1018
951
|
if (this._dir) {
|
|
1019
952
|
return this._dir;
|
|
1020
953
|
}
|
|
@@ -1029,7 +962,7 @@ Drive.prototype.getDir = function () {
|
|
|
1029
962
|
* @returns {object|null} 事件接口管理器实例,未配置返回null
|
|
1030
963
|
* @private
|
|
1031
964
|
*/
|
|
1032
|
-
|
|
965
|
+
Db.prototype._getEventApi = function () {
|
|
1033
966
|
var getter = this.config.get_event_api;
|
|
1034
967
|
if (typeof getter !== 'function' && this.parent) {
|
|
1035
968
|
getter = this.parent.config.get_event_api;
|
|
@@ -1050,7 +983,7 @@ Drive.prototype._getEventApi = function () {
|
|
|
1050
983
|
* @param {boolean} cover 是否覆盖文件
|
|
1051
984
|
* @private
|
|
1052
985
|
*/
|
|
1053
|
-
|
|
986
|
+
Db.prototype._handleConfigFile = function (f, cover) {
|
|
1054
987
|
if (!file.hasFile(f)) {
|
|
1055
988
|
this.save();
|
|
1056
989
|
} else if (cover) {
|
|
@@ -1062,7 +995,7 @@ Drive.prototype._handleConfigFile = function (f, cover) {
|
|
|
1062
995
|
if (!o.description) {
|
|
1063
996
|
delete o.description;
|
|
1064
997
|
}
|
|
1065
|
-
|
|
998
|
+
object.push(jobj, o, true);
|
|
1066
999
|
file.saveText(f, JSON.stringify(jobj, null, 2));
|
|
1067
1000
|
}
|
|
1068
1001
|
};
|
|
@@ -1071,7 +1004,7 @@ Drive.prototype._handleConfigFile = function (f, cover) {
|
|
|
1071
1004
|
* 更新应用,根据表生成目录结构和文件
|
|
1072
1005
|
* @param {boolean} cover 是否覆盖文件
|
|
1073
1006
|
*/
|
|
1074
|
-
|
|
1007
|
+
Db.prototype.updateApp = async function (cover) {
|
|
1075
1008
|
var cg = this.config;
|
|
1076
1009
|
var f;
|
|
1077
1010
|
var dir_api;
|
|
@@ -1111,7 +1044,7 @@ Drive.prototype.updateApp = async function (cover) {
|
|
|
1111
1044
|
* @param {string} dir 目录路径
|
|
1112
1045
|
* @private
|
|
1113
1046
|
*/
|
|
1114
|
-
|
|
1047
|
+
Db.prototype._ensureApiDir = function (dir) {
|
|
1115
1048
|
if (!fs.existsSync(dir)) {
|
|
1116
1049
|
fs.mkdirSync(dir);
|
|
1117
1050
|
}
|
|
@@ -1126,7 +1059,7 @@ Drive.prototype._ensureApiDir = function (dir) {
|
|
|
1126
1059
|
* @returns {string} 完整目录路径
|
|
1127
1060
|
* @private
|
|
1128
1061
|
*/
|
|
1129
|
-
|
|
1062
|
+
Db.prototype._getApiDir = function (dir, app, type, name) {
|
|
1130
1063
|
var l = path.sep;
|
|
1131
1064
|
var event_api = this._getEventApi();
|
|
1132
1065
|
var o =
|
|
@@ -1153,7 +1086,7 @@ Drive.prototype._getApiDir = function (dir, app, type, name) {
|
|
|
1153
1086
|
* @param {string} dir API存放目录
|
|
1154
1087
|
* @param {boolean} cover 是否覆盖文件
|
|
1155
1088
|
*/
|
|
1156
|
-
|
|
1089
|
+
Db.prototype.updateApi = async function (dir, cover) {
|
|
1157
1090
|
var cg = this.config;
|
|
1158
1091
|
var arr = cg.table.split('_');
|
|
1159
1092
|
|
|
@@ -1177,7 +1110,7 @@ Drive.prototype.updateApi = async function (dir, cover) {
|
|
|
1177
1110
|
* @param {string} path 检索路径
|
|
1178
1111
|
* @param {string} scope 接口域
|
|
1179
1112
|
*/
|
|
1180
|
-
|
|
1113
|
+
Db.prototype.newEvent = async function (dir, path, scope) {
|
|
1181
1114
|
var f = dir + '/main.js';
|
|
1182
1115
|
if (!file.hasFile(f)) {
|
|
1183
1116
|
var code = file.loadText(__dirname + '/event_script.js');
|
|
@@ -1192,7 +1125,7 @@ Drive.prototype.newEvent = async function (dir, path, scope) {
|
|
|
1192
1125
|
* @returns {Array} 处理后的列表
|
|
1193
1126
|
* @private
|
|
1194
1127
|
*/
|
|
1195
|
-
|
|
1128
|
+
Db.prototype._handleCommaList = function (list) {
|
|
1196
1129
|
return list.map((o) => {
|
|
1197
1130
|
var arr = o.split(',');
|
|
1198
1131
|
var value = arr[0];
|
|
@@ -1217,7 +1150,7 @@ Drive.prototype._handleCommaList = function (list) {
|
|
|
1217
1150
|
* @returns {Array} 处理后的列表
|
|
1218
1151
|
* @private
|
|
1219
1152
|
*/
|
|
1220
|
-
|
|
1153
|
+
Db.prototype._handleNumberList = function (list, map) {
|
|
1221
1154
|
if (map.indexOf('0') !== 0) {
|
|
1222
1155
|
list.unshift('');
|
|
1223
1156
|
}
|
|
@@ -1232,7 +1165,7 @@ Drive.prototype._handleNumberList = function (list, map) {
|
|
|
1232
1165
|
* @returns {Array} 处理后的列表
|
|
1233
1166
|
* @private
|
|
1234
1167
|
*/
|
|
1235
|
-
|
|
1168
|
+
Db.prototype._handleSimpleList = function (list) {
|
|
1236
1169
|
return list.map((value) => {
|
|
1237
1170
|
return {
|
|
1238
1171
|
name: value,
|
|
@@ -1248,7 +1181,7 @@ Drive.prototype._handleSimpleList = function (list) {
|
|
|
1248
1181
|
* @returns {object} 处理后的格式对象
|
|
1249
1182
|
* @private
|
|
1250
1183
|
*/
|
|
1251
|
-
|
|
1184
|
+
Db.prototype._handleDotMap = function (obj, map) {
|
|
1252
1185
|
var arr = map.split('.');
|
|
1253
1186
|
var format = {
|
|
1254
1187
|
table: arr[0],
|
|
@@ -1269,7 +1202,7 @@ Drive.prototype._handleDotMap = function (obj, map) {
|
|
|
1269
1202
|
* @returns {object} 处理后的格式对象
|
|
1270
1203
|
* @private
|
|
1271
1204
|
*/
|
|
1272
|
-
|
|
1205
|
+
Db.prototype._handleSimpleMap = function (obj, map) {
|
|
1273
1206
|
return {
|
|
1274
1207
|
table: map,
|
|
1275
1208
|
id: obj.name,
|
|
@@ -1282,7 +1215,7 @@ Drive.prototype._handleSimpleMap = function (obj, map) {
|
|
|
1282
1215
|
* @param {object} obj 字段对象
|
|
1283
1216
|
* @returns {object} 返回格式
|
|
1284
1217
|
*/
|
|
1285
|
-
|
|
1218
|
+
Db.prototype.getFormat = async function (obj) {
|
|
1286
1219
|
var map = obj.map;
|
|
1287
1220
|
var format = {
|
|
1288
1221
|
key: obj.name,
|
|
@@ -1316,7 +1249,7 @@ Drive.prototype.getFormat = async function (obj) {
|
|
|
1316
1249
|
* @returns {object} 处理结果
|
|
1317
1250
|
* @private
|
|
1318
1251
|
*/
|
|
1319
|
-
|
|
1252
|
+
Db.prototype._procFieldSel = function (field_name, field_type) {
|
|
1320
1253
|
var res = {
|
|
1321
1254
|
field: '',
|
|
1322
1255
|
field_obj: '',
|
|
@@ -1339,7 +1272,7 @@ Drive.prototype._procFieldSel = function (field_name, field_type) {
|
|
|
1339
1272
|
* @returns {string} 更新后的关键词
|
|
1340
1273
|
* @private
|
|
1341
1274
|
*/
|
|
1342
|
-
|
|
1275
|
+
Db.prototype._procStringQuery = function (query, field_name, keyword) {
|
|
1343
1276
|
var new_keyword = keyword;
|
|
1344
1277
|
query[field_name] = '`' + field_name + "` like '%{0}%'";
|
|
1345
1278
|
if (this.isSet(field_name, this.query_keyword)) {
|
|
@@ -1354,7 +1287,7 @@ Drive.prototype._procStringQuery = function (query, field_name, keyword) {
|
|
|
1354
1287
|
* @param {string} field_name 字段名
|
|
1355
1288
|
* @private
|
|
1356
1289
|
*/
|
|
1357
|
-
|
|
1290
|
+
Db.prototype._procDateTimeQuery = function (query, field_name) {
|
|
1358
1291
|
query[field_name + '_min'] = '`' + field_name + "` >= '{0}'";
|
|
1359
1292
|
query[field_name + '_max'] = '`' + field_name + "` <= '{0}'";
|
|
1360
1293
|
};
|
|
@@ -1370,7 +1303,7 @@ Drive.prototype._procDateTimeQuery = function (query, field_name) {
|
|
|
1370
1303
|
* @returns {object} 处理结果
|
|
1371
1304
|
* @private
|
|
1372
1305
|
*/
|
|
1373
|
-
|
|
1306
|
+
Db.prototype._procNumberQuery = function (
|
|
1374
1307
|
query,
|
|
1375
1308
|
update_obj,
|
|
1376
1309
|
field_name,
|
|
@@ -1418,7 +1351,7 @@ Drive.prototype._procNumberQuery = function (
|
|
|
1418
1351
|
* @returns {object} 处理结果
|
|
1419
1352
|
* @private
|
|
1420
1353
|
*/
|
|
1421
|
-
|
|
1354
|
+
Db.prototype._procFieldType = function (
|
|
1422
1355
|
type,
|
|
1423
1356
|
field_name,
|
|
1424
1357
|
query,
|
|
@@ -1475,7 +1408,7 @@ Drive.prototype._procFieldType = function (
|
|
|
1475
1408
|
* @returns {object} 处理结果
|
|
1476
1409
|
* @private
|
|
1477
1410
|
*/
|
|
1478
|
-
|
|
1411
|
+
Db.prototype._procSingleField = async function (
|
|
1479
1412
|
o,
|
|
1480
1413
|
query,
|
|
1481
1414
|
update_obj,
|
|
@@ -1531,7 +1464,7 @@ Drive.prototype._procSingleField = async function (
|
|
|
1531
1464
|
* @returns {object} 处理结果
|
|
1532
1465
|
* @private
|
|
1533
1466
|
*/
|
|
1534
|
-
|
|
1467
|
+
Db.prototype._procFields = async function (fields, table) {
|
|
1535
1468
|
var query = {};
|
|
1536
1469
|
var update_obj = {};
|
|
1537
1470
|
var field = '';
|
|
@@ -1588,7 +1521,7 @@ Drive.prototype._procFields = async function (fields, table) {
|
|
|
1588
1521
|
* @returns {object} 处理后的查询对象
|
|
1589
1522
|
* @private
|
|
1590
1523
|
*/
|
|
1591
|
-
|
|
1524
|
+
Db.prototype._procKeyword = function (keyword, table) {
|
|
1592
1525
|
var safe_keyword = escapeLikeKeyword(keyword);
|
|
1593
1526
|
var new_keyword = safe_keyword;
|
|
1594
1527
|
if (table.indexOf('user_') !== -1) {
|
|
@@ -1611,7 +1544,7 @@ Drive.prototype._procKeyword = function (keyword, table) {
|
|
|
1611
1544
|
* @returns {object} 基础模型
|
|
1612
1545
|
* @private
|
|
1613
1546
|
*/
|
|
1614
|
-
|
|
1547
|
+
Db.prototype._createSqlConfig = function (cg, processed, keyword_query) {
|
|
1615
1548
|
return {
|
|
1616
1549
|
name: cg.table,
|
|
1617
1550
|
title: cg.title,
|
|
@@ -1638,7 +1571,7 @@ Drive.prototype._createSqlConfig = function (cg, processed, keyword_query) {
|
|
|
1638
1571
|
* @param {string} table 表名
|
|
1639
1572
|
* @private
|
|
1640
1573
|
*/
|
|
1641
|
-
|
|
1574
|
+
Db.prototype._saveClientConfig = function (
|
|
1642
1575
|
client,
|
|
1643
1576
|
base_model,
|
|
1644
1577
|
cover,
|
|
@@ -1676,7 +1609,7 @@ Drive.prototype._saveClientConfig = function (
|
|
|
1676
1609
|
* @param {boolean} cover 是否覆盖文件
|
|
1677
1610
|
* @private
|
|
1678
1611
|
*/
|
|
1679
|
-
|
|
1612
|
+
Db.prototype._saveManageConfig = function (manage, base_model, cover) {
|
|
1680
1613
|
var m = { ...base_model };
|
|
1681
1614
|
delete m.method;
|
|
1682
1615
|
m.field_hide = [];
|
|
@@ -1696,7 +1629,7 @@ Drive.prototype._saveManageConfig = function (manage, base_model, cover) {
|
|
|
1696
1629
|
* @param {string} manage 管理端配置保存路径
|
|
1697
1630
|
* @param {boolean} cover 是否覆盖文件
|
|
1698
1631
|
*/
|
|
1699
|
-
|
|
1632
|
+
Db.prototype.newSql = async function (client, manage, cover) {
|
|
1700
1633
|
var cg = this.config;
|
|
1701
1634
|
var uid = this.config.user_id;
|
|
1702
1635
|
|
|
@@ -1726,7 +1659,7 @@ Drive.prototype.newSql = async function (client, manage, cover) {
|
|
|
1726
1659
|
* @param {object} model 配置模型
|
|
1727
1660
|
* @param {boolean} cover 是否覆盖文件
|
|
1728
1661
|
*/
|
|
1729
|
-
|
|
1662
|
+
Db.prototype.saveFile = function (file, model, cover) {
|
|
1730
1663
|
if (!file.hasFile(file)) {
|
|
1731
1664
|
file.saveText(file, JSON.stringify(model, null, 2));
|
|
1732
1665
|
} else if (cover) {
|
|
@@ -1737,7 +1670,7 @@ Drive.prototype.saveFile = function (file, model, cover) {
|
|
|
1737
1670
|
model[k] = jobj[k];
|
|
1738
1671
|
}
|
|
1739
1672
|
}
|
|
1740
|
-
|
|
1673
|
+
object.push(jobj, model, true);
|
|
1741
1674
|
file.saveText(file, JSON.stringify(jobj, null, 2));
|
|
1742
1675
|
}
|
|
1743
1676
|
};
|
|
@@ -1748,7 +1681,7 @@ Drive.prototype.saveFile = function (file, model, cover) {
|
|
|
1748
1681
|
* @param {string} manage 管理端配置保存路径
|
|
1749
1682
|
* @param {boolean} cover 是否覆盖文件
|
|
1750
1683
|
*/
|
|
1751
|
-
|
|
1684
|
+
Db.prototype.newParam = async function (client, manage, cover) {
|
|
1752
1685
|
var cg = this.config;
|
|
1753
1686
|
var lt = cg.fields;
|
|
1754
1687
|
var cm = this._initParamConfig(cg);
|
|
@@ -1779,7 +1712,7 @@ Drive.prototype.newParam = async function (client, manage, cover) {
|
|
|
1779
1712
|
* @returns {object} 参数配置对象
|
|
1780
1713
|
* @private
|
|
1781
1714
|
*/
|
|
1782
|
-
|
|
1715
|
+
Db.prototype._initParamConfig = function (cg) {
|
|
1783
1716
|
return {
|
|
1784
1717
|
name: cg.table,
|
|
1785
1718
|
title: cg.title,
|
|
@@ -1817,7 +1750,7 @@ Drive.prototype._initParamConfig = function (cg) {
|
|
|
1817
1750
|
* @returns {object} 参数模型对象
|
|
1818
1751
|
* @private
|
|
1819
1752
|
*/
|
|
1820
|
-
|
|
1753
|
+
Db.prototype._createParamModel = function (field, type, name) {
|
|
1821
1754
|
return {
|
|
1822
1755
|
name: name,
|
|
1823
1756
|
title: field.title,
|
|
@@ -1834,7 +1767,7 @@ Drive.prototype._createParamModel = function (field, type, name) {
|
|
|
1834
1767
|
* @returns {boolean} 是否为字符串类型
|
|
1835
1768
|
* @private
|
|
1836
1769
|
*/
|
|
1837
|
-
|
|
1770
|
+
Db.prototype._isStringType = function (type) {
|
|
1838
1771
|
return type === 'varchar' || type === 'text' || type === 'longtext';
|
|
1839
1772
|
};
|
|
1840
1773
|
|
|
@@ -1848,7 +1781,7 @@ Drive.prototype._isStringType = function (type) {
|
|
|
1848
1781
|
* @returns {string} 更新后的关键词
|
|
1849
1782
|
* @private
|
|
1850
1783
|
*/
|
|
1851
|
-
|
|
1784
|
+
Db.prototype._processStringField = function (cm, m, o, n, keyword) {
|
|
1852
1785
|
var new_keyword = keyword;
|
|
1853
1786
|
m.type = 'string';
|
|
1854
1787
|
m.default = o.default;
|
|
@@ -1881,7 +1814,7 @@ Drive.prototype._processStringField = function (cm, m, o, n, keyword) {
|
|
|
1881
1814
|
* @param {object} o 字段对象
|
|
1882
1815
|
* @private
|
|
1883
1816
|
*/
|
|
1884
|
-
|
|
1817
|
+
Db.prototype._addStringRange = function (m, o) {
|
|
1885
1818
|
var range = o.min_length && o.max_length ? [o.min_length, o.max_length] : [];
|
|
1886
1819
|
if (range.length > 0) {
|
|
1887
1820
|
m.string.range = range;
|
|
@@ -1899,7 +1832,7 @@ Drive.prototype._addStringRange = function (m, o) {
|
|
|
1899
1832
|
* @returns {boolean} 是否包含
|
|
1900
1833
|
* @private
|
|
1901
1834
|
*/
|
|
1902
|
-
|
|
1835
|
+
Db.prototype._hasKeyword = function (field_name, keyword) {
|
|
1903
1836
|
return string.has(field_name, keyword);
|
|
1904
1837
|
};
|
|
1905
1838
|
|
|
@@ -1910,7 +1843,7 @@ Drive.prototype._hasKeyword = function (field_name, keyword) {
|
|
|
1910
1843
|
* @returns {boolean} 是否等于
|
|
1911
1844
|
* @private
|
|
1912
1845
|
*/
|
|
1913
|
-
|
|
1846
|
+
Db.prototype._isEqual = function (field_name, values) {
|
|
1914
1847
|
return values.includes(field_name);
|
|
1915
1848
|
};
|
|
1916
1849
|
|
|
@@ -1920,7 +1853,7 @@ Drive.prototype._isEqual = function (field_name, values) {
|
|
|
1920
1853
|
* @param {string} n 字段名
|
|
1921
1854
|
* @private
|
|
1922
1855
|
*/
|
|
1923
|
-
|
|
1856
|
+
Db.prototype._addStringFormat = function (m, n) {
|
|
1924
1857
|
if (this._hasKeyword(n, 'phone') || this._isEqual(n, ['tel'])) {
|
|
1925
1858
|
m.string.format = 'phone';
|
|
1926
1859
|
} else if (
|
|
@@ -1947,7 +1880,7 @@ Drive.prototype._addStringFormat = function (m, n) {
|
|
|
1947
1880
|
* @param {string} n 字段名
|
|
1948
1881
|
* @private
|
|
1949
1882
|
*/
|
|
1950
|
-
|
|
1883
|
+
Db.prototype._processDateTimeField = function (cm, m, o, n) {
|
|
1951
1884
|
var format = o.type === 'timestamp' ? 'datetime' : o.type;
|
|
1952
1885
|
m.type = 'string';
|
|
1953
1886
|
m.default = '';
|
|
@@ -1975,7 +1908,7 @@ Drive.prototype._processDateTimeField = function (cm, m, o, n) {
|
|
|
1975
1908
|
* @param {string} n 字段名
|
|
1976
1909
|
* @private
|
|
1977
1910
|
*/
|
|
1978
|
-
|
|
1911
|
+
Db.prototype._addDateTimeRange = function (cm, m, n) {
|
|
1979
1912
|
var m_min = { ...m };
|
|
1980
1913
|
m_min.name = n + '_min';
|
|
1981
1914
|
m_min.title += '——开始时间';
|
|
@@ -1996,7 +1929,7 @@ Drive.prototype._addDateTimeRange = function (cm, m, n) {
|
|
|
1996
1929
|
* @param {object} cg 配置对象
|
|
1997
1930
|
* @private
|
|
1998
1931
|
*/
|
|
1999
|
-
|
|
1932
|
+
Db.prototype._processNumberField = function (cm, m, o, n, cg) {
|
|
2000
1933
|
m.type = 'number';
|
|
2001
1934
|
m.default = Number(o.default) + '';
|
|
2002
1935
|
m.number = {};
|
|
@@ -2016,7 +1949,7 @@ Drive.prototype._processNumberField = function (cm, m, o, n, cg) {
|
|
|
2016
1949
|
* @param {object} o 字段对象
|
|
2017
1950
|
* @private
|
|
2018
1951
|
*/
|
|
2019
|
-
|
|
1952
|
+
Db.prototype._addNumberRange = function (m, o) {
|
|
2020
1953
|
var range = o.min && o.max ? [o.min, o.max] : [];
|
|
2021
1954
|
if (range.length > 0) {
|
|
2022
1955
|
m.number.range = range;
|
|
@@ -2034,7 +1967,7 @@ Drive.prototype._addNumberRange = function (m, o) {
|
|
|
2034
1967
|
* @param {string} n 字段名
|
|
2035
1968
|
* @private
|
|
2036
1969
|
*/
|
|
2037
|
-
|
|
1970
|
+
Db.prototype._processPrimaryKey = function (cm, m, n) {
|
|
2038
1971
|
cm.del.query_required.push(n);
|
|
2039
1972
|
cm.set.query.push(n);
|
|
2040
1973
|
cm.get.query.push(n);
|
|
@@ -2049,7 +1982,7 @@ Drive.prototype._processPrimaryKey = function (cm, m, n) {
|
|
|
2049
1982
|
* @param {string} n 字段名
|
|
2050
1983
|
* @private
|
|
2051
1984
|
*/
|
|
2052
|
-
|
|
1985
|
+
Db.prototype._processNonPrimaryKey = function (cm, m, n) {
|
|
2053
1986
|
cm.add.body.push(n);
|
|
2054
1987
|
cm.set.body.push(n);
|
|
2055
1988
|
cm.list.push(m);
|
|
@@ -2069,7 +2002,7 @@ Drive.prototype._processNonPrimaryKey = function (cm, m, n) {
|
|
|
2069
2002
|
* @param {string} n 字段名
|
|
2070
2003
|
* @private
|
|
2071
2004
|
*/
|
|
2072
|
-
|
|
2005
|
+
Db.prototype._processNonTinyInt = function (cm, m, n) {
|
|
2073
2006
|
var ne = n;
|
|
2074
2007
|
if (!ne.endsWith('id')) {
|
|
2075
2008
|
this._addNumberRangeFields(cm, m, n);
|
|
@@ -2088,7 +2021,7 @@ Drive.prototype._processNonTinyInt = function (cm, m, n) {
|
|
|
2088
2021
|
* @param {string} n 字段名
|
|
2089
2022
|
* @private
|
|
2090
2023
|
*/
|
|
2091
|
-
|
|
2024
|
+
Db.prototype._addNumberRangeFields = function (cm, m, n) {
|
|
2092
2025
|
cm.get.query.push(n + '_min');
|
|
2093
2026
|
cm.get.query.push(n + '_max');
|
|
2094
2027
|
cm.set.query.push(n + '_min');
|
|
@@ -2113,7 +2046,7 @@ Drive.prototype._addNumberRangeFields = function (cm, m, n) {
|
|
|
2113
2046
|
* @param {object} cg 配置对象
|
|
2114
2047
|
* @private
|
|
2115
2048
|
*/
|
|
2116
|
-
|
|
2049
|
+
Db.prototype._addKeywordConfig = function (cm, keyword, cg) {
|
|
2117
2050
|
if (keyword) {
|
|
2118
2051
|
cm.get.query.push('keyword');
|
|
2119
2052
|
cm.set.query.push('keyword');
|
|
@@ -2140,7 +2073,7 @@ Drive.prototype._addKeywordConfig = function (cm, keyword, cg) {
|
|
|
2140
2073
|
* @param {boolean} cover 是否覆盖文件
|
|
2141
2074
|
* @private
|
|
2142
2075
|
*/
|
|
2143
|
-
|
|
2076
|
+
Db.prototype._saveParamFiles = function (cm, client, manage, cover) {
|
|
2144
2077
|
if (client) {
|
|
2145
2078
|
this.saveFile(client + '/param.json', cm, cover);
|
|
2146
2079
|
}
|
|
@@ -2158,7 +2091,7 @@ Drive.prototype._saveParamFiles = function (cm, client, manage, cover) {
|
|
|
2158
2091
|
* @param {Array} arr 匹配的对象
|
|
2159
2092
|
* @returns {boolean} 是否设置
|
|
2160
2093
|
*/
|
|
2161
|
-
|
|
2094
|
+
Db.prototype.isSet = function (name, arr) {
|
|
2162
2095
|
if (!arr) {
|
|
2163
2096
|
return false;
|
|
2164
2097
|
}
|
|
@@ -2179,7 +2112,7 @@ Drive.prototype.isSet = function (name, arr) {
|
|
|
2179
2112
|
* @param {string} type 数据类型
|
|
2180
2113
|
* @returns {boolean} 是否可以
|
|
2181
2114
|
*/
|
|
2182
|
-
|
|
2115
|
+
Db.prototype.isCan = function (name, arr, type) {
|
|
2183
2116
|
if (type === 'longtext') {
|
|
2184
2117
|
return false;
|
|
2185
2118
|
}
|
|
@@ -2202,7 +2135,7 @@ Drive.prototype.isCan = function (name, arr, type) {
|
|
|
2202
2135
|
* @returns {string} API路径
|
|
2203
2136
|
* @private
|
|
2204
2137
|
*/
|
|
2205
|
-
|
|
2138
|
+
Db.prototype._deteApiPath = function (cg) {
|
|
2206
2139
|
var arr = cg.table.split('_');
|
|
2207
2140
|
var p = '/api/';
|
|
2208
2141
|
if (arr.length > 1) {
|
|
@@ -2220,7 +2153,7 @@ Drive.prototype._deteApiPath = function (cg) {
|
|
|
2220
2153
|
* @returns {object} API配置
|
|
2221
2154
|
* @private
|
|
2222
2155
|
*/
|
|
2223
|
-
|
|
2156
|
+
Db.prototype._createApiConfig = function (cg, path) {
|
|
2224
2157
|
return {
|
|
2225
2158
|
name: cg.table,
|
|
2226
2159
|
title: cg.title,
|
|
@@ -2241,7 +2174,7 @@ Drive.prototype._createApiConfig = function (cg, path) {
|
|
|
2241
2174
|
* @returns {boolean} 是否包含用户ID字段
|
|
2242
2175
|
* @private
|
|
2243
2176
|
*/
|
|
2244
|
-
|
|
2177
|
+
Db.prototype._hasUserIdField = function (fields) {
|
|
2245
2178
|
for (var i = 0, item; (item = fields[i++]);) {
|
|
2246
2179
|
var name = item.name;
|
|
2247
2180
|
if (
|
|
@@ -2264,7 +2197,7 @@ Drive.prototype._hasUserIdField = function (fields) {
|
|
|
2264
2197
|
* @param {boolean} cover 是否覆盖文件
|
|
2265
2198
|
* @private
|
|
2266
2199
|
*/
|
|
2267
|
-
|
|
2200
|
+
Db.prototype._saveClientApiConfig = function (
|
|
2268
2201
|
client,
|
|
2269
2202
|
base_config,
|
|
2270
2203
|
cg,
|
|
@@ -2293,7 +2226,7 @@ Drive.prototype._saveClientApiConfig = function (
|
|
|
2293
2226
|
* @param {boolean} cover 是否覆盖文件
|
|
2294
2227
|
* @private
|
|
2295
2228
|
*/
|
|
2296
|
-
|
|
2229
|
+
Db.prototype._saveManageApiConfig = function (manage, base_config, cover) {
|
|
2297
2230
|
var o = { ...base_config };
|
|
2298
2231
|
o.oauth = {
|
|
2299
2232
|
scope: true,
|
|
@@ -2312,7 +2245,7 @@ Drive.prototype._saveManageApiConfig = function (manage, base_config, cover) {
|
|
|
2312
2245
|
* @param {string} manage 管理端配置保存路径
|
|
2313
2246
|
* @param {boolean} cover 是否覆盖文件
|
|
2314
2247
|
*/
|
|
2315
|
-
|
|
2248
|
+
Db.prototype.newApi = async function (client, manage, cover) {
|
|
2316
2249
|
var cg = this.config;
|
|
2317
2250
|
var path = this._deteApiPath(cg);
|
|
2318
2251
|
var base_config = this._createApiConfig(cg, path);
|
|
@@ -2331,7 +2264,7 @@ Drive.prototype.newApi = async function (client, manage, cover) {
|
|
|
2331
2264
|
* @param {string} type 模型类型
|
|
2332
2265
|
* @returns {object} 返回获取到的模型
|
|
2333
2266
|
*/
|
|
2334
|
-
|
|
2267
|
+
Db.prototype.getModel = function (type) {
|
|
2335
2268
|
let model = { ...this.config };
|
|
2336
2269
|
let dir = this.getDir();
|
|
2337
2270
|
let l = path.sep;
|
|
@@ -2344,4 +2277,4 @@ Drive.prototype.getModel = function (type) {
|
|
|
2344
2277
|
return model;
|
|
2345
2278
|
};
|
|
2346
2279
|
|
|
2347
|
-
module.exports =
|
|
2280
|
+
module.exports = Db;
|
package/lib/manager.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
const Manager = require("
|
|
2
|
-
const
|
|
1
|
+
const Manager = require("mm_mod").Manager;
|
|
2
|
+
const Db = require("./db");
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* 数据库帮助类
|
|
6
6
|
* @augments {Manager}
|
|
7
7
|
* @class
|
|
8
8
|
*/
|
|
9
|
-
class
|
|
9
|
+
class DbManager extends Manager {
|
|
10
10
|
/**
|
|
11
11
|
* 配置参数
|
|
12
12
|
* @type {object}
|
|
@@ -45,9 +45,7 @@ class Db extends Manager {
|
|
|
45
45
|
* @param {object} parent 父级模块
|
|
46
46
|
*/
|
|
47
47
|
constructor(config, parent) {
|
|
48
|
-
super({ ...
|
|
49
|
-
// 插件注册表(方案B:手动注册,供驱动 plugin() 查找)
|
|
50
|
-
this.plugins = {};
|
|
48
|
+
super({ ...DbManager.config, ...config }, parent);
|
|
51
49
|
}
|
|
52
50
|
}
|
|
53
51
|
|
|
@@ -55,15 +53,15 @@ class Db extends Manager {
|
|
|
55
53
|
* 数据库驱动类
|
|
56
54
|
* @type {Drive}
|
|
57
55
|
*/
|
|
58
|
-
|
|
56
|
+
DbManager.prototype.Drive = Db;
|
|
59
57
|
|
|
60
58
|
/**
|
|
61
59
|
* 注册一个数据库驱动(方案B:手动注册,不扫描目录)
|
|
62
60
|
* @param {object} config 驱动配置(必须包含 name)
|
|
63
61
|
* @returns {object} 驱动实例
|
|
64
62
|
*/
|
|
65
|
-
|
|
66
|
-
var drive = new Drive({ host: this.config.host, ...config }, this);
|
|
63
|
+
DbManager.prototype.add = function (config) {
|
|
64
|
+
var drive = new this.Drive({ host: this.config.host, ...config }, this);
|
|
67
65
|
this.set(drive.config.name, drive);
|
|
68
66
|
return drive;
|
|
69
67
|
};
|
|
@@ -73,7 +71,7 @@ Db.prototype.add = function (config) {
|
|
|
73
71
|
* @param {string} name 驱动名称
|
|
74
72
|
* @returns {Promise<boolean>} 是否删除成功
|
|
75
73
|
*/
|
|
76
|
-
|
|
74
|
+
DbManager.prototype.del = async function (name) {
|
|
77
75
|
return await Manager.prototype.del.call(this, name);
|
|
78
76
|
};
|
|
79
77
|
|
|
@@ -82,7 +80,7 @@ Db.prototype.del = async function (name) {
|
|
|
82
80
|
* @param {string} name 驱动名称
|
|
83
81
|
* @returns {Promise<boolean>} 是否执行成功
|
|
84
82
|
*/
|
|
85
|
-
|
|
83
|
+
DbManager.prototype.reload = async function (name) {
|
|
86
84
|
var drive = this.get(name);
|
|
87
85
|
if (!drive) {
|
|
88
86
|
this.log("warn", "驱动不存在,无法重载", name);
|
|
@@ -94,23 +92,12 @@ Db.prototype.reload = async function (name) {
|
|
|
94
92
|
return true;
|
|
95
93
|
};
|
|
96
94
|
|
|
97
|
-
/**
|
|
98
|
-
* 注册插件集合(供驱动 plugin() 查找,替代原全局插件池)
|
|
99
|
-
* @param {string} name 插件作用域名称(app 名)
|
|
100
|
-
* @param {object} obj 插件集合对象(需含 get 方法)
|
|
101
|
-
* @returns {object} 当前管理器实例
|
|
102
|
-
*/
|
|
103
|
-
Db.prototype.usePlugin = function (name, obj) {
|
|
104
|
-
this.plugins[name] = obj;
|
|
105
|
-
return this;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
95
|
/**
|
|
109
96
|
* 获取所有驱动配置列表(方案B:来自注册表,替代旧基类 getInfos)
|
|
110
97
|
* @returns {Array} 驱动配置数组
|
|
111
98
|
* @private
|
|
112
99
|
*/
|
|
113
|
-
|
|
100
|
+
DbManager.prototype._getInfos = function () {
|
|
114
101
|
return this.values().map((d) => d.config);
|
|
115
102
|
};
|
|
116
103
|
|
|
@@ -119,7 +106,7 @@ Db.prototype._getInfos = function () {
|
|
|
119
106
|
* @param {string} table 表
|
|
120
107
|
* @returns {object} 返回驱动项
|
|
121
108
|
*/
|
|
122
|
-
|
|
109
|
+
DbManager.prototype.getObj = function (table) {
|
|
123
110
|
var obj;
|
|
124
111
|
var mods = this.values();
|
|
125
112
|
for (var i = 0; i < mods.length; i++) {
|
|
@@ -140,7 +127,7 @@ Db.prototype.getObj = function (table) {
|
|
|
140
127
|
* @param {boolean} cover 是否覆盖文件
|
|
141
128
|
* @returns {string} 更新成功返回null, 失败返回错误提示
|
|
142
129
|
*/
|
|
143
|
-
|
|
130
|
+
DbManager.prototype.updateFile = async function (db, name, table, cover = true) {
|
|
144
131
|
if (name) {
|
|
145
132
|
var o = this.get(name);
|
|
146
133
|
if (o) {
|
|
@@ -179,12 +166,12 @@ Db.prototype.updateFile = async function (db, name, table, cover = true) {
|
|
|
179
166
|
* @param {boolean} all 是否更新全部表,true为是,false为否
|
|
180
167
|
* @returns {string} 更新成功返回null, 失败返回错误提示
|
|
181
168
|
*/
|
|
182
|
-
|
|
169
|
+
DbManager.prototype.updateDbManager = async function (db, name, table, all) {
|
|
183
170
|
var ret;
|
|
184
171
|
if (name) {
|
|
185
172
|
var o = this.get(name);
|
|
186
173
|
if (o) {
|
|
187
|
-
ret = await o.
|
|
174
|
+
ret = await o.updateDbManager(db);
|
|
188
175
|
} else {
|
|
189
176
|
ret = "该配置不存在";
|
|
190
177
|
}
|
|
@@ -194,13 +181,13 @@ Db.prototype.updateDb = async function (db, name, table, all) {
|
|
|
194
181
|
for (var i = 0; i < mods.length; i++) {
|
|
195
182
|
var m = mods[i];
|
|
196
183
|
if (m.config.table === table) {
|
|
197
|
-
ret = await m.
|
|
184
|
+
ret = await m.updateDbManager(db);
|
|
198
185
|
break;
|
|
199
186
|
}
|
|
200
187
|
}
|
|
201
188
|
} else if (all) {
|
|
202
189
|
for (var j = 0; j < mods.length; j++) {
|
|
203
|
-
await mods[j].
|
|
190
|
+
await mods[j].updateDbManager(db);
|
|
204
191
|
}
|
|
205
192
|
}
|
|
206
193
|
}
|
|
@@ -210,4 +197,4 @@ Db.prototype.updateDb = async function (db, name, table, all) {
|
|
|
210
197
|
/**
|
|
211
198
|
* @module 导出数据库管理器类
|
|
212
199
|
*/
|
|
213
|
-
module.exports =
|
|
200
|
+
module.exports = DbManager;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_dbs",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "数据库框架,通过设计数据库生成sql模板,或通过db.json配置文件生成mysql数据表(基于 mm_corejs,内置管理器池,无全局/原型扩展依赖)",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -16,8 +16,13 @@
|
|
|
16
16
|
"group": "data",
|
|
17
17
|
"author": "邱文武",
|
|
18
18
|
"license": "ISC",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_dbs.git"
|
|
22
|
+
},
|
|
19
23
|
"dependencies": {
|
|
20
24
|
"mm_corejs": "^1.1.4",
|
|
25
|
+
"mm_mod": "^1.0.1",
|
|
21
26
|
"mm_helper": "^1.0.0"
|
|
22
27
|
},
|
|
23
28
|
"scripts": {
|