@extravirgin/payload-plugin-meilisearch 0.0.2 → 0.0.4-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,395 +1,198 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
1
+ import { MeiliSearch } from 'meilisearch';
2
+ import { compile } from 'html-to-text';
3
+ import { convertLexicalToHTML, consolidateHTMLConverters } from '@payloadcms/richtext-lexical';
4
+ const getIndexName = (config, collectionSlug)=>`${config?.indexPrefix ?? ''}${collectionSlug}`;
5
+ export const indexCollection = (collection, config)=>{
6
+ if (!collection.hooks) {
7
+ collection.hooks = {};
47
8
  }
48
- };
49
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
- if (ar || !(i in from)) {
52
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
- ar[i] = from[i];
54
- }
9
+ if (!collection.hooks.afterChange) {
10
+ collection.hooks.afterChange = [];
11
+ }
12
+ collection.hooks.afterChange.push(handleAfterChange(config));
13
+ if (!collection.hooks.afterDelete) {
14
+ collection.hooks.afterDelete = [];
55
15
  }
56
- return to.concat(ar || Array.prototype.slice.call(from));
16
+ collection.hooks.afterDelete.push(afterDeleteHook(config));
57
17
  };
58
- Object.defineProperty(exports, "__esModule", { value: true });
59
- exports.indexCollections = void 0;
60
- var meilisearch_1 = require("meilisearch");
61
- var html_to_text_1 = require("html-to-text");
62
- var richtext_lexical_1 = require("@payloadcms/richtext-lexical");
63
- var getIndexName = function (config, collectionSlug) { var _a; return "".concat((_a = config === null || config === void 0 ? void 0 : config.indexPrefix) !== null && _a !== void 0 ? _a : '').concat(collectionSlug); };
64
- function handleAfterChange(_a) {
65
- var _b, _c, _d, _e, _f, _g;
66
- var doc = _a.doc, previousDoc = _a.previousDoc, req = _a.req, collection = _a.collection, config = _a.config;
67
- return __awaiter(this, void 0, void 0, function () {
68
- var baseDoc, collectionConfig, payload, client, index, documents, _h, locales, defaultLocale, fallback, _i, locales_1, locale, localizedDoc, _j, documentId, shouldIndex, transformedDoc, _k, documentId, shouldIndex, transformedDoc, _l, error_1;
69
- return __generator(this, function (_m) {
70
- switch (_m.label) {
71
- case 0:
72
- _m.trys.push([0, 19, , 20]);
73
- baseDoc = doc;
74
- collectionConfig = config.collections && (config === null || config === void 0 ? void 0 : config.collections.find(function (c) { return c.slug === collection.slug; }));
75
- payload = req.payload;
76
- client = getMeilisearchClient(config);
77
- return [4 /*yield*/, upsertIndex(client, getIndexName(config, collection.slug))];
78
- case 1:
79
- _m.sent();
80
- index = client.index(getIndexName(config, collection.slug));
81
- documents = [];
82
- if (!(((_b = payload.config) === null || _b === void 0 ? void 0 : _b.localization) && ((_c = payload.config.localization.locales) === null || _c === void 0 ? void 0 : _c.length) > 0)) return [3 /*break*/, 13];
83
- _h = payload.config.localization, locales = _h.locales, defaultLocale = _h.defaultLocale, fallback = _h.fallback;
84
- _i = 0, locales_1 = locales;
85
- _m.label = 2;
86
- case 2:
87
- if (!(_i < locales_1.length)) return [3 /*break*/, 10];
88
- locale = locales_1[_i];
89
- if (!(req.locale === locale.code)) return [3 /*break*/, 3];
90
- _j = doc;
91
- return [3 /*break*/, 5];
92
- case 3: return [4 /*yield*/, req.payload.findByID({
18
+ const handleAfterChange = (config)=>{
19
+ return async function({ doc, previousDoc, req, collection }) {
20
+ req.payload.logger.info(`Meilisearch [handleAfterChange] ${collection.slug} document ${doc.id}`);
21
+ try {
22
+ let baseDoc = doc;
23
+ const collectionConfig = config.collections && config?.collections.find((c)=>c.slug === collection.slug);
24
+ const payload = req.payload;
25
+ const client = getMeilisearchClient(config);
26
+ await upsertIndex(client, getIndexName(config, collection.slug));
27
+ const index = client.index(getIndexName(config, collection.slug));
28
+ const documents = [];
29
+ if (payload.config?.localization && payload.config.localization.locales?.length > 0) {
30
+ const { locales, defaultLocale, fallback } = payload.config.localization;
31
+ for (const locale of locales){
32
+ const localizedDoc = await req.payload.findByID({
93
33
  collection: collection.slug,
94
34
  id: doc.id,
95
- locale: locale,
96
- fallbackLocale: locale.fallbackLocale || defaultLocale,
97
- })];
98
- case 4:
99
- _j = _m.sent();
100
- _m.label = 5;
101
- case 5:
102
- localizedDoc = _j;
103
- documentId = doc.id + "_".concat(locale.code);
104
- shouldIndex = (_e = (_d = collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.shouldIndex) === null || _d === void 0 ? void 0 : _d.call(collectionConfig, localizedDoc, previousDoc, locale.code)) !== null && _e !== void 0 ? _e : 'index';
105
- if (!(shouldIndex === 'index')) return [3 /*break*/, 7];
106
- _k = [{}];
107
- return [4 /*yield*/, transformDocumentForMeilisearch(localizedDoc, collection.fields, req)];
108
- case 6:
109
- transformedDoc = __assign.apply(void 0, [__assign.apply(void 0, _k.concat([_m.sent()])), { id: documentId, documentId: doc.id, locale: locale.code }]);
110
- documents.push(transformedDoc);
111
- return [3 /*break*/, 9];
112
- case 7:
113
- if (!(shouldIndex === 'delete')) return [3 /*break*/, 9];
114
- return [4 /*yield*/, index.deleteDocument(documentId)];
115
- case 8:
116
- _m.sent();
117
- _m.label = 9;
118
- case 9:
119
- _i++;
120
- return [3 /*break*/, 2];
121
- case 10:
122
- if (!(documents.length > 0)) return [3 /*break*/, 12];
123
- return [4 /*yield*/, index.addDocuments(documents)];
124
- case 11:
125
- _m.sent();
126
- _m.label = 12;
127
- case 12: return [3 /*break*/, 18];
128
- case 13:
129
- documentId = doc.id;
130
- shouldIndex = (_g = (_f = collectionConfig === null || collectionConfig === void 0 ? void 0 : collectionConfig.shouldIndex) === null || _f === void 0 ? void 0 : _f.call(collectionConfig, doc, previousDoc)) !== null && _g !== void 0 ? _g : 'index';
131
- if (!(shouldIndex === 'index')) return [3 /*break*/, 16];
132
- _l = [{}];
133
- return [4 /*yield*/, transformDocumentForMeilisearch(doc, collection.fields, req)];
134
- case 14:
135
- transformedDoc = __assign.apply(void 0, [__assign.apply(void 0, _l.concat([_m.sent()])), { id: documentId, documentId: doc.id }]);
136
- return [4 /*yield*/, index.addDocuments([transformedDoc])];
137
- case 15:
138
- _m.sent();
139
- return [3 /*break*/, 18];
140
- case 16:
141
- if (!(shouldIndex === 'delete')) return [3 /*break*/, 18];
142
- return [4 /*yield*/, index.deleteDocument(documentId)];
143
- case 17:
144
- _m.sent();
145
- _m.label = 18;
146
- case 18: return [3 /*break*/, 20];
147
- case 19:
148
- error_1 = _m.sent();
149
- console.error('Meilisearch indexing error:', error_1);
150
- return [3 /*break*/, 20];
151
- case 20: return [2 /*return*/];
35
+ locale: locale.code,
36
+ draft: false,
37
+ overrideAccess: true,
38
+ fallbackLocale: locale.fallbackLocale || defaultLocale
39
+ });
40
+ const documentId = doc.id + `_${locale.code}`;
41
+ if (!localizedDoc) {
42
+ await index.deleteDocument(documentId);
43
+ continue;
44
+ }
45
+ const shouldIndex = collectionConfig?.shouldIndex?.(localizedDoc, previousDoc, locale.code) ?? 'index';
46
+ if (shouldIndex === 'index') {
47
+ const transformedDoc = {
48
+ ...await transformDocumentForMeilisearch(localizedDoc, collection.fields, req),
49
+ id: documentId,
50
+ documentId: doc.id,
51
+ locale: locale.code
52
+ };
53
+ documents.push(transformedDoc);
54
+ } else if (shouldIndex === 'delete') {
55
+ await index.deleteDocument(documentId);
56
+ }
57
+ }
58
+ if (documents.length > 0) {
59
+ await index.addDocuments(documents);
60
+ }
61
+ } else {
62
+ const documentId = doc.id;
63
+ const shouldIndex = collectionConfig?.shouldIndex?.(doc, previousDoc) ?? 'index';
64
+ if (shouldIndex === 'index') {
65
+ const transformedDoc = {
66
+ ...await transformDocumentForMeilisearch(doc, collection.fields, req),
67
+ id: documentId,
68
+ documentId: doc.id
69
+ };
70
+ await index.addDocuments([
71
+ transformedDoc
72
+ ]);
73
+ } else if (shouldIndex === 'delete') {
74
+ await index.deleteDocument(documentId);
75
+ }
152
76
  }
153
- });
154
- });
155
- }
156
- var indexCollections = function (collections, config) {
157
- return collections.map(function (collection) {
158
- var _a, _b;
159
- var collectionConfig = config.collections && (config === null || config === void 0 ? void 0 : config.collections.find(function (c) { return c.slug === collection.slug; }));
160
- if (!collectionConfig) {
161
- return collection;
77
+ } catch (error) {
78
+ console.error('Meilisearch indexing error:', error);
162
79
  }
163
- return __assign(__assign({}, collection), { hooks: __assign(__assign({}, (collection.hooks || {})), { afterChange: __spreadArray(__spreadArray([], (((_a = collection.hooks) === null || _a === void 0 ? void 0 : _a.afterChange) || []), true), [
164
- function (_a) {
165
- var doc = _a.doc, previousDoc = _a.previousDoc, req = _a.req;
166
- handleAfterChange({ doc: doc, previousDoc: previousDoc, req: req, collection: collection, config: config });
167
- },
168
- ], false), afterDelete: __spreadArray(__spreadArray([], (((_b = collection.hooks) === null || _b === void 0 ? void 0 : _b.afterDelete) || []), true), [
169
- function (_a) {
170
- var doc = _a.doc, req = _a.req;
171
- return __awaiter(void 0, void 0, void 0, function () {
172
- var payload, localization, client, index, locales, _i, locales_2, locale, documentId, error_2;
173
- var _b, _c;
174
- return __generator(this, function (_d) {
175
- switch (_d.label) {
176
- case 0:
177
- _d.trys.push([0, 9, , 10]);
178
- payload = req.payload;
179
- localization = (_b = payload.config) === null || _b === void 0 ? void 0 : _b.localization;
180
- client = getMeilisearchClient(config);
181
- return [4 /*yield*/, upsertIndex(client, getIndexName(config, collection.slug))];
182
- case 1:
183
- _d.sent();
184
- index = client.index(getIndexName(config, collection.slug));
185
- if (!(localization && ((_c = localization.locales) === null || _c === void 0 ? void 0 : _c.length) > 0)) return [3 /*break*/, 6];
186
- locales = localization.locales;
187
- _i = 0, locales_2 = locales;
188
- _d.label = 2;
189
- case 2:
190
- if (!(_i < locales_2.length)) return [3 /*break*/, 5];
191
- locale = locales_2[_i];
192
- documentId = doc.id + "_".concat(locale.code);
193
- return [4 /*yield*/, index.deleteDocument(documentId)];
194
- case 3:
195
- _d.sent();
196
- _d.label = 4;
197
- case 4:
198
- _i++;
199
- return [3 /*break*/, 2];
200
- case 5: return [3 /*break*/, 8];
201
- case 6: return [4 /*yield*/, index.deleteDocument(doc.id)];
202
- case 7:
203
- _d.sent();
204
- _d.label = 8;
205
- case 8: return [3 /*break*/, 10];
206
- case 9:
207
- error_2 = _d.sent();
208
- console.error('Meilisearch indexing error:', error_2);
209
- return [3 /*break*/, 10];
210
- case 10: return [2 /*return*/];
211
- }
212
- });
213
- });
214
- },
215
- ], false) }) });
216
- });
80
+ };
217
81
  };
218
- exports.indexCollections = indexCollections;
219
- var meilisearch;
82
+ const afterDeleteHook = (config)=>{
83
+ return async ({ doc, req, collection })=>{
84
+ try {
85
+ const payload = req.payload;
86
+ const localization = payload.config?.localization;
87
+ const client = getMeilisearchClient(config);
88
+ await upsertIndex(client, getIndexName(config, collection.slug));
89
+ const index = client.index(getIndexName(config, collection.slug));
90
+ if (localization && localization.locales?.length > 0) {
91
+ const { locales } = localization;
92
+ for (const locale of locales){
93
+ const documentId = doc.id + `_${locale.code}`;
94
+ await index.deleteDocument(documentId);
95
+ }
96
+ } else {
97
+ await index.deleteDocument(doc.id);
98
+ }
99
+ } catch (error) {
100
+ console.error('Meilisearch indexing error:', error);
101
+ }
102
+ };
103
+ };
104
+ let meilisearch;
220
105
  function getMeilisearchClient(config) {
221
106
  if (!meilisearch) {
222
- meilisearch = new meilisearch_1.MeiliSearch({
107
+ meilisearch = new MeiliSearch({
223
108
  host: config.host,
224
- apiKey: config.apiKey,
109
+ apiKey: config.apiKey
225
110
  });
226
111
  }
227
112
  return meilisearch;
228
113
  }
229
- function upsertIndex(client, indexUID) {
230
- var _a;
231
- return __awaiter(this, void 0, void 0, function () {
232
- var error_3;
233
- return __generator(this, function (_b) {
234
- switch (_b.label) {
235
- case 0:
236
- _b.trys.push([0, 2, , 6]);
237
- // Attempt to retrieve the index
238
- return [4 /*yield*/, client.getIndex(indexUID)];
239
- case 1:
240
- // Attempt to retrieve the index
241
- _b.sent();
242
- return [3 /*break*/, 6];
243
- case 2:
244
- error_3 = _b.sent();
245
- if (!(((_a = error_3 === null || error_3 === void 0 ? void 0 : error_3.cause) === null || _a === void 0 ? void 0 : _a.code) === 'index_not_found')) return [3 /*break*/, 4];
246
- // Index doesn't exist; create it
247
- return [4 /*yield*/, client.createIndex(indexUID, {
248
- primaryKey: 'id',
249
- })];
250
- case 3:
251
- // Index doesn't exist; create it
252
- _b.sent();
253
- console.log("Index '".concat(indexUID, "' has been created."));
254
- return [3 /*break*/, 5];
255
- case 4:
256
- // Rethrow any other errors
257
- throw error_3;
258
- case 5: return [3 /*break*/, 6];
259
- case 6: return [2 /*return*/];
260
- }
261
- });
262
- });
114
+ async function upsertIndex(client, indexUID) {
115
+ try {
116
+ // Attempt to retrieve the index
117
+ await client.getIndex(indexUID);
118
+ } catch (error) {
119
+ // Check if the error is because the index was not found
120
+ // @ts-ignore
121
+ if (error?.cause?.code === 'index_not_found') {
122
+ // Index doesn't exist; create it
123
+ await client.createIndex(indexUID, {
124
+ primaryKey: 'id'
125
+ });
126
+ console.log(`Index '${indexUID}' has been created.`);
127
+ } else {
128
+ // Rethrow any other errors
129
+ throw error;
130
+ }
131
+ }
263
132
  }
264
- var options = {
265
- wordwrap: 130,
266
- // ...
133
+ const options = {
134
+ wordwrap: 130
267
135
  };
268
- var compiledConvert = (0, html_to_text_1.compile)(options);
136
+ const compiledConvert = compile(options);
269
137
  // Helper function to extract text from HTML in nodejs
270
138
  function extractTextFromHTML(html) {
271
139
  return compiledConvert(html);
272
140
  }
273
141
  // Helper function to transform a document for Meilisearch indexing
274
- function transformDocumentForMeilisearch(doc, fields, req) {
275
- return __awaiter(this, void 0, void 0, function () {
276
- var transformedDoc;
277
- var _this = this;
278
- return __generator(this, function (_a) {
279
- switch (_a.label) {
280
- case 0:
281
- transformedDoc = {};
282
- return [4 /*yield*/, Promise.all(fields.map(function (field) { return __awaiter(_this, void 0, void 0, function () {
283
- var lexicalAdapter, sanitizedServerEditorConfig, html, _a, _b, _c, _d, _e, _f;
284
- var _this = this;
285
- var _g;
286
- return __generator(this, function (_h) {
287
- switch (_h.label) {
288
- case 0:
289
- if (!(field.type === 'richText')) return [3 /*break*/, 2];
290
- lexicalAdapter = field.editor;
291
- sanitizedServerEditorConfig = lexicalAdapter.editorConfig;
292
- return [4 /*yield*/, (0, richtext_lexical_1.convertLexicalToHTML)({
293
- converters: (0, richtext_lexical_1.consolidateHTMLConverters)({ editorConfig: sanitizedServerEditorConfig }),
294
- data: doc[field.name],
295
- })];
296
- case 1:
297
- html = _h.sent();
298
- transformedDoc[field.name] = extractTextFromHTML(html);
299
- return [3 /*break*/, 11];
300
- case 2:
301
- if (!(field.type === 'array' && field.fields)) return [3 /*break*/, 4];
302
- // For array fields, transform each item
303
- _a = transformedDoc;
304
- _b = field.name;
305
- return [4 /*yield*/, Promise.all((doc[field.name] || []).map(function (item) {
306
- return transformDocumentForMeilisearch(item, field.fields, req);
307
- }))];
308
- case 3:
309
- // For array fields, transform each item
310
- _a[_b] = _h.sent();
311
- return [3 /*break*/, 11];
312
- case 4:
313
- if (!(field.type === 'tabs')) return [3 /*break*/, 6];
314
- return [4 /*yield*/, Promise.all(field.tabs.map(function (tab, index) { return __awaiter(_this, void 0, void 0, function () {
315
- return __generator(this, function (_a) {
316
- switch (_a.label) {
317
- case 0: return [4 /*yield*/, transformDocumentForMeilisearch(doc, tab.fields, req)];
318
- case 1: return [2 /*return*/, _a.sent()];
319
- }
320
- });
321
- }); }))];
322
- case 5:
323
- // For tab fields, transform each tab
324
- (_h.sent()).forEach(function (t) { return Object.assign(transformedDoc, t); });
325
- return [3 /*break*/, 11];
326
- case 6:
327
- if (!(field.type === 'blocks')) return [3 /*break*/, 8];
328
- // For block fields, transform each block
329
- _c = transformedDoc;
330
- _d = field.name;
331
- return [4 /*yield*/, Promise.all((doc[field.name] || []).map(function (block) { return __awaiter(_this, void 0, void 0, function () {
332
- var fields;
333
- var _a;
334
- var _b;
335
- return __generator(this, function (_c) {
336
- switch (_c.label) {
337
- case 0:
338
- fields = (_b = field.blocks.find(function (b) { return b.slug === block.blockType; })) === null || _b === void 0 ? void 0 : _b.fields;
339
- if (!fields) {
340
- return [2 /*return*/, {
341
- type: block.blockType,
342
- data: "",
343
- }];
344
- }
345
- _a = {
346
- type: block.blockType
347
- };
348
- return [4 /*yield*/, transformDocumentForMeilisearch(block, fields, req)];
349
- case 1: return [2 /*return*/, (_a.data = _c.sent(),
350
- _a)];
351
- }
352
- });
353
- }); }))];
354
- case 7:
355
- // For block fields, transform each block
356
- _c[_d] = _h.sent();
357
- return [3 /*break*/, 11];
358
- case 8:
359
- if (!(field.type === 'group' && field.fields)) return [3 /*break*/, 10];
360
- // For group fields, transform the nested fields
361
- _e = transformedDoc;
362
- _f = field.name;
363
- return [4 /*yield*/, transformDocumentForMeilisearch(doc[field.name], field.fields, req)];
364
- case 9:
365
- // For group fields, transform the nested fields
366
- _e[_f] = _h.sent();
367
- return [3 /*break*/, 11];
368
- case 10:
369
- if (field.type === 'relationship' || field.type === 'upload') {
370
- // For relationship and upload fields, just include the ID
371
- transformedDoc[field.name] = ((_g = doc[field.name]) === null || _g === void 0 ? void 0 : _g.id) || doc[field.name];
372
- }
373
- else {
374
- // For other field types, include as-is
375
- try {
376
- // @ts-ignore
377
- transformedDoc[field.name] = doc[field.name];
378
- }
379
- catch (err) {
380
- console.log(err, field, doc);
381
- }
382
- }
383
- _h.label = 11;
384
- case 11: return [2 /*return*/];
385
- }
386
- });
387
- }); }))];
388
- case 1:
389
- _a.sent();
390
- return [2 /*return*/, transformedDoc];
142
+ async function transformDocumentForMeilisearch(doc, fields, req) {
143
+ const transformedDoc = {};
144
+ await Promise.all(fields.map(async (field)=>{
145
+ if (field.type === 'richText') {
146
+ // For rich text fields, convert Lexical to HTML, then extract text
147
+ const lexicalAdapter = field.editor;
148
+ const sanitizedServerEditorConfig = lexicalAdapter.editorConfig;
149
+ const html = await convertLexicalToHTML({
150
+ converters: consolidateHTMLConverters({
151
+ editorConfig: sanitizedServerEditorConfig
152
+ }),
153
+ data: doc[field.name]
154
+ });
155
+ transformedDoc[field.name] = extractTextFromHTML(html);
156
+ } else if (field.type === 'array' && field.fields) {
157
+ // For array fields, transform each item
158
+ transformedDoc[field.name] = await Promise.all((doc[field.name] || []).map((item)=>transformDocumentForMeilisearch(item, field.fields, req)));
159
+ } else if (field.type === 'tabs') {
160
+ // For tab fields, transform each tab
161
+ (await Promise.all(field.tabs.map(async (tab)=>{
162
+ return await transformDocumentForMeilisearch(doc, tab.fields, req);
163
+ }))).forEach((t)=>Object.assign(transformedDoc, t));
164
+ } else if (field.type === 'blocks') {
165
+ // For block fields, transform each block
166
+ transformedDoc[field.name] = await Promise.all((doc[field.name] || []).map(async (block)=>{
167
+ const fields = field.blocks.find((b)=>b.slug === block.blockType)?.fields;
168
+ if (!fields) {
169
+ return {
170
+ type: block.blockType,
171
+ data: ""
172
+ };
173
+ }
174
+ return {
175
+ type: block.blockType,
176
+ data: await transformDocumentForMeilisearch(block, fields, req)
177
+ };
178
+ }));
179
+ } else if (field.type === 'group' && field.fields) {
180
+ // For group fields, transform the nested fields
181
+ transformedDoc[field.name] = await transformDocumentForMeilisearch(doc[field.name], field.fields, req);
182
+ } else if (field.type === 'relationship' || field.type === 'upload') {
183
+ // For relationship and upload fields, just include the ID
184
+ transformedDoc[field.name] = doc[field.name]?.id || doc[field.name];
185
+ } else {
186
+ // For other field types, include as-is
187
+ try {
188
+ // @ts-ignore
189
+ transformedDoc[field.name] = doc[field.name];
190
+ } catch (err) {
191
+ console.log(err, field, doc);
391
192
  }
392
- });
393
- });
193
+ }
194
+ }));
195
+ return transformedDoc;
394
196
  }
197
+
395
198
  //# sourceMappingURL=indexCollections.js.map