@domain.js/main 0.4.16 → 0.4.17

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.
@@ -1,7 +1,6 @@
1
1
  import type { LoDashStatic } from "lodash";
2
2
  import moment from "moment";
3
3
  import * as mysql from "mysql2";
4
- import * as Sequelize from "sequelize";
5
4
  import { ModelBase, ModelStatic } from "../sequelize";
6
5
  interface Cnf {
7
6
  rest: {
@@ -12,7 +11,6 @@ interface Deps {
12
11
  _: LoDashStatic;
13
12
  mysql: Pick<typeof mysql, "escape">;
14
13
  moment: typeof moment extends (...args: infer A) => infer B ? (...args: A) => B : never;
15
- Sequelize: Pick<typeof Sequelize, "where" | "fn" | "col" | "literal" | "Op">;
16
14
  errors: {
17
15
  notAllowed: (...args: any[]) => Error;
18
16
  resourceDuplicateAdd: (...args: any[]) => Error;
@@ -21,9 +21,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
22
  exports.Utils = void 0;
23
23
  const mysql = __importStar(require("mysql2"));
24
+ const sequelize_1 = require("sequelize");
24
25
  function Utils(cnf, deps) {
25
26
  const { rest: { relativeMaxRangeDays: RELATIVE_MAX_RANGE = 100 }, } = cnf;
26
- const { _, errors, moment, Sequelize } = deps;
27
+ const { _, errors, moment } = deps;
27
28
  /**
28
29
  * 相对多少天的时间
29
30
  * @param Number days 相对多少天
@@ -202,7 +203,7 @@ function Utils(cnf, deps) {
202
203
  const RELATIVE_RANGE_ERROR = errors.notAllowed(`相对时间跨度最多 ${RELATIVE_MAX_RANGE} 天`);
203
204
  // findOptFilter 的处理
204
205
  // eslint-disable-next-line complexity
205
- const findOptFilter = (params, name, where, Op, col = name) => {
206
+ const findOptFilter = (params, name, where, col = name) => {
206
207
  let value;
207
208
  if (!params)
208
209
  return;
@@ -218,27 +219,27 @@ function Utils(cnf, deps) {
218
219
  throw RELATIVE_RANGE_ERROR;
219
220
  if (!where[col])
220
221
  where[col] = {};
221
- if (!where[col][Op.and])
222
- where[col][Op.and] = [];
222
+ if (!where[col][sequelize_1.Op.and])
223
+ where[col][sequelize_1.Op.and] = [];
223
224
  if (ignoreYear) {
224
225
  // 忽略年,这里要处理跨年的问题
225
226
  const startDate = moment(Date.now() + start * 86400000).format("MM-DD");
226
227
  const endDate = moment(Date.now() + end * 86400000).format("MM-DD");
227
228
  if (endDate < startDate) {
228
- where[col][Op.and].push({
229
- [Op.or]: [
230
- Sequelize.where(Sequelize.fn("DATE_FORMAT", Sequelize.col(name), "%m-%d"), Op.between, Sequelize.literal(`'${startDate}' AND '12-31'`)),
231
- Sequelize.where(Sequelize.fn("DATE_FORMAT", Sequelize.col(name), "%m-%d"), Op.between, Sequelize.literal(`'01-01' AND '${endDate}'`)),
229
+ where[col][sequelize_1.Op.and].push({
230
+ [sequelize_1.Op.or]: [
231
+ sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("DATE_FORMAT", sequelize_1.Sequelize.col(name), "%m-%d"), sequelize_1.Op.between, sequelize_1.Sequelize.literal(`'${startDate}' AND '12-31'`)),
232
+ sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("DATE_FORMAT", sequelize_1.Sequelize.col(name), "%m-%d"), sequelize_1.Op.between, sequelize_1.Sequelize.literal(`'01-01' AND '${endDate}'`)),
232
233
  ],
233
234
  });
234
235
  }
235
236
  else {
236
- where[col][Op.and].push(Sequelize.where(Sequelize.fn("DATE_FORMAT", Sequelize.col(name), "%m-%d"), Op.between, Sequelize.literal(`'${startDate}' AND '${endDate}'`)));
237
+ where[col][sequelize_1.Op.and].push(sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("DATE_FORMAT", sequelize_1.Sequelize.col(name), "%m-%d"), sequelize_1.Op.between, sequelize_1.Sequelize.literal(`'${startDate}' AND '${endDate}'`)));
237
238
  }
238
239
  }
239
240
  else {
240
- where[col][Op.and].push(Sequelize.where(Sequelize.fn("DATE", Sequelize.col(name)), {
241
- [Op.between]: [relativeDays(start), relativeDays(end, false)],
241
+ where[col][sequelize_1.Op.and].push(sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("DATE", sequelize_1.Sequelize.col(name)), {
242
+ [sequelize_1.Op.between]: [relativeDays(start), relativeDays(end, false)],
242
243
  }));
243
244
  }
244
245
  }
@@ -250,36 +251,36 @@ function Utils(cnf, deps) {
250
251
  value = null;
251
252
  if (!where[col])
252
253
  where[col] = {};
253
- where[col][Op.eq] = value;
254
+ where[col][sequelize_1.Op.eq] = value;
254
255
  }
255
256
  if (_.isNumber(params[name])) {
256
257
  if (!where[col])
257
258
  where[col] = {};
258
- where[col][Op.eq] = params[name];
259
+ where[col][sequelize_1.Op.eq] = params[name];
259
260
  }
260
261
  // 处理where in
261
262
  if (_.isString(params[`${name}s`])) {
262
263
  if (!where[col])
263
264
  where[col] = {};
264
- where[col][Op.in] = params[`${name}s`].trim().split(",");
265
+ where[col][sequelize_1.Op.in] = params[`${name}s`].trim().split(",");
265
266
  }
266
267
  // in 直接是数组的格式
267
268
  if (_.isArray(params[`${name}s`])) {
268
269
  if (!where[col])
269
270
  where[col] = {};
270
- where[col][Op.in] = params[`${name}s`];
271
+ where[col][sequelize_1.Op.in] = params[`${name}s`];
271
272
  }
272
273
  // 处理where not in
273
274
  if (_.isString(params[`${name}s!`])) {
274
275
  if (!where[col])
275
276
  where[col] = {};
276
- where[col][Op.notIn] = params[`${name}s!`].trim().split(",");
277
+ where[col][sequelize_1.Op.notIn] = params[`${name}s!`].trim().split(",");
277
278
  }
278
279
  // not in 直接是数组的格式
279
280
  if (_.isArray(params[`${name}s!`])) {
280
281
  if (!where[col])
281
282
  where[col] = {};
282
- where[col][Op.notIn] = params[`${name}s!`];
283
+ where[col][sequelize_1.Op.notIn] = params[`${name}s!`];
283
284
  }
284
285
  // 处理不等于的判断
285
286
  if (_.isString(params[`${name}!`])) {
@@ -289,10 +290,10 @@ function Utils(cnf, deps) {
289
290
  // 特殊处理null值
290
291
  if (value === ".null.") {
291
292
  value = null;
292
- where[col][Op.not] = value;
293
+ where[col][sequelize_1.Op.not] = value;
293
294
  }
294
295
  else {
295
- where[col][Op.ne] = value;
296
+ where[col][sequelize_1.Op.ne] = value;
296
297
  }
297
298
  }
298
299
  // 处理like
@@ -300,16 +301,16 @@ function Utils(cnf, deps) {
300
301
  value = params[`${name}_like`].trim().replace(/\*/g, "%");
301
302
  if (!where[col])
302
303
  where[col] = {};
303
- where[col][Op.like] = value;
304
+ where[col][sequelize_1.Op.like] = value;
304
305
  }
305
306
  // 处理likes [like or]
306
307
  if (_.isString(params[`${name}_likes`])) {
307
308
  const likes = params[`${name}_likes`].trim().split(",");
308
309
  if (!where[col])
309
310
  where[col] = {};
310
- where[col][Op.or] = likes.map((x) => {
311
+ where[col][sequelize_1.Op.or] = likes.map((x) => {
311
312
  value = x.trim().replace(/\*/g, "%");
312
- return { [Op.like]: value };
313
+ return { [sequelize_1.Op.like]: value };
313
314
  });
314
315
  }
315
316
  // 处理notLike
@@ -317,7 +318,7 @@ function Utils(cnf, deps) {
317
318
  value = params[`${name}_notLike`].trim().replace(/\*/g, "%");
318
319
  if (!where[col])
319
320
  where[col] = {};
320
- where[col][Op.notLike] = value;
321
+ where[col][sequelize_1.Op.notLike] = value;
321
322
  }
322
323
  // 处理大于,小于, 大于等于,小于等于的判断
323
324
  _.each(["gt", "gte", "lt", "lte"], (x) => {
@@ -327,48 +328,46 @@ function Utils(cnf, deps) {
327
328
  value = _.isString(params[c]) ? params[c].trim() : params[c];
328
329
  if (!where[col])
329
330
  where[col] = {};
330
- where[col][Op[x]] = value;
331
+ where[col][sequelize_1.Op[x]] = value;
331
332
  });
332
333
  // 处理 find_in_set 方式的过滤
333
334
  if (_.isString(params[`${name}_ins`]) || _.isNumber(params[`${name}_ins`])) {
334
- if (!where[Op.and])
335
- where[Op.and] = [];
336
- where[Op.and].push(Sequelize.where(Sequelize.fn("FIND_IN_SET", params[`${name}_ins`], Sequelize.col(name)), Op.gte, 1));
335
+ if (!where[sequelize_1.Op.and])
336
+ where[sequelize_1.Op.and] = [];
337
+ where[sequelize_1.Op.and].push(sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("FIND_IN_SET", params[`${name}_ins`], sequelize_1.Sequelize.col(name)), sequelize_1.Op.gte, 1));
337
338
  }
338
339
  // 处理 find_in_set 方式的过滤 _ins_and
339
340
  if (_.isString(params[`${name}_ins_and`])) {
340
- if (!where[Op.and])
341
- where[Op.and] = [];
341
+ if (!where[sequelize_1.Op.and])
342
+ where[sequelize_1.Op.and] = [];
342
343
  for (const v of params[`${name}_ins_and`].split(",")) {
343
- where[Op.and].push(Sequelize.where(Sequelize.fn("FIND_IN_SET", v.trim(), Sequelize.col(name)), Op.gte, 1));
344
+ where[sequelize_1.Op.and].push(sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("FIND_IN_SET", v.trim(), sequelize_1.Sequelize.col(name)), sequelize_1.Op.gte, 1));
344
345
  }
345
346
  }
346
347
  // 处理 find_in_set 方式的过滤 _ins_or
347
348
  if (_.isString(params[`${name}_ins_or`])) {
348
- if (!where[Op.and])
349
- where[Op.and] = [];
350
- where[Op.and].push({
351
- [Op.or]: params[`${name}_ins_or`].split(",").map((v) => Sequelize.where(Sequelize.fn("FIND_IN_SET", v.trim(), Sequelize.col(name)), Op.gte, 1)),
349
+ if (!where[sequelize_1.Op.and])
350
+ where[sequelize_1.Op.and] = [];
351
+ where[sequelize_1.Op.and].push({
352
+ [sequelize_1.Op.or]: params[`${name}_ins_or`].split(",").map((v) => sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("FIND_IN_SET", v.trim(), sequelize_1.Sequelize.col(name)), sequelize_1.Op.gte, 1)),
352
353
  });
353
354
  }
354
355
  // 处理 find_in_set 方式的过滤 _ins_not
355
356
  if (_.isString(params[`${name}_ins_not`])) {
356
- if (!where[Op.and])
357
- where[Op.and] = [];
357
+ if (!where[sequelize_1.Op.and])
358
+ where[sequelize_1.Op.and] = [];
358
359
  for (const v of params[`${name}_ins_not`].split(",")) {
359
- where[Op.and].push(Sequelize.where(Sequelize.fn("FIND_IN_SET", v.trim(), Sequelize.col(name)), Op.lt, 1));
360
+ where[sequelize_1.Op.and].push(sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("FIND_IN_SET", v.trim(), sequelize_1.Sequelize.col(name)), sequelize_1.Op.lt, 1));
360
361
  }
361
362
  }
362
363
  };
363
364
  // 返回列表查询的条件
364
365
  const findAllOpts = (Model, params) => {
365
- const { Op } = Sequelize;
366
- const { literal } = Model.sequelize;
367
366
  const where = {};
368
367
  const searchOrs = [];
369
368
  const includes = modelInclude(params, Model.includes);
370
369
  _.each(Model.filterAttrs || _.keys(Model.rawAttributes), (name) => {
371
- findOptFilter(params, name, where, Op);
370
+ findOptFilter(params, name, where);
372
371
  });
373
372
  if (Model.rawAttributes.isDeleted && !params._showDeleted) {
374
373
  where.isDeleted = "no";
@@ -384,12 +383,12 @@ function Utils(cnf, deps) {
384
383
  const includeWhere = {};
385
384
  const filterAttrs = x.model.filterAttrs || _.keys(x.model.rawAttributes);
386
385
  _.each(filterAttrs, (name) => {
387
- findOptFilter(params, `${x.as}.${name}`, includeWhere, Op, name);
386
+ findOptFilter(params, `${x.as}.${name}`, includeWhere, name);
388
387
  });
389
388
  if (x.model.rawAttributes.isDeleted && !params._showDeleted) {
390
- includeWhere[Op.or] = [{ isDeleted: "no" }];
389
+ includeWhere[sequelize_1.Op.or] = [{ isDeleted: "no" }];
391
390
  if (x.required === false)
392
- includeWhere[Op.or].push({ id: null });
391
+ includeWhere[sequelize_1.Op.or].push({ id: null });
393
392
  }
394
393
  // 将搜索条件添加到 include 的 where 条件上
395
394
  const searchOptResII = searchOpt(x.model, params._searchs, params.q, x.as);
@@ -411,14 +410,14 @@ function Utils(cnf, deps) {
411
410
  const _searchOrs = _.filter(_.compact(searchOrs), (x) => x.length);
412
411
  if (_.size(where)) {
413
412
  if (_searchOrs.length) {
414
- ret.where = Sequelize.and(where, literal(mergeSearchOrs(_searchOrs)));
413
+ ret.where = sequelize_1.Sequelize.and(where, (0, sequelize_1.literal)(mergeSearchOrs(_searchOrs)));
415
414
  }
416
415
  else {
417
416
  ret.where = where;
418
417
  }
419
418
  }
420
419
  else if (_searchOrs.length) {
421
- ret.where = literal(mergeSearchOrs(_searchOrs));
420
+ ret.where = (0, sequelize_1.literal)(mergeSearchOrs(_searchOrs));
422
421
  }
423
422
  // 处理需要返回的字段
424
423
  (() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.4.16",
3
+ "version": "0.4.17",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {