@fickou/adonis-controller-helpers
v1.0.31
Published
Help dev to filter database with query parameters
Downloads
15
Maintainers
Readme
Adonis-Controller-Helpers
Add helper on Controller for Adonis JS 5+
Table of contents
Installation
Run:
npm i --save @fickou/adonis-controller-helpers
Install provider:
node ace configure @fickou/adonis-controller-helpers
Sample Usage
Controller
In a controller
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
import IndexService from 'App/Services/users/Index'
import Database from "@ioc:Adonis/Lucid/Database";
export default UserController{
public index({ request, response }: HttpContextContract){
let data = await request.checkInputs();
await Database.transaction(async (trx) => {
const executor = new Index(trx);
return response.apiView(await executor.execute(data));
});
}
}
Service
In a service:
import Parameter from 'App/Models/settings/Parameter';
import ControllerHelper, from "@ioc:Adonis/Addons/ControllerHelper";
import {Service} from "@fickou/adonis-controller-helpers";
export default class Index extends Service {
async execute(payload) {
const query = Parameter.query({client: this.trx});
return ControllerHelper.searchPayload(query, payload);
}
}
// or
export default class Index extends Service {
async execute(payload) {
const query = ControllerHelper.buildQuery((this.trx || Database), (db) => db.query()
.from('parameters')
.select(['*']);
return ControllerHelper.searchDatabasePayload(query, payload);
}
}