@tsed-plus/meilisearch-mongoose-indexer
Version:
Extends the Ts.ED framework with the functionality to synchronize Mongoose models with Meilisearch
116 lines • 5.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MeiliMongoIndexerModule = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@tsed/common");
const di_1 = require("@tsed/di");
const meilisearch_1 = require("@tsed-plus/meilisearch");
const constants_1 = require("./constants");
const getKeyForDocument = (key) => {
return key._id.toString();
};
let MeiliMongoIndexerModule = class MeiliMongoIndexerModule {
$afterListen() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const providers = this.getProviders();
providers.forEach((provider) => this.addWatchForProvider(provider));
});
}
getOrCreateIndex(indexName, indexer) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const index = yield this.meilisearch.getIndex(indexName);
this.logger.info(`Meilisearch index "${indexName}" already exists`);
return index;
}
catch (e) {
if (e.code !== 'index_not_found') {
throw e;
}
this.logger.info(`Meilisearch create index "${indexName}"`);
const indexOptions = yield indexer.getCreateIndexOptions();
const task = yield this.meilisearch.createIndex(indexName, indexOptions);
yield this.meilisearch.waitForTask(task.taskUid);
return this.meilisearch.getIndex(task.indexUid);
}
});
}
updateIndex(indexName, index, indexer) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const updateSettingsAndOptions = yield indexer.getUpdateIndexAndSettingsOptions(index);
if (updateSettingsAndOptions) {
const { indexSettings: updateIndexSettings, indexOptions: updateIndexOptions, } = updateSettingsAndOptions;
if (updateIndexOptions) {
this.logger.info(`Meilisearch update index "${indexName}" options`);
const task = yield index.update(updateIndexOptions);
yield this.meilisearch.waitForTask(task.taskUid);
}
if (updateIndexSettings) {
this.logger.info(`Meilisearch update index "${indexName}" settings`);
const task = yield index.updateSettings(updateIndexSettings);
yield this.meilisearch.waitForTask(task.taskUid);
}
}
});
}
addWatchForProvider(provider) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const store = provider.store.get('meilisearchmongooseindexer');
if (!store.model) {
return;
}
const indexerInstance = this.injector.get(provider.token);
const mongooseModel = this.injector.get(store.model);
if (!mongooseModel || !indexerInstance) {
return;
}
const indexName = (yield (indexerInstance === null || indexerInstance === void 0 ? void 0 : indexerInstance.getIndexName())) ||
store.indexName ||
mongooseModel.collection.collectionName;
const index = yield this.getOrCreateIndex(indexName, indexerInstance);
yield this.updateIndex(indexName, index, indexerInstance);
mongooseModel
.watch([], { fullDocument: 'updateLookup' })
.on('change', (csd) => tslib_1.__awaiter(this, void 0, void 0, function* () {
switch (csd.operationType) {
case 'insert':
yield index.addDocuments([
yield indexerInstance.transformDocument(csd),
]);
break;
case 'update':
case 'replace': {
if (document) {
yield index.updateDocuments([
yield indexerInstance.transformDocument(csd.fullDocument || csd),
]);
}
break;
}
case 'delete':
yield index.deleteDocument(getKeyForDocument(csd.documentKey));
}
}));
});
}
getProviders() {
return Array.from(this.injector.getProviders(constants_1.PROVIDER_TYPE_MEILISEARCHMONGOOSEINDEXER));
}
};
tslib_1.__decorate([
(0, di_1.Inject)(),
tslib_1.__metadata("design:type", common_1.Logger)
], MeiliMongoIndexerModule.prototype, "logger", void 0);
tslib_1.__decorate([
(0, di_1.Inject)(),
tslib_1.__metadata("design:type", di_1.InjectorService)
], MeiliMongoIndexerModule.prototype, "injector", void 0);
tslib_1.__decorate([
(0, di_1.Inject)(),
tslib_1.__metadata("design:type", meilisearch_1.MeiliSearchService)
], MeiliMongoIndexerModule.prototype, "meilisearch", void 0);
MeiliMongoIndexerModule = tslib_1.__decorate([
(0, di_1.Module)()
], MeiliMongoIndexerModule);
exports.MeiliMongoIndexerModule = MeiliMongoIndexerModule;
//# sourceMappingURL=MeiliMongoIndexerModule.js.map