Skip to content

adityadarma/adonis-service-repository

Repository files navigation

Adonis Service Repository


gh-workflow-image npm-image npm-downloads license-image

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published