nestjs-collector
is a NestJS package designed to dynamically collect and manage services marked as collectables within your application. This package facilitates the organization and validation of different services or components, allowing you to efficiently group and utilize them based on shared characteristics or tags.
- Dynamic Collection: Automatically collects services marked as collectables at runtime.
- Tag-Based Organization: Groups services based on specified tags, enabling modular and scalable architecture.
- Runtime Efficiency: Streamlines the process of managing similar services, reducing the need for manual aggregation.
Install nestjs-collector
into your NestJS project using npm or yarn:
npm install nestjs-collector
Below is a basic usage example demonstrating how to use nestjs-collector
in a NestJS project.
Create a service that you wish to be collected:
// specific-rule.service.ts
import { Injectable } from '@nestjs/common';
import { Collectable } from 'nestjs-collector';
@Collectable('MyTag')
@Injectable()
export class SpecificRuleService {
validate(): boolean {
// Your validation logic here
return true;
}
}
Define a collector service that will aggregate all collectables with the same tag:
// rule-collector.service.ts
import { Injectable } from '@nestjs/common';
import { Collector, AbstractCollector } from 'nestjs-collector';
import { SpecificRuleService } from './specific-rule.service';
@Collector('MyTag')
@Injectable()
export class RuleCollectorService extends AbstractCollector<SpecificRuleService> {
collect() {
// Iterates over all collected services and applies their logic
return this.collectables.every(rule => rule.validate());
}
}
Ensure to import the CollectorModule
in your main application module:
// app.module.ts
import { Module } from '@nestjs/common';
import { CollectorModule } from 'nestjs-collector';
import { SpecificRuleService, RuleCollectorService } from './your-service-path';
@Module({
imports: [CollectorModule],
providers: [SpecificRuleService, RuleCollectorService],
})
export class AppModule {}
- @Collectable Decorator: Marks a service as collectable with a specified tag.
- @Collector Decorator: In a service extending
AbstractCollector
, aggregates all services marked as collectable with the matching tag. - Runtime Aggregation: On application startup,
nestjs-collector
dynamically collects all instances of services tagged with the same identifier and makes them available in the collector service.
For more in-depth examples, please refer to the tests/example
folder in this repository. It contains extensive guides and practical use cases, demonstrating the versatile applications of nestjs-collector
.
Contributions to nestjs-collector
are highly appreciated. Please read our contributing guidelines for details on how to contribute.
This project is licensed under the MIT License.