Aug 25, 2019 10:19 AM
Hi!
I’d like to build a simple recipe database. I have different ingredients which are parts in different recipes but the various quantities are the problem. I saw an article about this in here but being a total beginner couldn’t quite understand what it meant.
So I have a table for ingredients and a table for recipes but how I set the quantities. Like:
Recipe #1
2 lemon
2 peppermint
2 lavender
Recipe #2
1 lemon
5 lavender
4 spearmint
So when I am saving my recipe I would like to choose the quantity for each ingredient and the quantity of course is a different one according to the recipe I’m saving to my database.
Sep 01, 2020 02:14 PM
The “Prepare” script was:
let mealsT = base.getTable("Meals")
let mealsQ = await mealsT.selectRecordsAsync()
let meals = mealsQ.records
let recipesT = base.getTable("Recipes")
let today = new Date()
let todayS = today.toLocaleDateString()
let mealsF = meals.filter(x => {
let date = x.getCellValueAsString("Date")
let type = x.getCellValue("Meal Type").name
return date == todayS && type == "Dinner"
})
let recipe = await input.recordAsync("Select a recipe", recipesT)
let meal = mealsF.length ? mealsF[0] : null
if(meal){
output.markdown(`Recipe added to the meal: ${meal.name}`)
let recipes = meal.getCellValue("Recipes")
recipes.push(recipe)
mealsT.updateRecordAsync(meal.id, {
"Recipes": recipes
})
}else {
mealsT.createRecordAsync({
"Date": new Date(todayS),
"Meal Type": {name: "Dinner"},
"Recipes": [{id: recipe.id}]
})
output.markdown(`New meal created`)
}
And the “Create Leftovers” script was:
let mealsT = base.getTable("Meals")
let recipesT = base.getTable("Recipes")
let recipesQ = await recipesT.selectRecordsAsync()
let meal = await input.recordAsync("Select a meal", mealsT)
let start = new Date(meal.getCellValue("Date"))
let mealRecipes = meal.getCellValue("Recipes").map(x => x.id)
let recipes = recipesQ.records.filter(x => mealRecipes.includes(x.id))
let yields = recipes.map(x => x.getCellValue("Yield"))
let minYield = Math.min(...yields) ? Math.min(...yields) : 2
for (let i=1; i < minYield; i++) {
let x = new Date(start)
let current = new Date(x.setDate(x.getDate() + i))
mealsT.createRecordAsync({
"Date": current,
"Meal Type": meal.getCellValue("Meal Type"),
"Recipes": recipes.map(x => ({id: x.id})),
"Leftovers?": true
})
}
output.markdown(`${minYield - 1} leftover meals added`)
Sep 01, 2020 02:25 PM
@Kamille_Parks Thank you for such a quick response, you rock! I was just running some tests of the base to see how it behaves under certain circumstances. I noticed under shopping list, if two recipes use the same ingredient, but it is measured in different units, it will sum the quantity, and aggregate the unit. Any suggestions on which function I could edit to convert different units into one and return a sum decimal? (Not the end of the world, just trying to understand the logic pattern so I don’t mess anything up if I add functionality)
Sep 01, 2020 02:33 PM
The answer would be to pick a universal unit, and create a formula which converts every possible unit of measurement to it. I didn’t feel like going through that headache, which is why it doesn’t exist in the base I shared.
Jan 22, 2021 02:47 PM
@Kamille_Parks in one of your bases I downloaded it also has a script for Ingredients. Are you willing to share that script, and does this replace the form you’d previously mentioned? Thank you.
Jan 22, 2021 02:58 PM
What it does => Takes something like “¼ cup chicken stock” and separates it out into the three different fields that data is supposed to fill.
Will I share it => No. I don’t even use it, filling out the table the normal way isn’t hard, and if I release it I would be answering questions about it ad infinitum since it won’t match everyone’s exact setup.