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

Support fromRaw #4781

Merged
merged 1 commit into from Oct 29, 2021
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
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