mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-12-28 14:38:52 +00:00
Add count method
This commit is contained in:
parent
17f70b956b
commit
215bf1617d
@ -21,6 +21,11 @@ export interface QueryInformation {
|
||||
limit ?: number,
|
||||
}
|
||||
|
||||
export interface CountQueryInformation {
|
||||
table: string,
|
||||
where ?: Object
|
||||
}
|
||||
|
||||
export class DatabaseHelper {
|
||||
|
||||
private static connection : Connection;
|
||||
@ -188,4 +193,40 @@ export class DatabaseHelper {
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a COUNT query on the database
|
||||
*
|
||||
* @param info Information about the count query
|
||||
*/
|
||||
static async Count(info: CountQueryInformation) : Promise<number> {
|
||||
|
||||
let sql = "SELECT COUNT(*) as count FROM " + info.table;
|
||||
let args = [];
|
||||
|
||||
if(info.where) {
|
||||
sql += " WHERE ";
|
||||
|
||||
for (const key in info.where) {
|
||||
if (info.where.hasOwnProperty(key)) {
|
||||
const value = info.where[key];
|
||||
sql += "AND " + key + " = ? ";
|
||||
args.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
sql = sql.replace("WHERE AND", "WHERE");
|
||||
}
|
||||
|
||||
return await new Promise((r, e) => {
|
||||
this.connection.query(sql, args, (err, results, f) => {
|
||||
if(err){
|
||||
e(err);
|
||||
return;
|
||||
}
|
||||
|
||||
r(results[0].count);
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user