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.

33 lines (32 loc) 1.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MeiliClient = void 0; const meilisearch_1 = require("meilisearch"); class MeiliClient { constructor(host, apiKey) { if (typeof host !== "string" || typeof apiKey !== "string") { throw new Error("Both host and apiKey must be strings."); } this.client = new meilisearch_1.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; } } exports.MeiliClient = MeiliClient;