Provide a hook for dealing with file uploads or a dedicated CollectionAfterCreateHook #5565
Replies: 2 comments
-
Note - I've also tried the above making the hook synchronous to work around this issue, but while I don't get an "Unfound" error, the document doesn't updating. Im assuming this is some kind of race condition where the original update is happening after my change or something. |
Beta Was this translation helpful? Give feedback.
-
Hey @mortocks, You can use the collection afterChangeHook exactly as you intended to do so. The reason the There isn't anything else you should need to do, as long as the update works the entire change is committed all at once. Let me know if you have any trouble! |
Beta Was this translation helpful? Give feedback.
-
Background
We have a collection where users upload images which need to be moderated. As a first pass we're wanting AI to attempt moderation just to reduce the workload for the moderation team.
Ideally this kind of work would be done in the
CollectionBeforeChangeHook
hook but since the collection has a file upload, we need to wait until the file as actually been uploaded before we can do anything with it, so we have to move the work to theCollectionAfterChangeHook
hook.Unfortunately, we can't directly modify the record and return it like we can in the
CollectionBeforeChangeHook
so we have to manually update the record using thereq.payload
object.Here's an pseudo example of the current implementation;
The issue is that while the hook is called
CollectionAfterChangeHook
, we're assuming that technically the record hasn't been written in the database yet so calling update returns NOT FOUND error, however if we try to use thecreate
function to manually create the record, the file's won't get uploaded and we get the errorMissingFile: No files were uploaded.
Potential solutions
Provide a dedicated hook called
CollectionAfterCreate
which behaves like theCollectionAfterChangeHook
but is called after the record is written to the db but runs before the document is returned.Alternately, let records be updated via the
CollectionAfterChangeHook
by just returning the shape (although I can see how this would cause confusion to the correct use case for this hook so it's probably not ideal and cause confusion as to when to write to the dbProvide a hook that runs after file uploads happens, something like
CollectionAfterFileUpload
. I think this would be the most complicated both from implementation and from an API for the userProvide a some way of dealing with the image data in the
CollectionBeforeChange
hook. Currently we just get file information and not the actual image - again I think this solution is probably not idea.Primary cause
I think the API of hooks is really powerful and works in most data use-cases, it's just because there are file uploads that the order of events becomes difficult to create this kind of functionality. As a short term solution we're moving to cron jobs but using payloads hooks would be a much more elegant solution.
Would also move to hear that we're doing something wrong and there is a solution in the current API 😉
Beta Was this translation helpful? Give feedback.
All reactions