@extravirgin/payload-plugin-meilisearch 0.0.3 → 0.0.4
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.
- package/README.md +1 -226
- package/dist/index.d.ts +16 -2
- package/dist/index.js +17 -5
- package/dist/index.js.map +1 -1
- package/dist/indexCollections.d.ts +3 -3
- package/dist/indexCollections.js +172 -370
- package/dist/indexCollections.js.map +1 -1
- package/package.json +76 -43
- package/LICENSE +0 -21
- package/dist/mocks/mockFile.d.ts +0 -1
- package/dist/mocks/mockFile.js +0 -3
- package/dist/mocks/mockFile.js.map +0 -1
- package/dist/onInitExtension.d.ts +0 -3
- package/dist/onInitExtension.js +0 -17
- package/dist/onInitExtension.js.map +0 -1
- package/dist/plugin.d.ts +0 -3
- package/dist/plugin.js +0 -103
- package/dist/plugin.js.map +0 -1
- package/dist/types.d.ts +0 -18
- package/dist/types.js +0 -3
- package/dist/types.js.map +0 -1
- package/dist/webpack.d.ts +0 -3
- package/dist/webpack.js +0 -32
- package/dist/webpack.js.map +0 -1
package/dist/indexCollections.js
CHANGED
|
@@ -1,395 +1,197 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
16
|
+
collection.hooks.afterDelete.push(afterDeleteHook(config));
|
|
57
17
|
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
+
try {
|
|
21
|
+
let baseDoc = doc;
|
|
22
|
+
const collectionConfig = config.collections && config?.collections.find((c)=>c.slug === collection.slug);
|
|
23
|
+
const payload = req.payload;
|
|
24
|
+
const client = getMeilisearchClient(config);
|
|
25
|
+
await upsertIndex(client, getIndexName(config, collection.slug));
|
|
26
|
+
const index = client.index(getIndexName(config, collection.slug));
|
|
27
|
+
const documents = [];
|
|
28
|
+
if (payload.config?.localization && payload.config.localization.locales?.length > 0) {
|
|
29
|
+
const { locales, defaultLocale, fallback } = payload.config.localization;
|
|
30
|
+
for (const locale of locales){
|
|
31
|
+
const localizedDoc = await req.payload.findByID({
|
|
93
32
|
collection: collection.slug,
|
|
94
33
|
id: doc.id,
|
|
95
|
-
locale: locale,
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
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*/];
|
|
34
|
+
locale: locale.code,
|
|
35
|
+
draft: false,
|
|
36
|
+
overrideAccess: true,
|
|
37
|
+
fallbackLocale: locale.fallbackLocale || defaultLocale
|
|
38
|
+
});
|
|
39
|
+
const documentId = doc.id + `_${locale.code}`;
|
|
40
|
+
if (!localizedDoc) {
|
|
41
|
+
await index.deleteDocument(documentId);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const shouldIndex = collectionConfig?.shouldIndex?.(localizedDoc, previousDoc, locale.code) ?? 'index';
|
|
45
|
+
if (shouldIndex === 'index') {
|
|
46
|
+
const transformedDoc = {
|
|
47
|
+
...await transformDocumentForMeilisearch(localizedDoc, collection.fields, req),
|
|
48
|
+
id: documentId,
|
|
49
|
+
documentId: doc.id,
|
|
50
|
+
locale: locale.code
|
|
51
|
+
};
|
|
52
|
+
documents.push(transformedDoc);
|
|
53
|
+
} else if (shouldIndex === 'delete') {
|
|
54
|
+
await index.deleteDocument(documentId);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (documents.length > 0) {
|
|
58
|
+
await index.addDocuments(documents);
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
const documentId = doc.id;
|
|
62
|
+
const shouldIndex = collectionConfig?.shouldIndex?.(doc, previousDoc) ?? 'index';
|
|
63
|
+
if (shouldIndex === 'index') {
|
|
64
|
+
const transformedDoc = {
|
|
65
|
+
...await transformDocumentForMeilisearch(doc, collection.fields, req),
|
|
66
|
+
id: documentId,
|
|
67
|
+
documentId: doc.id
|
|
68
|
+
};
|
|
69
|
+
await index.addDocuments([
|
|
70
|
+
transformedDoc
|
|
71
|
+
]);
|
|
72
|
+
} else if (shouldIndex === 'delete') {
|
|
73
|
+
await index.deleteDocument(documentId);
|
|
74
|
+
}
|
|
152
75
|
}
|
|
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;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error('Meilisearch indexing error:', error);
|
|
162
78
|
}
|
|
163
|
-
|
|
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
|
-
});
|
|
79
|
+
};
|
|
217
80
|
};
|
|
218
|
-
|
|
219
|
-
|
|
81
|
+
const afterDeleteHook = (config)=>{
|
|
82
|
+
return async ({ doc, req, collection })=>{
|
|
83
|
+
try {
|
|
84
|
+
const payload = req.payload;
|
|
85
|
+
const localization = payload.config?.localization;
|
|
86
|
+
const client = getMeilisearchClient(config);
|
|
87
|
+
await upsertIndex(client, getIndexName(config, collection.slug));
|
|
88
|
+
const index = client.index(getIndexName(config, collection.slug));
|
|
89
|
+
if (localization && localization.locales?.length > 0) {
|
|
90
|
+
const { locales } = localization;
|
|
91
|
+
for (const locale of locales){
|
|
92
|
+
const documentId = doc.id + `_${locale.code}`;
|
|
93
|
+
await index.deleteDocument(documentId);
|
|
94
|
+
}
|
|
95
|
+
} else {
|
|
96
|
+
await index.deleteDocument(doc.id);
|
|
97
|
+
}
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.error('Meilisearch indexing error:', error);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
let meilisearch;
|
|
220
104
|
function getMeilisearchClient(config) {
|
|
221
105
|
if (!meilisearch) {
|
|
222
|
-
meilisearch = new
|
|
106
|
+
meilisearch = new MeiliSearch({
|
|
223
107
|
host: config.host,
|
|
224
|
-
apiKey: config.apiKey
|
|
108
|
+
apiKey: config.apiKey
|
|
225
109
|
});
|
|
226
110
|
}
|
|
227
111
|
return meilisearch;
|
|
228
112
|
}
|
|
229
|
-
function upsertIndex(client, indexUID) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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
|
-
});
|
|
113
|
+
async function upsertIndex(client, indexUID) {
|
|
114
|
+
try {
|
|
115
|
+
// Attempt to retrieve the index
|
|
116
|
+
await client.getIndex(indexUID);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
// Check if the error is because the index was not found
|
|
119
|
+
// @ts-ignore
|
|
120
|
+
if (error?.cause?.code === 'index_not_found') {
|
|
121
|
+
// Index doesn't exist; create it
|
|
122
|
+
await client.createIndex(indexUID, {
|
|
123
|
+
primaryKey: 'id'
|
|
124
|
+
});
|
|
125
|
+
console.log(`Index '${indexUID}' has been created.`);
|
|
126
|
+
} else {
|
|
127
|
+
// Rethrow any other errors
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
263
131
|
}
|
|
264
|
-
|
|
265
|
-
wordwrap: 130
|
|
266
|
-
// ...
|
|
132
|
+
const options = {
|
|
133
|
+
wordwrap: 130
|
|
267
134
|
};
|
|
268
|
-
|
|
135
|
+
const compiledConvert = compile(options);
|
|
269
136
|
// Helper function to extract text from HTML in nodejs
|
|
270
137
|
function extractTextFromHTML(html) {
|
|
271
138
|
return compiledConvert(html);
|
|
272
139
|
}
|
|
273
140
|
// Helper function to transform a document for Meilisearch indexing
|
|
274
|
-
function transformDocumentForMeilisearch(doc, fields, req) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
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];
|
|
141
|
+
async function transformDocumentForMeilisearch(doc, fields, req) {
|
|
142
|
+
const transformedDoc = {};
|
|
143
|
+
await Promise.all(fields.map(async (field)=>{
|
|
144
|
+
if (field.type === 'richText') {
|
|
145
|
+
// For rich text fields, convert Lexical to HTML, then extract text
|
|
146
|
+
const lexicalAdapter = field.editor;
|
|
147
|
+
const sanitizedServerEditorConfig = lexicalAdapter.editorConfig;
|
|
148
|
+
const html = await convertLexicalToHTML({
|
|
149
|
+
converters: consolidateHTMLConverters({
|
|
150
|
+
editorConfig: sanitizedServerEditorConfig
|
|
151
|
+
}),
|
|
152
|
+
data: doc[field.name]
|
|
153
|
+
});
|
|
154
|
+
transformedDoc[field.name] = extractTextFromHTML(html);
|
|
155
|
+
} else if (field.type === 'array' && field.fields) {
|
|
156
|
+
// For array fields, transform each item
|
|
157
|
+
transformedDoc[field.name] = await Promise.all((doc[field.name] || []).map((item)=>transformDocumentForMeilisearch(item, field.fields, req)));
|
|
158
|
+
} else if (field.type === 'tabs') {
|
|
159
|
+
// For tab fields, transform each tab
|
|
160
|
+
(await Promise.all(field.tabs.map(async (tab)=>{
|
|
161
|
+
return await transformDocumentForMeilisearch(doc, tab.fields, req);
|
|
162
|
+
}))).forEach((t)=>Object.assign(transformedDoc, t));
|
|
163
|
+
} else if (field.type === 'blocks') {
|
|
164
|
+
// For block fields, transform each block
|
|
165
|
+
transformedDoc[field.name] = await Promise.all((doc[field.name] || []).map(async (block)=>{
|
|
166
|
+
const fields = field.blocks.find((b)=>b.slug === block.blockType)?.fields;
|
|
167
|
+
if (!fields) {
|
|
168
|
+
return {
|
|
169
|
+
type: block.blockType,
|
|
170
|
+
data: ""
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
type: block.blockType,
|
|
175
|
+
data: await transformDocumentForMeilisearch(block, fields, req)
|
|
176
|
+
};
|
|
177
|
+
}));
|
|
178
|
+
} else if (field.type === 'group' && field.fields) {
|
|
179
|
+
// For group fields, transform the nested fields
|
|
180
|
+
transformedDoc[field.name] = await transformDocumentForMeilisearch(doc[field.name], field.fields, req);
|
|
181
|
+
} else if (field.type === 'relationship' || field.type === 'upload') {
|
|
182
|
+
// For relationship and upload fields, just include the ID
|
|
183
|
+
transformedDoc[field.name] = doc[field.name]?.id || doc[field.name];
|
|
184
|
+
} else {
|
|
185
|
+
// For other field types, include as-is
|
|
186
|
+
try {
|
|
187
|
+
// @ts-ignore
|
|
188
|
+
transformedDoc[field.name] = doc[field.name];
|
|
189
|
+
} catch (err) {
|
|
190
|
+
console.log(err, field, doc);
|
|
391
191
|
}
|
|
392
|
-
}
|
|
393
|
-
});
|
|
192
|
+
}
|
|
193
|
+
}));
|
|
194
|
+
return transformedDoc;
|
|
394
195
|
}
|
|
196
|
+
|
|
395
197
|
//# sourceMappingURL=indexCollections.js.map
|