@nestbolt/taggable
Configuration
TaggableModule.forRoot() and forRootAsync() module registration explained.
forRoot
TaggableModule.forRoot() registers the module globally. The taggable module currently requires no configuration options -- call it with no arguments:
import { TaggableModule } from "@nestbolt/taggable";
@Module({
imports: [
TypeOrmModule.forRoot({ /* ... */ }),
TaggableModule.forRoot(),
],
})
export class AppModule {}The module automatically registers the tags and taggables tables via TypeORM and makes TaggableService available for injection across your entire application.
forRootAsync
Use forRootAsync() when your module setup depends on other providers (e.g., ConfigService):
import { ConfigModule, ConfigService } from "@nestjs/config";
import { TaggableModule } from "@nestbolt/taggable";
@Module({
imports: [
ConfigModule.forRoot(),
TaggableModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({}),
}),
],
})
export class AppModule {}forRootAsync Options
| Option | Type | Description |
|---|---|---|
imports | any[] | Modules to import for dependency injection |
inject | any[] | Providers to inject into the factory function |
useFactory | (...args) => TaggableModuleOptions | Factory function that returns the options |
What the Module Provides
When registered, the module:
- Creates database tables --
tagsandtaggablesentities are registered with TypeORM viaTypeOrmModule.forFeature(). - Provides TaggableService -- injectable service for all tag operations.
- Registers globally -- no need to import the module in feature modules.
TaggableService API Reference
TaggableService is available via dependency injection after module registration:
@Injectable()
export class MyService {
constructor(private readonly taggableService: TaggableService) {}
}| Method | Returns | Description |
|---|---|---|
createTag(name, opts?) | Promise<TagEntity> | Create a new tag |
findOrCreateTag(name, opts?) | Promise<TagEntity> | Find by slug or create |
findTagById(id) | Promise<TagEntity | null> | Find tag by ID |
findTagBySlug(slug, type?) | Promise<TagEntity | null> | Find tag by slug |
findTagsByType(type) | Promise<TagEntity[]> | Get all tags of a type |
getAllTags() | Promise<TagEntity[]> | Get all tags sorted by name |
deleteTag(id) | Promise<void> | Delete tag and all pivot records |
attachTag(Entity, entityId, tagId) | Promise<void> | Attach tag to entity |
detachTag(Entity, entityId, tagId) | Promise<void> | Detach tag from entity |
syncTags(Entity, entityId, tagIds) | Promise<void> | Sync entity tags |
getEntityTags(Entity, entityId) | Promise<TagEntity[]> | Get entity's tags |
hasTag(Entity, entityId, tagId) | Promise<boolean> | Check if entity has tag |
getEntitiesWithTag(Entity, tagId) | Promise<string[]> | Get entity IDs with tag |
getTagCount(Entity, entityId) | Promise<number> | Count entity's tags |
getOptions() | TaggableModuleOptions | Get module options |
See Tag CRUD and Attaching Tags for detailed usage of each method.