@gustavo-valsechi/node 1.0.3 → 1.0.4
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.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +117 -5
- package/dist/index.mjs +123 -5
- package/dist/src/database/index.js +1 -1
- package/dist/src/database/index.mjs +1 -1
- package/dist/src/router/index.js +9 -2
- package/dist/src/router/index.mjs +20 -2
- package/dist/src/server/index.js +10 -3
- package/dist/src/server/index.mjs +21 -3
- package/dist/src/tools/index.d.mts +23 -1
- package/dist/src/tools/index.d.ts +23 -1
- package/dist/src/tools/index.js +122 -0
- package/dist/src/tools/index.mjs +118 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { databaseInit } from './src/database/index.mjs';
|
|
2
2
|
export { apiRouter, mainRouter } from './src/router/index.mjs';
|
|
3
3
|
export { app, serverInit } from './src/server/index.mjs';
|
|
4
|
-
export { responseWrapper } from './src/tools/index.mjs';
|
|
4
|
+
export { OPERATORS, converter, existsOperator, paginate, refactorWhere, responseWrapper } from './src/tools/index.mjs';
|
|
5
5
|
import 'typeorm';
|
|
6
6
|
import 'fastify';
|
|
7
7
|
import 'http';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { databaseInit } from './src/database/index.js';
|
|
2
2
|
export { apiRouter, mainRouter } from './src/router/index.js';
|
|
3
3
|
export { app, serverInit } from './src/server/index.js';
|
|
4
|
-
export { responseWrapper } from './src/tools/index.js';
|
|
4
|
+
export { OPERATORS, converter, existsOperator, paginate, refactorWhere, responseWrapper } from './src/tools/index.js';
|
|
5
5
|
import 'typeorm';
|
|
6
6
|
import 'fastify';
|
|
7
7
|
import 'http';
|
package/dist/index.js
CHANGED
|
@@ -30,10 +30,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
OPERATORS: () => OPERATORS,
|
|
33
34
|
apiRouter: () => apiRouter,
|
|
34
35
|
app: () => app,
|
|
36
|
+
converter: () => converter,
|
|
35
37
|
databaseInit: () => databaseInit,
|
|
38
|
+
existsOperator: () => existsOperator,
|
|
36
39
|
mainRouter: () => mainRouter,
|
|
40
|
+
paginate: () => paginate,
|
|
41
|
+
refactorWhere: () => refactorWhere,
|
|
37
42
|
responseWrapper: () => responseWrapper,
|
|
38
43
|
serverInit: () => serverInit
|
|
39
44
|
});
|
|
@@ -70,7 +75,7 @@ var databaseInit = (entities) => {
|
|
|
70
75
|
applicationName: (0, import_os.hostname)(),
|
|
71
76
|
entities
|
|
72
77
|
});
|
|
73
|
-
DB.initialize().then(() => console.log("Data Source has been initialized!")).catch((err) => console.error("Error during Data Source initialization", err));
|
|
78
|
+
DB.initialize().then(() => console.log("\u{1F680} Data Source has been initialized!")).catch((err) => console.error("\u{1F534} Error during Data Source initialization", err));
|
|
74
79
|
return DB;
|
|
75
80
|
};
|
|
76
81
|
|
|
@@ -89,6 +94,108 @@ var responseWrapper = async (res, promise) => {
|
|
|
89
94
|
});
|
|
90
95
|
};
|
|
91
96
|
|
|
97
|
+
// src/tools/operators.ts
|
|
98
|
+
var import_typeorm2 = require("typeorm");
|
|
99
|
+
var import_lodash = __toESM(require("lodash"));
|
|
100
|
+
var OPERATORS = {
|
|
101
|
+
$in: (value) => (0, import_typeorm2.In)(value),
|
|
102
|
+
$and: (value) => (0, import_typeorm2.And)(value),
|
|
103
|
+
$notIn: (value) => (0, import_typeorm2.Not)((0, import_typeorm2.In)(value)),
|
|
104
|
+
$like: (value) => (0, import_typeorm2.Like)(value),
|
|
105
|
+
$notLike: (value) => (0, import_typeorm2.Not)((0, import_typeorm2.Like)(value)),
|
|
106
|
+
$gt: (value) => (0, import_typeorm2.MoreThan)(value),
|
|
107
|
+
$gte: (value) => (0, import_typeorm2.MoreThanOrEqual)(value),
|
|
108
|
+
$lt: (value) => (0, import_typeorm2.LessThan)(value),
|
|
109
|
+
$lte: (value) => (0, import_typeorm2.LessThanOrEqual)(value),
|
|
110
|
+
$between: ([from, to]) => (0, import_typeorm2.Between)(from, to),
|
|
111
|
+
$not: (value) => (0, import_typeorm2.Not)(value),
|
|
112
|
+
$ne: (value) => (0, import_typeorm2.Not)(value),
|
|
113
|
+
$raw: (value) => (0, import_typeorm2.Raw)((alias) => import_lodash.default.replace(value, "ALIAS", alias))
|
|
114
|
+
};
|
|
115
|
+
var existsOperator = (value) => {
|
|
116
|
+
let includeOp = false;
|
|
117
|
+
for (const op in OPERATORS) {
|
|
118
|
+
if (includeOp) continue;
|
|
119
|
+
includeOp = import_lodash.default.includes(JSON.stringify(value), op);
|
|
120
|
+
}
|
|
121
|
+
return includeOp;
|
|
122
|
+
};
|
|
123
|
+
var converter = (operator) => {
|
|
124
|
+
operator = import_lodash.default.isObject(operator) ? operator : JSON.parse(operator || "{}");
|
|
125
|
+
let convertion = operator;
|
|
126
|
+
for (const op in OPERATORS) {
|
|
127
|
+
if (!operator[op] && operator[op] !== 0) continue;
|
|
128
|
+
if (import_lodash.default.isObject(operator[op]) && import_lodash.default.isArray(operator[op])) {
|
|
129
|
+
import_lodash.default.forEach(operator[op], (condition, index) => {
|
|
130
|
+
if (!existsOperator(condition)) return;
|
|
131
|
+
for (const opSecondary in OPERATORS) {
|
|
132
|
+
if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
|
|
133
|
+
operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
convertion = OPERATORS[op](operator[op]);
|
|
138
|
+
}
|
|
139
|
+
return convertion;
|
|
140
|
+
};
|
|
141
|
+
var refactorWhere = (where) => {
|
|
142
|
+
for (const key in where) {
|
|
143
|
+
let includeOp = existsOperator(where[key]);
|
|
144
|
+
if (includeOp) {
|
|
145
|
+
where[key] = converter(where[key]);
|
|
146
|
+
if (import_lodash.default.isObject(where[key]) && !import_lodash.default.isArray(where[key])) {
|
|
147
|
+
for (const keyObject in where[key]) {
|
|
148
|
+
includeOp = existsOperator(where[key][keyObject]);
|
|
149
|
+
if (!includeOp) continue;
|
|
150
|
+
where[key][keyObject] = converter(where[key][keyObject]);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (import_lodash.default.isObject(where[key]) && import_lodash.default.isArray(where[key])) {
|
|
154
|
+
import_lodash.default.forEach(where[key], (condition, index) => {
|
|
155
|
+
includeOp = existsOperator(condition);
|
|
156
|
+
if (includeOp) {
|
|
157
|
+
condition = converter(condition);
|
|
158
|
+
if (import_lodash.default.isObject(condition) && !import_lodash.default.isArray(condition)) {
|
|
159
|
+
for (const keyObject in condition) {
|
|
160
|
+
includeOp = existsOperator(condition[keyObject]);
|
|
161
|
+
if (!includeOp) continue;
|
|
162
|
+
condition[keyObject] = converter(condition[keyObject]);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
where[key][index] = condition;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return where;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// src/tools/paginate.ts
|
|
175
|
+
var import_lodash2 = __toESM(require("lodash"));
|
|
176
|
+
var parse = (json) => {
|
|
177
|
+
if (!json) return;
|
|
178
|
+
if (import_lodash2.default.isString(json)) return JSON.parse(json);
|
|
179
|
+
return json;
|
|
180
|
+
};
|
|
181
|
+
var paginate = async (repository, options) => {
|
|
182
|
+
let where = parse(options.filters);
|
|
183
|
+
if (where) where = refactorWhere(where);
|
|
184
|
+
const [data, count] = await repository.findAndCount({
|
|
185
|
+
select: options.fields,
|
|
186
|
+
relations: options.relations,
|
|
187
|
+
where,
|
|
188
|
+
order: parse(options.order),
|
|
189
|
+
take: options.limit,
|
|
190
|
+
skip: Number(options.page) * (Number(options.limit) || 0) || 0
|
|
191
|
+
});
|
|
192
|
+
return {
|
|
193
|
+
totalPage: Math.ceil(count / options.limit),
|
|
194
|
+
total: count,
|
|
195
|
+
content: data
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
|
|
92
199
|
// src/router/health.ts
|
|
93
200
|
var healthRouter = (router, options, done) => {
|
|
94
201
|
router.get("/", async (req, res) => {
|
|
@@ -135,20 +242,20 @@ var serverInit = (appConfig) => {
|
|
|
135
242
|
};
|
|
136
243
|
app.listen(listenOptions, (err, address) => {
|
|
137
244
|
if (err) {
|
|
138
|
-
console.error("Error in server initialization!", err);
|
|
245
|
+
console.error("\u{1F534} Error in server initialization!", err);
|
|
139
246
|
process.exit(1);
|
|
140
247
|
}
|
|
141
|
-
console.log(
|
|
248
|
+
console.log(`\u{1F680} Server has been started in ${address}...`);
|
|
142
249
|
});
|
|
143
250
|
return app;
|
|
144
251
|
};
|
|
145
252
|
|
|
146
253
|
// src/router/api.ts
|
|
147
|
-
var
|
|
254
|
+
var import_lodash3 = __toESM(require("lodash"));
|
|
148
255
|
var apiRouter = (registers) => {
|
|
149
256
|
const apiPrefix = "api";
|
|
150
257
|
app.register((router, options, done) => {
|
|
151
|
-
|
|
258
|
+
import_lodash3.default.forEach(registers, (register, prefix) => {
|
|
152
259
|
router.register(register, { prefix: `${apiPrefix}/${prefix}` });
|
|
153
260
|
});
|
|
154
261
|
done();
|
|
@@ -162,10 +269,15 @@ var mainRouter = (router, options, done) => {
|
|
|
162
269
|
};
|
|
163
270
|
// Annotate the CommonJS export names for ESM import in node:
|
|
164
271
|
0 && (module.exports = {
|
|
272
|
+
OPERATORS,
|
|
165
273
|
apiRouter,
|
|
166
274
|
app,
|
|
275
|
+
converter,
|
|
167
276
|
databaseInit,
|
|
277
|
+
existsOperator,
|
|
168
278
|
mainRouter,
|
|
279
|
+
paginate,
|
|
280
|
+
refactorWhere,
|
|
169
281
|
responseWrapper,
|
|
170
282
|
serverInit
|
|
171
283
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -29,7 +29,7 @@ var databaseInit = (entities) => {
|
|
|
29
29
|
applicationName: hostname(),
|
|
30
30
|
entities
|
|
31
31
|
});
|
|
32
|
-
DB.initialize().then(() => console.log("Data Source has been initialized!")).catch((err) => console.error("Error during Data Source initialization", err));
|
|
32
|
+
DB.initialize().then(() => console.log("\u{1F680} Data Source has been initialized!")).catch((err) => console.error("\u{1F534} Error during Data Source initialization", err));
|
|
33
33
|
return DB;
|
|
34
34
|
};
|
|
35
35
|
|
|
@@ -48,6 +48,119 @@ var responseWrapper = async (res, promise) => {
|
|
|
48
48
|
});
|
|
49
49
|
};
|
|
50
50
|
|
|
51
|
+
// src/tools/operators.ts
|
|
52
|
+
import {
|
|
53
|
+
Like,
|
|
54
|
+
MoreThan,
|
|
55
|
+
MoreThanOrEqual,
|
|
56
|
+
In,
|
|
57
|
+
Not,
|
|
58
|
+
LessThan,
|
|
59
|
+
Between,
|
|
60
|
+
Raw,
|
|
61
|
+
And,
|
|
62
|
+
LessThanOrEqual
|
|
63
|
+
} from "typeorm";
|
|
64
|
+
import _ from "lodash";
|
|
65
|
+
var OPERATORS = {
|
|
66
|
+
$in: (value) => In(value),
|
|
67
|
+
$and: (value) => And(value),
|
|
68
|
+
$notIn: (value) => Not(In(value)),
|
|
69
|
+
$like: (value) => Like(value),
|
|
70
|
+
$notLike: (value) => Not(Like(value)),
|
|
71
|
+
$gt: (value) => MoreThan(value),
|
|
72
|
+
$gte: (value) => MoreThanOrEqual(value),
|
|
73
|
+
$lt: (value) => LessThan(value),
|
|
74
|
+
$lte: (value) => LessThanOrEqual(value),
|
|
75
|
+
$between: ([from, to]) => Between(from, to),
|
|
76
|
+
$not: (value) => Not(value),
|
|
77
|
+
$ne: (value) => Not(value),
|
|
78
|
+
$raw: (value) => Raw((alias) => _.replace(value, "ALIAS", alias))
|
|
79
|
+
};
|
|
80
|
+
var existsOperator = (value) => {
|
|
81
|
+
let includeOp = false;
|
|
82
|
+
for (const op in OPERATORS) {
|
|
83
|
+
if (includeOp) continue;
|
|
84
|
+
includeOp = _.includes(JSON.stringify(value), op);
|
|
85
|
+
}
|
|
86
|
+
return includeOp;
|
|
87
|
+
};
|
|
88
|
+
var converter = (operator) => {
|
|
89
|
+
operator = _.isObject(operator) ? operator : JSON.parse(operator || "{}");
|
|
90
|
+
let convertion = operator;
|
|
91
|
+
for (const op in OPERATORS) {
|
|
92
|
+
if (!operator[op] && operator[op] !== 0) continue;
|
|
93
|
+
if (_.isObject(operator[op]) && _.isArray(operator[op])) {
|
|
94
|
+
_.forEach(operator[op], (condition, index) => {
|
|
95
|
+
if (!existsOperator(condition)) return;
|
|
96
|
+
for (const opSecondary in OPERATORS) {
|
|
97
|
+
if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
|
|
98
|
+
operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
convertion = OPERATORS[op](operator[op]);
|
|
103
|
+
}
|
|
104
|
+
return convertion;
|
|
105
|
+
};
|
|
106
|
+
var refactorWhere = (where) => {
|
|
107
|
+
for (const key in where) {
|
|
108
|
+
let includeOp = existsOperator(where[key]);
|
|
109
|
+
if (includeOp) {
|
|
110
|
+
where[key] = converter(where[key]);
|
|
111
|
+
if (_.isObject(where[key]) && !_.isArray(where[key])) {
|
|
112
|
+
for (const keyObject in where[key]) {
|
|
113
|
+
includeOp = existsOperator(where[key][keyObject]);
|
|
114
|
+
if (!includeOp) continue;
|
|
115
|
+
where[key][keyObject] = converter(where[key][keyObject]);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (_.isObject(where[key]) && _.isArray(where[key])) {
|
|
119
|
+
_.forEach(where[key], (condition, index) => {
|
|
120
|
+
includeOp = existsOperator(condition);
|
|
121
|
+
if (includeOp) {
|
|
122
|
+
condition = converter(condition);
|
|
123
|
+
if (_.isObject(condition) && !_.isArray(condition)) {
|
|
124
|
+
for (const keyObject in condition) {
|
|
125
|
+
includeOp = existsOperator(condition[keyObject]);
|
|
126
|
+
if (!includeOp) continue;
|
|
127
|
+
condition[keyObject] = converter(condition[keyObject]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
where[key][index] = condition;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return where;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// src/tools/paginate.ts
|
|
140
|
+
import _2 from "lodash";
|
|
141
|
+
var parse = (json) => {
|
|
142
|
+
if (!json) return;
|
|
143
|
+
if (_2.isString(json)) return JSON.parse(json);
|
|
144
|
+
return json;
|
|
145
|
+
};
|
|
146
|
+
var paginate = async (repository, options) => {
|
|
147
|
+
let where = parse(options.filters);
|
|
148
|
+
if (where) where = refactorWhere(where);
|
|
149
|
+
const [data, count] = await repository.findAndCount({
|
|
150
|
+
select: options.fields,
|
|
151
|
+
relations: options.relations,
|
|
152
|
+
where,
|
|
153
|
+
order: parse(options.order),
|
|
154
|
+
take: options.limit,
|
|
155
|
+
skip: Number(options.page) * (Number(options.limit) || 0) || 0
|
|
156
|
+
});
|
|
157
|
+
return {
|
|
158
|
+
totalPage: Math.ceil(count / options.limit),
|
|
159
|
+
total: count,
|
|
160
|
+
content: data
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
|
|
51
164
|
// src/router/health.ts
|
|
52
165
|
var healthRouter = (router, options, done) => {
|
|
53
166
|
router.get("/", async (req, res) => {
|
|
@@ -94,20 +207,20 @@ var serverInit = (appConfig) => {
|
|
|
94
207
|
};
|
|
95
208
|
app.listen(listenOptions, (err, address) => {
|
|
96
209
|
if (err) {
|
|
97
|
-
console.error("Error in server initialization!", err);
|
|
210
|
+
console.error("\u{1F534} Error in server initialization!", err);
|
|
98
211
|
process.exit(1);
|
|
99
212
|
}
|
|
100
|
-
console.log(
|
|
213
|
+
console.log(`\u{1F680} Server has been started in ${address}...`);
|
|
101
214
|
});
|
|
102
215
|
return app;
|
|
103
216
|
};
|
|
104
217
|
|
|
105
218
|
// src/router/api.ts
|
|
106
|
-
import
|
|
219
|
+
import _3 from "lodash";
|
|
107
220
|
var apiRouter = (registers) => {
|
|
108
221
|
const apiPrefix = "api";
|
|
109
222
|
app.register((router, options, done) => {
|
|
110
|
-
|
|
223
|
+
_3.forEach(registers, (register, prefix) => {
|
|
111
224
|
router.register(register, { prefix: `${apiPrefix}/${prefix}` });
|
|
112
225
|
});
|
|
113
226
|
done();
|
|
@@ -120,10 +233,15 @@ var mainRouter = (router, options, done) => {
|
|
|
120
233
|
done();
|
|
121
234
|
};
|
|
122
235
|
export {
|
|
236
|
+
OPERATORS,
|
|
123
237
|
apiRouter,
|
|
124
238
|
app,
|
|
239
|
+
converter,
|
|
125
240
|
databaseInit,
|
|
241
|
+
existsOperator,
|
|
126
242
|
mainRouter,
|
|
243
|
+
paginate,
|
|
244
|
+
refactorWhere,
|
|
127
245
|
responseWrapper,
|
|
128
246
|
serverInit
|
|
129
247
|
};
|
|
@@ -53,7 +53,7 @@ var databaseInit = (entities) => {
|
|
|
53
53
|
applicationName: (0, import_os.hostname)(),
|
|
54
54
|
entities
|
|
55
55
|
});
|
|
56
|
-
DB.initialize().then(() => console.log("Data Source has been initialized!")).catch((err) => console.error("Error during Data Source initialization", err));
|
|
56
|
+
DB.initialize().then(() => console.log("\u{1F680} Data Source has been initialized!")).catch((err) => console.error("\u{1F534} Error during Data Source initialization", err));
|
|
57
57
|
return DB;
|
|
58
58
|
};
|
|
59
59
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -29,7 +29,7 @@ var databaseInit = (entities) => {
|
|
|
29
29
|
applicationName: hostname(),
|
|
30
30
|
entities
|
|
31
31
|
});
|
|
32
|
-
DB.initialize().then(() => console.log("Data Source has been initialized!")).catch((err) => console.error("Error during Data Source initialization", err));
|
|
32
|
+
DB.initialize().then(() => console.log("\u{1F680} Data Source has been initialized!")).catch((err) => console.error("\u{1F534} Error during Data Source initialization", err));
|
|
33
33
|
return DB;
|
|
34
34
|
};
|
|
35
35
|
export {
|
package/dist/src/router/index.js
CHANGED
|
@@ -50,6 +50,13 @@ var responseWrapper = async (res, promise) => {
|
|
|
50
50
|
});
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
+
// src/tools/operators.ts
|
|
54
|
+
var import_typeorm = require("typeorm");
|
|
55
|
+
var import_lodash = __toESM(require("lodash"));
|
|
56
|
+
|
|
57
|
+
// src/tools/paginate.ts
|
|
58
|
+
var import_lodash2 = __toESM(require("lodash"));
|
|
59
|
+
|
|
53
60
|
// src/router/health.ts
|
|
54
61
|
var healthRouter = (router, options, done) => {
|
|
55
62
|
router.get("/", async (req, res) => {
|
|
@@ -69,11 +76,11 @@ var import_fastify_plugin = __toESM(require("fastify-plugin"));
|
|
|
69
76
|
var app = (0, import_fastify.default)();
|
|
70
77
|
|
|
71
78
|
// src/router/api.ts
|
|
72
|
-
var
|
|
79
|
+
var import_lodash3 = __toESM(require("lodash"));
|
|
73
80
|
var apiRouter = (registers) => {
|
|
74
81
|
const apiPrefix = "api";
|
|
75
82
|
app.register((router, options, done) => {
|
|
76
|
-
|
|
83
|
+
import_lodash3.default.forEach(registers, (register, prefix) => {
|
|
77
84
|
router.register(register, { prefix: `${apiPrefix}/${prefix}` });
|
|
78
85
|
});
|
|
79
86
|
done();
|
|
@@ -13,6 +13,24 @@ var responseWrapper = async (res, promise) => {
|
|
|
13
13
|
});
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
+
// src/tools/operators.ts
|
|
17
|
+
import {
|
|
18
|
+
Like,
|
|
19
|
+
MoreThan,
|
|
20
|
+
MoreThanOrEqual,
|
|
21
|
+
In,
|
|
22
|
+
Not,
|
|
23
|
+
LessThan,
|
|
24
|
+
Between,
|
|
25
|
+
Raw,
|
|
26
|
+
And,
|
|
27
|
+
LessThanOrEqual
|
|
28
|
+
} from "typeorm";
|
|
29
|
+
import _ from "lodash";
|
|
30
|
+
|
|
31
|
+
// src/tools/paginate.ts
|
|
32
|
+
import _2 from "lodash";
|
|
33
|
+
|
|
16
34
|
// src/router/health.ts
|
|
17
35
|
var healthRouter = (router, options, done) => {
|
|
18
36
|
router.get("/", async (req, res) => {
|
|
@@ -32,11 +50,11 @@ import fp from "fastify-plugin";
|
|
|
32
50
|
var app = fastify();
|
|
33
51
|
|
|
34
52
|
// src/router/api.ts
|
|
35
|
-
import
|
|
53
|
+
import _3 from "lodash";
|
|
36
54
|
var apiRouter = (registers) => {
|
|
37
55
|
const apiPrefix = "api";
|
|
38
56
|
app.register((router, options, done) => {
|
|
39
|
-
|
|
57
|
+
_3.forEach(registers, (register, prefix) => {
|
|
40
58
|
router.register(register, { prefix: `${apiPrefix}/${prefix}` });
|
|
41
59
|
});
|
|
42
60
|
done();
|
package/dist/src/server/index.js
CHANGED
|
@@ -51,6 +51,13 @@ var responseWrapper = async (res, promise) => {
|
|
|
51
51
|
});
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
+
// src/tools/operators.ts
|
|
55
|
+
var import_typeorm = require("typeorm");
|
|
56
|
+
var import_lodash = __toESM(require("lodash"));
|
|
57
|
+
|
|
58
|
+
// src/tools/paginate.ts
|
|
59
|
+
var import_lodash2 = __toESM(require("lodash"));
|
|
60
|
+
|
|
54
61
|
// src/router/health.ts
|
|
55
62
|
var healthRouter = (router, options, done) => {
|
|
56
63
|
router.get("/", async (req, res) => {
|
|
@@ -65,7 +72,7 @@ var healthRouter = (router, options, done) => {
|
|
|
65
72
|
};
|
|
66
73
|
|
|
67
74
|
// src/router/api.ts
|
|
68
|
-
var
|
|
75
|
+
var import_lodash3 = __toESM(require("lodash"));
|
|
69
76
|
|
|
70
77
|
// src/router/index.ts
|
|
71
78
|
var mainRouter = (router, options, done) => {
|
|
@@ -105,10 +112,10 @@ var serverInit = (appConfig) => {
|
|
|
105
112
|
};
|
|
106
113
|
app.listen(listenOptions, (err, address) => {
|
|
107
114
|
if (err) {
|
|
108
|
-
console.error("Error in server initialization!", err);
|
|
115
|
+
console.error("\u{1F534} Error in server initialization!", err);
|
|
109
116
|
process.exit(1);
|
|
110
117
|
}
|
|
111
|
-
console.log(
|
|
118
|
+
console.log(`\u{1F680} Server has been started in ${address}...`);
|
|
112
119
|
});
|
|
113
120
|
return app;
|
|
114
121
|
};
|
|
@@ -16,6 +16,24 @@ var responseWrapper = async (res, promise) => {
|
|
|
16
16
|
});
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
+
// src/tools/operators.ts
|
|
20
|
+
import {
|
|
21
|
+
Like,
|
|
22
|
+
MoreThan,
|
|
23
|
+
MoreThanOrEqual,
|
|
24
|
+
In,
|
|
25
|
+
Not,
|
|
26
|
+
LessThan,
|
|
27
|
+
Between,
|
|
28
|
+
Raw,
|
|
29
|
+
And,
|
|
30
|
+
LessThanOrEqual
|
|
31
|
+
} from "typeorm";
|
|
32
|
+
import _ from "lodash";
|
|
33
|
+
|
|
34
|
+
// src/tools/paginate.ts
|
|
35
|
+
import _2 from "lodash";
|
|
36
|
+
|
|
19
37
|
// src/router/health.ts
|
|
20
38
|
var healthRouter = (router, options, done) => {
|
|
21
39
|
router.get("/", async (req, res) => {
|
|
@@ -30,7 +48,7 @@ var healthRouter = (router, options, done) => {
|
|
|
30
48
|
};
|
|
31
49
|
|
|
32
50
|
// src/router/api.ts
|
|
33
|
-
import
|
|
51
|
+
import _3 from "lodash";
|
|
34
52
|
|
|
35
53
|
// src/router/index.ts
|
|
36
54
|
var mainRouter = (router, options, done) => {
|
|
@@ -70,10 +88,10 @@ var serverInit = (appConfig) => {
|
|
|
70
88
|
};
|
|
71
89
|
app.listen(listenOptions, (err, address) => {
|
|
72
90
|
if (err) {
|
|
73
|
-
console.error("Error in server initialization!", err);
|
|
91
|
+
console.error("\u{1F534} Error in server initialization!", err);
|
|
74
92
|
process.exit(1);
|
|
75
93
|
}
|
|
76
|
-
console.log(
|
|
94
|
+
console.log(`\u{1F680} Server has been started in ${address}...`);
|
|
77
95
|
});
|
|
78
96
|
return app;
|
|
79
97
|
};
|
|
@@ -2,4 +2,26 @@ import { FastifyReply } from 'fastify';
|
|
|
2
2
|
|
|
3
3
|
declare const responseWrapper: (res: FastifyReply, promise: Promise<any>) => Promise<void>;
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
declare const OPERATORS: any;
|
|
6
|
+
declare const existsOperator: (value: any) => boolean;
|
|
7
|
+
declare const converter: (operator: any) => any;
|
|
8
|
+
declare const refactorWhere: (where: any) => any;
|
|
9
|
+
|
|
10
|
+
declare const paginate: (repository: any, options: {
|
|
11
|
+
fields: Array<string>;
|
|
12
|
+
relations: Array<string>;
|
|
13
|
+
filters: string | {
|
|
14
|
+
[field: string]: any;
|
|
15
|
+
};
|
|
16
|
+
order: string | {
|
|
17
|
+
[field: string]: "asc" | "desc";
|
|
18
|
+
};
|
|
19
|
+
limit: number;
|
|
20
|
+
page: number;
|
|
21
|
+
}) => Promise<{
|
|
22
|
+
totalPage: number;
|
|
23
|
+
total: any;
|
|
24
|
+
content: any;
|
|
25
|
+
}>;
|
|
26
|
+
|
|
27
|
+
export { OPERATORS, converter, existsOperator, paginate, refactorWhere, responseWrapper };
|
|
@@ -2,4 +2,26 @@ import { FastifyReply } from 'fastify';
|
|
|
2
2
|
|
|
3
3
|
declare const responseWrapper: (res: FastifyReply, promise: Promise<any>) => Promise<void>;
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
declare const OPERATORS: any;
|
|
6
|
+
declare const existsOperator: (value: any) => boolean;
|
|
7
|
+
declare const converter: (operator: any) => any;
|
|
8
|
+
declare const refactorWhere: (where: any) => any;
|
|
9
|
+
|
|
10
|
+
declare const paginate: (repository: any, options: {
|
|
11
|
+
fields: Array<string>;
|
|
12
|
+
relations: Array<string>;
|
|
13
|
+
filters: string | {
|
|
14
|
+
[field: string]: any;
|
|
15
|
+
};
|
|
16
|
+
order: string | {
|
|
17
|
+
[field: string]: "asc" | "desc";
|
|
18
|
+
};
|
|
19
|
+
limit: number;
|
|
20
|
+
page: number;
|
|
21
|
+
}) => Promise<{
|
|
22
|
+
totalPage: number;
|
|
23
|
+
total: any;
|
|
24
|
+
content: any;
|
|
25
|
+
}>;
|
|
26
|
+
|
|
27
|
+
export { OPERATORS, converter, existsOperator, paginate, refactorWhere, responseWrapper };
|
package/dist/src/tools/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,11 +17,24 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/tools/index.ts
|
|
21
31
|
var tools_exports = {};
|
|
22
32
|
__export(tools_exports, {
|
|
33
|
+
OPERATORS: () => OPERATORS,
|
|
34
|
+
converter: () => converter,
|
|
35
|
+
existsOperator: () => existsOperator,
|
|
36
|
+
paginate: () => paginate,
|
|
37
|
+
refactorWhere: () => refactorWhere,
|
|
23
38
|
responseWrapper: () => responseWrapper
|
|
24
39
|
});
|
|
25
40
|
module.exports = __toCommonJS(tools_exports);
|
|
@@ -38,7 +53,114 @@ var responseWrapper = async (res, promise) => {
|
|
|
38
53
|
return res.status(status).send({ message: err.message, type: err.type, code: err.code });
|
|
39
54
|
});
|
|
40
55
|
};
|
|
56
|
+
|
|
57
|
+
// src/tools/operators.ts
|
|
58
|
+
var import_typeorm = require("typeorm");
|
|
59
|
+
var import_lodash = __toESM(require("lodash"));
|
|
60
|
+
var OPERATORS = {
|
|
61
|
+
$in: (value) => (0, import_typeorm.In)(value),
|
|
62
|
+
$and: (value) => (0, import_typeorm.And)(value),
|
|
63
|
+
$notIn: (value) => (0, import_typeorm.Not)((0, import_typeorm.In)(value)),
|
|
64
|
+
$like: (value) => (0, import_typeorm.Like)(value),
|
|
65
|
+
$notLike: (value) => (0, import_typeorm.Not)((0, import_typeorm.Like)(value)),
|
|
66
|
+
$gt: (value) => (0, import_typeorm.MoreThan)(value),
|
|
67
|
+
$gte: (value) => (0, import_typeorm.MoreThanOrEqual)(value),
|
|
68
|
+
$lt: (value) => (0, import_typeorm.LessThan)(value),
|
|
69
|
+
$lte: (value) => (0, import_typeorm.LessThanOrEqual)(value),
|
|
70
|
+
$between: ([from, to]) => (0, import_typeorm.Between)(from, to),
|
|
71
|
+
$not: (value) => (0, import_typeorm.Not)(value),
|
|
72
|
+
$ne: (value) => (0, import_typeorm.Not)(value),
|
|
73
|
+
$raw: (value) => (0, import_typeorm.Raw)((alias) => import_lodash.default.replace(value, "ALIAS", alias))
|
|
74
|
+
};
|
|
75
|
+
var existsOperator = (value) => {
|
|
76
|
+
let includeOp = false;
|
|
77
|
+
for (const op in OPERATORS) {
|
|
78
|
+
if (includeOp) continue;
|
|
79
|
+
includeOp = import_lodash.default.includes(JSON.stringify(value), op);
|
|
80
|
+
}
|
|
81
|
+
return includeOp;
|
|
82
|
+
};
|
|
83
|
+
var converter = (operator) => {
|
|
84
|
+
operator = import_lodash.default.isObject(operator) ? operator : JSON.parse(operator || "{}");
|
|
85
|
+
let convertion = operator;
|
|
86
|
+
for (const op in OPERATORS) {
|
|
87
|
+
if (!operator[op] && operator[op] !== 0) continue;
|
|
88
|
+
if (import_lodash.default.isObject(operator[op]) && import_lodash.default.isArray(operator[op])) {
|
|
89
|
+
import_lodash.default.forEach(operator[op], (condition, index) => {
|
|
90
|
+
if (!existsOperator(condition)) return;
|
|
91
|
+
for (const opSecondary in OPERATORS) {
|
|
92
|
+
if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
|
|
93
|
+
operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
convertion = OPERATORS[op](operator[op]);
|
|
98
|
+
}
|
|
99
|
+
return convertion;
|
|
100
|
+
};
|
|
101
|
+
var refactorWhere = (where) => {
|
|
102
|
+
for (const key in where) {
|
|
103
|
+
let includeOp = existsOperator(where[key]);
|
|
104
|
+
if (includeOp) {
|
|
105
|
+
where[key] = converter(where[key]);
|
|
106
|
+
if (import_lodash.default.isObject(where[key]) && !import_lodash.default.isArray(where[key])) {
|
|
107
|
+
for (const keyObject in where[key]) {
|
|
108
|
+
includeOp = existsOperator(where[key][keyObject]);
|
|
109
|
+
if (!includeOp) continue;
|
|
110
|
+
where[key][keyObject] = converter(where[key][keyObject]);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (import_lodash.default.isObject(where[key]) && import_lodash.default.isArray(where[key])) {
|
|
114
|
+
import_lodash.default.forEach(where[key], (condition, index) => {
|
|
115
|
+
includeOp = existsOperator(condition);
|
|
116
|
+
if (includeOp) {
|
|
117
|
+
condition = converter(condition);
|
|
118
|
+
if (import_lodash.default.isObject(condition) && !import_lodash.default.isArray(condition)) {
|
|
119
|
+
for (const keyObject in condition) {
|
|
120
|
+
includeOp = existsOperator(condition[keyObject]);
|
|
121
|
+
if (!includeOp) continue;
|
|
122
|
+
condition[keyObject] = converter(condition[keyObject]);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
where[key][index] = condition;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return where;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
// src/tools/paginate.ts
|
|
135
|
+
var import_lodash2 = __toESM(require("lodash"));
|
|
136
|
+
var parse = (json) => {
|
|
137
|
+
if (!json) return;
|
|
138
|
+
if (import_lodash2.default.isString(json)) return JSON.parse(json);
|
|
139
|
+
return json;
|
|
140
|
+
};
|
|
141
|
+
var paginate = async (repository, options) => {
|
|
142
|
+
let where = parse(options.filters);
|
|
143
|
+
if (where) where = refactorWhere(where);
|
|
144
|
+
const [data, count] = await repository.findAndCount({
|
|
145
|
+
select: options.fields,
|
|
146
|
+
relations: options.relations,
|
|
147
|
+
where,
|
|
148
|
+
order: parse(options.order),
|
|
149
|
+
take: options.limit,
|
|
150
|
+
skip: Number(options.page) * (Number(options.limit) || 0) || 0
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
totalPage: Math.ceil(count / options.limit),
|
|
154
|
+
total: count,
|
|
155
|
+
content: data
|
|
156
|
+
};
|
|
157
|
+
};
|
|
41
158
|
// Annotate the CommonJS export names for ESM import in node:
|
|
42
159
|
0 && (module.exports = {
|
|
160
|
+
OPERATORS,
|
|
161
|
+
converter,
|
|
162
|
+
existsOperator,
|
|
163
|
+
paginate,
|
|
164
|
+
refactorWhere,
|
|
43
165
|
responseWrapper
|
|
44
166
|
});
|
package/dist/src/tools/index.mjs
CHANGED
|
@@ -12,6 +12,124 @@ var responseWrapper = async (res, promise) => {
|
|
|
12
12
|
return res.status(status).send({ message: err.message, type: err.type, code: err.code });
|
|
13
13
|
});
|
|
14
14
|
};
|
|
15
|
+
|
|
16
|
+
// src/tools/operators.ts
|
|
17
|
+
import {
|
|
18
|
+
Like,
|
|
19
|
+
MoreThan,
|
|
20
|
+
MoreThanOrEqual,
|
|
21
|
+
In,
|
|
22
|
+
Not,
|
|
23
|
+
LessThan,
|
|
24
|
+
Between,
|
|
25
|
+
Raw,
|
|
26
|
+
And,
|
|
27
|
+
LessThanOrEqual
|
|
28
|
+
} from "typeorm";
|
|
29
|
+
import _ from "lodash";
|
|
30
|
+
var OPERATORS = {
|
|
31
|
+
$in: (value) => In(value),
|
|
32
|
+
$and: (value) => And(value),
|
|
33
|
+
$notIn: (value) => Not(In(value)),
|
|
34
|
+
$like: (value) => Like(value),
|
|
35
|
+
$notLike: (value) => Not(Like(value)),
|
|
36
|
+
$gt: (value) => MoreThan(value),
|
|
37
|
+
$gte: (value) => MoreThanOrEqual(value),
|
|
38
|
+
$lt: (value) => LessThan(value),
|
|
39
|
+
$lte: (value) => LessThanOrEqual(value),
|
|
40
|
+
$between: ([from, to]) => Between(from, to),
|
|
41
|
+
$not: (value) => Not(value),
|
|
42
|
+
$ne: (value) => Not(value),
|
|
43
|
+
$raw: (value) => Raw((alias) => _.replace(value, "ALIAS", alias))
|
|
44
|
+
};
|
|
45
|
+
var existsOperator = (value) => {
|
|
46
|
+
let includeOp = false;
|
|
47
|
+
for (const op in OPERATORS) {
|
|
48
|
+
if (includeOp) continue;
|
|
49
|
+
includeOp = _.includes(JSON.stringify(value), op);
|
|
50
|
+
}
|
|
51
|
+
return includeOp;
|
|
52
|
+
};
|
|
53
|
+
var converter = (operator) => {
|
|
54
|
+
operator = _.isObject(operator) ? operator : JSON.parse(operator || "{}");
|
|
55
|
+
let convertion = operator;
|
|
56
|
+
for (const op in OPERATORS) {
|
|
57
|
+
if (!operator[op] && operator[op] !== 0) continue;
|
|
58
|
+
if (_.isObject(operator[op]) && _.isArray(operator[op])) {
|
|
59
|
+
_.forEach(operator[op], (condition, index) => {
|
|
60
|
+
if (!existsOperator(condition)) return;
|
|
61
|
+
for (const opSecondary in OPERATORS) {
|
|
62
|
+
if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
|
|
63
|
+
operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
convertion = OPERATORS[op](operator[op]);
|
|
68
|
+
}
|
|
69
|
+
return convertion;
|
|
70
|
+
};
|
|
71
|
+
var refactorWhere = (where) => {
|
|
72
|
+
for (const key in where) {
|
|
73
|
+
let includeOp = existsOperator(where[key]);
|
|
74
|
+
if (includeOp) {
|
|
75
|
+
where[key] = converter(where[key]);
|
|
76
|
+
if (_.isObject(where[key]) && !_.isArray(where[key])) {
|
|
77
|
+
for (const keyObject in where[key]) {
|
|
78
|
+
includeOp = existsOperator(where[key][keyObject]);
|
|
79
|
+
if (!includeOp) continue;
|
|
80
|
+
where[key][keyObject] = converter(where[key][keyObject]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (_.isObject(where[key]) && _.isArray(where[key])) {
|
|
84
|
+
_.forEach(where[key], (condition, index) => {
|
|
85
|
+
includeOp = existsOperator(condition);
|
|
86
|
+
if (includeOp) {
|
|
87
|
+
condition = converter(condition);
|
|
88
|
+
if (_.isObject(condition) && !_.isArray(condition)) {
|
|
89
|
+
for (const keyObject in condition) {
|
|
90
|
+
includeOp = existsOperator(condition[keyObject]);
|
|
91
|
+
if (!includeOp) continue;
|
|
92
|
+
condition[keyObject] = converter(condition[keyObject]);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
where[key][index] = condition;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return where;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// src/tools/paginate.ts
|
|
105
|
+
import _2 from "lodash";
|
|
106
|
+
var parse = (json) => {
|
|
107
|
+
if (!json) return;
|
|
108
|
+
if (_2.isString(json)) return JSON.parse(json);
|
|
109
|
+
return json;
|
|
110
|
+
};
|
|
111
|
+
var paginate = async (repository, options) => {
|
|
112
|
+
let where = parse(options.filters);
|
|
113
|
+
if (where) where = refactorWhere(where);
|
|
114
|
+
const [data, count] = await repository.findAndCount({
|
|
115
|
+
select: options.fields,
|
|
116
|
+
relations: options.relations,
|
|
117
|
+
where,
|
|
118
|
+
order: parse(options.order),
|
|
119
|
+
take: options.limit,
|
|
120
|
+
skip: Number(options.page) * (Number(options.limit) || 0) || 0
|
|
121
|
+
});
|
|
122
|
+
return {
|
|
123
|
+
totalPage: Math.ceil(count / options.limit),
|
|
124
|
+
total: count,
|
|
125
|
+
content: data
|
|
126
|
+
};
|
|
127
|
+
};
|
|
15
128
|
export {
|
|
129
|
+
OPERATORS,
|
|
130
|
+
converter,
|
|
131
|
+
existsOperator,
|
|
132
|
+
paginate,
|
|
133
|
+
refactorWhere,
|
|
16
134
|
responseWrapper
|
|
17
135
|
};
|