Combine specific rows from multiple data collections

Hello
Need a little help to get some direction in order to achieve my goal.
I have multiple data collections with the same fields,each data collection belongs to specfic user.
I want to take specfic rows from all the data collection and insert them into one data collection.
For example, search all data collection, if the field “price” is not null, take the entire row and copy it to another data collection. This way I will merge rows from multiple data collection into one data collection.
Where should I begin from?.
I saw some staff about field links and symbol links but that’s not what i need, i think i can achieve my goal only with a code, I thought to achieve it with hooks, on each data collection I will insert hook “if field x is not empty,insert the following fields into data collection x as well”.
Is it possible?.

Thank you

Is it a one-time task?Or every any time you insert or update any of your collections?

Every time I insert or update a collection.
It doesn’t have to be online, I can use a script every 30 min if needed but i gues with hooks it will be online which is better.

@meirhasin so you can use hooks.

//backend/data.js
import wixData from 'wix-data';
function updateGeneralCollection(item){
 return wixData.save("GeneralCollectionName", item);
}
export function CollectionA_beforeInsert(item, context) {
 if (item.price){
  return updateGeneralCollection(item).then(() => item);
 }
 return item;
}
export function CollectionA_beforeUpdate(item, context) {
 if (item.price){
  return updateGeneralCollection(item).then(() => item);
 }
 return item;
}
export function CollectionB_beforeInsert(item, context) {
 if (item.price){
  return updateGeneralCollection(item).then(() => item);
 }
 return item;
}
//etc...

But of course this is not going to work unless you update or insert a new item to your collections.
And use supress auth if needed.

(fixed)

1 Like

@jonatandor35 Thank you very much! i’m testing it.

@meirhasin and be aware that if you delete the price value of record#1 from collectionA after that, it won’t remove it from the general collection unless you modify the code.

@jonatandor35 works perfect!. thank you very much.