When a new user creates an account, I need to copy the Firebase DB

So I have a JSON tree for users data, but when a new user is created how do I make a copy of the default data for a new user to save their individual settings for?

Also related to this, if I wanted to add a new item to the data in the future, how would I do that and be able to update all existing users with the new line item from the update?
What I mean for example is, in the screenshot below if I wanted to add a new item called “Move” to go under “Build” how would I be able to do that so all the EXISTING users get that new item, but without disturbing any of their saved items from prior to the update.

I hope that isn’t too confusing, I couldn’t find anything similar to this question in the forums already

Thank you very much for all the help Thunkers!

I deal with this situation all of the time. My strategy is to use 2 nested loops. The outer loop iterates through the default->task node(drive, clean, build). For each task, do a second nested loop through each user. If they have the outer loop task (e.g. drive) in their personal task object, do nothing. If they do not have the task, set the user task to the outer loop task.

You can use this solution to address both new users and new task.

1 Like

Oh that’s great, do you have an example already in a thread for how to iterate? I don’t quite understand “how to iterate” through nodes.

I suppose once I knew how to do that I would next need to know how to check if they have the outer loop.

Edit: I believe this is a “for each item j” but what to do after that, and how to store it I’m lost

Do you do this upon initial sign in? Personally I would prefer to wait until the email was confirmed just to prevent spam and additional unwanted data. Am I correct in my thinking?

Here are the blocks

First and foremost when working with complex JSON trees, use a lot of debugging code and test at each step BEFORE going to the next step. Ask me why I do it this way… :sob: :face_with_symbols_over_mouth:

Next create a for each loop for each PROPERTY in the MasterTaskObject. Notice that the value in defaultTask is the NAME of the child object, not the object itself.

Create the nested loop for each username

Get the object for the task for the current user (this is the magic)

If the user doesn’t have that task copy the default task to the user
image

Here is the output

Remix
https://x.thunkable.com/copy/9fa82d340efb94827c97bccd16781066

P.S.

I agree, you should not create a new username until there has been a successful SIGN IN. Again I would strongly recommend you use the Firebase UserID for the child nodes under username. You can then add a property for userHandle under the child. That will eliminate the problems with multiple jimbo’s using your app.

P.P.S.

Don’t try to re-create these blocks by hand. Open the remix project and save screen1 to MyScreens. Then open your project and add this screen to your existing project. Then copy the button1 Click event into any existing screen. You can then tweak the cloud path as necessary to work for your project.

2 Likes

This is great, thank you for this. The ‘when screen 1 starts’ components are just because this isn’t tied to an actual FB DB right? Those components I don’t actually need once it’s connected?

This needs to be inside my sign in screen right? Because then I can more easily put my Firebase ID for the child node?

Start event is for testing.

Sign in screen makes sure every time users sign in, you can automatically propagates any new tasks

Very nice work @drted.

I tend to use the “Export JSON” option in Firebase RTDB to create the structure of all wanted keys
image
then save the text to a variable and then use


[get object from JSON] to create the new objects and use it in the code.

1 Like

Handy!

1 Like

Does this store each users individual values though or would this overwrite their values with a new default value? Or am I misunderstanding?

Thanks again guys

Edit: Couldn’t you do this with a cloud variable and still get the same result, I mean without having to download it from RTDB?

1 Like

I’m making some progress.

What should I put in the 2 blank spots in the screenshot below to finally complete the default data?

1 Like

I was referring to creating the object of the database keys. The loops will stay the same and you will need to put some default values for each new user the same way @drted suggested.

After the userID and / you need to have the key “task”.

Just follow the hierarchy of your data structure.

DefaultTask or userTask?

I don’t quite understand what you meant, could an option similar to this work?

Does anyone have any other suggestions on how to make a copy of a Firebase Database?

Thank you guys for all your help so far up to this point!

Dear @overshield,
I advise you to follow the instructions of @drted. He clearly told you to remix the project and test it. One good start for testing is to create a new user in your database and then run the project to see if the default values will be automatically inserted in the database.

When this step is successful, then you can start modifying the code to get your changes inserted in the project.

Happy Thunking

Yes I have, sorry I should have explained that too. We’ve both tried for a while with several different techniques at this point. I have also tried adding a new user manually in the DB and then tried to update it’s properties but still no success.

Thanks anyway though, if you come up with anymore ideas please share.

Happy Thunking

1 Like

Wait I have a question @drted
Are these exact components required for all of this to work?
Or can I replace this with my FBDB?
My final default DB has 151 objects, with 2 properties each.

I’m not required to build my entire Default DB out of object blocks am I?

These block create a sample tree in Firebase. You don’t NEED to use them, but the graphically illustrate the hierarchy. Since this tree has the root node of Overshield, you can just run this project and look at what it does in Firebase without messing with your existing project. Try adding and deleting nodes either in Thunkable or firebase and see how those changes affect the button click event.

Ok I’ve figured it out to where new users are created properly now with the correct data!

2 final parts.

  1. If I remove an object from the default data and then rerun the click event, it doesn’t remove those same lines from the users.
  2. Adding a 2nd task object doesn’t get included to the users like this below
  1. correct, there aren’t any blocks to do that, but you can add them. You’ll need to play around with it. As a rule, to remove a node, set the cloud variable VALUE = NULL.
  2. Hmmm, what does the output look like?

Ok I’ll give NULL a try for the nodes I want removed.
The output appears to be a normal completely successful event.

It may be running, but is it detecting the missing nodes that you want copied from the default to philbo?