Creating Custom Adapters
Learn how to create custom adapters for Motia to integrate with your infrastructure
This guide explains how to create custom adapters for Motia. Adapters implement specific interfaces to provide pluggable infrastructure components.
Why Create Custom Adapters
Create custom adapters when you need to:
- Integrate with custom infrastructure (databases, message queues, etc.)
- Implement specific performance optimizations
- Add features not available in existing adapters
- Support new storage backends
Adapter Architecture
Adapters in Motia follow the interface pattern:
- Interfaces define contracts in
@motiadev/core - Implementations satisfy these contracts
- Composition is used throughout - adapters are injected, not extended
Interface Contracts
Stream Adapter
Stream adapters extend the abstract StreamAdapter<TData> class.
Required Methods:
Optional Methods (with defaults):
send(channel, event)- Send events to subscriberssubscribe(channel, handler)- Subscribe to eventsunsubscribe(channel)- Unsubscribe from eventsclear(groupId)- Clear all items in a groupquery(groupId, filter)- Query with filters
Example Implementation:
State Adapter
State adapters implement the StateAdapter interface, which extends InternalStateManager.
Required Methods:
Example Implementation:
Event Adapter
Event adapters implement the EventAdapter interface.
Required Methods:
Example Implementation:
Cron Adapter
Cron adapters implement the CronAdapter interface for distributed locking.
Required Methods:
Example Implementation:
Step-by-Step Creation
1. Choose the Adapter Interface
Import the interface or abstract class from @motiadev/core:
2. Implement Required Methods
Create a class that implements all required methods. For StreamAdapter, extend the abstract class. For others, implement the interface.
3. Handle Initialization and Cleanup
- Initialization: Perform setup in the constructor or an
init()method - Cleanup: Implement
shutdown()orcleanup()methods to release resources (connections, timers, etc.)
4. Register in Configuration
Add your adapter to motia.config.ts:
Best Practices
Error Handling
Always handle errors gracefully and provide meaningful error messages:
Resource Cleanup
Always clean up resources in shutdown methods:
Health Checks
Implement health checks for production adapters:
Testing
Test adapters with in-memory implementations:
Type Safety
Maintain type safety by using generics correctly:
Performance Considerations
- Use efficient data structures
- Implement connection pooling for remote adapters
- Consider caching for frequently accessed data
- Batch operations when possible
Testing Your Adapter
- Create unit tests for each method
- Test error scenarios (connection failures, invalid inputs)
- Test concurrent access for distributed adapters
- Verify cleanup in shutdown methods
- Test with actual Motia steps to ensure integration works
Publishing Adapter Packages
If creating a reusable adapter package:
- Create a new package in
packages/adapter-* - Export your adapter class
- Include TypeScript types
- Add README with usage examples
- Follow the naming convention:
@motiadev/adapter-{name}-{type}
Example: @motiadev/adapter-mongodb-state
What's Next?
📖 Using Adapters
Learn how to configure and use adapters
📚 Adapters Overview
Review adapter concepts and architecture