Automatically update a collection/database when new product is added

Because the Products collection is read-only, I created another collection to store customized values that is used in code. How can I link it to the Products collection so that if a new product is added, a new entry/row appears in my second collection?

Hi @marenfpodarm ,
You can create a cron job ( https://support.wix.com/en/article/velo-scheduling-recurring-jobs ) that calls a function that reads a DB, e.g: Products, and populates another DB with desired values.

I would say an cron-job is even not needed in this case.

  1. Start a checking function in your onReady-Part of your code, which will start immediately and automaticaly when you visit the chosen page.

  2. This function will query all your stores-items, which has a certain LENGHT!

  3. Also you run a query of your second (own-database) and also you look for the lenght.

  4. You make a comparisson of both QUERY-LENGTHs.

  5. And the rest i think is self-explaining…

Conclusion: All you need to know is → if both query-lenghts are equal or not.

@russian-dima This solution may not be enough for a scenario where a product was replaced. The length remains and no db update will be done. the comparison should be on id level.
In addition, after you update dozens of products, you might not want to run DB update upon onReady, this will result in performance issues.
@marenfpodarm I’d suggest combining the solutions and run the DB update in the background with a crone job and when the page loads, use the DB update function in onReady() so any product leftovers that were update since the last crone job will be covered upon page load.

@jacobg
Ok, indeed i did not think about the update-option. Good point!

Thank you all!

@jacobg @russian-dima
Your answer to this question also solves my problem. I’m trying to achieve the same mentioned. Have a job that reads a database (e.g. Products, but in my case the Posts from my blog) and populates another database with any new addition (of Products in the example given, but in my case added posts).

I’m new to coding and don’t know exactly how to go about coding that job. I’ve got the job file and everything set up, but don’t know how to write the .js file for this job. Any suggestions?

I’m guessing something with “query” and “update” functions… If I could have an example to start with it would help a lot. Thank you!

Just was thrown back to this post again.

How to be able to avoid the usage of a cron-job? :grin:

I am of the opinion that a cron-job still not needed.

@Velo-Ninja This solution may not be enough for a scenario where a product was replaced. The length remains and no db update will be done. the comparison should be on id level.

Yes this was a good point, i did not think of first time.
But what if we generate a function, which would run through the …"
_updatedDate"-Field and check for CHANGES ?

Every DB has this field included out of the box.

So now we would have 1 running LOOP, checking for all last updated dates (like a CHECKSUM), comparing both databases + checking for length of both databases.

CHECKSUM right ? = no changes were maden in DB
CHECKSUM wrong ? = attention, DB has changed its value(s)

Now you are able to know, if and which entry has been changed.

This would always run onReady(), when you enter the related page.

Adding the generated function into an INTERVAL, this normaly should do the same thing like a cron job.