@vymalo/medusa-meilisearch
Version:
🔍 Powerful, lightning-fast search integration for MedusaJS using Meilisearch
84 lines • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("@medusajs/utils");
const utils_2 = require("../utils");
class MeiliSearchService extends utils_1.AbstractSearchService {
static identifier = 'vymalo-meilisearch';
static DISPLAY_NAME = 'Meilisearch Search';
isDefault = false;
defaultIndex = 'products';
logger;
client;
settings;
constructor(deps, options) {
super(deps, options);
this.logger = deps.logger;
this.client = deps.meilisearch_client;
this.settings = options.settings;
}
async createIndex(indexName = this.defaultIndex, options) {
return this.client.createIndex(indexName, options);
}
async getIndex(indexName) {
return await this.client.getIndex(indexName);
}
async addDocuments(indexName, documents, type) {
const transformedDocuments = await this.getTransformedDocuments(type, documents);
return await this.client
.index(indexName)
.addDocuments(transformedDocuments, { primaryKey: 'id' });
}
async replaceDocuments(indexName, documents, type) {
const transformedDocuments = await this.getTransformedDocuments(type, documents);
return await this.client
.index(indexName)
.addDocuments(transformedDocuments, { primaryKey: 'id' });
}
async deleteDocument(indexName, document_id) {
return await this.client.index(indexName).deleteDocument(document_id);
}
async deleteAllDocuments(indexName) {
return await this.client.index(indexName).deleteAllDocuments();
}
async search(indexName, query, options) {
const { paginationOptions, filter, additionalOptions } = options;
return await this.client
.index(indexName)
.search(query, { filter, ...paginationOptions, ...additionalOptions });
}
async updateSettings(indexName, settings) {
const indexSettings = settings.indexSettings || {};
await this.upsertIndex(indexName, settings);
return await this.client.index(indexName).updateSettings(indexSettings);
}
async upsertIndex(indexName, settings) {
try {
await this.client.getIndex(indexName);
}
catch (error) {
if (typeof error !== 'object') {
throw error;
}
if ('code' in error && error.code === 'index_not_found') {
await this.createIndex(indexName, {
primaryKey: settings?.primaryKey || 'id',
});
}
}
}
async getTransformedDocuments(type, documents) {
if (!documents?.length) {
return [];
}
switch (type) {
case utils_1.SearchUtils.indexTypes.PRODUCTS:
const productsTransformer = this.settings?.[utils_1.SearchUtils.indexTypes.PRODUCTS]?.transformer ||
utils_2.transformProduct;
return documents.map((p) => productsTransformer(p));
default:
return documents;
}
}
}
exports.default = MeiliSearchService;
//# sourceMappingURL=service.js.map