Forum Academy Marketplace Showcase Pricing Features

Store item x quantity for arbitrary list in Bubble

I’m hoping someone knows of a more elegant way to do this. I’d like to be able to just record the idea of “5 times this thing”.

Basically, I want to mimic an associative array in Bubble.

In this case I’m letting the user build a Dish out of a list of Foods. They need to be able to record the amount of each Food in the dish. I don’t know how many Foods they’ll want to include, so I can’t hard-code fields like “food 1” and “quantity 1”.

In theory I could use two parallel lists if I could keep the index numbers synced up, but I assume that’s a bad idea.

So all I can think of is to create another thing Multi to record Food and a multiplier. So the database would look like this:

FOOD

  • id | 123
  • name(text) | beef
  • protein(num) | 25
  • fat(num) | 20
  • carbs(num) | 0

Dish

  • name(text) | hamburger
  • ingredients(list of Multi) | 987, 988, 989…

Multi

  • id | 987
  • item(Food) | 123 (Food, beef)
  • times(num) | 250

So I’d have to create a new thing of type Multi every single time I wanted to record a quantity of a food. I assume that will work, but is there a better way?

A good example of why an array would be better is that I can’t think of a way to let the thing Multi record a multiplier against any other thing in the database without making item into text and recording just the unique id. Then I’d have to run a search for the thing with that unique id instead of it being automatically linked.

So if I’d have to create a special Multi, or at least a special field, for every different kind of thing that could be multiplied so that the field can have the correct type.

Not to mention that iterating over the list of things is of course going to cost a workflow for each and every item.

1 Like

Could store it as a JSON stringified object, then parse it to use in javascript when you want to do something with calculations with it … whether its worth the effort depends on how you want to use the values in the app.

Thanks. I’m working through how I want to use the data now. Do you know of any examples of someone storing a JSON string and parsing it back into lists or data? I thought about that but couldn’t find anything helpful.

I think it’d be breaking new ground. I had a little go at it …

Pros: less database operations, so faster overall.
Cons: more complex solution, not easy to get right.

that would be a neat step to add to expressions. Store a blob of text in a cell rather than have to make many different things and fields to hold the same information. If you could parse it in to/out of associative arrays that would be so much easier.

Got the exact same issue I am trying to solve. I needed to store PH_Levels and Date of when something was recorded. I have implemented it using a simple String - PH_LevelDate (717/04/05, 6~~17/04/05)

And then I use truncate and truncate from end to parse the fields. Works well for simple display purposes, but gets slow when searching and computing. And still haven’t figured out a way to Graph it.

So also looking for ways to store a 2d list.

After a coaching session with Gaby I’m going to un-flatten my database and do as many calculations as possible as soon as the data’s available.

It seems that things generally work better when

  1. there are a lot of things with few fields, rather than a few things with a lot of fields
  2. the results of calculations were done as soon as the data was available and stored in the database so they can simply be retrieved

So a better place to start from would be more things, rather than less things. In my case that means a new thing to related a nutrient to an amount of that nutrient, rather than two crudely named fields in the same thing. In your case @pranjal that might mean a new thing with two fields, one for the PH reading and one for the date/time. For example, instead of this:
Thing

  • parent_child_1_1
  • parent_child_1_2
  • parent_child_2_1
  • parent_child_2_2
  • etc

This:
Thing_Parent

  • list of children
    Thing_Child
  • 1
  • 2
1 Like

Thanks. The only issue with using Things is that you need to use APIs (which are slower and increases my workflow count) to create new items there which can then be stored in a List of Things.

But list of things is much better than my string solution because you can perform direct analysis on it.