test-entity-library-asm 2.3.3 → 2.3.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.
@@ -1,9 +1,12 @@
1
1
  import { DataSource, EntityTarget, Repository, ObjectLiteral } from 'typeorm';
2
2
  export declare class CustomRepository<T extends ObjectLiteral> extends Repository<T> {
3
3
  constructor(target: EntityTarget<T>, dataSource: DataSource);
4
- getVerifyLocals(status: number | null, cursor: string | null, limit: number, filters: any): Promise<{
4
+ getVerifyLocals2(status: number | null, page: number, pageSize: number, filters: any): Promise<{
5
5
  data: T[];
6
- nextCursor: string | null;
6
+ total: number;
7
+ page: number;
8
+ pageSize: number;
9
+ totalPages: number;
7
10
  }>;
8
11
  anyOtherRepository(args: any): Promise<void>;
9
12
  }
@@ -58,15 +58,15 @@ var CustomRepository = /** @class */ (function (_super) {
58
58
  function CustomRepository(target, dataSource) {
59
59
  return _super.call(this, target, dataSource.manager) || this;
60
60
  }
61
- CustomRepository.prototype.getVerifyLocals = function (status, cursor, limit, filters) {
61
+ CustomRepository.prototype.getVerifyLocals2 = function (status, page, pageSize, filters) {
62
62
  return __awaiter(this, void 0, void 0, function () {
63
- var queryBuilder, globalValue, decodedCursor, cursorDate, results, nextCursor;
64
- var _a;
63
+ var queryBuilder, globalValue, _a, data, total;
65
64
  return __generator(this, function (_b) {
66
65
  switch (_b.label) {
67
66
  case 0:
68
- queryBuilder = this.createQueryBuilder('entity').take(limit + 1) // Fetch one more record than needed to determine if there's a next page
69
- ;
67
+ queryBuilder = this.createQueryBuilder('entity')
68
+ .skip((page - 1) * pageSize)
69
+ .take(pageSize);
70
70
  if (status !== null && status >= 0) {
71
71
  queryBuilder.andWhere('entity.status = :status', { status: status });
72
72
  }
@@ -77,24 +77,53 @@ var CustomRepository = /** @class */ (function (_super) {
77
77
  'OR JSON_EXTRACT(entity.local_information, "$.address.formatted_address") LIKE :globalValue ' +
78
78
  'OR entity.status LIKE :globalValue)', { globalValue: globalValue });
79
79
  }
80
- if (cursor) {
81
- decodedCursor = Buffer.from(cursor, 'base64').toString('ascii');
82
- cursorDate = new Date(decodedCursor) // Convert the decoded cursor to a Date object
83
- ;
84
- queryBuilder.andWhere('entity.created > :cursor', {
85
- cursor: cursorDate.toISOString(),
86
- });
87
- }
88
- queryBuilder.orderBy('entity.created', 'ASC');
89
- return [4 /*yield*/, queryBuilder.getMany()];
80
+ Object.keys(filters).forEach(function (key) {
81
+ var _a, _b, _c, _d;
82
+ if (key !== 'global' && filters[key].constraints) {
83
+ var filter = filters[key];
84
+ var value = filter.constraints[0].value;
85
+ var matchMode = filter.constraints[0].matchMode;
86
+ if (value !== null) {
87
+ var jsonPath = key
88
+ .split('.')
89
+ .map(function (segment) { return "$.".concat(segment); })
90
+ .join('');
91
+ switch (matchMode) {
92
+ case 'startsWith':
93
+ queryBuilder.andWhere("JSON_EXTRACT(entity.".concat(key.split('.')[0], ", \"").concat(jsonPath, "\") LIKE :").concat(key), (_a = {},
94
+ _a[key] = "".concat(value, "%"),
95
+ _a));
96
+ break;
97
+ case 'contains':
98
+ queryBuilder.andWhere("JSON_EXTRACT(entity.".concat(key.split('.')[0], ", \"").concat(jsonPath, "\") LIKE :").concat(key), (_b = {},
99
+ _b[key] = "%".concat(value, "%"),
100
+ _b));
101
+ break;
102
+ case 'equals':
103
+ queryBuilder.andWhere("JSON_EXTRACT(entity.".concat(key.split('.')[0], ", \"").concat(jsonPath, "\") = :").concat(key), (_c = {},
104
+ _c[key] = value,
105
+ _c));
106
+ break;
107
+ case 'dateIs':
108
+ queryBuilder.andWhere("DATE(JSON_EXTRACT(entity.".concat(key.split('.')[0], ", \"").concat(jsonPath, "\")) = :").concat(key), (_d = {},
109
+ _d[key] = value,
110
+ _d));
111
+ break;
112
+ default:
113
+ break;
114
+ }
115
+ }
116
+ }
117
+ });
118
+ return [4 /*yield*/, queryBuilder.getManyAndCount()];
90
119
  case 1:
91
- results = _b.sent();
92
- nextCursor = results.length > limit ? (_a = results.pop()) === null || _a === void 0 ? void 0 : _a.created : null;
120
+ _a = _b.sent(), data = _a[0], total = _a[1];
93
121
  return [2 /*return*/, {
94
- data: results,
95
- nextCursor: nextCursor
96
- ? Buffer.from(nextCursor.toISOString()).toString('base64')
97
- : null,
122
+ data: data,
123
+ total: total,
124
+ page: page,
125
+ pageSize: pageSize,
126
+ totalPages: Math.ceil(total / pageSize),
98
127
  }];
99
128
  }
100
129
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "test-entity-library-asm",
3
- "version": "2.3.3",
3
+ "version": "2.3.5",
4
4
  "description": "Entidades de ejemplo para una base de datos",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -5,13 +5,15 @@ export class CustomRepository<T extends ObjectLiteral> extends Repository<T> {
5
5
  super(target, dataSource.manager)
6
6
  }
7
7
 
8
- async getVerifyLocals(
8
+ async getVerifyLocals2(
9
9
  status: number | null,
10
- cursor: string | null,
11
- limit: number,
10
+ page: number,
11
+ pageSize: number,
12
12
  filters: any
13
- ): Promise<{ data: T[]; nextCursor: string | null }> {
14
- const queryBuilder = this.createQueryBuilder('entity').take(limit + 1) // Fetch one more record than needed to determine if there's a next page
13
+ ) {
14
+ const queryBuilder = this.createQueryBuilder('entity')
15
+ .skip((page - 1) * pageSize)
16
+ .take(pageSize)
15
17
 
16
18
  if (status !== null && status >= 0) {
17
19
  queryBuilder.andWhere('entity.status = :status', { status })
@@ -28,24 +30,73 @@ export class CustomRepository<T extends ObjectLiteral> extends Repository<T> {
28
30
  )
29
31
  }
30
32
 
31
- if (cursor) {
32
- const decodedCursor = Buffer.from(cursor, 'base64').toString('ascii')
33
- const cursorDate = new Date(decodedCursor) // Convert the decoded cursor to a Date object
34
- queryBuilder.andWhere('entity.created > :cursor', {
35
- cursor: cursorDate.toISOString(),
36
- })
37
- }
33
+ Object.keys(filters).forEach((key) => {
34
+ if (key !== 'global' && filters[key].constraints) {
35
+ const filter = filters[key]
36
+ const value = filter.constraints[0].value
37
+ const matchMode = filter.constraints[0].matchMode
38
38
 
39
- queryBuilder.orderBy('entity.created', 'ASC')
39
+ if (value !== null) {
40
+ const jsonPath = key
41
+ .split('.')
42
+ .map((segment) => `$.${segment}`)
43
+ .join('')
44
+ switch (matchMode) {
45
+ case 'startsWith':
46
+ queryBuilder.andWhere(
47
+ `JSON_EXTRACT(entity.${
48
+ key.split('.')[0]
49
+ }, "${jsonPath}") LIKE :${key}`,
50
+ {
51
+ [key]: `${value}%`,
52
+ }
53
+ )
54
+ break
55
+ case 'contains':
56
+ queryBuilder.andWhere(
57
+ `JSON_EXTRACT(entity.${
58
+ key.split('.')[0]
59
+ }, "${jsonPath}") LIKE :${key}`,
60
+ {
61
+ [key]: `%${value}%`,
62
+ }
63
+ )
64
+ break
65
+ case 'equals':
66
+ queryBuilder.andWhere(
67
+ `JSON_EXTRACT(entity.${
68
+ key.split('.')[0]
69
+ }, "${jsonPath}") = :${key}`,
70
+ {
71
+ [key]: value,
72
+ }
73
+ )
74
+ break
75
+ case 'dateIs':
76
+ queryBuilder.andWhere(
77
+ `DATE(JSON_EXTRACT(entity.${
78
+ key.split('.')[0]
79
+ }, "${jsonPath}")) = :${key}`,
80
+ {
81
+ [key]: value,
82
+ }
83
+ )
84
+ break
85
+ default:
86
+ break
87
+ }
88
+ }
89
+ }
90
+ })
40
91
 
41
- const results = await queryBuilder.getMany()
42
- const nextCursor = results.length > limit ? results.pop()?.created : null
92
+ const [data, total] = await queryBuilder.getManyAndCount()
43
93
 
44
94
  return {
45
- data: results,
46
- nextCursor: nextCursor
47
- ? Buffer.from(nextCursor.toISOString()).toString('base64')
48
- : null,
95
+ data,
96
+ total,
97
+ page,
98
+ pageSize,
99
+ totalPages: Math.ceil(total / pageSize),
49
100
  }
50
101
  }
51
102