🕑 Chapters
- Macro WhereTrue & WhereFalse
- Macro Any
- Macro SelectCount
- Macro SelectIds
- Macro SelectId
- Macro SelectIdOrFail
- Macro Outro
Model Query Builder Macro Provider
import type { ApplicationContract } from '@ioc:Adonis/Core/Application'
/*
|--------------------------------------------------------------------------
| Provider
|--------------------------------------------------------------------------
|
| Your application is not ready when this file is loaded by the framework.
| Hence, the top level imports relying on the IoC container will not work.
| You must import them inside the life-cycle methods defined inside
| the provider class.
|
| @example:
|
| public async ready () {
| const Database = this.app.container.resolveBinding('Adonis/Lucid/Database')
| const Event = this.app.container.resolveBinding('Adonis/Core/Event')
| Event.on('db:query', Database.prettyPrint)
| }
|
*/
export default class ModelQueryBuilderMacroProvider {
constructor(protected app: ApplicationContract) {}
public register() {
// Register your own bindings
}
public async boot() {
// All bindings are ready, feel free to use them
const { ModelQueryBuilder } = this.app.container.resolveBinding('Adonis/Lucid/Database')
// adds truthy where statement check on the query builder
ModelQueryBuilder.macro('whereTrue', function(columnName: string) {
return this.where(columnName, true)
})
// adds falsy where statement check on the query builder
ModelQueryBuilder.macro('whereFalse', function(columnName: string) {
return this.where(columnName, false)
})
// returns whether any records mached the built query
ModelQueryBuilder.macro('any', async function() {
const result = await this.count('* as total')
return result.at(0).$extras.total > 0
})
// returns just the final count result for the query
ModelQueryBuilder.macro('selectCount', async function() {
const result = await this.count('* as total')
return BigInt(result.at(0).$extras.total)
})
// returns an array of ids for the built query
ModelQueryBuilder.macro('selectIds', async function(primaryKey: string = 'id') {
const results = await this.select(primaryKey)
return results.map(result => result[primaryKey])
})
// returns the first id for the built query
ModelQueryBuilder.macro('selectId', async function(primaryKey: string = 'id') {
const result = await this.select(primaryKey).first()
return result && result[primaryKey]
})
// returns the first id for the built query, throws if no records
ModelQueryBuilder.macro('selectIdOrFail', async function(primaryKey: string = 'id') {
const result = await this.select(primaryKey).firstOrFail()
return result[primaryKey]
})
}
public async ready() {
// App is ready
}
public async shutdown() {
// Cleanup, since app is going down
}
}
Model Query Builder Macro Context
declare module '@ioc:Adonis/Lucid/Orm' {
interface ModelQueryBuilderContract<
Model extends LucidModel,
Result = InstanceType<Model>
> {
// macro typescript definitions here
whereTrue(columnName: string): this
whereFalse(columnName: string): this
any(): Promise<boolean>
selectCount(): Promise<BigInt>
selectIds(primaryKey?: string): Promise<number[]>
selectId(primaryKey?: string): Promise<number | undefined>
selectIdOrFail(primaryKey?: string): Promise<number>
}
}