Skip to content

Commit 42c38de

Browse files
committed
Avoid bootstrap call to MeiliSearch without host
1 parent 33331e4 commit 42c38de

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

config/functions/bootstrap.js

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ const meilisearch = {
1717
lifecycles: () => strapi.plugins.meilisearch.services.lifecycles
1818
}
1919

20-
async function getClient () {
21-
const credentials = await getCredentials()
20+
async function getClient (credentials) {
2221
const client = meilisearch.client(credentials)
2322
return await meilisearch.http(client)
2423
}
@@ -38,29 +37,55 @@ async function getCredentials () {
3837
return { apiKey, host }
3938
}
4039

40+
function addHookedCollectionsToStore ({ store, collections }) {
41+
store.set({
42+
key: 'meilisearch_hooked',
43+
value: collections
44+
})
45+
}
46+
47+
function addLifecycles ({ client, collections }) {
48+
// Add lifecyles
49+
collections.map(collection => {
50+
const model = strapi.models[collection]
51+
const meilisearchLifecycles = Object.keys(meilisearch.lifecycles())
52+
model.lifecycles = model.lifecycles || {}
53+
54+
meilisearchLifecycles.map(lifecycleName => {
55+
const fn = model.lifecycles[lifecycleName] || (() => {})
56+
model.lifecycles[lifecycleName] = (data) => {
57+
fn(data)
58+
meilisearch.lifecycles()[lifecycleName](data, collection, client)
59+
}
60+
})
61+
})
62+
}
63+
4164
async function initHooks (store) {
4265
try {
43-
const models = strapi.models
44-
const httpClient = await getClient()
45-
const indexes = (await getIndexes(httpClient)).map(index => index.uid)
46-
const indexedCollections = Object.keys(models).filter(model => indexes.includes(model))
47-
indexedCollections.map(collection => {
48-
const model = strapi.models[collection]
49-
const meilisearchLifecycles = Object.keys(meilisearch.lifecycles())
50-
model.lifecycles = model.lifecycles || {}
66+
const credentials = await getCredentials()
67+
console.log({ credentials })
68+
if (credentials.host) {
69+
const client = await getClient(credentials)
70+
// get list of indexes in MeiliSearch Instance
71+
const indexes = (await getIndexes(client)).map(index => index.uid)
72+
73+
// Collections in Strapi
74+
const models = strapi.models
5175

52-
meilisearchLifecycles.map(lifecycleName => {
53-
const fn = model.lifecycles[lifecycleName] || (() => {})
54-
model.lifecycles[lifecycleName] = (data) => {
55-
fn(data)
56-
meilisearch.lifecycles()[lifecycleName](data, collection, httpClient)
57-
}
76+
// get list of Indexes In MeilISearch that are Collections in Strapi
77+
const indexedCollections = Object.keys(models).filter(model => indexes.includes(model))
78+
addLifecycles({
79+
collections: indexedCollections,
80+
client
5881
})
59-
})
60-
store.set({
61-
key: 'meilisearch_hooked',
62-
value: indexedCollections
63-
})
82+
addHookedCollectionsToStore({
83+
collections: indexedCollections,
84+
store
85+
})
86+
}
87+
88+
// Add collections to hooked store
6489
} catch (e) {
6590
console.error(e)
6691
}

0 commit comments

Comments
 (0)