jcc-express-mvc 1.2.5 → 1.2.6

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.
Files changed (49) hide show
  1. package/dist/Database/src/BaseEntity.js +208 -0
  2. package/dist/Database/src/BaseModel.js +44 -0
  3. package/dist/Database/src/Builder.js +430 -0
  4. package/dist/Database/src/Cast/index.js +98 -0
  5. package/dist/Database/src/Cast/types.js +25 -0
  6. package/dist/Database/src/Date/index.js +36 -0
  7. package/dist/Database/src/Model.js +53 -0
  8. package/dist/Database/src/Query.js +16 -0
  9. package/dist/Database/src/QueryBuilder.js +354 -0
  10. package/dist/Database/src/QueryInstance.js +28 -0
  11. package/dist/Database/src/RelationBuilder.js +97 -0
  12. package/dist/Database/src/Schema/BaseSchemaEntity/index.js +26 -0
  13. package/dist/Database/src/Schema/BluePrint/index.js +257 -0
  14. package/dist/Database/src/Schema/index.js +18 -0
  15. package/dist/Database/src/db/connection.js +22 -0
  16. package/dist/Database/src/type.js +2 -0
  17. package/dist/Database/src/utils/index.js +112 -0
  18. package/dist/index.js +30 -0
  19. package/dist/lib/App.js +46 -0
  20. package/dist/lib/Auth/AuthMiddleware.js +43 -0
  21. package/dist/lib/Auth/index.js +94 -0
  22. package/dist/lib/Config/Config.js +17 -0
  23. package/dist/lib/Error/AppError.js +12 -0
  24. package/dist/lib/Error/AppErrorHandler.js +35 -0
  25. package/dist/lib/Error/Constants/error.js +15 -0
  26. package/dist/lib/Error/Constants/index.js +6 -0
  27. package/dist/lib/HttpKernel/HttpKernel.js +12 -0
  28. package/dist/lib/Interface/index.js +2 -0
  29. package/dist/lib/Middlewares/index.js +109 -0
  30. package/dist/lib/Middlewares/isLogin.js +29 -0
  31. package/dist/lib/Passport/config.js +69 -0
  32. package/dist/lib/Request/FormRequest.js +51 -0
  33. package/dist/lib/Request/request.js +36 -0
  34. package/dist/lib/Response/index.js +46 -0
  35. package/dist/lib/Routes/RouteBuilder.js +90 -0
  36. package/dist/lib/Routes/Router.js +95 -0
  37. package/dist/lib/Server/index.js +45 -0
  38. package/dist/lib/Services/ServiceContainer.js +30 -0
  39. package/dist/lib/Services/ServiceProvider.js +49 -0
  40. package/dist/lib/Templating-engine/engineHelper.js +127 -0
  41. package/dist/lib/Templating-engine/expressions.js +20 -0
  42. package/dist/lib/Templating-engine/index.js +228 -0
  43. package/dist/lib/Type/index.js +2 -0
  44. package/dist/lib/Validation/index.js +76 -0
  45. package/dist/lib/Validation/rules.js +72 -0
  46. package/dist/lib/Validation/validate.js +312 -0
  47. package/dist/lib/util/index.js +165 -0
  48. package/lib/Services/ServiceContainer.ts +3 -3
  49. package/package.json +4 -3
