I would like to duplicate a filtered set of data from a private database to a public database and have the sync run either periodically or when a new item is added to the private database. Is this possible and what would be the best way to approach this?
Need a bit more info to answer this.You mention “database”. Sometimes this word is also (mis)used for “collections”. So are we talking about 2 different databases (like 2 different Wix Projects/sites or 1 Wix/1 non Wix) or are we talking about 1 Wix site with 2 member collections, 1 public, 1 private?
Sorry, yeah, I meant collections. Here’s what I’m messing around with as an afterInsert hook on the private collection but just tried it and the public collection didn’t update.
import wixData from ‘wix-data’ ;
export function PublicationsPrivate _afterInsert ( item , context ) {
let hookContext = context ;
if ( item .flagPublic == TRUE) {
let toInsert = {
“date” : item . date ,
“title” : item . citation ,
“link” : item . link
};
wixData . insert ( "PublicationsPublic" , toInsert )
. then (( results ) => {
**let** item = results ; //see item below
})
. **catch** (( err ) => {
**let** errorMsg = err ;
});
}
**return** item ;
}
I’m thinking a hook may not be the way to go and it might be easier to code it up on the page where I have the form for the insert. Basically I want to send information from the form into two different collections, but only one of the collections will get the item unless a certain field is flagged.
@lstright To be honest, it does not really matter in a technical way where you put the sync, in a hook or during input. You say it does not update. Don’t see anything wrong with your code. Have you looked at either the browser console or Wix Site monitor for error messages (because I cannot see if this is a frontend or a backend function, but I guess the last)?
If you see any error message like “Permission denied”, than usually the 2nd collection has write permissions for admin only and you will have to lift restriction with "“suppressAuth” : true".
Let me know.
@giri-zano That worked. Thank you!