Skip to content

Commit ce71eea

Browse files
committed
feat(meilisearch-connector): update addEntriesToMeilisearch
1 parent 835c6c2 commit ce71eea

File tree

2 files changed

+104
-9
lines changed

2 files changed

+104
-9
lines changed

server/__tests__/meilisearch.test.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,96 @@ describe('Tests content types', () => {
113113
expect(tasks).toEqual(10)
114114
})
115115

116+
117+
test('Test to add entries linked to multiple indexes in Meilisearch', async () => {
118+
const pluginMock = jest.fn(() => ({
119+
// This rewrites only the needed methods to reach the system under test (removeSensitiveFields)
120+
service: jest.fn().mockImplementation(() => {
121+
return {
122+
async actionInBatches({ contentType = 'restaurant', callback }) {
123+
await callback({
124+
entries: [
125+
{
126+
id: 1,
127+
title: 'title',
128+
internal_notes: 'note123',
129+
secret: '123',
130+
},
131+
{
132+
id: 2,
133+
title: 'abc',
134+
internal_notes: 'note234',
135+
secret: '234',
136+
},
137+
],
138+
contentType,
139+
})
140+
},
141+
getCollectionName: ({ contentType }) => contentType,
142+
addIndexedContentType: jest.fn(),
143+
subscribeContentType: jest.fn(),
144+
getCredentials: () => ({}),
145+
}
146+
}),
147+
}))
148+
149+
// Spy
150+
const client = new Meilisearch({ host: 'abc' })
151+
152+
const meilisearchService = createMeilisearchService({
153+
strapi: {
154+
plugin: pluginMock,
155+
contentTypes: {
156+
restaurant: {
157+
attributes: {
158+
id: { private: false },
159+
title: { private: false },
160+
internal_notes: { private: true },
161+
secret: { private: true },
162+
},
163+
},
164+
},
165+
config: {
166+
get: jest.fn(() => ({
167+
restaurant: {
168+
noSanitizePrivateFields: ['internal_notes'],
169+
indexName: ['customIndex', 'anotherIndex'],
170+
},
171+
})),
172+
},
173+
log: mockLogger,
174+
},
175+
contentTypes: {
176+
restaurant: {
177+
attributes: {
178+
id: { private: false },
179+
title: { private: false },
180+
internal_notes: { private: true },
181+
secret: { private: true },
182+
},
183+
},
184+
},
185+
})
186+
187+
const mockEntry = { attributes: { id: 1 } }
188+
const tasks = await meilisearchService.addEntriesToMeilisearch({
189+
contentType: 'restaurant',
190+
entries: [mockEntry, mockEntry],
191+
})
192+
193+
expect(strapi.log.info).toHaveBeenCalledTimes(2)
194+
expect(strapi.log.info).toHaveBeenCalledWith(
195+
'The task to add 2 documents to the Meilisearch index "customIndex" has been enqueued (Task uid: undefined).'
196+
)
197+
expect(strapi.log.info).toHaveBeenCalledWith(
198+
'The task to add 2 documents to the Meilisearch index "anotherIndex" has been enqueued (Task uid: undefined).'
199+
)
200+
expect(client.index('').addDocuments).toHaveBeenCalledTimes(2)
201+
expect(client.index).toHaveBeenCalledWith('customIndex')
202+
expect(client.index).toHaveBeenCalledWith('anotherIndex')
203+
expect(tasks).toEqual(10)
204+
})
205+
116206
test('Test to delete entries from Meilisearch', async () => {
117207
const customStrapi = createStrapiMock({
118208
restaurantConfig: {

server/services/meilisearch/connector.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ module.exports = ({ strapi, adapter, config }) => {
251251
},
252252

253253
/**
254-
* Add entries from a contentType to its index in Meilisearch.
254+
* Add entries from a contentType to all its indexes in Meilisearch.
255255
*
256256
* @param {object} options
257257
* @param {string} options.contentType - ContentType name.
@@ -264,25 +264,30 @@ module.exports = ({ strapi, adapter, config }) => {
264264

265265
if (!Array.isArray(entries)) entries = [entries]
266266

267-
const indexUid = config.getIndexNameOfContentType({ contentType })
267+
const indexUids = config.getIndexNamesOfContentType({ contentType })
268268
const documents = await sanitizeEntries({
269269
contentType,
270270
entries,
271271
config,
272272
adapter,
273273
})
274274

275-
const task = await client
276-
.index(indexUid)
277-
.addDocuments(documents, { primaryKey: '_meilisearch_id' })
275+
const tasks = await Promise.all(
276+
indexUids.map(async indexUid => {
277+
const task = await client
278+
.index(indexUid)
279+
.addDocuments(documents, { primaryKey: '_meilisearch_id' })
278280

279-
await store.addIndexedContentType({ contentType })
281+
await store.addIndexedContentType({ contentType })
280282

281-
strapi.log.info(
282-
`The task to add ${documents.length} documents to the Meilisearch index "${indexUid}" has been enqueued (Task uid: ${task.taskUid}).`,
283+
strapi.log.info(
284+
`The task to add ${documents.length} documents to the Meilisearch index "${indexUid}" has been enqueued (Task uid: ${task.taskUid}).`,
285+
)
286+
return task
287+
}),
283288
)
284289

285-
return task
290+
return tasks.flat()[0]
286291
},
287292

288293
/**

0 commit comments

Comments
 (0)