Services in Orionjs encapsulate business logic and provide a clean way to organize your application code. They follow the dependency injection pattern, making your code more modular and easier to test.
Basic Usage
import {Service} from '@orion-js/services'
@Service()
export class UserService {
async findById(userId: string) {
// Implementation...
return user
}
async create(userData: {name: string; email: string}) {
// Implementation...
return newUser
}
}
Dependency Injection
Services can be injected into other services or controllers. In v4, you must use the factory function pattern for dependency injection:
import {Service, Inject} from '@orion-js/services'
import {UserService} from './UserService'
@Service()
export class AuthService {
// Use factory function for dependency injection
@Inject(() => UserService)
private userService: UserService
async login(email: string, password: string) {
// Use injected service
const user = await this.userService.findByEmail(email)
// Implementation...
return token
}
}
Circular Dependencies
The factory function pattern used in v4 automatically handles circular dependencies:
import {Service, Inject} from '@orion-js/services'
import {AuthService} from './AuthService'
@Service()
export class TokenService {
@Inject(() => AuthService)
private authService: AuthService
validateToken(token: string) {
// Implementation...
}
}
Testing
Services are designed to be easily testable with Jest:
// In your test
import {mockService} from '@orion-js/services'
import {UserService} from './UserService'
import {AuthService} from './AuthService'
describe('AuthService', () => {
it('should authenticate valid users', async () => {
// Mock the dependency
mockService(UserService, {
findByEmail: async () => ({
id: '123',
email: 'test@example.com',
password: 'hashed_password'
})
})
const authService = new AuthService()
const result = await authService.login('test@example.com', 'password')
expect(result).toBeDefined()
})
})
Getting Service Instances
You can get service instances from anywhere in your application:
import {getInstance} from '@orion-js/services'
import {UserService} from './UserService'
// Get an instance of the service
const userService = getInstance(UserService)
// Use the service
await userService.findById('123')
Services form the backbone of Orionjs applications, providing a structured way to organize business logic and maintain separation of concerns.