@gustavo-valsechi/node 1.0.3 → 1.0.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.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, moduler, 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, moduler, 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,16 @@ 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
+ moduler: () => moduler,
41
+ paginate: () => paginate,
42
+ refactorWhere: () => refactorWhere,
37
43
  responseWrapper: () => responseWrapper,
38
44
  serverInit: () => serverInit
39
45
  });
@@ -70,7 +76,7 @@ var databaseInit = (entities) => {
70
76
  applicationName: (0, import_os.hostname)(),
71
77
  entities
72
78
  });
73
- DB.initialize().then(() => console.log("Data Source has been initialized!")).catch((err) => console.error("Error during Data Source initialization", err));
79
+ 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
80
  return DB;
75
81
  };
76
82
 
@@ -89,6 +95,122 @@ var responseWrapper = async (res, promise) => {
89
95
  });
90
96
  };
91
97
 
98
+ // src/tools/operators.ts
99
+ var import_typeorm2 = require("typeorm");
100
+ var import_lodash = __toESM(require("lodash"));
101
+ var OPERATORS = {
102
+ $in: (value) => (0, import_typeorm2.In)(value),
103
+ $and: (value) => (0, import_typeorm2.And)(value),
104
+ $notIn: (value) => (0, import_typeorm2.Not)((0, import_typeorm2.In)(value)),
105
+ $like: (value) => (0, import_typeorm2.Like)(value),
106
+ $notLike: (value) => (0, import_typeorm2.Not)((0, import_typeorm2.Like)(value)),
107
+ $gt: (value) => (0, import_typeorm2.MoreThan)(value),
108
+ $gte: (value) => (0, import_typeorm2.MoreThanOrEqual)(value),
109
+ $lt: (value) => (0, import_typeorm2.LessThan)(value),
110
+ $lte: (value) => (0, import_typeorm2.LessThanOrEqual)(value),
111
+ $between: ([from, to]) => (0, import_typeorm2.Between)(from, to),
112
+ $not: (value) => (0, import_typeorm2.Not)(value),
113
+ $ne: (value) => (0, import_typeorm2.Not)(value),
114
+ $raw: (value) => (0, import_typeorm2.Raw)((alias) => import_lodash.default.replace(value, "ALIAS", alias))
115
+ };
116
+ var existsOperator = (value) => {
117
+ let includeOp = false;
118
+ for (const op in OPERATORS) {
119
+ if (includeOp) continue;
120
+ includeOp = import_lodash.default.includes(JSON.stringify(value), op);
121
+ }
122
+ return includeOp;
123
+ };
124
+ var converter = (operator) => {
125
+ operator = import_lodash.default.isObject(operator) ? operator : JSON.parse(operator || "{}");
126
+ let convertion = operator;
127
+ for (const op in OPERATORS) {
128
+ if (!operator[op] && operator[op] !== 0) continue;
129
+ if (import_lodash.default.isObject(operator[op]) && import_lodash.default.isArray(operator[op])) {
130
+ import_lodash.default.forEach(operator[op], (condition, index) => {
131
+ if (!existsOperator(condition)) return;
132
+ for (const opSecondary in OPERATORS) {
133
+ if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
134
+ operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
135
+ }
136
+ });
137
+ }
138
+ convertion = OPERATORS[op](operator[op]);
139
+ }
140
+ return convertion;
141
+ };
142
+ var refactorWhere = (where) => {
143
+ for (const key in where) {
144
+ let includeOp = existsOperator(where[key]);
145
+ if (includeOp) {
146
+ where[key] = converter(where[key]);
147
+ if (import_lodash.default.isObject(where[key]) && !import_lodash.default.isArray(where[key])) {
148
+ for (const keyObject in where[key]) {
149
+ includeOp = existsOperator(where[key][keyObject]);
150
+ if (!includeOp) continue;
151
+ where[key][keyObject] = converter(where[key][keyObject]);
152
+ }
153
+ }
154
+ if (import_lodash.default.isObject(where[key]) && import_lodash.default.isArray(where[key])) {
155
+ import_lodash.default.forEach(where[key], (condition, index) => {
156
+ includeOp = existsOperator(condition);
157
+ if (includeOp) {
158
+ condition = converter(condition);
159
+ if (import_lodash.default.isObject(condition) && !import_lodash.default.isArray(condition)) {
160
+ for (const keyObject in condition) {
161
+ includeOp = existsOperator(condition[keyObject]);
162
+ if (!includeOp) continue;
163
+ condition[keyObject] = converter(condition[keyObject]);
164
+ }
165
+ }
166
+ }
167
+ where[key][index] = condition;
168
+ });
169
+ }
170
+ }
171
+ }
172
+ return where;
173
+ };
174
+
175
+ // src/tools/paginate.ts
176
+ var import_lodash2 = __toESM(require("lodash"));
177
+ var parse = (json) => {
178
+ if (!json) return;
179
+ if (import_lodash2.default.isString(json)) return JSON.parse(json);
180
+ return json;
181
+ };
182
+ var paginate = async (repository, options) => {
183
+ let where = parse(options.filters);
184
+ if (where) where = refactorWhere(where);
185
+ const [data, count] = await repository.findAndCount({
186
+ select: options.fields,
187
+ relations: options.relations,
188
+ where,
189
+ order: parse(options.order),
190
+ take: options.limit,
191
+ skip: Number(options.page) * (Number(options.limit) || 0) || 0
192
+ });
193
+ return {
194
+ totalPage: Math.ceil(count / options.limit),
195
+ total: count,
196
+ content: data
197
+ };
198
+ };
199
+
200
+ // src/tools/moduler.ts
201
+ var import_path = __toESM(require("path"));
202
+ var import_fs = __toESM(require("fs"));
203
+ var moduler = () => {
204
+ let content = {};
205
+ import_fs.default.readdirSync(__dirname).forEach((file) => {
206
+ if (file === "index.ts" || !file.endsWith(".ts")) return;
207
+ const filePath = import_path.default.join(__dirname, file);
208
+ const module2 = require(filePath);
209
+ content = { ...content, ...module2 };
210
+ });
211
+ return content;
212
+ };
213
+
92
214
  // src/router/health.ts
93
215
  var healthRouter = (router, options, done) => {
94
216
  router.get("/", async (req, res) => {
@@ -135,20 +257,20 @@ var serverInit = (appConfig) => {
135
257
  };
136
258
  app.listen(listenOptions, (err, address) => {
137
259
  if (err) {
138
- console.error("Error in server initialization!", err);
260
+ console.error("\u{1F534} Error in server initialization!", err);
139
261
  process.exit(1);
140
262
  }
141
- console.log(`Server has been started in ${address}...`);
263
+ console.log(`\u{1F680} Server has been started in ${address}...`);
142
264
  });
143
265
  return app;
144
266
  };
145
267
 
146
268
  // src/router/api.ts
147
- var import_lodash = __toESM(require("lodash"));
269
+ var import_lodash3 = __toESM(require("lodash"));
148
270
  var apiRouter = (registers) => {
149
271
  const apiPrefix = "api";
150
272
  app.register((router, options, done) => {
151
- import_lodash.default.forEach(registers, (register, prefix) => {
273
+ import_lodash3.default.forEach(registers, (register, prefix) => {
152
274
  router.register(register, { prefix: `${apiPrefix}/${prefix}` });
153
275
  });
154
276
  done();
@@ -162,10 +284,16 @@ var mainRouter = (router, options, done) => {
162
284
  };
163
285
  // Annotate the CommonJS export names for ESM import in node:
164
286
  0 && (module.exports = {
287
+ OPERATORS,
165
288
  apiRouter,
166
289
  app,
290
+ converter,
167
291
  databaseInit,
292
+ existsOperator,
168
293
  mainRouter,
294
+ moduler,
295
+ paginate,
296
+ refactorWhere,
169
297
  responseWrapper,
170
298
  serverInit
171
299
  });
package/dist/index.mjs CHANGED
@@ -1,3 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
1
8
  // src/database/index.ts
2
9
  import "dotenv/config";
3
10
  import { hostname } from "os";
@@ -29,7 +36,7 @@ var databaseInit = (entities) => {
29
36
  applicationName: hostname(),
30
37
  entities
31
38
  });
32
- DB.initialize().then(() => console.log("Data Source has been initialized!")).catch((err) => console.error("Error during Data Source initialization", err));
39
+ 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
40
  return DB;
34
41
  };
35
42
 
@@ -48,6 +55,133 @@ var responseWrapper = async (res, promise) => {
48
55
  });
49
56
  };
50
57
 
58
+ // src/tools/operators.ts
59
+ import {
60
+ Like,
61
+ MoreThan,
62
+ MoreThanOrEqual,
63
+ In,
64
+ Not,
65
+ LessThan,
66
+ Between,
67
+ Raw,
68
+ And,
69
+ LessThanOrEqual
70
+ } from "typeorm";
71
+ import _ from "lodash";
72
+ var OPERATORS = {
73
+ $in: (value) => In(value),
74
+ $and: (value) => And(value),
75
+ $notIn: (value) => Not(In(value)),
76
+ $like: (value) => Like(value),
77
+ $notLike: (value) => Not(Like(value)),
78
+ $gt: (value) => MoreThan(value),
79
+ $gte: (value) => MoreThanOrEqual(value),
80
+ $lt: (value) => LessThan(value),
81
+ $lte: (value) => LessThanOrEqual(value),
82
+ $between: ([from, to]) => Between(from, to),
83
+ $not: (value) => Not(value),
84
+ $ne: (value) => Not(value),
85
+ $raw: (value) => Raw((alias) => _.replace(value, "ALIAS", alias))
86
+ };
87
+ var existsOperator = (value) => {
88
+ let includeOp = false;
89
+ for (const op in OPERATORS) {
90
+ if (includeOp) continue;
91
+ includeOp = _.includes(JSON.stringify(value), op);
92
+ }
93
+ return includeOp;
94
+ };
95
+ var converter = (operator) => {
96
+ operator = _.isObject(operator) ? operator : JSON.parse(operator || "{}");
97
+ let convertion = operator;
98
+ for (const op in OPERATORS) {
99
+ if (!operator[op] && operator[op] !== 0) continue;
100
+ if (_.isObject(operator[op]) && _.isArray(operator[op])) {
101
+ _.forEach(operator[op], (condition, index) => {
102
+ if (!existsOperator(condition)) return;
103
+ for (const opSecondary in OPERATORS) {
104
+ if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
105
+ operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
106
+ }
107
+ });
108
+ }
109
+ convertion = OPERATORS[op](operator[op]);
110
+ }
111
+ return convertion;
112
+ };
113
+ var refactorWhere = (where) => {
114
+ for (const key in where) {
115
+ let includeOp = existsOperator(where[key]);
116
+ if (includeOp) {
117
+ where[key] = converter(where[key]);
118
+ if (_.isObject(where[key]) && !_.isArray(where[key])) {
119
+ for (const keyObject in where[key]) {
120
+ includeOp = existsOperator(where[key][keyObject]);
121
+ if (!includeOp) continue;
122
+ where[key][keyObject] = converter(where[key][keyObject]);
123
+ }
124
+ }
125
+ if (_.isObject(where[key]) && _.isArray(where[key])) {
126
+ _.forEach(where[key], (condition, index) => {
127
+ includeOp = existsOperator(condition);
128
+ if (includeOp) {
129
+ condition = converter(condition);
130
+ if (_.isObject(condition) && !_.isArray(condition)) {
131
+ for (const keyObject in condition) {
132
+ includeOp = existsOperator(condition[keyObject]);
133
+ if (!includeOp) continue;
134
+ condition[keyObject] = converter(condition[keyObject]);
135
+ }
136
+ }
137
+ }
138
+ where[key][index] = condition;
139
+ });
140
+ }
141
+ }
142
+ }
143
+ return where;
144
+ };
145
+
146
+ // src/tools/paginate.ts
147
+ import _2 from "lodash";
148
+ var parse = (json) => {
149
+ if (!json) return;
150
+ if (_2.isString(json)) return JSON.parse(json);
151
+ return json;
152
+ };
153
+ var paginate = async (repository, options) => {
154
+ let where = parse(options.filters);
155
+ if (where) where = refactorWhere(where);
156
+ const [data, count] = await repository.findAndCount({
157
+ select: options.fields,
158
+ relations: options.relations,
159
+ where,
160
+ order: parse(options.order),
161
+ take: options.limit,
162
+ skip: Number(options.page) * (Number(options.limit) || 0) || 0
163
+ });
164
+ return {
165
+ totalPage: Math.ceil(count / options.limit),
166
+ total: count,
167
+ content: data
168
+ };
169
+ };
170
+
171
+ // src/tools/moduler.ts
172
+ import path from "path";
173
+ import fs from "fs";
174
+ var moduler = () => {
175
+ let content = {};
176
+ fs.readdirSync(__dirname).forEach((file) => {
177
+ if (file === "index.ts" || !file.endsWith(".ts")) return;
178
+ const filePath = path.join(__dirname, file);
179
+ const module = __require(filePath);
180
+ content = { ...content, ...module };
181
+ });
182
+ return content;
183
+ };
184
+
51
185
  // src/router/health.ts
52
186
  var healthRouter = (router, options, done) => {
53
187
  router.get("/", async (req, res) => {
@@ -94,20 +228,20 @@ var serverInit = (appConfig) => {
94
228
  };
95
229
  app.listen(listenOptions, (err, address) => {
96
230
  if (err) {
97
- console.error("Error in server initialization!", err);
231
+ console.error("\u{1F534} Error in server initialization!", err);
98
232
  process.exit(1);
99
233
  }
100
- console.log(`Server has been started in ${address}...`);
234
+ console.log(`\u{1F680} Server has been started in ${address}...`);
101
235
  });
102
236
  return app;
103
237
  };
104
238
 
105
239
  // src/router/api.ts
106
- import _ from "lodash";
240
+ import _3 from "lodash";
107
241
  var apiRouter = (registers) => {
108
242
  const apiPrefix = "api";
109
243
  app.register((router, options, done) => {
110
- _.forEach(registers, (register, prefix) => {
244
+ _3.forEach(registers, (register, prefix) => {
111
245
  router.register(register, { prefix: `${apiPrefix}/${prefix}` });
112
246
  });
113
247
  done();
@@ -120,10 +254,16 @@ var mainRouter = (router, options, done) => {
120
254
  done();
121
255
  };
122
256
  export {
257
+ OPERATORS,
123
258
  apiRouter,
124
259
  app,
260
+ converter,
125
261
  databaseInit,
262
+ existsOperator,
126
263
  mainRouter,
264
+ moduler,
265
+ paginate,
266
+ refactorWhere,
127
267
  responseWrapper,
128
268
  serverInit
129
269
  };
@@ -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 {
@@ -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 import_lodash = __toESM(require("lodash"));
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
- import_lodash.default.forEach(registers, (register, prefix) => {
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 _ from "lodash";
53
+ import _3 from "lodash";
36
54
  var apiRouter = (registers) => {
37
55
  const apiPrefix = "api";
38
56
  app.register((router, options, done) => {
39
- _.forEach(registers, (register, prefix) => {
57
+ _3.forEach(registers, (register, prefix) => {
40
58
  router.register(register, { prefix: `${apiPrefix}/${prefix}` });
41
59
  });
42
60
  done();
@@ -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 import_lodash = __toESM(require("lodash"));
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(`Server has been started in ${address}...`);
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 _ from "lodash";
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(`Server has been started in ${address}...`);
94
+ console.log(`\u{1F680} Server has been started in ${address}...`);
77
95
  });
78
96
  return app;
79
97
  };
@@ -2,4 +2,28 @@ import { FastifyReply } from 'fastify';
2
2
 
3
3
  declare const responseWrapper: (res: FastifyReply, promise: Promise<any>) => Promise<void>;
4
4
 
5
- export { responseWrapper };
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
+ declare const moduler: () => Record<string, any>;
28
+
29
+ export { OPERATORS, converter, existsOperator, moduler, paginate, refactorWhere, responseWrapper };
@@ -2,4 +2,28 @@ import { FastifyReply } from 'fastify';
2
2
 
3
3
  declare const responseWrapper: (res: FastifyReply, promise: Promise<any>) => Promise<void>;
4
4
 
5
- export { responseWrapper };
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
+ declare const moduler: () => Record<string, any>;
28
+
29
+ export { OPERATORS, converter, existsOperator, moduler, paginate, refactorWhere, responseWrapper };
@@ -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,25 @@ 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
+ moduler: () => moduler,
37
+ paginate: () => paginate,
38
+ refactorWhere: () => refactorWhere,
23
39
  responseWrapper: () => responseWrapper
24
40
  });
25
41
  module.exports = __toCommonJS(tools_exports);
@@ -38,7 +54,129 @@ var responseWrapper = async (res, promise) => {
38
54
  return res.status(status).send({ message: err.message, type: err.type, code: err.code });
39
55
  });
40
56
  };
57
+
58
+ // src/tools/operators.ts
59
+ var import_typeorm = require("typeorm");
60
+ var import_lodash = __toESM(require("lodash"));
61
+ var OPERATORS = {
62
+ $in: (value) => (0, import_typeorm.In)(value),
63
+ $and: (value) => (0, import_typeorm.And)(value),
64
+ $notIn: (value) => (0, import_typeorm.Not)((0, import_typeorm.In)(value)),
65
+ $like: (value) => (0, import_typeorm.Like)(value),
66
+ $notLike: (value) => (0, import_typeorm.Not)((0, import_typeorm.Like)(value)),
67
+ $gt: (value) => (0, import_typeorm.MoreThan)(value),
68
+ $gte: (value) => (0, import_typeorm.MoreThanOrEqual)(value),
69
+ $lt: (value) => (0, import_typeorm.LessThan)(value),
70
+ $lte: (value) => (0, import_typeorm.LessThanOrEqual)(value),
71
+ $between: ([from, to]) => (0, import_typeorm.Between)(from, to),
72
+ $not: (value) => (0, import_typeorm.Not)(value),
73
+ $ne: (value) => (0, import_typeorm.Not)(value),
74
+ $raw: (value) => (0, import_typeorm.Raw)((alias) => import_lodash.default.replace(value, "ALIAS", alias))
75
+ };
76
+ var existsOperator = (value) => {
77
+ let includeOp = false;
78
+ for (const op in OPERATORS) {
79
+ if (includeOp) continue;
80
+ includeOp = import_lodash.default.includes(JSON.stringify(value), op);
81
+ }
82
+ return includeOp;
83
+ };
84
+ var converter = (operator) => {
85
+ operator = import_lodash.default.isObject(operator) ? operator : JSON.parse(operator || "{}");
86
+ let convertion = operator;
87
+ for (const op in OPERATORS) {
88
+ if (!operator[op] && operator[op] !== 0) continue;
89
+ if (import_lodash.default.isObject(operator[op]) && import_lodash.default.isArray(operator[op])) {
90
+ import_lodash.default.forEach(operator[op], (condition, index) => {
91
+ if (!existsOperator(condition)) return;
92
+ for (const opSecondary in OPERATORS) {
93
+ if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
94
+ operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
95
+ }
96
+ });
97
+ }
98
+ convertion = OPERATORS[op](operator[op]);
99
+ }
100
+ return convertion;
101
+ };
102
+ var refactorWhere = (where) => {
103
+ for (const key in where) {
104
+ let includeOp = existsOperator(where[key]);
105
+ if (includeOp) {
106
+ where[key] = converter(where[key]);
107
+ if (import_lodash.default.isObject(where[key]) && !import_lodash.default.isArray(where[key])) {
108
+ for (const keyObject in where[key]) {
109
+ includeOp = existsOperator(where[key][keyObject]);
110
+ if (!includeOp) continue;
111
+ where[key][keyObject] = converter(where[key][keyObject]);
112
+ }
113
+ }
114
+ if (import_lodash.default.isObject(where[key]) && import_lodash.default.isArray(where[key])) {
115
+ import_lodash.default.forEach(where[key], (condition, index) => {
116
+ includeOp = existsOperator(condition);
117
+ if (includeOp) {
118
+ condition = converter(condition);
119
+ if (import_lodash.default.isObject(condition) && !import_lodash.default.isArray(condition)) {
120
+ for (const keyObject in condition) {
121
+ includeOp = existsOperator(condition[keyObject]);
122
+ if (!includeOp) continue;
123
+ condition[keyObject] = converter(condition[keyObject]);
124
+ }
125
+ }
126
+ }
127
+ where[key][index] = condition;
128
+ });
129
+ }
130
+ }
131
+ }
132
+ return where;
133
+ };
134
+
135
+ // src/tools/paginate.ts
136
+ var import_lodash2 = __toESM(require("lodash"));
137
+ var parse = (json) => {
138
+ if (!json) return;
139
+ if (import_lodash2.default.isString(json)) return JSON.parse(json);
140
+ return json;
141
+ };
142
+ var paginate = async (repository, options) => {
143
+ let where = parse(options.filters);
144
+ if (where) where = refactorWhere(where);
145
+ const [data, count] = await repository.findAndCount({
146
+ select: options.fields,
147
+ relations: options.relations,
148
+ where,
149
+ order: parse(options.order),
150
+ take: options.limit,
151
+ skip: Number(options.page) * (Number(options.limit) || 0) || 0
152
+ });
153
+ return {
154
+ totalPage: Math.ceil(count / options.limit),
155
+ total: count,
156
+ content: data
157
+ };
158
+ };
159
+
160
+ // src/tools/moduler.ts
161
+ var import_path = __toESM(require("path"));
162
+ var import_fs = __toESM(require("fs"));
163
+ var moduler = () => {
164
+ let content = {};
165
+ import_fs.default.readdirSync(__dirname).forEach((file) => {
166
+ if (file === "index.ts" || !file.endsWith(".ts")) return;
167
+ const filePath = import_path.default.join(__dirname, file);
168
+ const module2 = require(filePath);
169
+ content = { ...content, ...module2 };
170
+ });
171
+ return content;
172
+ };
41
173
  // Annotate the CommonJS export names for ESM import in node:
42
174
  0 && (module.exports = {
175
+ OPERATORS,
176
+ converter,
177
+ existsOperator,
178
+ moduler,
179
+ paginate,
180
+ refactorWhere,
43
181
  responseWrapper
44
182
  });
@@ -1,3 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
1
8
  // src/tools/response.ts
2
9
  var responseWrapper = async (res, promise) => {
3
10
  return promise.then((data) => {
@@ -12,6 +19,139 @@ var responseWrapper = async (res, promise) => {
12
19
  return res.status(status).send({ message: err.message, type: err.type, code: err.code });
13
20
  });
14
21
  };
22
+
23
+ // src/tools/operators.ts
24
+ import {
25
+ Like,
26
+ MoreThan,
27
+ MoreThanOrEqual,
28
+ In,
29
+ Not,
30
+ LessThan,
31
+ Between,
32
+ Raw,
33
+ And,
34
+ LessThanOrEqual
35
+ } from "typeorm";
36
+ import _ from "lodash";
37
+ var OPERATORS = {
38
+ $in: (value) => In(value),
39
+ $and: (value) => And(value),
40
+ $notIn: (value) => Not(In(value)),
41
+ $like: (value) => Like(value),
42
+ $notLike: (value) => Not(Like(value)),
43
+ $gt: (value) => MoreThan(value),
44
+ $gte: (value) => MoreThanOrEqual(value),
45
+ $lt: (value) => LessThan(value),
46
+ $lte: (value) => LessThanOrEqual(value),
47
+ $between: ([from, to]) => Between(from, to),
48
+ $not: (value) => Not(value),
49
+ $ne: (value) => Not(value),
50
+ $raw: (value) => Raw((alias) => _.replace(value, "ALIAS", alias))
51
+ };
52
+ var existsOperator = (value) => {
53
+ let includeOp = false;
54
+ for (const op in OPERATORS) {
55
+ if (includeOp) continue;
56
+ includeOp = _.includes(JSON.stringify(value), op);
57
+ }
58
+ return includeOp;
59
+ };
60
+ var converter = (operator) => {
61
+ operator = _.isObject(operator) ? operator : JSON.parse(operator || "{}");
62
+ let convertion = operator;
63
+ for (const op in OPERATORS) {
64
+ if (!operator[op] && operator[op] !== 0) continue;
65
+ if (_.isObject(operator[op]) && _.isArray(operator[op])) {
66
+ _.forEach(operator[op], (condition, index) => {
67
+ if (!existsOperator(condition)) return;
68
+ for (const opSecondary in OPERATORS) {
69
+ if (!condition[opSecondary] && condition[opSecondary] !== 0) continue;
70
+ operator[op][index] = OPERATORS[opSecondary](condition[opSecondary]);
71
+ }
72
+ });
73
+ }
74
+ convertion = OPERATORS[op](operator[op]);
75
+ }
76
+ return convertion;
77
+ };
78
+ var refactorWhere = (where) => {
79
+ for (const key in where) {
80
+ let includeOp = existsOperator(where[key]);
81
+ if (includeOp) {
82
+ where[key] = converter(where[key]);
83
+ if (_.isObject(where[key]) && !_.isArray(where[key])) {
84
+ for (const keyObject in where[key]) {
85
+ includeOp = existsOperator(where[key][keyObject]);
86
+ if (!includeOp) continue;
87
+ where[key][keyObject] = converter(where[key][keyObject]);
88
+ }
89
+ }
90
+ if (_.isObject(where[key]) && _.isArray(where[key])) {
91
+ _.forEach(where[key], (condition, index) => {
92
+ includeOp = existsOperator(condition);
93
+ if (includeOp) {
94
+ condition = converter(condition);
95
+ if (_.isObject(condition) && !_.isArray(condition)) {
96
+ for (const keyObject in condition) {
97
+ includeOp = existsOperator(condition[keyObject]);
98
+ if (!includeOp) continue;
99
+ condition[keyObject] = converter(condition[keyObject]);
100
+ }
101
+ }
102
+ }
103
+ where[key][index] = condition;
104
+ });
105
+ }
106
+ }
107
+ }
108
+ return where;
109
+ };
110
+
111
+ // src/tools/paginate.ts
112
+ import _2 from "lodash";
113
+ var parse = (json) => {
114
+ if (!json) return;
115
+ if (_2.isString(json)) return JSON.parse(json);
116
+ return json;
117
+ };
118
+ var paginate = async (repository, options) => {
119
+ let where = parse(options.filters);
120
+ if (where) where = refactorWhere(where);
121
+ const [data, count] = await repository.findAndCount({
122
+ select: options.fields,
123
+ relations: options.relations,
124
+ where,
125
+ order: parse(options.order),
126
+ take: options.limit,
127
+ skip: Number(options.page) * (Number(options.limit) || 0) || 0
128
+ });
129
+ return {
130
+ totalPage: Math.ceil(count / options.limit),
131
+ total: count,
132
+ content: data
133
+ };
134
+ };
135
+
136
+ // src/tools/moduler.ts
137
+ import path from "path";
138
+ import fs from "fs";
139
+ var moduler = () => {
140
+ let content = {};
141
+ fs.readdirSync(__dirname).forEach((file) => {
142
+ if (file === "index.ts" || !file.endsWith(".ts")) return;
143
+ const filePath = path.join(__dirname, file);
144
+ const module = __require(filePath);
145
+ content = { ...content, ...module };
146
+ });
147
+ return content;
148
+ };
15
149
  export {
150
+ OPERATORS,
151
+ converter,
152
+ existsOperator,
153
+ moduler,
154
+ paginate,
155
+ refactorWhere,
16
156
  responseWrapper
17
157
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gustavo-valsechi/node",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",