UNPKG

@medusajs/index

Version:
114 lines 6.68 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _Configuration_schemaObjectRepresentation, _Configuration_indexMetadataService, _Configuration_indexSyncService, _Configuration_dataSynchronizer, _Configuration_logger; Object.defineProperty(exports, "__esModule", { value: true }); exports.Configuration = void 0; const utils_1 = require("@medusajs/framework/utils"); const _types_1 = require("../../types"); const index_metadata_status_1 = require("../index-metadata-status"); class Configuration { constructor({ schemaObjectRepresentation, indexMetadataService, indexSyncService, dataSynchronizer, logger, }) { _Configuration_schemaObjectRepresentation.set(this, void 0); _Configuration_indexMetadataService.set(this, void 0); _Configuration_indexSyncService.set(this, void 0); _Configuration_dataSynchronizer.set(this, void 0); _Configuration_logger.set(this, void 0); __classPrivateFieldSet(this, _Configuration_schemaObjectRepresentation, schemaObjectRepresentation ?? {}, "f"); __classPrivateFieldSet(this, _Configuration_indexMetadataService, indexMetadataService, "f"); __classPrivateFieldSet(this, _Configuration_indexSyncService, indexSyncService, "f"); __classPrivateFieldSet(this, _Configuration_dataSynchronizer, dataSynchronizer, "f"); __classPrivateFieldSet(this, _Configuration_logger, logger, "f"); } async checkChanges() { __classPrivateFieldGet(this, _Configuration_logger, "f").info("[Index engine] Checking for index changes"); const schemaObjectRepresentation = __classPrivateFieldGet(this, _Configuration_schemaObjectRepresentation, "f"); const currentConfig = await __classPrivateFieldGet(this, _Configuration_indexMetadataService, "f").list(); const currentConfigMap = new Map(currentConfig.map((c) => [c.entity, c])); const entityPresent = new Set(); const newConfig = []; const updatedConfig = []; const deletedConfig = []; const idxSyncData = []; for (const [entityName, schemaEntityObjectRepresentation] of Object.entries(schemaObjectRepresentation)) { if (_types_1.schemaObjectRepresentationPropertiesToOmit.includes(entityName)) { continue; } const entity = schemaEntityObjectRepresentation.entity; const fields = schemaEntityObjectRepresentation.fields.sort().join(","); const fields_hash = (0, utils_1.simpleHash)(fields); const existingEntityConfig = currentConfigMap.get(entity); entityPresent.add(entity); if (!existingEntityConfig || existingEntityConfig.fields_hash !== fields_hash) { const meta = { id: existingEntityConfig?.id, entity, fields, fields_hash, }; if (!existingEntityConfig) { newConfig.push(meta); } else { updatedConfig.push({ ...meta, status: index_metadata_status_1.IndexMetadataStatus.PENDING, }); } idxSyncData.push({ entity, last_key: null, }); } } for (const [entity] of currentConfigMap) { if (!entityPresent.has(entity)) { deletedConfig.push({ entity }); } } if (newConfig.length > 0) { await __classPrivateFieldGet(this, _Configuration_indexMetadataService, "f").create(newConfig); } if (updatedConfig.length > 0) { await __classPrivateFieldGet(this, _Configuration_indexMetadataService, "f").update(updatedConfig); } if (deletedConfig.length > 0) { await __classPrivateFieldGet(this, _Configuration_indexMetadataService, "f").delete(deletedConfig); await __classPrivateFieldGet(this, _Configuration_dataSynchronizer, "f").removeEntities(deletedConfig.map((c) => c.entity)); } if (idxSyncData.length > 0) { const ids = await __classPrivateFieldGet(this, _Configuration_indexSyncService, "f").list({ entity: idxSyncData.map((c) => c.entity), }); idxSyncData.forEach((sync) => { const id = ids.find((i) => i.entity === sync.entity)?.id; if (id) { sync.id = id; } }); await __classPrivateFieldGet(this, _Configuration_indexSyncService, "f").upsert(idxSyncData); } const changes = await __classPrivateFieldGet(this, _Configuration_indexMetadataService, "f").list({ status: [ index_metadata_status_1.IndexMetadataStatus.PENDING, index_metadata_status_1.IndexMetadataStatus.PROCESSING, index_metadata_status_1.IndexMetadataStatus.ERROR, ], }); __classPrivateFieldGet(this, _Configuration_logger, "f").info(`[Index engine] Found ${changes.length} index change${changes.length > 1 ? "s" : ""} that are either pending or processing`); return changes; } } exports.Configuration = Configuration; _Configuration_schemaObjectRepresentation = new WeakMap(), _Configuration_indexMetadataService = new WeakMap(), _Configuration_indexSyncService = new WeakMap(), _Configuration_dataSynchronizer = new WeakMap(), _Configuration_logger = new WeakMap(); //# sourceMappingURL=configuration.js.map