@sqb/connect
Version:
Multi-dialect database connection framework written with TypeScript
27 lines (26 loc) • 828 B
JavaScript
import { And, Count, Select } from '@sqb/builder';
import { prepareFilter } from './command.helper.js';
export class CountCommand {
// istanbul ignore next
constructor() {
throw new Error('This class is abstract');
}
static async execute(args) {
const { connection, entity, filter, params } = args;
let where;
if (filter) {
where = And();
await prepareFilter(entity, filter, where, 'T');
}
const query = Select(Count()).from(entity.tableName + ' T');
if (where)
query.where(where);
// Execute query
const resp = await connection.execute(query, {
params,
objectRows: false,
cursor: false,
});
return (resp && resp.rows && resp.rows[0][0]) || 0;
}
}