Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count method #167

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased][unreleased]

- New API methods for Database:
- async count(table, condition): number

## [1.1.5][] - 2021-07-04

- Implement CRUD plugin
Expand Down
5 changes: 5 additions & 0 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ class Database {
return column;
}

async count(table, ...conditions) {
const rows = await this.select(table, ...conditions);
return rows.length;
}

async dict(table, fields, ...conditions) {
const [keyField, valField] = fields;
const obj = Object.create(null);
Expand Down
8 changes: 8 additions & 0 deletions test/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ metatests.test('Database.col', async (test) => {
test.end();
});

metatests.test('Database.count', async (test) => {
const res1 = await db.count('Country');
test.strictEqual(res1, 4);
const res2 = await db.count('City', { name: 'Kiev' });
test.strictEqual(res2, 1);
test.end();
});

metatests.test('Database.dict', async (test) => {
const res = await db.dict('City', ['name', 'countryId']);
test.strictEqual(typeof res, 'object');
Expand Down
4 changes: 3 additions & 1 deletion types/metasql.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export class Database {}
export class Database {
count(table: string, ...conditions: Array<object>): Promise<number>;
}