Service repository is a pattern to separate business logic and query logic. This package is designed to help simplify the maintenance of large and medium scale applications.
node ace add @adityadarma/adonis-service-repository
node ace make:service ServiceName
If you want to change the core service according to your wishes without changing the existing methods, you can publish it first.
node ace service:publish
protected nameService: NameService;
construct(nameService: NameService)
{
this.nameService = nameService;
}
async data()
{
return this.nameService.functionName().getData();
}
async json()
{
return this.nameService.functionName().toJson();
}
async withResource()
{
return this.nameService.functionName().toJsonFromResource(ClassResource);
}
Every all exception, must have handle to class ServiceException
async nameMethod()
{
try {
.........
if (false) {
throw new ServiceException('Error exception');
}
..........
return this.setData(data)
.setMessage('Message data')
.setCode(200);
// OR
return this.setData(data)
.setResource(ClassResource)
.setMessage('Message data')
.setCode(200);
// OR
return await this.setData(data)
.setMessage('Message data')
.setCode(200)
.setResourceAsync(ClassResource);
} catch (error) {
return this.exceptionResponse(error);
}
}
node ace make:repository nameRepository
construct(nameRepository: NameRepository)
{
this.nameRepository = nameRepository;
}
async data()
{
this.nameRepository.functionName();
}
node ace make:resource nameResource
*Note: use flag --async
to create resource asyncronous
construct(nameResource: NameResource)
{
this.nameResource = nameResource;
}
async data()
{
try {
.........
if (false) {
throw new ServiceException('Error exception');
}
..........
return this.setData(data)
.setResource(ClassResource)
.setMessage('Message data')
.setCode(200);
// OR
return await this.setData(data)
.setMessage('Message data')
.setCode(200)
.setResourceAsync(ClassResource);
} catch (error) {
return this.exceptionResponse(error);
}
}
This package is open-sourced software licensed under the MIT license.