UNPKG

nestjs-meili

Version:

Seamless and declarative integration of [MeiliSearch](https://www.meilisearch.com/) into [NestJS](https://nestjs.com/). Use decorators to configure indexes and inject `MeiliSearch` with type safety and zero boilerplate.

30 lines (29 loc) 936 B
import { MeiliSearch } from "meilisearch"; export class MeiliClient { client; constructor(host, apiKey) { if (typeof host !== "string" || typeof apiKey !== "string") { throw new Error("Both host and apiKey must be strings."); } this.client = new MeiliSearch({ host, apiKey }); } async initIndex(name, primaryKey) { if (!this.client) { throw new Error("Client is not initialized yet."); } let index = this.client.index(name); try { await index.getStats(); } catch (error) { if (error.message.includes("not found")) { await this.client.createIndex(name, { primaryKey }); index = this.client.index(name); } else { throw new Error(`Error initializing index: ${error.message}`); } } return index; } }