Skip to content

Commit

Permalink
feat(QueryBuilder): add clearLimit and clearOffset methods
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 1, 2020
1 parent 6293d2f commit 6ba940f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
clearWhere (): this
clearOrder (): this
clearHaving (): this
clearLimit (): this
clearOffset (): this

forUpdate (...tableNames: string[]): this
forShare (...tableNames: string[]): this
Expand Down
16 changes: 16 additions & 0 deletions src/Database/QueryBuilder/Chainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,22 @@ export abstract class Chainable extends Macroable implements ChainableContract {
return this
}

/**
* Clear limit
*/
public clearLimit (): this {
this.knexQuery['_single'].limit = null
return this
}

/**
* Clear offset
*/
public clearOffset (): this {
this.knexQuery['_single'].offset = null
return this
}

/**
* Specify `FOR UPDATE` lock mode for a given
* query
Expand Down
5 changes: 5 additions & 0 deletions src/Database/QueryBuilder/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil
return executeQuery(this.knexQuery, this.client, this.getProfilerAction())
}

// public async paginate () {
// const countQuery = this.clone()
// countQuery.clearOrder().clearLim
// }

/**
* Get sql representation of the query
*/
Expand Down
62 changes: 62 additions & 0 deletions test/database/query-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6049,6 +6049,68 @@ test.group('Query Builder | clearHaving', (group) => {
})
})

test.group('Query Builder | clearLimit', (group) => {
group.before(async () => {
await setup()
})

group.after(async () => {
await cleanup()
})

test('clear limit', async (assert) => {
const connection = new Connection('primary', getConfig(), getLogger())
connection.connect()

const db = getQueryBuilder(getQueryClient(connection))
const { sql, bindings } = db
.from('users')
.limit(10)
.clearLimit()
.toSQL()

const { sql: knexSql, bindings: knexBindings } = connection.client!
.from('users')
.toSQL()

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

await connection.disconnect()
})
})

test.group('Query Builder | clearOffset', (group) => {
group.before(async () => {
await setup()
})

group.after(async () => {
await cleanup()
})

test('clear offset', async (assert) => {
const connection = new Connection('primary', getConfig(), getLogger())
connection.connect()

const db = getQueryBuilder(getQueryClient(connection))
const { sql, bindings } = db
.from('users')
.offset(1)
.clearOffset()
.toSQL()

const { sql: knexSql, bindings: knexBindings } = connection.client!
.from('users')
.toSQL()

assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)

await connection.disconnect()
})
})

test.group('Query Builder | count', (group) => {
group.before(async () => {
await setup()
Expand Down

0 comments on commit 6ba940f

Please sign in to comment.