Skip to content

Commit 406a9a2

Browse files
authored
Merge pull request #3 from L-Weisz/feat/indexNames
Feat/index names
2 parents 74ef381 + ae2b79e commit 406a9a2

File tree

6 files changed

+80
-16
lines changed

6 files changed

+80
-16
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,22 @@ To link a single collection to multiple indexes, you can assign an array of inde
221221

222222
**Example 1: Linking a Single Collection to a Single Index**
223223

224-
In the following example, the `restaurant` content-type in Meilisearch is called `my_restaurant` instead of the default `restaurant`.
224+
In the following examples, the `restaurant` content-type in Meilisearch is called `my_restaurant` instead of the default `restaurant`.
225+
226+
```js
227+
// config/plugins.js
228+
229+
module.exports = () => ({
230+
//...
231+
meilisearch: {
232+
config: {
233+
restaurant: {
234+
indexName: "my_restaurants",
235+
}
236+
}
237+
}
238+
})
239+
```
225240

226241
```js
227242
// config/plugins.js

server/__tests__/configuration-validation.test.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('Test plugin configuration', () => {
113113
expect(strapiMock.log.error).toHaveBeenCalledTimes(0)
114114
})
115115

116-
test('Test indexName with string', async () => {
116+
test('Test indexName with empty string', async () => {
117117
validatePluginConfig({
118118
restaurant: {
119119
indexName: '',
@@ -122,14 +122,50 @@ describe('Test plugin configuration', () => {
122122
expect(strapiMock.log.warn).toHaveBeenCalledTimes(0)
123123
expect(strapiMock.log.error).toHaveBeenCalledTimes(1)
124124
expect(strapiMock.log.error).toHaveBeenCalledWith(
125-
'The "indexName" option of "restaurant" should be a non-empty array of strings',
125+
'The "indexName" option of "restaurant" should be a non-empty string or an array of non-empty strings',
126126
)
127127
})
128128

129-
test('Test indexName with non-empty array', async () => {
129+
test('Test indexName with non-empty string', async () => {
130130
validatePluginConfig({
131131
restaurant: {
132-
indexName: ['hello'],
132+
indexName: 'validName',
133+
},
134+
})
135+
expect(strapiMock.log.warn).toHaveBeenCalledTimes(0)
136+
expect(strapiMock.log.error).toHaveBeenCalledTimes(0)
137+
})
138+
139+
test('Test indexName with empty array', async () => {
140+
validatePluginConfig({
141+
restaurant: {
142+
indexName: [],
143+
},
144+
})
145+
expect(strapiMock.log.warn).toHaveBeenCalledTimes(0)
146+
expect(strapiMock.log.error).toHaveBeenCalledTimes(1)
147+
expect(strapiMock.log.error).toHaveBeenCalledWith(
148+
'The "indexName" option of "restaurant" should be a non-empty string or an array of non-empty strings',
149+
)
150+
})
151+
152+
test('Test indexName with non-empty array containing empty string', async () => {
153+
validatePluginConfig({
154+
restaurant: {
155+
indexName: [''],
156+
},
157+
})
158+
expect(strapiMock.log.warn).toHaveBeenCalledTimes(0)
159+
expect(strapiMock.log.error).toHaveBeenCalledTimes(1)
160+
expect(strapiMock.log.error).toHaveBeenCalledWith(
161+
'The "indexName" option of "restaurant" should be a non-empty string or an array of non-empty strings',
162+
)
163+
})
164+
165+
test('Test indexName with non-empty array of strings', async () => {
166+
validatePluginConfig({
167+
restaurant: {
168+
indexName: ['validName1', 'validName2'],
133169
},
134170
})
135171
expect(strapiMock.log.warn).toHaveBeenCalledTimes(0)

server/__tests__/meilisearch.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('Tests content types', () => {
110110
)
111111
expect(client.index('').addDocuments).toHaveBeenCalledTimes(1)
112112
expect(client.index).toHaveBeenCalledWith('customIndex')
113-
expect(tasks).toEqual(10)
113+
expect(tasks).toEqual([10])
114114
})
115115

116116
test('Test to add entries linked to multiple indexes in Meilisearch', async () => {
@@ -199,7 +199,7 @@ describe('Tests content types', () => {
199199
expect(client.index('').addDocuments).toHaveBeenCalledTimes(2)
200200
expect(client.index).toHaveBeenCalledWith('customIndex')
201201
expect(client.index).toHaveBeenCalledWith('anotherIndex')
202-
expect(tasks).toEqual(10)
202+
expect(tasks).toEqual([10, 10])
203203
})
204204

205205
test('Test to delete entries from Meilisearch', async () => {

server/configuration-validation.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,21 @@ function CollectionConfig({ collectionName, configuration }) {
182182

183183
return {
184184
validateIndexName() {
185-
// indexName is either undefined or a non empty array of string
186-
if (indexName !== undefined && !Array.isArray(indexName)) {
185+
// indexName is either undefined, a non-empty string, or a non-empty array of non-empty strings
186+
const isStringAndNotEmpty =
187+
typeof indexName === 'string' && indexName !== ''
188+
189+
const isArrayWithNonEmptyStrings =
190+
Array.isArray(indexName) &&
191+
indexName.length > 0 &&
192+
indexName.every(name => typeof name === 'string' && name !== '')
193+
194+
if (
195+
indexName !== undefined &&
196+
!(isStringAndNotEmpty || isArrayWithNonEmptyStrings)
197+
) {
187198
log.error(
188-
`The "indexName" option of "${collectionName}" should be a non-empty array of strings`,
199+
`The "indexName" option of "${collectionName}" should be a non-empty string or an array of non-empty strings`,
189200
)
190201
} else if (indexName !== undefined) {
191202
options.indexName = indexName

server/services/meilisearch/config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ module.exports = ({ strapi }) => {
3333

3434
const contentTypeConfig = meilisearchConfig[collection] || {}
3535

36-
let indexName = contentTypeConfig.indexName
37-
if (indexName !== undefined && !Array.isArray(indexName))
38-
indexName = [indexName]
36+
let indexNames = [contentTypeConfig.indexName]
37+
.flat(2)
38+
.filter(index => index)
3939

40-
return indexName || [collection]
40+
return indexNames.length > 0 ? indexNames : [collection]
4141
},
4242

4343
/**

server/services/meilisearch/connector.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,11 @@ module.exports = ({ strapi, adapter, config }) => {
104104
const task = await client
105105
.index(indexUid)
106106
.deleteDocuments(documentsIds)
107+
107108
strapi.log.info(
108109
`A task to delete ${documentsIds.length} documents of the index "${indexUid}" in Meilisearch has been enqueued (Task uid: ${task.taskUid}).`,
109110
)
111+
110112
return task
111113
}),
112114
)
@@ -267,7 +269,7 @@ module.exports = ({ strapi, adapter, config }) => {
267269
* @param {object} options
268270
* @param {string} options.contentType - ContentType name.
269271
* @param {object[] | object} options.entries - Entry from the document.
270-
* @returns {Promise<{ taskUid: number }>} - Task identifier.
272+
* @returns {Promise<{ taskUid: number }[]>} - Task identifiers.
271273
*/
272274
addEntriesToMeilisearch: async function ({ contentType, entries }) {
273275
const { apiKey, host } = await store.getCredentials()
@@ -298,7 +300,7 @@ module.exports = ({ strapi, adapter, config }) => {
298300
}),
299301
)
300302

301-
return tasks.flat()[0]
303+
return tasks.flat()
302304
},
303305

304306
/**

0 commit comments

Comments
 (0)