What is a Module?
A Module in Modelence is similar to a feature module in other frameworks. It groups related functionality together, making your codebase more maintainable and easier to reason about.Module Structure
A typical module includes:- Stores - MongoDB collection definitions
- Queries - Read operations that fetch data
- Mutations - Write operations that modify data
- Configuration - Module-specific settings
- Cron Jobs - Scheduled tasks (optional)
Data migrations are configured at the
startApp() level (not inside individual
modules). See the Migrations documentation.Stores
Stores define your MongoDB collections with schemas, indexes, and custom methods. Including stores in your module ensures they’re automatically provisioned when the server starts.Authentication & Authorization
Readuser from the handler context and check it explicitly. For an
unauthenticated caller, user is null:
Queries
Queries are read operations that fetch data without modifying state. For full query patterns, including client usage withcallMethod,
modelenceQuery, and typed client modules, see
Queries.
Mutations
Mutations are write operations that create, update, or delete data. For full mutation patterns, including client usage withcallMethod,
modelenceMutation, and typed client modules, see
Mutations.
Rate Limiting
Protect your queries and mutations from abuse by declaring rate limit rules on the module and consuming them inside handlers:Best Practices
1. Keep Modules Focused
Each module should represent a single domain or feature:2. Use Clear Naming
Name your queries and mutations descriptively:3. Handle Errors Gracefully
Always handle potential errors and provide meaningful messages:4. Keep Business Logic in Modules
Don’t put business logic directly in your database stores. Keep it in your module methods:5. Validate Method Arguments
Method arguments arrive from the client untyped and unvalidated — there is noinput option on a method definition. Parse them at the top of the handler to
get validation and types in one step:
Next Steps
Queries
Learn how to define and call query methods
Mutations
Learn how to define and call mutation methods
Migrations
Learn how migration scripts run and how to handle cron race conditions
Configuration
Learn about configuration options
Stores
Deep dive into working with MongoDB stores