Ts.ED Integration
The @monque/tsed package provides a seamless integration between Monque and Ts.ED, allowing you to define jobs and cron jobs using decorators and leverage dependency injection.
Quick Reference
Section titled “Quick Reference”| Method / Type | Description |
|---|---|
MonqueModule | Main Ts.ED module |
@JobController() | Register a job controller |
@Job() | Register a job handler |
@Cron() | Register a scheduled job |
MonqueService | Injectable job management service |
Installation
Section titled “Installation”Dependencies
Section titled “Dependencies”Monque requires several peer dependencies to be present in your project. While many modern package managers (like Bun or NPM 7+) install peer dependencies automatically, it is highly recommended to install them explicitly to ensure version consistency and avoid unexpected resolution issues.
| Package | Version | Required |
|---|---|---|
@monque/core | ^1.1.0 | Yes |
mongodb | ^7.0.0 | Yes |
@tsed/core | ^8.24.0 | Yes |
@tsed/di | ^8.24.0 | Yes |
@tsed/mongoose | ^8.24.0 | Only for Mongoose integration |
Configuration
Section titled “Configuration”Import MonqueModule in your Server.ts and configure the connection via the @Configuration decorator. Monque supports multiple database resolution strategies to fit your application’s architecture.
Direct Strategy (Db instance)
Section titled “Direct Strategy (Db instance)”Use this if you already have a connected native MongoDB Db instance available synchronously.
Factory Strategy (Async)
Section titled “Factory Strategy (Async)”The recommended approach if you need to establish a connection during application bootstrap. The factory supports async execution.
Mongoose Strategy
Section titled “Mongoose Strategy”If your application already uses @tsed/mongoose, Monque can reuse the existing connection. It automatically extracts the native Db instance from the Mongoose connection registry. Monque does not need its own connection string in this case; it simply hooks into the established Mongoose pool.
1. Define a Job Controller
Section titled “1. Define a Job Controller”Create a class decorated with @JobController. Related job handlers are grouped here, similar to how REST controllers group HTTP endpoints. Methods decorated with @Job will process jobs.
Type Safety
Section titled “Type Safety”Monque is designed with type safety as a core principle. You can define job payloads using TypeScript interfaces and pass them as generics to the Job<T> type:
2. Schedule Jobs (Cron)
Section titled “2. Schedule Jobs (Cron)”Use the @Cron decorator to schedule recurring tasks. Cron jobs are automatically registered and scheduled with Monque during the application’s $onInit lifecycle phase.
3. Enqueue Jobs
Section titled “3. Enqueue Jobs”Inject MonqueService into any Ts.ED Service or Controller to dispatch jobs.
Job Isolation
Section titled “Job Isolation”One of the key benefits of the Ts.ED integration is Job Isolation. Every job execution runs in its own dedicated DIContext. This ensures that:
- Request-scoped services (scoped to
ProviderScope.REQUEST) are fresh for each job execution. - State does not leak between different job executions.
- You can use standard Ts.ED patterns for context-bound logic (e.g.
PlatformContext).
Producer-only Mode
Section titled “Producer-only Mode”In production environments, you may want to separate your API servers (producers) from your worker servers (consumers). Use disableJobProcessing to create producer-only instances:
This pattern enables horizontal scaling where API nodes focus on handling requests while dedicated worker nodes process background jobs.
API Reference
Section titled “API Reference”Services
Section titled “Services”MonqueService
Section titled “MonqueService”An injectable wrapper for the main Monque instance. It provides a DI-friendly way to manage jobs, delegating all calls to the underlying Monque engine.
Commonly used methods:
enqueue()- Add a job to the queue.schedule()- Schedule a recurring job.now()- Run a job immediately.
Testing
Section titled “Testing”Use @tsed/platform-http/testing and PlatformTest to test your jobs. You can mock MonqueService or use a real MongoDB connection with Testcontainers.
Next Steps
Section titled “Next Steps”- Explore Core Concepts to understand how jobs work.
- Learn about Scheduling recurring tasks.
- Check the Core API Reference for advanced job management.