Skip to content

Commit

Permalink
feat: found-rows, #31
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSwitch committed Jul 9, 2018
1 parent 43d4292 commit 535ba43
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ function buildQuery(opts) {
// Limit
const sql_limit = `LIMIT ${opts.start ? `${opts.start},` : '' }${opts.limit}`;

// Found rows
const sql_found_rows = opts.found_rows ? 'SQL_CALC_FOUND_ROWS ' : '';

// SubQuery
const is_subquery = opts.is_subquery;

Expand Down Expand Up @@ -145,7 +148,7 @@ function buildQuery(opts) {


// Put it all together
const sql = `SELECT ${sql_fields.toString()}
const sql = `SELECT ${sql_found_rows}${sql_fields.toString()}
FROM ${sql_table} ${sql_alias}
${sql_joins.join('\n')}
${sql_filter.length ? 'WHERE' : ''}
Expand Down
28 changes: 28 additions & 0 deletions test/specs/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,34 @@ describe('get', () => {
expect(resp).to.eql({_count: 10});

});

it('should return found_rows when defined in request options', async() => {

const data = [{name: 'hello'}];
dare.execute = (query, callback) => {

const expected = `
SELECT SQL_CALC_FOUND_ROWS a.name
FROM test a
LIMIT 10
`;

sqlEqual(query, expected);

callback(null, data);
};

const resp = await dare
.get({
table: 'test',
fields: ['name'],
limit: 10,
found_rows: true
});

expect(resp).to.eql(data);

});
});
});

0 comments on commit 535ba43

Please sign in to comment.