Skip to content

Commit

Permalink
Support fromRaw (knex#4781)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierCavadenti committed Nov 4, 2021
1 parent cf61172 commit 427ff42
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/query/method-constants.js
Expand Up @@ -9,6 +9,7 @@ module.exports = [
'column',
'from',
'fromJS',
'fromRaw',
'into',
'withSchema',
'table',
Expand Down
5 changes: 5 additions & 0 deletions lib/query/querybuilder.js
Expand Up @@ -1266,6 +1266,11 @@ class Builder extends EventEmitter {
return this;
}

fromRaw(sql, bindings) {
const raw = sql.isRawInstance ? sql : this.client.raw(sql, bindings);
return this.from(raw);
}

// Passes query to provided callback function, useful for e.g. composing
// domain-specific helpers
modify(callback) {
Expand Down
9 changes: 9 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -8421,6 +8421,15 @@ describe('QueryBuilder', () => {
);
});

it('uses fromRaw api, #1767', () => {
testsql(qb().select('*').fromRaw('(select * from users where age > 18)'), {
mysql: 'select * from (select * from users where age > 18)',
mssql: 'select * from (select * from users where age > 18)',
pg: 'select * from (select * from users where age > 18)',
'pg-redshift': 'select * from (select * from users where age > 18)',
});
});

it('has a modify method which accepts a function that can modify the query', () => {
// arbitrary number of arguments can be passed to `.modify(queryBuilder, ...)`,
// builder is bound to `this`
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Expand Up @@ -491,6 +491,7 @@ export declare namespace Knex {
column: Select<TRecord, TResult>;
hintComment: HintComment<TRecord, TResult>;
from: Table<TRecord, TResult>;
fromRaw: Table<TRecord, TResult>;
into: Table<TRecord, TResult>;
table: Table<TRecord, TResult>;
distinct: Distinct<TRecord, TResult>;
Expand Down

0 comments on commit 427ff42

Please sign in to comment.