@adityadarma/adonis-service-repository
v2.0.0
Published
<br />
Downloads
25
Maintainers
Readme
Adonis Service Repository
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.
Installation
node ace add @adityadarma/adonis-service-repository
Usage
*Service
Create service
node ace make:service ServiceName
Using Core or Base
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
Used on controller
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);
}
Use Service & Exception
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);
}
}
*Repository
Create repository
node ace make:repository nameRepository
Used on service
construct(nameRepository: NameRepository)
{
this.nameRepository = nameRepository;
}
async data()
{
this.nameRepository.functionName();
}
*Resource
Create Resource
node ace make:resource nameResource
*Note: use flag --async
to create resource asyncronous
Used on service
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);
}
}
License
This package is open-sourced software licensed under the MIT license.