58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
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 QueryBuilderProvider {
|
|
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');
|
|
|
|
ModelQueryBuilder.macro('pluck', async function (valueColumn: string, id?: string) {
|
|
let rolesPluck = {};
|
|
(await this).forEach((user, index) => {
|
|
let idc;
|
|
if (!id) {
|
|
idc = index;
|
|
} else {
|
|
idc = user[id];
|
|
}
|
|
const value = user[valueColumn];
|
|
// rolesPluck[idc] = user.name;
|
|
rolesPluck[idc] = value;
|
|
});
|
|
return rolesPluck;
|
|
});
|
|
}
|
|
|
|
public async ready() {
|
|
// App is ready
|
|
}
|
|
|
|
public async shutdown() {
|
|
// Cleanup, since app is going down
|
|
}
|
|
}
|