Skip to content

Commit

Permalink
feat: accept custom data to set set on query event
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 17, 2020
1 parent 87f9599 commit e9f944b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ declare module '@ioc:Adonis/Lucid/DatabaseQueryBuilder' {
debug (debug: boolean): this
timeout (time: number, options?: { cancel: boolean }): this
useTransaction (trx: TransactionClientContract): this
reporterData (data: any): this
toQuery (): string
exec (): Promise<Result>
toSQL (): knex.Sql
Expand Down
11 changes: 11 additions & 0 deletions src/Database/QueryBuilder/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil
*/
protected static macros = {}
protected static getters = {}
private customReporterData: any

/**
* Ensures that we are not executing `update` or `del` when using read only
Expand All @@ -74,9 +75,19 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil
return {
connection: this.client.connectionName,
inTransaction: this.client.isTransaction,
...this.customReporterData,
}
}

/**
* Define custom reporter data. It will be merged with
* the existing data
*/
public reporterData (data: any) {
this.customReporterData = data
return this
}

/**
* Delete rows under the current query
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Database/QueryBuilder/Insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class InsertQueryBuilder extends Macroable implements InsertQueryBuilderC
super()
}

private customReporterData: any

/**
* Required by macroable
*/
Expand All @@ -38,9 +40,19 @@ export class InsertQueryBuilder extends Macroable implements InsertQueryBuilderC
return {
connection: this.client.connectionName,
inTransaction: this.client.isTransaction,
...this.customReporterData,
}
}

/**
* Define custom reporter data. It will be merged with
* the existing data
*/
public reporterData (data: any) {
this.customReporterData = data
return this
}

/**
* Define table for performing the insert query
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Database/QueryBuilder/Raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { QueryRunner } from '../../QueryRunner'
* Exposes the API to execute raw queries
*/
export class RawQueryBuilder implements RawQueryBuilderContract {
private customReporterData: any

constructor (public knexQuery: knex.Raw, public client: QueryClientContract) {
}

Expand All @@ -29,9 +31,19 @@ export class RawQueryBuilder implements RawQueryBuilderContract {
return {
connection: this.client.connectionName,
inTransaction: this.client.isTransaction,
...this.customReporterData,
}
}

/**
* Define custom reporter data. It will be merged with
* the existing data
*/
public reporterData (data: any) {
this.customReporterData = data
return this
}

/**
* Wrap the query with before/after strings.
*/
Expand Down
17 changes: 16 additions & 1 deletion src/Orm/QueryBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
*/
protected wrapResultsToModelInstances: boolean = true

/**
* Custom reporter data
*/
private customReporterData: any

/**
* Options that must be passed to all new model instances
*/
Expand Down Expand Up @@ -120,7 +125,8 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
*/
private async execQuery () {
const isWriteQuery = ['update', 'del', 'insert'].includes(this.knexQuery['_method'])
const rows = await new QueryRunner(this.client, this.getQueryData()).run(this.knexQuery)
const queryData = Object.assign(this.getQueryData(), this.customReporterData)
const rows = await new QueryRunner(this.client, queryData).run(this.knexQuery)

/**
* Return the rows as it is when query is a write query
Expand Down Expand Up @@ -167,6 +173,15 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
}
}

/**
* Define custom reporter data. It will be merged with
* the existing data
*/
public reporterData (data: any) {
this.customReporterData = data
return this
}

/**
* Clone the current query builder
*/
Expand Down
16 changes: 16 additions & 0 deletions test/orm/model-query-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,20 @@ test.group('Model query builder', (group) => {
assert.equal(sql, knexSql)
assert.deepEqual(bindings, knexBindings)
})

test('make aggregate queries with the model query builder', async (assert) => {
class User extends BaseModel {
@column({ isPrimary: true })
public id: number

@column()
public username: string
}

User.boot()
await db.insertQuery().table('users').insert([{ username: 'virk' }, { username: 'nikk' }])

const users = await User.query().count('* as total')
assert.equal(Number(users[0].total), 2)
})
})

0 comments on commit e9f944b

Please sign in to comment.