Skip to content

Commit 6172075

Browse files
author
Haye de Wit
authored
update index settings when updating a collection (#328)
1 parent b7374fd commit 6172075

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

connectors/collection.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,26 @@ module.exports = ({ services, models, logger }) => {
152152
}
153153
return entries
154154
},
155+
156+
/**
157+
* Returns MeiliSearch index settings from model definition.
158+
* @param collection - Name of the Collection.
159+
* @typedef Settings
160+
* @type {import('meilisearch').Settings}
161+
* @return {Settings} - MeiliSearch index settings
162+
*/
163+
getSettings: function (collection) {
164+
const model = models[collection].meilisearch || {}
165+
const settings = model.settings || {}
166+
167+
if (typeof settings !== 'object') {
168+
logger.warn(
169+
`[MEILISEARCH]: "settings" provided in the model of the ${collection} must be an object.`
170+
)
171+
return {}
172+
}
173+
174+
return settings
175+
},
155176
}
156177
}

connectors/meilisearch/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,20 @@ module.exports = async ({ storeConnector, collectionConnector }) => {
279279
entries: transformedEntries,
280280
})
281281

282+
// Get MeiliSearch Index settings from model
283+
const settings = collectionConnector.getSettings(collection)
284+
285+
// Update MeiliSearch index settings if settings not empty
286+
if (settings && Object.keys(settings).length !== 0) {
287+
try {
288+
await client.index(indexUid).updateSettings(settings)
289+
} catch (error) {
290+
console.error(
291+
`[MEILISEARCH]: Failed updating MeiliSearch settings for collection: ${collection}. Please check your settings.`
292+
)
293+
}
294+
}
295+
282296
// Add documents in MeiliSearch
283297
const { updateId } = await client
284298
.index(indexUid)

0 commit comments

Comments
 (0)