@@ -0,0 +1,208 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseEntity = void 0;
4
+ const connection_1 = require("./db/connection");
5
+ const QueryInstance_1 = require("./QueryInstance");
6
+ const Cast_1 = require("./Cast");
7
+ class BaseEntity extends connection_1.Database {
8
+ constructor() {
9
+ super(...arguments);
10
+ this.attributes = {};
11
+ this._select = "*";
12
+ this._tableName = "";
13
+ this._where = [];
14
+ this._joins = [];
15
+ this._distinct = false;
16
+ this._insertData = {
17
+ keys: [],
18
+ values: [],
19
+ };
20
+ this._updateData = {}; //{ keys: Array<any>; values: Array<any> };
21
+ this._relationships = [];
22
+ this.fillable = [];
23
+ this.guard = [];
24
+ this.hidden = [];
25
+ this._max = "";
26
+ this._min = "";
27
+ this._sum = "";
28
+ this._avg = "";
29
+ this.sqlQuery = "";
30
+ }
31
+ /**
32
+ * Resets all query builder state variables to their default values.
33
+ */
34
+ _resetQuery() {
35
+ this._select = "*";
36
+ this._tableName = "";
37
+ this._where = [];
38
+ this._joins = [];
39
+ this._orderBy = "";
40
+ this._limit = "";
41
+ this._offset = "";
42
+ this._groupBy = "";
43
+ this._having = "";
44
+ this._distinct = false;
45
+ this._insertData = { keys: [], values: [] };
46
+ this._updateData = {};
47
+ this._delete = false;
48
+ this._count = false;
49
+ this._relationships = [];
50
+ this._max = "";
51
+ this._min = "";
52
+ this._sum = "";
53
+ this._avg = "";
54
+ return;
55
+ }
56
+ /**
57
+ * Constructs and returns the SQL query based on the current query builder state.
58
+ *
59
+ * @returns {string} The generated SQL query.
60
+ */
61
+ compileToSql() {
62
+ if (this._count) {
63
+ this.sqlQuery += this.buildCount();
64
+ }
65
+ else if (this._sum) {
66
+ this.sqlQuery += this.buildSum();
67
+ }
68
+ else if (this._avg) {
69
+ this.sqlQuery += this.buildAvg();
70
+ }
71
+ else if (this._max) {
72
+ this.sqlQuery += this.buildMax();
73
+ }
74
+ else if (this._min) {
75
+ this.sqlQuery += this.buildMin();
76
+ }
77
+ else if (Object.values(this._insertData.keys).length > 1) {
78
+ this.sqlQuery += this.buidInsert();
79
+ }
80
+ else if (Object.values(this._updateData).length > 1) {
81
+ this.sqlQuery += this.buildUpdate();
82
+ }
83
+ else if (this._delete) {
84
+ this.sqlQuery += this.buildDelete();
85
+ }
86
+ else {
87
+ this.sqlQuery += this.buildselect();
88
+ }
89
+ if (this._joins.length > 0) {
90
+ this.sqlQuery += this.buildJoin();
91
+ }
92
+ if (this._where.length > 0) {
93
+ this.sqlQuery += this.buildWhere();
94
+ }
95
+ if (this._groupBy) {
96
+ this.sqlQuery += this.buildGroup();
97
+ }
98
+ if (this._having) {
99
+ this.sqlQuery += this.buildHaving();
100
+ }
101
+ if (this._orderBy) {
102
+ this.sqlQuery += this.buildOrderBy();
103
+ }
104
+ if (this._limit) {
105
+ this.sqlQuery += this.buildLimit();
106
+ }
107
+ if (this._offset) {
108
+ this.sqlQuery += this.buildOffset();
109
+ }
110
+ }
111
+ buidInsert() {
112
+ return `INSERT INTO ${this._tableName} (${this._insertData.keys.join(",")}) VALUES (${this._insertData.values.join(", ")})`;
113
+ }
114
+ buildUpdate() {
115
+ let updateString = Object.keys(this._updateData)
116
+ .map((key) => `${key}='${this._updateData[key]}'`)
117
+ .join(", ");
118
+ return `UPDATE ${this._tableName} SET ${updateString}`;
119
+ }
120
+ buildJoin() {
121
+ return this._joins
122
+ .map((join) => {
123
+ ` ${join.type} JOIN ${join.table} ON ${join.firstColumn} ${join.operator} ${join.secondColumn}`;
124
+ })
125
+ .join();
126
+ }
127
+ // private buildSelect(): string {}
128
+ buildOffset() {
129
+ return ` OFFSET ${this._offset}`;
130
+ }
131
+ buildselect() {
132
+ return `SELECT ${this._distinct ? "DISTINCT " : ""}${this._select} FROM ${this._tableName}`;
133
+ }
134
+ buildDelete() {
135
+ return `DELETE FROM ${this._tableName}`;
136
+ }
137
+ buildWhere() {
138
+ return (" WHERE " +
139
+ this._where
140
+ .map((condition, index) => {
141
+ const connector = index > 0 && condition.connector
142
+ ? ` ${condition.connector} `
143
+ : index > 0
144
+ ? " AND "
145
+ : "";
146
+ return `${connector}${condition.column} ${condition.operator} '${condition.value}'`;
147
+ })
148
+ .join(""));
149
+ }
150
+ buildCount() {
151
+ return `SELECT COUNT(*) as count FROM ${this._tableName}`;
152
+ }
153
+ buildMin() {
154
+ return `SELECT MIN(${this._min}) FROM ${this._tableName}`;
155
+ }
156
+ buildMax() {
157
+ return `SELECT MAX(${this._max}) FROM ${this._tableName} `;
158
+ }
159
+ buildAvg() {
160
+ return `SELECT MAX(${this._avg}) FROM ${this._tableName} `;
161
+ }
162
+ buildSum() {
163
+ return `SELECT SUM(${this._sum}) FROM ${this._tableName} `;
164
+ }
165
+ buildHaving() {
166
+ return ` HAVING ${this._having}`;
167
+ }
168
+ buildGroup() {
169
+ return ` GROUP BY ${this._groupBy}`;
170
+ }
171
+ buildOrderBy() {
172
+ return ` ORDER BY ${this._orderBy}`;
173
+ }
174
+ buildLimit() {
175
+ return ` LIMIT ${this._limit}`;
176
+ }
177
+ getCompileSqlCode() {
178
+ this.compileToSql();
179
+ // this._resetQuery();
180
+ let sql = this.sqlQuery;
181
+ this.sqlQuery = "";
182
+ return sql;
183
+ }
184
+ setGuard(values) {
185
+ this.guard = values;
186
+ }
187
+ setFillable(values) {
188
+ this.fillable = values;
189
+ }
190
+ setHidden(values) {
191
+ this.hidden = values;
192
+ }
193
+ setCasts(values) {
194
+ this.casts = values;
195
+ }
196
+ removeHiddenValues(item) {
197
+ const keysSet = new Set(this.hidden);
198
+ const newItem = { ...item };
199
+ for (const key of keysSet) {
200
+ delete newItem[key];
201
+ }
202
+ return newItem;
203
+ }
204
+ newInstance(results) {
205
+ return results.map((result) => new QueryInstance_1.QueryInstance(Cast_1.Cast.getCastType(this.casts || {}, this.removeHiddenValues(result))));
206
+ }
207
+ }
208
+ exports.BaseEntity = BaseEntity;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseModel = void 0;
4
+ const QueryBuilder_1 = require("./QueryBuilder");
5
+ const utils_1 = require("./utils");
6
+ class BaseModel extends QueryBuilder_1.QueryBuilder {
7
+ /**
8
+ * Defines a hasMany relationship between the current model and another model.
9
+ *
10
+ * @param {string} relatedModel - The name of the related model.
11
+ * @param {string} foreingKey - The foreign key in the related model (defaults to id).
12
+ * @param {string} localKey - The local key in the current model (defaults to 'id').
13
+ * @returns {Function} A function that retrieves the related data based on the relationship.
14
+ */
15
+ hasMany(relatedModel, foreingKey, localKey = "id") {
16
+ return async (query = {}) => {
17
+ const result = (0, utils_1.extractFirst)(query);
18
+ const value = result[localKey];
19
+ return {
20
+ results: this.select().table(relatedModel).where(foreingKey, value),
21
+ type: "hasMany",
22
+ };
23
+ };
24
+ }
25
+ /**
26
+ * Defines a belongsTo relationship between the current model and another model.
27
+ *
28
+ * @param {string} relatedModel - The name of the related model.
29
+ * @param {string} foreingKey - The foreign key in the related model (defaults to id).
30
+ * @param {string} localKey - The local key in the current model (defaults to 'id').
31
+ * @returns {Function} A function that retrieves the related data based on the relationship.
32
+ */
33
+ belongsTo(relatedModel, foreingKey, localKey = "id") {
34
+ return async (query = {}) => {
35
+ const result = (0, utils_1.extractFirst)(query);
36
+ const value = result[foreingKey];
37
+ return {
38
+ results: this.select().table(relatedModel).where(localKey, value),
39
+ type: "belongsTo",
40
+ };
41
+ };
42
+ }
43
+ }
44
+ exports.BaseModel = BaseModel;
@@ -0,0 +1,430 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Builder = void 0;
4
+ const RelationBuilder_1 = require("./RelationBuilder");
5
+ const utils_1 = require("./utils");
6
+ class Builder extends RelationBuilder_1.RelationBuilder {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.guard = [];
10
+ }
11
+ /**
12
+ * Specifies the columns to select in a query.
13
+ *
14
+ * @param {...string} columns - The columns to select.
15
+ * @returns {this} The instance of the model, allowing for method chaining.
16
+ */
17
+ select(...columns) {
18
+ this._select = columns.length > 0 ? columns.join(", ") : "*";
19
+ return this;
20
+ }
21
+ /**
22
+ * Marks the query to return distinct results.
23
+ *
24
+ * @returns {this} The instance of the model, allowing for method chaining.
25
+ */
26
+ distinct() {
27
+ this._distinct = true;
28
+ return this;
29
+ }
30
+ /**
31
+ * Specifies the offset for starting the query results.
32
+ *
33
+ * @param {number} count - The number of rows to skip before starting to return rows.
34
+ * @returns {this} The instance of the model, allowing for method chaining.
35
+ */
36
+ offset(value) {
37
+ this._offset = value;
38
+ return this;
39
+ }
40
+ /**
41
+ * Specifies the table to query from.
42
+ *
43
+ * @param {string} tableName - The name of the table to query from.
44
+ * @returns {this} The instance of the model, allowing for method chaining.
45
+ */
46
+ from(tableName) {
47
+ this._tableName = (0, utils_1.pluralize)(tableName);
48
+ return this;
49
+ }
50
+ /**
51
+ * Adds a WHERE clause to the query.
52
+ *
53
+ * @param {string} column - The column to apply the condition on.
54
+ * @param {string} operator - The operator to use for the condition (e.g., '=', '>', '<').
55
+ * @param {any} value - The value to compare the column against.
56
+ * @returns {this} The instance of the model, allowing for method chaining.
57
+ */
58
+ where(column, operator, value) {
59
+ if (!value) {
60
+ value = operator;
61
+ operator = "=";
62
+ }
63
+ this._where.push({ column, operator, value });
64
+ return this;
65
+ }
66
+ /**
67
+ * Inserts data into the database and retrieves the inserted record.
68
+ *
69
+ * @param {Object} data - The data to insert into the database.
70
+ * @returns {Promise<any>} The inserted record.
71
+ */
72
+ async insert(data, fillable, guard) {
73
+ try {
74
+ const tableName = this._tableName || this.constructor.name;
75
+ this.setFillable(fillable);
76
+ this.setGuard(guard);
77
+ const { keys, values } = (0, utils_1.transformData)(data, fillable, guard);
78
+ this._insertData = { keys, values };
79
+ const result = await this.runQuery(this.getCompileSqlCode());
80
+ this._resetQuery();
81
+ return this.select()
82
+ .table(tableName)
83
+ .where("id", result.insertId)
84
+ .first();
85
+ }
86
+ catch (error) {
87
+ throw new Error(error?.message);
88
+ }
89
+ }
90
+ /**
91
+ * Adds an OR WHERE clause to the query.
92
+ *
93
+ * @param {string} column - The column to apply the condition on.
94
+ * @param {string} operator - The operator to use for the condition (e.g., '=', '>', '<').
95
+ * @param {any} value - The value to compare the column against.
96
+ * @returns {this} The instance of the model, allowing for method chaining.
97
+ */
98
+ orWhere(column, operator, value) {
99
+ if (arguments.length === 2) {
100
+ value = operator;
101
+ operator = "=";
102
+ }
103
+ this._where.push({ column, operator, value, connector: "OR" });
104
+ return this;
105
+ }
106
+ /**
107
+ * Specifies the column and direction for ordering the query results.
108
+ *
109
+ * @param {string} column - The column to order by.
110
+ * @param {string} [direction="asc"] - The direction of ordering ("asc" for ascending, "desc" for descending).
111
+ * @returns {this} The instance of the model, allowing for method chaining.
112
+ */
113
+ orderBy(column, direction = "asc") {
114
+ this._orderBy = `${column} ${direction.toUpperCase()}`;
115
+ return this;
116
+ }
117
+ /**
118
+ * Limits the number of results returned by the query.
119
+ *
120
+ * @param {number} count - The maximum number of results to return.
121
+ * @returns {this} The instance of the model, allowing for method chaining.
122
+ */
123
+ limit(count) {
124
+ this._limit = count;
125
+ return this;
126
+ }
127
+ /**
128
+ * Limits the number of results returned by the query.
129
+ *
130
+ * @param {number} count - The maximum number of results to return.
131
+ * @returns {this} The instance of the model, allowing for method chaining.
132
+ */
133
+ take(count) {
134
+ this._limit = count;
135
+ return this;
136
+ }
137
+ /**
138
+ * Specifies the columns to group the query results by.
139
+ *
140
+ * @param {...string} columns - The columns to group by.
141
+ * @returns {this} The instance of the model, allowing for method chaining.
142
+ */
143
+ groupBy(...columns) {
144
+ this._groupBy = columns.join(", ");
145
+ return this;
146
+ }
147
+ /**
148
+ * Adds a HAVING clause to the query.
149
+ *
150
+ * @param {string} column - The column to apply the condition on.
151
+ * @param {string} operator - The operator to use for the condition (e.g., '=', '>', '<').
152
+ * @param {any} value - The value to compare the column against.
153
+ * @returns {this} The instance of the model, allowing for method chaining.
154
+ */
155
+ having(column, operator, value) {
156
+ this._having = `${column} ${operator} '${value}'`;
157
+ return this;
158
+ }
159
+ /**
160
+ * Adds a JOIN clause to the query.
161
+ *
162
+ * @param {string} table - The name of the table to join.
163
+ * @param {string} firstColumn - The column from the current table to join on.
164
+ * @param {string} operator - The operator to use for the join condition (e.g., '=', '>', '<').
165
+ * @param {string} secondColumn - The column from the joined table to join on.
166
+ * @returns {this} The instance of the model, allowing for method chaining.
167
+ */
168
+ join(type, table, firstColumn, operator, secondColumn) {
169
+ this._joins.push({
170
+ type,
171
+ table,
172
+ firstColumn,
173
+ operator,
174
+ secondColumn,
175
+ });
176
+ return this;
177
+ }
178
+ /**
179
+ * Adds an INNER JOIN clause to the query.
180
+ *
181
+ * @param {string} table - The name of the table to join.
182
+ * @param {string} firstColumn - The column from the current table to join on.
183
+ * @param {string} operator - The operator to use for the join condition (e.g., '=', '>', '<').
184
+ * @param {string} secondColumn - The column from the joined table to join on.
185
+ * @returns {this} The instance of the model, allowing for method chaining.
186
+ */
187
+ innerJoin(table, firstColumn, operator, secondColumn) {
188
+ this.join("INNER", table, firstColumn, operator, secondColumn);
189
+ return this;
190
+ }
191
+ /**
192
+ * Adds a LEFT JOIN clause to the query.
193
+ *
194
+ * @param {string} table - The name of the table to join.
195
+ * @param {string} firstColumn - The column from the current table to join on.
196
+ * @param {string} operator - The operator to use for the join condition (e.g., '=', '>', '<').
197
+ * @param {string} secondColumn - The column from the joined table to join on.
198
+ * @returns {this} The instance of the model, allowing for method chaining.
199
+ */
200
+ leftJoin(table, firstColumn, operator, secondColumn) {
201
+ this.join("LEFT", table, firstColumn, operator, secondColumn);
202
+ return this;
203
+ }
204
+ /**
205
+ * Adds a RIGHT JOIN clause to the query.
206
+ *
207
+ * @param {string} table - The name of the table to join.
208
+ * @param {string} firstColumn - The column from the current table to join on.
209
+ * @param {string} operator - The operator to use for the join condition (e.g., '=', '>', '<').
210
+ * @param {string} secondColumn - The column from the joined table to join on.
211
+ * @returns {this} The instance of the model, allowing for method chaining.
212
+ */
213
+ rightJoin(table, firstColumn, operator, secondColumn) {
214
+ this.join("RIGHT", table, firstColumn, operator, secondColumn);
215
+ return this;
216
+ }
217
+ /**
218
+ * Sets the data to update for the query.
219
+ *
220
+ * @param {Object} data - The data to update.
221
+ * @returns {Promise<any>} The instance of the model, allowing for method chaining.
222
+ */
223
+ async update(data = {}, fillable = [], guard = []) {
224
+ try {
225
+ const id = data["id"];
226
+ delete data["id"];
227
+ this.setFillable(fillable);
228
+ this.setGuard(guard);
229
+ this.where("id", id);
230
+ const tableName = this._tableName || this.constructor.name;
231
+ this._updateData = data;
232
+ // Object.keys(this.casts).length > 0 ? new Cast(this.casts, data) : data;
233
+ await this.runQuery(this.getCompileSqlCode());
234
+ return this.select().from(tableName).where("id", id).first();
235
+ }
236
+ catch (error) {
237
+ throw new Error(`${this.getCompileSqlCode()} \n ${error.message}`);
238
+ }
239
+ }
240
+ /**
241
+ * Fetch the first record from the database table.
242
+ * The table name is derived from the current class name or the previously set table name.
243
+ * @returns {Object} The first record if found, otherwise an empty object.
244
+ */
245
+ async first() {
246
+ try {
247
+ const tableName = this._tableName || this.constructor.name;
248
+ let query = this.table(tableName);
249
+ if (!this._limit) {
250
+ query.limit(1);
251
+ }
252
+ if (!this._orderBy) {
253
+ query.orderBy("id");
254
+ }
255
+ return (0, utils_1.extractFirst)(await query.get());
256
+ }
257
+ catch (error) {
258
+ throw new Error(error.message);
259
+ }
260
+ }
261
+ /**
262
+ * Executes the constructed query, loads specified relations, and resets the query builder state.
263
+ *
264
+ * @returns {Promise<Array>} A Promise that resolves with the fetched results including loaded relations.
265
+ */
266
+ async get() {
267
+ try {
268
+ const results = await this.runQuery(this.getCompileSqlCode());
269
+ this.addAttributes(results);
270
+ for (const relation of this._relationships) {
271
+ await this.loadRelations(this._tableName, relation, results);
272
+ }
273
+ this._resetQuery();
274
+ return this.newInstance(results);
275
+ }
276
+ catch (error) {
277
+ console.log(error.message);
278
+ throw new Error(this.getCompileSqlCode());
279
+ }
280
+ }
281
+ addGuard(columns) {
282
+ this.guard = columns;
283
+ }
284
+ addFillable(columns) {
285
+ this.fillable = columns;
286
+ }
287
+ addHidden(columns) {
288
+ this.hidden = columns;
289
+ }
290
+ // public addHidden(columns: Array<string>) {
291
+ // this.hidden = columns;
292
+ // }
293
+ latest(column = "created_at") {
294
+ this.orderBy(column, "desc");
295
+ return this;
296
+ }
297
+ table(table) {
298
+ this.from(table);
299
+ return this;
300
+ }
301
+ /**
302
+ * Adds the specified relationships to the model.
303
+ *
304
+ * @param {...string} relations - The names of the relationships to add.
305
+ * @returns {this} The instance of the model, allowing for method chaining.
306
+ */
307
+ with(...relations) {
308
+ this._relationships?.push(...relations);
309
+ return this;
310
+ }
311
+ /**
312
+ * Iterates over each record, applies a callback function to each record, and collects the results in an array.
313
+ *
314
+ * @param {Function} callback - The function to be applied to each record. The function should accept a record as an argument and return a value.
315
+ * @returns {Promise<Array>} A promise that resolves to an array of results from applying the callback function to each record.
316
+ */
317
+ async each(callback) {
318
+ const results = await this.get(); //(this.getCompileSqlCode())) || [];
319
+ for (let result of results) {
320
+ callback(result);
321
+ }
322
+ }
323
+ /**
324
+ * Iterates over map record returned by the query, applies a callback function to map record, and collects the results in an array.
325
+ *
326
+ * @param {Function} callback - The function to be applied to map record. The function should accept a record as an argument and return a value.
327
+ * @returns {Promise<Array>} A promise that resolves to an array of results from applying the callback function to each record.
328
+ */
329
+ async map(callback) {
330
+ const data = [];
331
+ const results = (await this.get()) || []; //(await this.runQuery(this.getCompileSqlCode())) || [];
332
+ for (let result of results) {
333
+ if (callback(result)) {
334
+ data.push(callback(result));
335
+ }
336
+ }
337
+ return data;
338
+ }
339
+ /**
340
+ * Retrieves the value of a specified field from the first record that matches the current query conditions.
341
+ *
342
+ * @param {string} field - The field name whose value is to be retrieved.
343
+ * @returns {Promise<any>} A promise that resolves to the value of the specified field, or null if no records are found.
344
+ */
345
+ async value(field) {
346
+ const result = await this.get(); //await this.runQuery(this.getCompileSqlCode());
347
+ const record = (0, utils_1.extractFirst)(result);
348
+ return record[field] || null;
349
+ }
350
+ /**
351
+ * Checks if any records exist that match the current query conditions.
352
+ *
353
+ * @returns {Promise<boolean>} A promise that resolves to true if records exist, false otherwise.
354
+ */
355
+ async exists() {
356
+ const result = await this.get();
357
+ return Array.isArray(result) && result.length >= 1;
358
+ }
359
+ /**
360
+ * Checks if no records exist that match the current query conditions.
361
+ *
362
+ * @returns {Promise<boolean>} A promise that resolves to true if no records exist, false otherwise.
363
+ */
364
+ async doesntExist() {
365
+ const result = await this.get(); //QueryBuilder.runQuery(QueryBuilder.getFinalQuery());
366
+ return !Array.isArray(result) || result.length < 1;
367
+ }
368
+ /**
369
+ * Executes a count query based on the current query builder state.
370
+ *
371
+ * @returns {Promise<number>} A Promise that resolves with the count of records.
372
+ */
373
+ async count() {
374
+ try {
375
+ this._count = true;
376
+ const record = await this.get();
377
+ return (0, utils_1.countValue)((0, utils_1.extractFirst)(record));
378
+ }
379
+ catch (error) {
380
+ throw new Error(error.message);
381
+ }
382
+ }
383
+ /**
384
+ * Executes a max query based on the current query builder state.
385
+ *
386
+ * @returns {Promise<number>} A Promise that resolves with the max of records.
387
+ */
388
+ async max(column) {
389
+ this._max = column;
390
+ const record = await this.get();
391
+ return (0, utils_1.countValue)((0, utils_1.extractFirst)(record));
392
+ return 0;
393
+ }
394
+ /**
395
+ * Executes a min query based on the current query builder state.
396
+ *
397
+ * @returns {Promise<number>} A Promise that resolves with the min of records.
398
+ */
399
+ async min(column) {
400
+ this._min = column;
401
+ const record = await this.get();
402
+ return (0, utils_1.countValue)((0, utils_1.extractFirst)(record));
403
+ return 0;
404
+ }
405
+ /**
406
+ * Executes a sum of a specify column query based on the current query builder state.
407
+ *
408
+ * @returns {Promise<number>} A Promise that resolves with the min of records.
409
+ */
410
+ async sum(column) {
411
+ this._sum = column;
412
+ const record = await this.get();
413
+ return (0, utils_1.countValue)((0, utils_1.extractFirst)(record));
414
+ }
415
+ /**
416
+ * Executes a average query based on the current query builder state.
417
+ *
418
+ * @returns {Promise<number>} A Promise that resolves with the sum of records.
419
+ */
420
+ async avg(column) {
421
+ this._avg = column;
422
+ const record = await this.get();
423
+ return (0, utils_1.countValue)((0, utils_1.extractFirst)(record));
424
+ }
425
+ addAttributes(record) {
426
+ return (this.attributes = { ...(0, utils_1.extractFirst)(record[record.length - 1]) });
427
+ }
428
+ }
429
+ exports.Builder = Builder;
430
+ Builder.query = new Builder();