@mondart/nestjs-common-module-meilisearch
v1.1.11
Published
Minio module for NestJS applications, providing an easy-to-use interface for interacting with Minio object storage.
Readme
@mondart/nestjs-common-module-meilisearch
NestJS integration for MeiliSearch: a
MeiliClientService that owns the connection, a set of CQRS
commands/queries for reading and writing documents, DTOs for building your
own controllers around them, and a BullMQ-based processor for queuing writes
instead of calling MeiliSearch inline.
The command/query handlers are built on @nestjs/cqrs's CommandBus/
QueryBus, and read an optional meili.prefix config value (via
ConfigService) to namespace index names. Import CqrsModule and provide a
ConfigService in the host application — MeilisearchModule doesn't do
either for you.
Registration
import { CqrsModule } from '@nestjs/cqrs';
import { MeilisearchModule } from '@mondart/nestjs-common-module-meilisearch';
@Module({
imports: [
CqrsModule.forRoot(),
MeilisearchModule.forRoot({
host: 'http://localhost:7700',
apiKey: 'masterKey',
isEnabled: true,
}),
],
})
export class AppModule {}Or asynchronously:
MeilisearchModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
host: config.get('MEILI_HOST'),
apiKey: config.get('MEILI_API_KEY'),
}),
});MeilisearchModule is @Global(), so MeiliClientService and the
command/query handlers below are available everywhere without re-importing
the module.
Writing documents
Dispatch the write commands through CommandBus. Each resolves its target
index as <meili.prefix><index>.
constructor(private readonly commandBus: CommandBus) {}
// Upsert: adds `data.body` to the index
await this.commandBus.execute(
new AddOrUpdateObjectCommand('products', { id: 42, body: { name: 'Mug' } }),
);
// Partial update: sends `data` itself as the document
await this.commandBus.execute(
new SavePartiallyCommand('products', { id: 42, price: 12.5 }),
);
// Delete by id
await this.commandBus.execute(new DeleteObjectCommand('products', '42'));The AddOrUpdateObjectRequestDto, SavePartiallyRequestDto, and
DeleteObjectRequestDto classes (validated with class-validator) mirror
these commands' shape and are meant for a controller you write in the
consuming app to translate incoming requests into commands.
Reading documents
constructor(private readonly queryBus: QueryBus) {}
const page = await this.queryBus.execute(
new BrowseQuery('products', { limit: 20, offset: 0 }),
);
const results = await this.queryBus.execute(
new SearchQuery('products', { q: 'mug', limit: 10 }),
);BrowseRequestDto and SearchRequestDto are the corresponding
request-validation DTOs for building your own controller.
Queuing writes through BullMQ
MeiliProcessorEventListener is a WorkerHost base class that tracks job
duration and queue stats (via MeiliProcessorHelper) and drives the BullMQ
worker lifecycle, but its process() is a no-op placeholder — subclass it
and dispatch the matching command yourself:
import { Processor } from '@nestjs/bullmq';
import { Job } from 'bullmq';
import {
MeiliProcessorEventListener,
MeiliProcessorHelper,
AddOrUpdateObjectCommand,
} from '@mondart/nestjs-common-module-meilisearch';
@Processor('meili-sync')
export class MeiliSyncProcessor extends MeiliProcessorEventListener {
constructor(
meiliProcessorHelper: MeiliProcessorHelper,
private readonly commandBus: CommandBus,
) {
super('meili-sync', meiliProcessorHelper);
}
async process(job: Job) {
return this.commandBus.execute(
new AddOrUpdateObjectCommand(job.data.index, job.data.body),
);
}
}