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โ
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();
Related Pagesโ
- Repository Pattern โ data access
- Request Lifecycle โ request flow
- CQRS Pattern โ CQRS