Skip to content

Commit 98cab0f

Browse files
Merge #470 #471
470: Make afterDeleteMany a working hook r=bidoubiwa a=bidoubiwa fixes: #467 471: Make afterUpdateMany a working hook r=bidoubiwa a=bidoubiwa fixes: #396 TODO - [ ] needs to paginate on the entries Co-authored-by: Charlotte Vermandel <[email protected]>
3 parents cba7071 + d058890 + bc4ef79 commit 98cab0f

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

server/services/lifecycle/lifecycle.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,39 @@ module.exports = ({ strapi }) => {
7272
)
7373
})
7474
},
75-
async afterUpdateMany() {
76-
strapi.log.error(
77-
`Meilisearch could not find an example on how to access the \`afterUpdateMany\` hook. Please consider making an issue to explain your use case`
78-
)
75+
async afterUpdateMany(event) {
76+
const meilisearch = strapi
77+
.plugin('meilisearch')
78+
.service('meilisearch')
79+
80+
const nbrEntries = await contentTypeService.numberOfEntries({
81+
contentType: contentTypeUid,
82+
where: event.params.where,
83+
})
84+
85+
const entries = []
86+
const BATCH_SIZE = 500
87+
88+
for (let pos = 0; pos < nbrEntries; pos += BATCH_SIZE) {
89+
const batch = await contentTypeService.getEntries({
90+
contentType: contentTypeUid,
91+
filters: event.params.where,
92+
start: pos,
93+
limit: BATCH_SIZE,
94+
})
95+
entries.push(...batch)
96+
}
97+
98+
meilisearch
99+
.updateEntriesInMeilisearch({
100+
contentType: contentTypeUid,
101+
entries: entries,
102+
})
103+
.catch(e => {
104+
strapi.log.error(
105+
`Meilisearch could not update the entries: ${e.message}`
106+
)
107+
})
79108
},
80109
async afterDelete(event) {
81110
const { result, params } = event
@@ -106,10 +135,8 @@ module.exports = ({ strapi }) => {
106135
)
107136
})
108137
},
109-
async afterDeleteMany() {
110-
strapi.log.error(
111-
`Meilisearch could not find an example on how to access the \`afterDeleteMany\` hook. Please consider making an issue to explain your use case`
112-
)
138+
async afterDeleteMany(event) {
139+
this.afterDelete(event)
113140
},
114141
})
115142

0 commit comments

Comments
 (0)