doomiwork 4.2.3 → 4.2.5

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/dist/index.js CHANGED
@@ -245,24 +245,25 @@ function requireAppsetting () {
245
245
  if (hasRequiredAppsetting) return appsetting.exports;
246
246
  hasRequiredAppsetting = 1;
247
247
  (function (module, exports) {
248
- const path = require$$1__default["default"];
249
- const fs = require$$0__default["default"];
250
- class appsetting{
248
+ const path = require$$1__default["default"];
249
+ const fs = require$$0__default["default"];
250
+ class appsetting {
251
251
  static #instance;
252
252
 
253
- constructor(){
253
+ constructor(baseFolder = process.cwd()) {
254
254
  if (appsetting.#instance) throw new Error('Use getCurrentApp() instead of new appsetting()');
255
255
  const config = process.env.CONFIGFILE || 'configuration.json';
256
- const configfile = path.join(process.cwd(), config);
256
+ const configfile = path.join(baseFolder, config);
257
257
  if (!fs.existsSync(configfile)) throw new Error('missing app configuration file');
258
258
  this.settingjson = require(configfile);
259
+ this.folder = baseFolder;
259
260
  }
260
261
  /**
261
262
  * 单例模式
262
263
  * @param {*} config
263
264
  */
264
- static getCurrentApp(){
265
- if (!appsetting.#instance) appsetting.#instance = new appsetting();
265
+ static getCurrentApp(baseFolder = process.cwd()) {
266
+ if (!appsetting.#instance) appsetting.#instance = new appsetting(baseFolder);
266
267
  return appsetting.#instance;
267
268
  }
268
269
  /**
@@ -270,18 +271,18 @@ function requireAppsetting () {
270
271
  * @param {*} settingKey
271
272
  * @param {*} defaultValue
272
273
  */
273
- getSetting (settingKey,defaultValue) {
274
+ getSetting(settingKey, defaultValue) {
274
275
  if (!this.settingjson || !this.settingjson.appsetting) return defaultValue;
275
- const item= this.settingjson.appsetting[settingKey];
276
- return item??defaultValue;
276
+ const item = this.settingjson.appsetting[settingKey];
277
+ return item ?? defaultValue;
277
278
  }
278
- getSection(sectionName){
279
+ getSection(sectionName) {
279
280
  return this.settingjson[sectionName];
280
281
  }
281
282
  /*
282
283
  * 获取配置文件中的数据库连接串
283
284
  */
284
- getConnection (connName) {
285
+ getConnection(connName) {
285
286
  return this.settingjson.connections[connName];
286
287
  };
287
288
  }
@@ -304,47 +305,44 @@ function requireDoomiwork () {
304
305
  */
305
306
  const { loadController } = requireRouterconfig();
306
307
  const DoomiApiResult = requireDoomiapiresult();
307
- const config = requireAppsetting().getCurrentApp();
308
+ const config = requireAppsetting();
308
309
  const fs = require$$0__default["default"];
309
310
  const path = require$$1__default["default"];
310
311
  class Doomiwork {
312
+ /**
313
+ * 启动框架
314
+ * @param {*} application app对象
315
+ * @param {*} option 启动的参数
316
+ * @param {*} injector
317
+ */
311
318
  static startWork(application,option={}) {
312
- application.config = config;
319
+ /// 默认的配置文件所在目录就是程序运行的根目录
320
+ const ApplicationConfiguration = config.getCurrentApp(option.configfolder ?? process.cwd());
321
+ application.config = ApplicationConfiguration;
313
322
  ///启动redis进行缓存
314
323
  ////为每个controller注入同一个redis实例
315
- if (config.getSetting("redis", false) === true && option.redis){
316
- application.redis = option.redis;// redisHelper.getInstance();
317
- }
318
- ////记录用户行为的日志记录器
319
- // const logHandler = config.getSetting("logaction", null)
320
- // if (logHandler){
321
- // let actionLogger = path.join(startupRoot, logHandler);
322
- // if (actionLogger && fs.existsSync(actionLogger)) {
323
- // const logInstance = require(actionLogger);
324
- // application.actionLogger = new logInstance();
325
- // }
326
- // }
324
+ if (ApplicationConfiguration.getSetting("redis", false) === true && option.redis) application.redis = option.redis;// redisHelper.getInstance();
327
325
  ///用户操作日志记录
328
- if(config.getSetting("logaction", false)===true && option.logaction) application.actionLogger = option.logaction;
326
+ if (ApplicationConfiguration.getSetting("logaction", false)===true && option.logaction) application.actionLogger = option.logaction;
329
327
  ///日志记录器
330
- if (config.getSetting("logger", false) === true && option.logger) application.logger = option.logger;// redisHelper.getInstance();
328
+ if (ApplicationConfiguration.getSetting("logger", false) === true && option.logger) application.logger = option.logger;// redisHelper.getInstance();
331
329
  //application.logger = logHelper.getInstance().getLogger("framework");
332
- /**全局所有请求拦截器 */
333
- application.use('/',(req,res,next)=>{
334
- req.user = req.headers;
335
- return next();
336
- });
330
+ /**全局所有请求拦截器 默认将整个请求头都注入到req.user 中*/
331
+ if (option.interceptorForUser !==false){
332
+ application.use('/', (req, res, next) => {
333
+ req.user = req.headers;
334
+ return next();
335
+ });
336
+ }
337
337
  /**
338
338
  * 注册监控路由
339
339
  */
340
340
  if (!(option.monitor===false)) this.registerMonitorRouter(application);
341
341
  /** 加载所有的控制器 */
342
342
  if (!option.routes){
343
- const roterfile = path.join(process.cwd(), 'routerconfig.json');
343
+ const roterfile = path.join(option.configfolder ?? process.cwd(), 'routerconfig.json');
344
344
  ///将配置中的所有需要加载的controller文件都加载出来
345
- if (fs.existsSync(roterfile)){
346
- option.routes = require(roterfile);
347
- }
345
+ if (fs.existsSync(roterfile))option.routes = require(roterfile);
348
346
  }
349
347
  loadController(application, option.routes);
350
348
  Doomiwork.app = application;
@@ -932,13 +930,15 @@ function requireDataconfig () {
932
930
  const path = require$$1__default["default"];
933
931
  class dataConfig
934
932
  {
935
- constructor(){
936
- let configfile = path.join(process.cwd(),'dataconfig.json');
937
- let comcfgfile = path.join(process.cwd(),'simplecommon.json');
933
+ static #instance;
934
+ constructor(folder = process.cwd()){
935
+ if (dataConfig.#instance) throw new Error('Use getCurrent() instead of new dataConfig()');
936
+ let configfile = path.join(folder,'dataconfig.json');
937
+ let comcfgfile = path.join(folder,'simplecommon.json');
938
938
  ////检查dataconfig.json文件是否存在,优先加载dataconfig.json
939
939
  if (!fs.existsSync(configfile)){
940
940
  ///如果json文件不存在,则加载后缀为js的文件
941
- configfile = path.join(process.cwd(),'dataconfig.js');
941
+ configfile = path.join(folder,'dataconfig.js');
942
942
  if (!fs.existsSync(configfile))
943
943
  this.cache = {}; //throw new Error('missing app configuration file')
944
944
  else
@@ -949,13 +949,13 @@ function requireDataconfig () {
949
949
  if (fs.existsSync(comcfgfile)){
950
950
  this.commoncache = require(comcfgfile);
951
951
  }else {
952
- comcfgfile = path.join(process.cwd(),'simplecommon.js');
952
+ comcfgfile = path.join(folder,'simplecommon.js');
953
953
  if (fs.existsSync(comcfgfile))
954
954
  this.commoncache = require(comcfgfile);
955
955
  else
956
956
  this.commoncache = {};
957
957
  }
958
-
958
+ this.folder = folder;
959
959
  }
960
960
  /*
961
961
  *根据名称获取到配置中的对应节
@@ -968,9 +968,9 @@ function requireDataconfig () {
968
968
  * 单例模式
969
969
  * @param {*} config
970
970
  */
971
- static getCurrent(){
972
- if (!dataConfig.data) dataConfig.data = new dataConfig();
973
- return dataConfig.data;
971
+ static getCurrent(folder = process.cwd()){
972
+ if (!dataConfig.#instance) dataConfig.#instance = new dataConfig(folder);
973
+ return dataConfig.#instance;
974
974
  }
975
975
 
976
976
  getDataConfig(itemName) {
@@ -992,7 +992,7 @@ function requireRequestparser () {
992
992
  if (hasRequiredRequestparser) return requestparser;
993
993
  hasRequiredRequestparser = 1;
994
994
  const { parseKeyValue, validatorParamsType } = requireKeywordparse();
995
- const dataconfig = requireDataconfig().getCurrent();
995
+ const dataconfig = requireDataconfig();
996
996
  const mysql = require$$0__default$3["default"];
997
997
  const ORDER_STRING = ['asc', 'desc'];
998
998
  const FORBID_SQL_KEYWORD = /;|(--)|(\bWHERE\b)|(\bCASE\b)|(\bWHEN\b)|(\bSLEEP\b)|(\bSHOW\b)|(\bCOUNT\(\b)|(\bCREATE\b)|(\bCALL\b)|(\bBY\b)|(\bORDER\b)|(\bJOIN\b)|(\bUNION\b)|(\bFROM\b)|(\bSELECT\b)|(\bDROP\b)|(\bTRUNCATE\b)|(\bDELETE\b)|(\bUPDATE\b)|(\bINSERT\b)|(\bEXEC\b)|(\bEXECUTE\b)/gi;
@@ -1173,9 +1173,9 @@ function requireRequestparser () {
1173
1173
  * 列表请求上下文中获取需要的信息
1174
1174
  * 如Page ,PageSize , Sort 等等
1175
1175
  */
1176
- const getListInfo = (req, dataKey, cfgType = 0, dao) => {
1177
- if (!dataKey) return null;
1178
- const dataConfig = dataconfig.getConfig(dataKey, cfgType);
1176
+ const getListInfo = (req, { key, folder, configtype = 0, dao }) => {
1177
+ if (!key) return null;
1178
+ const dataConfig = dataconfig.getCurrent(folder ?? process.cwd()).getConfig(key, configtype);
1179
1179
  if (!dataConfig?.list) return null;
1180
1180
  ///确认是否是需要导出Excel
1181
1181
  const export2Excel = (req.query.exportexcel + "").toLowerCase() === "true";
@@ -1232,9 +1232,9 @@ function requireRequestparser () {
1232
1232
  * 详细页面请求上下文中获取需要的信息
1233
1233
  * 如Page ,PageSize , Sort 等等
1234
1234
  */
1235
- const getDetailInfo = (req,dataKey) => {
1236
- if (dataKey) {
1237
- const dataConfig = dataconfig.getConfig(dataKey);
1235
+ const getDetailInfo = (req,{key,folder}) => {
1236
+ if (key) {
1237
+ const dataConfig = dataconfig.getCurrent(folder ?? process.cwd()).getConfig(key);
1238
1238
  if (dataConfig && dataConfig.detail) {
1239
1239
  req.dataConfig = dataConfig;
1240
1240
  const { primary, field: fields, primaryIsAutoIncrease } = dataConfig.detail;
@@ -1316,7 +1316,7 @@ function requirePoolmanager () {
1316
1316
  if (PoolManager[connect]!=null){
1317
1317
  return PoolManager[connect];
1318
1318
  }
1319
- const connectSection = appsetting.getSection(connect || 'dev');
1319
+ const connectSection = appsetting.getConnection(connect || 'dev');
1320
1320
  if (!connectSection) throw new Error('数据库连接配置不存在');
1321
1321
  const pool = mysql.createPool(connectSection);
1322
1322
  PoolManager[connect] = pool;
@@ -1984,20 +1984,19 @@ function requireController () {
1984
1984
  /*
1985
1985
  * 获取查询列表记录的方法
1986
1986
  */
1987
- async getListData(req, dataKey, configFile = 0) {
1988
- if (this.logger) this.logger.trace("准备获取dataconfig文件中对应的 %s list数据",dataKey);
1989
- const listinfo = getListInfo(req, dataKey, configFile, this._daoModel); //, ignorefilter==1
1990
- if (!listinfo) return DoomiApiResult.fail(-10, `缺失${dataKey}对应的查询语句`);
1991
-
1987
+ async getListData(req, key, configtype = 0) {
1988
+ if (this.logger) this.logger.trace("准备获取dataconfig文件中对应的 %s list数据", key);
1989
+ const listinfo = getListInfo(req, { key, folder: this.app.config.folder, configtype, dao:this._daoModel}); //, ignorefilter==1
1990
+ if (!listinfo) return DoomiApiResult.fail(-10, `缺失${key}对应的查询语句`);
1992
1991
  ////直接操作数据库之前,可由子类再次Handler
1993
- let beforeData = await this.beforeAccessDB(req, listinfo.sql, listinfo.params, "list", this);
1992
+ const beforeData = await this.beforeAccessDB(req, listinfo.sql, listinfo.params, "list", this);
1994
1993
  ////如果返回Null,则表示不加载数据了
1995
- if (beforeData.canceled === true) return DoomiApiResult.fail(1, '操作取消');
1994
+ if (beforeData.canceled) return DoomiApiResult.fail(1, '操作取消');
1996
1995
  ////操作数据库
1997
- let result = await this._daoModel.loadData(beforeData.sql, beforeData.sqlParams);
1996
+ const result = await this._daoModel.loadData(beforeData.sql, beforeData.sqlParams);
1998
1997
  if(!result.successed) {
1999
1998
  ////失败也需要记录
2000
- if (req.query.exportexcel==='true') this.logUserAction(req,this._daoModel.getBusiness(),5,-1);
1999
+ if (String(req.query.exportexcel).toLowerCase() === 'true') this.logUserAction(req,this._daoModel.getBusiness(),5,-1);
2001
2000
  return result;
2002
2001
  }
2003
2002
  ///从数据库中获取到了原始数据
@@ -2012,8 +2011,8 @@ function requireController () {
2012
2011
  }
2013
2012
  extra.total = await this.getListRecordCount(result);
2014
2013
  ///处理导出Excel
2015
- if (req.query.exportexcel==='true') {
2016
- const excelResult = await this.export2Excel(extra.rows, req.query.excelkey || dataKey);
2014
+ if (String(req.query.exportexcel).toLowerCase() === 'true') {
2015
+ const excelResult = await this.export2Excel(extra.rows, req.query.excelkey || key);
2017
2016
  this.logUserAction(req, this._daoModel.getBusiness(), 5, 0);
2018
2017
  return excelResult;
2019
2018
  }
@@ -2026,19 +2025,19 @@ function requireController () {
2026
2025
  * @param {*} data
2027
2026
  */
2028
2027
  async getListRecordCount(result){
2029
- if (result.rows && result.rows.length == 2 && result.rows[1].length) return result.rows[1][0].total;
2028
+ if (result && result.rows && result.rows.length == 2 && result.rows[1].length) return result.rows[1][0].total;
2030
2029
  return 0;
2031
2030
  }
2032
2031
  /*
2033
2032
  * 获取详细记录的方法
2034
2033
  */
2035
- async getDataById(req, dataKey, sqlCommand,idValue) {
2036
- if (this.logger) this.logger.trace("准备获取dataconfig文件中对应的 %s 详细数据",dataKey);
2037
- const detailinfo = getDetailInfo(req, dataKey);
2038
- if (!detailinfo) return DoomiApiResult.fail(-10, `缺失${dataKey}对应的详情配置`);
2039
- const beforeData = await this.beforeAccessDB(req, sqlCommand || this._daoModel.getByIdSql(), idValue ||req.params.id, "detail", this);
2034
+ async getDataById(req, key, sqlCommand,dataUid) {
2035
+ if (this.logger) this.logger.trace("准备获取dataconfig文件中对应的 %s 详细数据", key);
2036
+ const detailinfo = getDetailInfo(req, { key,folder:this.app.config.folder });
2037
+ if (!detailinfo) return DoomiApiResult.fail(-10, `缺失${key}对应的详情配置`);
2038
+ const beforeData = await this.beforeAccessDB(req, sqlCommand || this._daoModel.getByIdSql(), dataUid ||req.params.id, "detail", this);
2040
2039
  ////如果返回Null,则表示不加载数据了
2041
- if (beforeData.canceled===true) return DoomiApiResult.fail(1, '操作已取消');
2040
+ if (beforeData.canceled) return DoomiApiResult.fail(1, '操作已取消');
2042
2041
  /////处理SQL中的一些定义符
2043
2042
  const parseSql = parseTagInSql(req,beforeData.sql);
2044
2043
  ////操作数据库
@@ -2077,23 +2076,23 @@ function requireController () {
2077
2076
  /*
2078
2077
  * 新增实体记录的方法
2079
2078
  */
2080
- async create(req, dataKey, sqlCommand) {
2081
- if (this.logger) this.logger.trace("准备根据dataconfig文件中对应的 %s 配置插入数据",dataKey);
2082
- let mapping = req.query.Mapping??true; //|| 'true'==='true' ;//commonHelper.ifNull(req.query.Mapping, true);
2079
+ async create(req, key, sqlCommand) {
2080
+ if (this.logger) this.logger.trace("准备根据dataconfig文件中对应的 %s 配置插入数据", key);
2081
+ const mapping = String(req.query.Mapping??true)===String(true); //|| 'true'==='true' ;//commonHelper.ifNull(req.query.Mapping, true);
2083
2082
  let detailinfo, resultItem = { successed: true, data: req.body };
2084
2083
  ///是否需要将data中的key-value值到配置文件中进行转换
2085
- if (String(mapping) === String(true)) {
2086
- detailinfo = getDetailInfo(req, dataKey);
2087
- if (!detailinfo) return DoomiApiResult.fail(-10, `缺失${dataKey}对应的详情配置`);
2084
+ if (mapping) {
2085
+ detailinfo = getDetailInfo(req, { key,folder:this.app.config.folder });
2086
+ if (!detailinfo) return DoomiApiResult.fail(-10, `缺失${key}对应的详情配置`);
2088
2087
  resultItem = View2Data(req, detailinfo.fields, "create");
2089
2088
  ///如果转换的验证过程中出现错误,则返回
2090
2089
  if (!resultItem.successed) return resultItem;
2091
2090
  }
2092
- let beforeData = await this.beforeAccessDB(req, sqlCommand || this._daoModel.insertSql(), resultItem.data, "create", this);
2091
+ const beforeData = await this.beforeAccessDB(req, sqlCommand || this._daoModel.insertSql(), resultItem.data, "create", this);
2093
2092
  ////如果返回Null,则表示不加载数据了
2094
2093
  if (beforeData.canceled) return DoomiApiResult.fail(1, '操作取消');
2095
2094
  if (this.logger) this.logger.trace("插入数据准备 : ",beforeData);
2096
- let result = await this._daoModel.create(beforeData.sql, beforeData.sqlParams, req);
2095
+ const result = await this._daoModel.create(beforeData.sql, beforeData.sqlParams, req);
2097
2096
  if(!result.successed) {
2098
2097
  this.logUserAction(req,this._daoModel.getBusiness(),1,-1);
2099
2098
  return result;
@@ -2123,26 +2122,26 @@ function requireController () {
2123
2122
  /*
2124
2123
  * 修改实体记录的方法
2125
2124
  */
2126
- async update(req, dataKey, sqlCommand, id) {
2127
- if (this.logger) this.logger.trace("准备根据dataconfig文件中对应的 %s 配置修改数据",dataKey);
2128
- if (id == null) id = req.params.id;
2129
- let mapping = req.query.Mapping??true;// commonHelper.ifNull(req.query.Mapping, true);
2125
+ async update(req, key, sqlCommand, id) {
2126
+ if (this.logger) this.logger.trace("准备根据dataconfig文件中对应的 %s 配置修改数据", key);
2127
+ id = id ?? req.params.id;
2128
+ const mapping = String(req.query.Mapping ?? true)===String(true);// commonHelper.ifNull(req.query.Mapping, true);
2130
2129
  let detailinfo, resultItem = { successed: true, data: req.body };
2131
2130
  ///是否需要将data中的key-value值到配置文件中进行转换
2132
- if (String(mapping) === String(true)) {
2133
- detailinfo = getDetailInfo(req, dataKey);
2134
- if (!detailinfo) return DoomiApiResult.fail(-10, `缺失${dataKey}对应的详情配置`);
2131
+ if (mapping) {
2132
+ detailinfo = getDetailInfo(req, {key,folder:this.app.config.folder });
2133
+ if (!detailinfo) return DoomiApiResult.fail(-10, `缺失${key}对应的详情配置`);
2135
2134
  ///从Request中获取到用户提交的数据
2136
2135
  resultItem = View2Data(req, detailinfo.fields, "update");
2137
2136
  }
2138
2137
  ///如果不需要转换,则直接使用前端提交上来的值
2139
- let beforeData = await this.beforeAccessDB(req, sqlCommand || this._daoModel.updateSql(), resultItem.data, "update", this);
2138
+ const beforeData = await this.beforeAccessDB(req, sqlCommand || this._daoModel.updateSql(), resultItem.data, "update", this);
2140
2139
  ////如果返回Null,则表示不加载数据了
2141
2140
  if (beforeData.canceled) return DoomiApiResult.fail(1, '操作取消');
2142
2141
  if (this.logger) this.logger.trace("修改数据准备 : ",beforeData);
2143
2142
  /////处理SQL中的一些定义符
2144
- let parseSql = parseTagInSql(req,beforeData.sql);
2145
- let result = await this._daoModel.update(parseSql, beforeData.sqlParams, id);
2143
+ const parseSql = parseTagInSql(req,beforeData.sql);
2144
+ const result = await this._daoModel.update(parseSql, beforeData.sqlParams, id);
2146
2145
  if(!result.successed) {
2147
2146
  this.logUserAction(req,this._daoModel.getBusiness(),3,-1);
2148
2147
  return result;
@@ -2170,13 +2169,13 @@ function requireController () {
2170
2169
  */
2171
2170
  async delete(req, id, sqlCommand) {
2172
2171
  if (this.logger) this.logger.trace("准备删除数据",id);
2173
- let beforeData = await this.beforeAccessDB(req, sqlCommand || this._daoModel.deleteSql(id), id, "delete", this);
2172
+ const beforeData = await this.beforeAccessDB(req, sqlCommand || this._daoModel.deleteSql(id), id, "delete", this);
2174
2173
  ////如果返回Null,则表示不加载数据了
2175
2174
  if (beforeData.canceled) return DoomiApiResult.fail(1, '操作取消');
2176
2175
  /////处理SQL中的一些定义符
2177
- let parseSql = parseTagInSql(req,beforeData.sql);
2176
+ const parseSql = parseTagInSql(req,beforeData.sql);
2178
2177
  ////操作数据库
2179
- let result = await this._daoModel.delete(parseSql, beforeData.sqlParams,req.user?.id);
2178
+ const result = await this._daoModel.delete(parseSql, beforeData.sqlParams,req.user?.id);
2180
2179
  if(!result.successed) {
2181
2180
  this.logUserAction(req,this._daoModel.getBusiness(),2,-1);
2182
2181
  return result;