mirror of
				https://gitlab.com/comunic/comunicapiv2
				synced 2025-11-04 03:24:04 +00:00 
			
		
		
		
	Add count method
This commit is contained in:
		@@ -21,6 +21,11 @@ export interface QueryInformation {
 | 
				
			|||||||
	limit ?: number,
 | 
						limit ?: number,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export interface CountQueryInformation {
 | 
				
			||||||
 | 
						table: string,
 | 
				
			||||||
 | 
						where ?: Object
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class DatabaseHelper {
 | 
					export class DatabaseHelper {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static connection : Connection;
 | 
						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);
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user