Service Usage Overview
Namespace Calls
UserService.getUser() Concise syntax
Type Safe
Auto-completion Compile validation
Subset Support
Only needed fields Performance optimization
TanStack Query
useUser Hook Auto caching
Basic Usage
Service Import
Generated Services are exported as Namespaces fromservices.generated.ts.
- Consistency: All Services managed in one place
- Easy import: No need to find file paths
- Auto update: Auto-synced on
pnpm generate - Clear naming: Grouped by Namespace, no conflicts
Basic API Calls
Call Service static functions directly.- Async call with
await - Response is auto-parsed (handled inside fetch function)
- Types fully guaranteed
- Subset parameter required (for getUser, etc.)
Namespace-based structure: - β Class instance: No need for
new UserService() - β
Static
function: Call UserService.getUser() directly - All functions work independentlyUnderstanding the Subset System
Sonamuβs core feature, the Subset system.- Performance: Query only needed fields to reduce network cost
- Type safety: Returns accurate type for each Subset
- Explicitness: Clearly express what data is needed in code
Using in React
TanStack Query Hook (Recommended)
The easiest way is to use auto-generated TanStack Query Hooks.- Auto caching (reuses same userId)
- Auto revalidation (on focus, reconnection)
- Auto loading/error state management
- Auto duplicate request removal
Function Component (useEffect)
You can also call directly without using Hooks.Recommended: Use TanStack Query Hooks when possible. They provide auto caching, revalidation,
and state management, making code much more concise.
Event Handlers
Call APIs based on user actions.Using Services in SSR
Sonamu supports SSR based on Vite + React. For SSR, use the SSR-specific Services automatically generated inqueries.generated.ts.
What are SSR Services?
Separate from frontend Services (services.generated.ts), SSR-specific Services are automatically generated on the backend.
Using in registerSSR
Use these Services when registering SSR routes.Frontend Service vs SSR Service
Key Difference: SSR Services execute backend Model methods directly without HTTP requests,
eliminating network overhead.
Learn More
For detailed information about SSR, see the following documents.Data Preloading
Preload data on the server with registerSSR
SSR Setup
Understand the SSR system
Understanding the Service Creation Mechanism
Services are Namespace-based static function collections.Services are Namespaces, Not Classes
- Stateless: No instance creation needed
- Static calls: Call
UserService.getUser()directly - Type-safe: Full type inference via TypeScript Namespaces
- Tree-shakable: Unused functions are excluded from the bundle
Services Have No Dependency Injection (DI)
Services are pure functions, so no dependencies are injected:Service Creation Process
Creation Steps:- Backend: Write
@apidecorator on Model - Sonamu Sync: Run
pnpm sonamu sync - Auto-generation:
services.generated.tsfile created - Namespace Functions: Each API method converted to Namespace function
- Hook Generation: GET methods automatically generate
useQueryHooks
Where Are Model Instances?
Created directly in Server Components:new UserModel() safe?
- Models are stateless, so creating new instances each time is safe
- The constructor only initializes Subset queries
- Context is dynamically retrieved via
Sonamu.getContext()
Service vs Model Comparison
Recommended patterns: - Client Component: Use Service - Server Component: Use Model
directly - API endpoint:
@api decorator on ModelClient Components
Services can be freely used in Client Components.Error Handling
SonamuError Handling
Handle errors from Service calls in a type-safe manner.Error Handling in React
Advanced Patterns
Parallel Requests
Call multiple APIs simultaneously to improve performance.- Sequential: 300ms + 200ms + 150ms = 650ms
- Parallel: max(300ms, 200ms, 150ms) = 300ms
Subset Optimization
Choose appropriate Subset for the situation.TanStack Query Utilization
Conditional Fetching
Cache Invalidation
Prefetching
Practical Example
Complete CRUD Flow
Cautions
Next Steps
How Services Work
Understanding auto-generation
TanStack Query Hook
Easier with React
Authentication
Authentication system
Error Handling
Error handling patterns