Create things by using a loop

Hey there!

I’ve been reading a lot of posts for the past hours and remained a bit confused about how to use loops to create new things with a specific condition.

I have two tables. A user table, and a Dates table. every date has a user property.

Every time a user logs in, a new date is created in the dates table and is assigned to that user. Because users do not sign in every day date gaps are created for each user.

So if a user logged in on Monday, and next time on the following Wednesday, there will be one missing date for that user in the dates table.

My goal is to create all missing dates for a user, whenever he/she logs in.

I think the way should be having a loop where a new date is created with minus -1 day till the loop hits a date that already exists in the table, but cannot yet understand how to set that condition.

would love to get some help with that :slight_smile:
Thanks a lot!

What exactly is the purpose of the Date datatype in your app?

I can’t quite see what you’re trying to do, or why?

If every User has to have a Date entry for every day, why not just create one every day, instead of complicating things by trying to fill in gaps afterwards?

And if every User has a Date entry for every date, what exactly is the purpose of it? (what information does it hold?)

Thanks for the reply!

The purpose of the Date datatype is to hold a daily number per user

I’m building a habit tracker app which aggregate a summary of daily usage (for example, daily sum of cigarettes)

Users should be able to report daily usage in two different containers:

  1. Daily situation container: reporting the number of cigs smoked “while driving”, “watching TV” etc… - this already exist in the app
  2. By reporting a daily estimation number, without having to choose any situation. Just a number for the day

I believe this requires having a Date datatype, so each date could hold a number per user (any better ideas?)

Your suggestions sounds great. I would want to create a new date, every day. whats the best way to do that? (I’ve tried some solutions and got stuck)

Thanks again!

Just run a backend workflow every day to create a new Date entry for the User…

Note: I’ve got no idea how many Users your app has, or will have, but the answer to that will determine the best way to go about it.

For example, you could run a single recursive workflow every day to create a new Date entry for each User in your database…

If you have a few hundred Users that will only take a few minutes. If it’s thousands, it might take a few hours, and if it’s tens or hundreds of thousands it might take more than a day… so it likely won’t be plausible or efficient.

Or, you could run a single workflow for each User each day at the time they were first created… that will space things out more evenly across the day.

If you have a high number of Users and you want to create ALL their Date entries each day at the same time, you’ll be better off using the Data API Bulk Create endpoint.

But, that raises the question… why do you need to create a Date entry for days where Users are not recording any data?

You’ll just be clogging up your database with thousands (potentially millions) of empty database entries, and using up server capacity on creating empty database entries for Users who aren’t actively using your app…

But, at the end of the day, like so much in Bubble, the best way to proceed will depend on the specifics of your app and use case.