Is it possible to have an afterChange hook on a global that query all docs of a collections and updates all the docs with the property that changed in the global to the new value?
If so is the afterChange the way to do it or there's a better way? How can I make the query?
Yes, this is possible. AfterChange hook makes sense. This is similar to how it would look. An update w/ a where clause.
Careful on this, though, as it could be updating a large number of records depending on how strict your where clause is.
Thank you, this example seems pretty straightforward. I'll try this and report back. This operation could potentially be updating all records in the collection. Most collections have about 50 records and some could grow to a max of 200 in the future. Could that be a problem? The async operation waits for it to be done, so if it's taking a while is there some sort of feedback in the admin that's telling me it's done updating all the records?
I'd probably try to configure a test project that would update that many records to see if the performance is acceptable.
There is nothing available right now to allow for a background process with a notification afterward.
You could possibly leave the promise unawaited and allow it to run in the background. However, you would not get any indication of failure.
I have a test database on local, so I'll do some tests first and report back here. But yeah if there's any kind of issues during the process and it stops midway I will find some entries unmodified which can effect the application functionality unexpectedly.
After some quick test it looks like it's working great. On small collections it's immediate and I get the green confirmation at the bottom "updated successfully", on a collection of 100 records updating about 80 of them I can see a loading overlay on the entire screen for 1-2sec saying updating name of global and then I see the green confirmation, so I believe that's only after the change is done. If I have a syntax error in the update request I get a red alert at the bottom "Cast to string failed for value …" the same error I get in the log, which is great. But of course this hook is done afterChange so the global does update successfully.
I just tried the beforeChangeHook and actually works as expected, if en error is encountered it's shown in the red alert at the bottom and it doesn't complete the change, so the global is not updated. It's not resetting the form field, but if I refresh the page and I can see the old value. That's great because I'm sure nothing gets updated if there's an error. Now I just need to update the logic for my dynamic fields of my use case, but I thinks I found a solution.
Awesome work! 🙌
While trying to make a dynamic afterchangehook I'm stuck on an issue. How can I pass a variable inside the update operation?
const property = opzione.property;
await req.payload.update({
collection: "e02",
where: {
property: { equals: opzione.oldValue },
},
data: {
property: opzione.newValue,
},
});
here the variable property is not passed inside the query, but I get
QueryError: The following path cannot be queried: property
Star
Discord
online
Get dedicated engineering support directly from the Payload team..