Data Collection A:
ID,name,email → has values
Data Collection B:
ID,name,email → need the same values from data collection A.
Please help with any simple solution for this
Data Collection A:
ID,name,email → has values
Data Collection B:
ID,name,email → need the same values from data collection A.
Please help with any simple solution for this
More details are needed. When do you want this copy to take place? Once a new record has inserted to collectionA? Once an existing record in collection A has been updated? Once a day? On button click?
Hi, I’m looking to do the same thing. Specifically, automatically moving the data from one field of Database A to Database B. Thanks!
@deleteduser Um, what more detail do you need? I have a database, “Database A,” and I want to AUTOMATICALLY move data from one field of “Database A” to another database, “Database B.” Let’s say once every 24 hours. Is that specific enough?
@ezrasmithdesign if you want it to happen once the data has been inserted to collection A.
Then use afterInsert hook and put inside an insert() function for collection B:
https://www.wix.com/corvid/reference/wix-data.Hooks.html#afterInsert
https://www.wix.com/corvid/reference/wix-data.html#insert
If an existing record may be updated and you want to mirror the update as well, then use afterUpdate hook:
https://www.wix.com/corvid/reference/wix-data.Hooks.html#afterUpdate
https://www.wix.com/corvid/reference/wix-data.html#update
If you don’t want it to happen in real time , but only once a day, create a scheduled job, get all items from collectionA using qeury() and use bulkInsert() to add them to B:
https://www.wix.com/corvid/reference/wix-data.html#query
https://www.wix.com/corvid/reference/wix-data.html#bulkInsert
https://support.wix.com/en/article/corvid-scheduling-recurring-jobs
Awesome! Thanks @jonatandor35 !
@ezrasmithdesign you’re welcome. And you should know that maybe there could be even easier ways to achieve what you want if you added some more details, such as how you save the records to collection , and why you don’t just save it to A and B together one after the other.
Like:
Promise.all([wixData.insert("collectionA", obj), wixData.insert("collectionB", obj)])
.then((res) => {
console.log(res[0]);
console.log(res[1]);
})
If you’re talking about the private members data collection, you won’t be able to use hooks for this mirroring.
I want to duplicate the services collection as I want to add more fields to the Booking service
Has this issue been taken up by the wix team?
Hello, did you ever find a solution to this? I am trying to do the same thing.
Hello, did you ever find a solution to this?
I’ve been looking for the same soluction but it’s not working what iḿ trying
import wixData from 'wix-data';
export function Members$PrivateMembersData_afterInsert(item, PrivateMembersData ) {
let hookContext = PrivateMembersData;
// see below
// some changes to the received item
return item;
}
export function testes_rapha$mytable_Insert(item, mytable);
export function Members$PrivateMembersData_afterUpdate(item2, PrivateMembersData ) {
let hookContext = PrivateMembersData;
// some changes to the received item
return item2;
}
wixData.update("testes_rapha$mytable", item2)
.then( (results) => {
let item = results; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );
Hi, did anyone in this thread get this to work and might have any example code they’d be willing to share? Or might be able to help with my example below?
I’m trying to test a simple example on my own. Let’s say:
CollectionA (collection ID = CollectionA)
ID (field key = _id)
TitleA (field key = titleA)
ImageA (field key = imageA)
urlA (field key = urlA)
CollectionB (collection ID = CollectionB)
ID (field key = _id)
TitleB (field key = titleB)
ImageB (field key = imageB)
urlB (field key = urlB)
A site member enters some data into a form on my site → the data populates into CollectionA → I’d like the identical data to then immediately get auto-populated into CollectionB (including the identical ID that was created in CollectionA). And if the line gets updated/modified in CollectionA, then those updates/modifications will overwrite the line in CollectionB (as opposed to generating a new line).
J.D. posted some nice resources which are great. But I’m confused if I am supposed to be attaching inserts and hooks to both collections, or just CollectionB. That’s why it’d be really great to have a simple example code (for this type of scenario) if anyone has one!
I might be able to dig up the code I got to work eventually, but basically a data hook "run[s] code before or after certain interactions with your site’s collections (from Wix: https://support.wix.com/en/article/velo-about-data-hooks)..) So, you will need a data hook for each thing you want to trigger your code. For your example, I would actually use J.D.'s second idea of a double database insert initially to populate the two databases (instead of using an afterInsert hook), and then create a hook that is triggered whenever Database A is updated to automatically update Database B. In other words: data gets copied and uploaded to two databases (no hooks); afterUpdate hook on Database A to sync data across both databases.
Can you explain more precisely what you are trying to do? Almost 100% of the time I find there is a better and simpler way of achieving something than my initial idea
(and by “more precisely” I mean what is the point of having two databases with identical content?)
@ezrasmithdesign Thanks for getting back! If you do find your old code, please let me know here or feel free to email it to me (timothyfehling@gmail.com). I learn best by example. If I can figure out my simple example above, I know I’ll be in great shape.
The example I wrote above is pretty much exactly what I’m trying to do.
The reason I have to configure it all this way is because I have some configuration on my site that specifically requires CollectionA to have Permissions of “Who can view content: Site Author.” And then I have some other configuration on my site that requires CollectionB to have Permissions of “Who can view content: Anyone.” But, both collections need to display identical data in real-time.
What I get really confused about is the insert() function in general. In my scenario, site members are submitting data into a Data Form → the data populates into CollectionA. So isn’t their submission what’s “inserting” an item into a collection ?
I’m thinking I’d have to use some combination of both afterInsert and afterUpdate. afterInsert to populate CollectionA’s data into CollectionB. And afterUpdate to update CollectionB’s data after an item in CollectionA gets updated by my site member. Right ?? I’m having a hard time wrapping my head around Hooks no matter how much I read through them and the examples (and the link you shared).
If the data in the two databases are identical, you should just set the read permission to “Anyone” (“Anyone can view content”) and then add and update permissions to “Site member author.” That way only site members can update and add content, but anyone can view it without fussing around with moving data between two databases.
But, if you still think that copying data between databases is the best option for you, here is some code I worked up (not tested):
Initial insert:
import wixData from 'wix-data';
//set up trigger when form submit button is clicked
export function formSubmit(event) {
//get content from Form:
let toInsert = {
"title": "Title.", //use $w("Title Input Field from From").value,
"Image": "Image", //connect to form field ^
"url": "Url" //connect to form field ^
};
//insert to A:
wixData.insert("databaseA", toInsert)
.then((results) => {
let item = results; //see item below
})
.catch((err) => {
let errorMsg = err;
});
//insert to B
wixData.insert("databaseB", toInsert)
.then((results) => {
let item = results; //see item below
})
.catch((err) => {
let errorMsg = err;
});
//notice that both insert function use the same toInsert object defined above
}
For the hooks, open the “Databases” section of the “Site Structure” panel, then click the ellipses next to Database A. Then add an “After Update” hook that would look something like this:
//In the data.js file (tab)
import wixData from 'wix-data';
export function DatabaseA_afterUpdate(item, context) {
let toInsertB = item;
wixData.insert("databaseB", toInsertB)
.then((results) => {
let item = results; //see item below
})
.catch((err) => {
let errorMsg = err;
});
}
You may run into issues because of the permissions on the databases, so if it doesn’t work right away check your permissions.
Re: Permissions, I wish I could do that and simply have 1 Collection. But it’s not possible unfortunately, for my scenario on the site.
Thanks so much for this example code! This is great.
A few questions:
The Initial insert code: do I place this code on the page with my Data Form? Or does it go on a data.js page?
Your green " formSubmit ." My form submission is a Data Form and not a Wix Form. It’s a container box (boxSubmit). So should I type that in instead? export function boxSubmit (event) {
Your labels " databaseA " and " databaseB ": are those Collection IDs? Or Collection Titles?
Your note about: “title”: “Title.” , //use $w(“Title Input Field from From”).value,
So if my title column (in the Collection) is called TitleA, and the field key = titleA, and the input field that connects/submits to that is labeled as #input1, would I write it like this:
“title”: “Title.”,$w(“#input1”).value
or like this
“titleA”: “TitleA.”,$w(“#input1”).value
I read through it all a few times and I think I understand everything else. Thanks again.
Yes, insert code happens on the form page. Data.js is just a file, not a page.
It doesn’t matter what you call it. You just need to add an onClick event to whichever button is the submit button.
Collection ID! Check here: https://www.wix.com/velo/reference/wix-data/insert
Neither. It would look like
"titleA": $w('#input1').value,
You’re welcome, happy to help.
Thank you ezrasmithdesign for your help and example code, and thank you J.D. for those links. I was able to get this all to work. I’ll paste my example below for anyone to reference in the future:
CollectionA (collection ID = CollectionA)
ID (field key = _id)
Title (field key = title)
URL (field key = url)
CollectionB (collection ID = CollectionB)
ID (field key = _id)
Title (field key = title)
URL (field key = url)
You’ll need to experiment with your Collections Permissions based on your site and specific scenario, but here’s what I did on my end to get it to work the way I wanted:
Collection A Permissions:
Site member author: can view content
Site member: can add content
Site member author: can delete content
Site member author: can update content
Collection B Permissions:
Anyone: can view content
Site member: can add content
Site member: can delete content
Site member: can update content
In this example, let’s say you have a page on your site called Page1 and a container box for site members to submit some info (their Title, their URL, and a Submit button). Using a dataset on Page1, you connect your Title field, URL field and Submit button to your CollectionA collection, WRITE mode. When a site member hits Submit, this channels their info into CollectionA.
You also decide to put another container box with the same fields, but an Update button instead.
So we have:
SUBMIT container box
Title field (field key = title) = #input1
URL field (field key = url) = #input2
Submit button = #btnSubmit
UPDATE container box
Title field (field key = title) = #input3
URL field (field key = url) = #input4
Update button = #btnUpdate
On your Page1, use this code:
import wixData from ‘wix-data’ ;
export function btnSubmit ( event ) {
let toInsert = {
“title” : $w ( ‘#input1’ ). value ,
“url” : $w ( ‘#input2’ ). value
};
wixData . insert ( "CollectionA" , toInsert )
. then ( ( results ) => {
let item = results ;
})
. catch ( ( err ) => {
let errorMsg = err ;
});
}
export function btnUpdate ( event ) {
let toUpdate = {
“_id” : ‘ID’ ,
“title” : $w ( ‘#input3’ ). value ,
“url” : $w ( ‘#input4’ ). value
};
wixData . update ( "CollectionA" , toUpdate )
. then ( ( results ) => {
let item = results ;
})
. catch ( ( err ) => {
let errorMsg = err ;
});
}
Next, in Dev mode, go to the Databases panel > click the 3 dots next to CollectionA > Add/Remove Hooks > add afterInsert, and add afterUpdate.
This will launch a data.js tab. On this tab, use this code:
import wixData from ‘wix-data’ ;
export function CollectionA_afterInsert ( item , context ) {
let toInsert = item ;
wixData . insert ( “CollectionB” , toInsert )
. then (( results ) => {
let item = results ;
})
. catch (( err ) => {
let errorMsg = err ;
});
}
export function CollectionA_afterUpdate ( item , context ) {
let toUpdate = item ;
wixData . update ( “CollectionB” , toUpdate )
. then (( results ) => {
let item = results ;
})
. catch (( err ) => {
let errorMsg = err ;
});
}
Now, whenever, a site member submits info in the Submit box, it will channel into CollectionA and then automatically populate in CollectionB too. And if the site member decides to update info in the Update box, this will successfully overwrite the existing item/line with their updates. It won’t create new items/lines.