🏗️ Architecture

Service Layer Patterns

Architecture and patterns for the Gauzy service layer.

Base Service

All services extend TenantAwareCrudService, which provides:

  • Automatic tenant scoping
  • Standard CRUD operations
  • Pagination support
@Injectable()
export class EmployeeService extends TenantAwareCrudService<Employee> {
  constructor(
    @InjectRepository(Employee)
    private readonly employeeRepo: Repository<Employee>,
  ) {
    super(employeeRepo);
  }

  // Custom methods
  async findActiveByOrg(orgId: string): Promise<Employee[]> {
    return this.repository.find({
      where: {
        organizationId: orgId,
        isActive: true,
        tenantId: RequestContext.currentTenantId(),
      },
    });
  }
}

Service Hierarchy

Diagram; its source is available below
Diagram source
graph TB
    CrudService[CrudService - Base CRUD]
    TenantBase[TenantAwareCrudService - Tenant Scoping]
    Concrete[EmployeeService - Business Logic]

    CrudService --> TenantBase
    TenantBase --> Concrete

Common Patterns

Factory Pattern

async create(dto: CreateDTO): Promise<Entity> {
  const entity = this.repository.create(dto);
  entity.tenantId = RequestContext.currentTenantId();
  return this.repository.save(entity);
}

Decorator Pattern

@PermissionGuard(PermissionsEnum.EMPLOYEES_EDIT)
async update(id: string, dto: UpdateDTO): Promise<Entity> {
  return super.update(id, dto);
}

Strategy Pattern (Multi-ORM)

const strategy =
  this.configService.get("DB_ORM") === "mikroorm"
    ? new MikroOrmStrategy()
    : new TypeOrmStrategy();

Edit this page

Copyright © 2023-Present Ever Co. LTD.Docs by Sourcey