mm_os 4.1.4 → 4.1.6

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/com/db/drive.js CHANGED
@@ -18,20 +18,20 @@ var keyword_default_tip = '昵称、手机号、钱包地址';
18
18
  class Drive extends Item {
19
19
  static config = {
20
20
  // 库表名称(唯一标识)
21
- 'name': 'db',
21
+ name: '',
22
22
  // 标题(中文名)
23
- 'title': '',
23
+ title: '',
24
24
  // 表名
25
- 'table': '',
25
+ table: '',
26
26
  // 主键,用于实体模型
27
- 'key': '',
27
+ key: '',
28
28
  // 字段
29
- 'fields': [
29
+ fields: [
30
30
  /* */
31
31
  ],
32
- 'index': [],
32
+ index: [],
33
33
  // 主程序文件 - 默认为空
34
- 'main': ''
34
+ main: ''
35
35
  };
36
36
 
37
37
  /**
@@ -48,7 +48,7 @@ class Drive extends Item {
48
48
  /**
49
49
  * 重置配置参数
50
50
  */
51
- Drive.prototype._reset = function () {
51
+ Drive.prototype._preset = function () {
52
52
  this.query_string = ['name', 'title', 'keywords', 'tag', 'description', 'content'];
53
53
 
54
54
  // 是否设置数值类型为可查询
@@ -183,6 +183,8 @@ Drive.prototype._handleDefaultValue = function (tp, not_null, default_val) {
183
183
  if (not_null && !default_val) {
184
184
  if (tp === 'varchar' || tp === 'text') {
185
185
  return '';
186
+ } else if (tp === 'datetime' || tp === 'timestamp') {
187
+ return 'CURRENT_TIMESTAMP';
186
188
  } else {
187
189
  return '0';
188
190
  }
@@ -315,10 +317,10 @@ Drive.prototype.model = function (fields) {
315
317
  */
316
318
  Drive.prototype.newIndexModel = function (o) {
317
319
  return {
318
- 'name': o.Key_name,
319
- 'type': o.Non_unique ? 'unique' : 'index',
320
- 'fields': o.Column_name.replace(/`/g, '').split(','),
321
- 'comment': o.Manager_comment
320
+ name: o.Key_name,
321
+ type: o.Non_unique ? 'unique' : 'index',
322
+ fields: o.Column_name.replace(/`/g, '').split(','),
323
+ comment: o.Manager_comment
322
324
  };
323
325
  };
324
326
 
@@ -385,7 +387,7 @@ Drive.prototype.updateFile = async function (db, cover) {
385
387
  list.push(field);
386
388
  }
387
389
  cg.fields = list;
388
- cg.index = await this._updateFile(db);
390
+ cg.index = await this._updateFile(db);
389
391
  if (!cg.name) {
390
392
  cg.name = cg.table;
391
393
  }
@@ -484,15 +486,25 @@ Drive.prototype._generateFieldSql = function (o) {
484
486
  * @private
485
487
  */
486
488
  Drive.prototype._getTypeSql = function (o) {
487
- var type;
488
- if (o.decimal) {
489
- type = o.type + '(' + o.max_length + ',' + o.decimal + ')';
490
- } else {
491
- type = o.type + '(' + o.max_length + ')';
489
+ var type = o.type;
490
+ var max_len = o.max_length || 0;
491
+
492
+ if (max_len > 0) {
493
+ // decimal仅对浮点类型有效(int/smallint/bigint等使用decimal=0时不追加)
494
+ var is_float_type = o.type.indexOf('float') !== -1 ||
495
+ o.type.indexOf('double') !== -1 ||
496
+ o.type.indexOf('decimal') !== -1;
497
+ if (is_float_type && o.decimal !== undefined && o.decimal !== null) {
498
+ type += '(' + max_len + ',' + o.decimal + ')';
499
+ } else {
500
+ type += '(' + max_len + ')';
501
+ }
492
502
  }
493
503
 
494
- if (!o.symbol && (o.type !== 'varchar' && o.type !== 'longtext' && o.type !== 'text' &&
495
- o.type !== 'date' && o.type !== 'time' && o.type !== 'datetime' && o.type !== 'timestamp')) {
504
+ // 类型未包含unsigned且为非字符/日期类型时追加UNSIGNED
505
+ if (!o.symbol && type.indexOf('unsigned') === -1 &&
506
+ (o.type !== 'varchar' && o.type !== 'longtext' && o.type !== 'text' &&
507
+ o.type !== 'date' && o.type !== 'time' && o.type !== 'datetime' && o.type !== 'timestamp')) {
496
508
  type += ' UNSIGNED';
497
509
  }
498
510
 
@@ -556,7 +568,7 @@ Drive.prototype._isNumericType = function (type) {
556
568
  * @private
557
569
  */
558
570
  Drive.prototype._isAutoInc = function (o) {
559
- return o.auto && !this._isDateTimeType(o.type);
571
+ return !!o.auto && !this._isDateTimeType(o.type);
560
572
  };
561
573
 
562
574
  /**
@@ -570,11 +582,15 @@ Drive.prototype._getValueSql = function (o) {
570
582
  return 'DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
571
583
  }
572
584
 
585
+ if (this._isAutoInc(o)) {
586
+ return '';
587
+ }
588
+
573
589
  if (o.default === null) {
574
590
  return 'DEFAULT NULL';
575
591
  }
576
592
 
577
- if (o.default) {
593
+ if (o.default !== undefined) {
578
594
  return this._getDefaultValueSql(o);
579
595
  }
580
596
 
@@ -588,7 +604,7 @@ Drive.prototype._getValueSql = function (o) {
588
604
  * @private
589
605
  */
590
606
  Drive.prototype._isAutoTime = function (o) {
591
- return o.auto && this._isDateTimeType(o.type);
607
+ return !!o.auto && this._isDateTimeType(o.type);
592
608
  };
593
609
 
594
610
  /**
@@ -614,7 +630,7 @@ Drive.prototype._getDefaultValueSql = function (o) {
614
630
  * @private
615
631
  */
616
632
  Drive.prototype._isCurrentDefault = function (o) {
617
- return this._isDateTimeType(o.type) && o.default.indexOf('CURR') !== -1;
633
+ return this._isDateTimeType(o.type) && o.default && o.default.indexOf('CURR') !== -1;
618
634
  };
619
635
 
620
636
  /**
@@ -649,23 +665,23 @@ Drive.prototype._getDefaultEmptySql = function (o) {
649
665
  * @private
650
666
  */
651
667
  Drive.prototype._getNoteSql = function (o) {
652
- var note = o.title + ':';
668
+ var note = (o.title || '') + ':';
653
669
 
654
670
  if (o.type === 'varchar' || o.type === 'text' || o.type === 'longtext') {
655
671
  if (o.max_length) {
656
- note += '[' + o.min_length + ',' + o.max_length + ']';
672
+ note += '[' + (o.min_length || '0') + ',' + o.max_length + ']';
657
673
  } else if (o.min_length) {
658
674
  note += '[' + o.min_length + ']';
659
675
  }
660
676
  } else {
661
677
  if (o.max) {
662
- note += '[' + o.min + ',' + o.max + ']';
678
+ note += '[' + (o.min || '0') + ',' + o.max + ']';
663
679
  } else if (o.min) {
664
680
  note += '[' + o.min + ']';
665
681
  }
666
682
  }
667
683
 
668
- note += o.description;
684
+ note += (o.description || '');
669
685
  if (o.map) {
670
686
  note += '(' + o.map + ')';
671
687
  }
@@ -697,8 +713,8 @@ Drive.prototype._buildFieldSql = function (name, type, notnull, value, note) {
697
713
  * @param {Array} fields 数据库字段列表
698
714
  */
699
715
  Drive.prototype._addOrUpdateField = async function (db, table, o, fields) {
700
- var arr = fields.get({
701
- name: o.name
716
+ var arr = fields.filter(function (f) {
717
+ return f.name === o.name;
702
718
  });
703
719
  var sql = this._generateFieldSql(o);
704
720
 
@@ -720,7 +736,7 @@ Drive.prototype.updateDb = async function (db) {
720
736
  var cg = this.config;
721
737
  var table = cg.table + '';
722
738
  var list = cg.fields;
723
-
739
+ db.table = table;
724
740
  var fields = await this._initTable(db, cg, list);
725
741
 
726
742
  if (fields.length > 0) {
@@ -816,7 +832,11 @@ Drive.prototype.updateApp = async function (cover) {
816
832
  f = dirs.config_file;
817
833
  dir_api = dirs.dir_api;
818
834
  } else {
819
- f = this.config_file + '.db.json';
835
+ if (this.config_file.endsWith('.db.json')) {
836
+ f = this.config_file;
837
+ } else {
838
+ f = this.config_file + '.db.json';
839
+ }
820
840
  }
821
841
 
822
842
  if (f) {
@@ -824,7 +844,9 @@ Drive.prototype.updateApp = async function (cover) {
824
844
  if (!dir_api) {
825
845
  dir_api = f.dirname().dirname();
826
846
  }
847
+ // 处理db配置文件,生成xxx.db.json文件
827
848
  this._handleConfigFile(f, cover);
849
+ // 更新API及相关配置文件
828
850
  this.updateApi(dir_api, cover);
829
851
  }
830
852
  };
@@ -886,7 +908,7 @@ Drive.prototype.updateApi = async function (dir, cover) {
886
908
  var client = this._getApiDir(dir, app, 'client', name);
887
909
  var manage = this._getApiDir(dir, app, 'manage', name);
888
910
 
889
- this.newSql(client, manage, cover);
911
+ await this.newSql(client, manage, cover);
890
912
  await this.newParam(client, manage, cover);
891
913
  this.newApi(client, manage, cover);
892
914
  };
@@ -1048,7 +1070,6 @@ Drive.prototype._procFieldSel = function (field_name, field_type) {
1048
1070
  if (this.isCan(field_name, this.get_obj_not)) {
1049
1071
  res.field_obj += ',`' + field_name + '`';
1050
1072
  }
1051
-
1052
1073
  return res;
1053
1074
  };
1054
1075
 
@@ -1159,7 +1180,7 @@ Drive.prototype._procFieldType = function (
1159
1180
 
1160
1181
  /**
1161
1182
  * 处理单个字段
1162
- * @param {object} field 字段对象
1183
+ * @param {object} o 字段对象
1163
1184
  * @param {object} query 查询对象
1164
1185
  * @param {object} update_obj 更新对象
1165
1186
  * @param {string} uid 用户ID
@@ -1170,14 +1191,12 @@ Drive.prototype._procFieldType = function (
1170
1191
  * @private
1171
1192
  */
1172
1193
  Drive.prototype._procSingleField = async function (
1173
- field, query, update_obj, uid, query_default_user, orderby, keyword
1194
+ o, query, update_obj, uid, query_default_user, orderby, keyword
1174
1195
  ) {
1175
- var o = field;
1176
- var p = o.type;
1177
- var n = o.name;
1196
+ var { type, name } = o;
1178
1197
  var result = {
1179
- field: '',
1180
- field_obj: '',
1198
+ field: '*',
1199
+ field_obj: '*',
1181
1200
  keyword: keyword,
1182
1201
  orderby: orderby,
1183
1202
  update_obj: update_obj,
@@ -1185,12 +1204,12 @@ Drive.prototype._procSingleField = async function (
1185
1204
  format: []
1186
1205
  };
1187
1206
 
1188
- var field_sel = this._procFieldSel(n, p);
1207
+ var field_sel = this._procFieldSel(name, type);
1189
1208
  result.field = field_sel.field;
1190
1209
  result.field_obj = field_sel.field_obj;
1191
1210
 
1192
1211
  var type_info = this._procFieldType(
1193
- p, n, query, update_obj, uid, query_default_user, orderby, keyword
1212
+ type, name, query, update_obj, uid, query_default_user, orderby, keyword
1194
1213
  );
1195
1214
  result.keyword = type_info.keyword;
1196
1215
  result.orderby = type_info.orderby;
@@ -1249,8 +1268,8 @@ Drive.prototype._procFields = async function (fields, table) {
1249
1268
  return {
1250
1269
  query,
1251
1270
  update: update_obj,
1252
- field,
1253
- field_obj,
1271
+ field: field.trim(','),
1272
+ field_obj: field_obj.trim(','),
1254
1273
  query_default,
1255
1274
  format,
1256
1275
  orderby,
@@ -1287,15 +1306,15 @@ Drive.prototype._procKeyword = function (keyword, table) {
1287
1306
  * @returns {object} 基础模型
1288
1307
  * @private
1289
1308
  */
1290
- Drive.prototype._createBaseModel = function (cg, processed, keyword_query) {
1309
+ Drive.prototype._createSqlConfig = function (cg, processed, keyword_query) {
1291
1310
  return {
1292
1311
  name: cg.table,
1293
1312
  title: cg.title,
1294
1313
  table: cg.table,
1295
1314
  key: cg.key,
1296
1315
  orderby_default: '`' + cg.key + '` desc',
1297
- field_obj: processed.field_obj.replace(',', ''),
1298
- field_default: processed.field.replace(',', ''),
1316
+ field_obj: processed.field_obj,
1317
+ field_default: processed.field,
1299
1318
  method: 'get get_obj avg sum count',
1300
1319
  query: { ...processed.query, ...keyword_query },
1301
1320
  query_default: processed.query_default,
@@ -1314,7 +1333,7 @@ Drive.prototype._createBaseModel = function (cg, processed, keyword_query) {
1314
1333
  * @param {boolean} cover 是否覆盖文件
1315
1334
  * @private
1316
1335
  */
1317
- Drive.prototype._saveClientConfig = function (client, base_model, orderby, uid, table, cover) {
1336
+ Drive.prototype._saveClientConfig = function (client, base_model, cover, orderby, uid, table) {
1318
1337
  var oj = { ...base_model };
1319
1338
  if (orderby) {
1320
1339
  oj.orderby_default = orderby;
@@ -1364,10 +1383,10 @@ Drive.prototype.newSql = async function (client, manage, cover) {
1364
1383
 
1365
1384
  var processed = await this._procFields(cg.fields, cg.table);
1366
1385
  var keyword_query = this._procKeyword(processed.keyword, cg.table);
1367
- var base_model = this._createBaseModel(cg, processed, keyword_query);
1386
+ var base_model = this._createSqlConfig(cg, processed, keyword_query);
1368
1387
 
1369
1388
  if (client) {
1370
- this._saveClientConfig(client, base_model, processed.orderby, uid, cg.table, cover);
1389
+ this._saveClientConfig(client, base_model, cover, processed.orderby, uid, cg.table);
1371
1390
  }
1372
1391
 
1373
1392
  if (manage) {
@@ -1413,7 +1432,7 @@ Drive.prototype.newParam = async function (client, manage, cover) {
1413
1432
  var o = lt[i];
1414
1433
  var p = o.type;
1415
1434
  var n = o.name;
1416
- var m = this._createBaseModel(o, p, n);
1435
+ var m = this._createParamModel(o, p, n);
1417
1436
 
1418
1437
  if (this._isStringType(p)) {
1419
1438
  keyword = this._processStringField(cm, m, o, n, keyword);
@@ -1465,14 +1484,14 @@ Drive.prototype._initParamConfig = function (cg) {
1465
1484
  };
1466
1485
 
1467
1486
  /**
1468
- * 创建基础模型
1487
+ * 创建参数模型
1469
1488
  * @param {object} field 字段对象
1470
1489
  * @param {string} type 字段类型
1471
1490
  * @param {string} name 字段名
1472
- * @returns {object} 基础模型对象
1491
+ * @returns {object} 参数模型对象
1473
1492
  * @private
1474
1493
  */
1475
- Drive.prototype._createBaseModel = function (field, type, name) {
1494
+ Drive.prototype._createParamModel = function (field, type, name) {
1476
1495
  return {
1477
1496
  name: name,
1478
1497
  title: field.title,
@@ -1846,16 +1865,16 @@ Drive.prototype.isCan = function (name, arr, type) {
1846
1865
  return false;
1847
1866
  }
1848
1867
  if (!arr) {
1849
- return false;
1868
+ return true;
1850
1869
  }
1851
- var bl = false;
1870
+ var bl = true;
1852
1871
  for (var i = 0; i < arr.length; i++) {
1853
1872
  if (name.indexOf(arr[i]) !== -1) {
1854
- bl = true;
1873
+ bl = false;
1855
1874
  break;
1856
1875
  }
1857
1876
  }
1858
- return !bl;
1877
+ return bl;
1859
1878
  };
1860
1879
 
1861
1880
 
@@ -1880,10 +1899,10 @@ Drive.prototype._deteApiPath = function (cg) {
1880
1899
  * 创建基础API配置
1881
1900
  * @param {object} cg 配置对象
1882
1901
  * @param {string} path API路径
1883
- * @returns {object} 基础API配置
1902
+ * @returns {object} API配置
1884
1903
  * @private
1885
1904
  */
1886
- Drive.prototype._createBaseApiConfig = function (cg, path) {
1905
+ Drive.prototype._createApiConfig = function (cg, path) {
1887
1906
  return {
1888
1907
  'name': cg.table,
1889
1908
  'title': cg.title,
@@ -1968,7 +1987,7 @@ Drive.prototype._saveManageApiConfig = function (manage, base_config, cover) {
1968
1987
  Drive.prototype.newApi = async function (client, manage, cover) {
1969
1988
  var cg = this.config;
1970
1989
  var path = this._deteApiPath(cg);
1971
- var base_config = this._createBaseApiConfig(cg, path);
1990
+ var base_config = this._createApiConfig(cg, path);
1972
1991
 
1973
1992
  if (client) {
1974
1993
  this._saveClientApiConfig(client, base_config, cg, cover);
package/com/db/index.js CHANGED
@@ -138,7 +138,7 @@ Db.prototype.getObj = function (table) {
138
138
  */
139
139
  Db.prototype.updateFile = async function (db, name, table, cover = true) {
140
140
  if (name) {
141
- var o = this.get(name);
141
+ var o = this.getMod(name);
142
142
  if (o) {
143
143
  await o.call('updateFile', db, cover);
144
144
  } else {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mm_os",
3
3
  "description": "MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。",
4
- "version": "4.1.4",
4
+ "version": "4.1.6",
5
5
  "main": "index.js",
6
6
  "keywords": [
7
7
  "AI",
@@ -1,22 +0,0 @@
1
- {
2
- // 名称
3
- "name": "msg",
4
- // 标题
5
- "title": "消息处理器",
6
- // 描述
7
- "description": "负责处理消息",
8
- // 主文件
9
- "main": "index.js",
10
- // 作用域,决定加载到什么区域
11
- "scope": "server",
12
- // 状态 0:禁用 1:启用
13
- "state": 1,
14
- // 排序,越小越靠前
15
- "sort": 100,
16
- // 依赖的领域服务
17
- "services": [],
18
- // 依赖的仓储
19
- "stores": [],
20
- // 依赖的富血模型
21
- "models": ["msg"]
22
- }
@@ -1,23 +0,0 @@
1
- /**
2
- * 处理器(应用层)
3
- */
4
- module.exports = {
5
- /**
6
- * 初始化
7
- * @param {object} app 应用实例
8
- * @param {object} eventer 事件总线
9
- */
10
- async _init(app, eventer) {
11
- // this.log('debug', `初始化!`, app.config.name);
12
- // 可将监听写在这里
13
- // eventer.on('');
14
- },
15
- /**
16
- * 主要逻辑
17
- * @param {...any} args 逻辑参数
18
- */
19
- async main(...args) {
20
- this.log('debug', `主要逻辑`);
21
- // 主要代码写在这
22
- }
23
- };
@@ -1,22 +0,0 @@
1
- {
2
- // 名称
3
- "name": "player",
4
- // 标题
5
- "title": "玩家处理器",
6
- // 描述
7
- "description": "负责处理玩家相关的操作",
8
- // 主文件
9
- "main": "index.js",
10
- // 作用域,决定加载到什么区域
11
- "scope": "server",
12
- // 状态 0:禁用 1:启用
13
- "state": 1,
14
- // 排序,越小越靠前
15
- "sort": 100,
16
- // 依赖的领域服务
17
- "services": [],
18
- // 依赖的仓储
19
- "stores": [],
20
- // 依赖的富血模型
21
- "models": ["player"]
22
- }