@aceitadev/adatabase 0.3.5 → 0.4.0
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/QueryBuilder.d.ts +1 -0
- package/dist/QueryBuilder.js +41 -0
- package/package.json +1 -1
package/dist/QueryBuilder.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export declare class QueryBuilder<T extends ActiveRecord> {
|
|
|
16
16
|
limit(count: number): this;
|
|
17
17
|
offset(count: number): this;
|
|
18
18
|
first(): Promise<T | null>;
|
|
19
|
+
count(): Promise<number>;
|
|
19
20
|
get(): Promise<T[]>;
|
|
20
21
|
private getColumnName;
|
|
21
22
|
private mapRowsToEntities;
|
package/dist/QueryBuilder.js
CHANGED
|
@@ -63,6 +63,47 @@ class QueryBuilder {
|
|
|
63
63
|
return rows.length > 0 ? rows[0] : null;
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
|
+
count() {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
const table = (0, Table_1.getTableName)(this.model);
|
|
69
|
+
if (!table) {
|
|
70
|
+
throw new PersistenceException_1.PersistenceException("Model has no @Table", null);
|
|
71
|
+
}
|
|
72
|
+
const columnsMeta = (0, Column_1.getColumnMeta)(this.model);
|
|
73
|
+
if (!columnsMeta) {
|
|
74
|
+
throw new PersistenceException_1.PersistenceException("Model has no @Column decorators", null);
|
|
75
|
+
}
|
|
76
|
+
const allowedOperators = ['=', '!=', '<>', '>', '<', '>=', '<=', 'LIKE', 'IN', 'IS NULL', 'IS NOT NULL'];
|
|
77
|
+
const params = [];
|
|
78
|
+
const baseAlias = 't1';
|
|
79
|
+
let sql = `SELECT COUNT(*) as count FROM \`${table}\` AS ${baseAlias}`;
|
|
80
|
+
if (this.whereClauses.length > 0) {
|
|
81
|
+
sql += " WHERE ";
|
|
82
|
+
this.whereClauses.forEach((c, i) => {
|
|
83
|
+
if (!allowedOperators.includes(c.operator.toUpperCase())) {
|
|
84
|
+
throw new Error(`Invalid operator used: ${c.operator}`);
|
|
85
|
+
}
|
|
86
|
+
if (i > 0) {
|
|
87
|
+
sql += ` ${c.booleanOp} `;
|
|
88
|
+
}
|
|
89
|
+
const colName = this.getColumnName(c.field, columnsMeta);
|
|
90
|
+
sql += `\`${baseAlias}\`.\`${colName}\` ${c.operator} ?`;
|
|
91
|
+
params.push(c.value);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const rows = yield (0, Database_1.query)(sql, params);
|
|
96
|
+
if (rows && rows.length > 0 && rows[0].count !== undefined) {
|
|
97
|
+
return Number(rows[0].count);
|
|
98
|
+
}
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
console.error("Failed to execute count query:", error);
|
|
103
|
+
throw new PersistenceException_1.PersistenceException("Failed to count records");
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
66
107
|
get() {
|
|
67
108
|
return __awaiter(this, void 0, void 0, function* () {
|
|
68
109
|
const table = (0, Table_1.getTableName)(this.model);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aceitadev/adatabase",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Uma biblioteca para facilitar a interação com bancos de dados MySQL e PostgreSQL em projetos TypeScript/Node.js.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|