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

Array of ids #103

Merged
merged 4 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions models/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ class Table {
});
}

async arrayOfIds(search, options) {
const result = await this.find(search, ['id'], options);
const array = [];
for (let i = 0; i < result.length; i++) {
const element = result[i];
array.push(element.id);
}
return array;
}

findIdIn(ids, columns, query, options) {
return this.findIn('id', ids, query, columns, options);
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chinchay",
"version": "0.1.47",
"version": "0.1.48",
"description": "Building MVC files for express and knex.",
"main": "index.js",
"scripts": {
Expand Down
31 changes: 31 additions & 0 deletions test/models/table/find/arrayOfIds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';

// Require the dev-dependencies
const { assert } = require('chai');
const knex = require('../../../../knex');
const coffee = require('../../../../models/coffee-example');


// Our parent block
describe('TABLE GATEWAY: ArrayOfIds', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex.seed.run();
});

it('happy path', async () => { // eslint-disable-line
const results = await coffee.arrayOfIds();
const expected = [1, 2, 3, 4];
results.sort();
assert.isArray(results);
assert.deepEqual(results, expected);
});

it('empty response', async () => { // eslint-disable-line
const results = await coffee.arrayOfIds({ name: 'nonexistant' });
const expected = [];
results.sort();
assert.isArray(results);
assert.deepEqual(results, expected);
});
});