Sync 2 collections

I’d like to have 2 collections that one mirrors the other. i.e. if I insert a new record or update an existing one in collection1, it’ll immediately update collection2 accordingly (but not vise versa. The updates always take place on collection1 first).
So to do that I’ve created an afterUpdate and afterInsert hooks, which work fine, but the problem is that these hooks do not trigger on collection sync (from sandbox to live).
any advice how to achieve my goal?

Thanks,

1 Like

Take a look at Reference Fields.
When you create reference fields , you create a pointer to a different collection, creating a connection between the collections in the actual structure of the primary collection.
This connection gives you access to all the information in all the referenced collections from within the primary collection: https://support.wix.com/en/article/about-connecting-multiple-database-collections

Maybe the update() function can also be helpful for what you are trying to achieve: https://www.wix.com/corvid/reference/wix-data.html#update

@yevheniias , thanks, but I don’t think it can help me to create a copy of a collection.

Let me explain why I need it (or exactly what I need).
So it goes like this:
Collection A contains items that are used by the program.
Collection B is a catalog collection for the sales & operation department.
They both have common fields that must be in sync, and they both have unique fields of their own.
The common fields should only get updated via collection A (i.e. not manually by the sales person).

So I created an afterUpdate/afterInsert hooks that copy the common field values from collection A to collection B.

And I created another hook so if a sale person tries to update a value in a common field in Collection B, this update attempt will be rejected (a sales person can only manually update the unique fields in collection B, not the common fields).

Now everything can work fine except that if I update an item in collection A by synchronizing from the sandbox, it won’t be reflected in collection B (because the hook don’t trigger).
Any advice?

I’ve the same issue,

But I think it’s preferable to not trigger the hook on syncing other you risk to duplicate your insert.

The way I solved that is by having a dashboard page call “syncer” with a button. on click the syncer erase collection2 and duplicate all items from collection1 into 2.

That will fork fine if you have less than 13000 items. Scripts timeout after that.

Other solution:

Since hooks are active in sandboxe why not import collection B as well? a bit manual but no code required

Thank you!
It’s a nice idea. I’ll try it (but in my case it’s more complicated as Collection2 contains some independent fields, so I can’t just erase it, I’ll need to update the common fields only and it might be a “heavier” action which affect the timeout point ).

@plomteuxquentin , I modified your solution, and now the common fields get updated in collection2 via collection1 (only), while both collections have there own unique fields.
Thank you for the advice.
:slight_smile: