We have an array element in our json that we want to save off into its own table. However, we are struggling with saving off the data to the table without needing to add a column.
{ fruits: [
{
fruitID: "12345"
name: "apple",
color: "red",
recipes: [
{
title: "Apple Pie",
description: "asbjfnksdkm",
videoTutorial: "foo.com"
},
{
title: "Apple Cinnamon Roll",
description: "Cinnamon",
videoTutorial: "foo2.com"
}
]
},{
fruitID: "43214"
name: "banana",
color: "yellow",
recipes: [
{
title: "Banana Split",
description: "Ice Cream",
videoTutorial: "weee.com"
},
{
title: "Banana Foster",
description: "Cinnamon",
videoTutorial: "wooo.com"
}
]
}]
}
How do we extract "recipes" from "fruits" without needing a column?
Our response comes in an immediate gets translated in a data codable that contains a fruits codable. Here's our codable structure:
public struct DataClass: Codable {
public let fruits: [Fruit]
enum CodingKeys: String, CodingKey {
case fruits
}
public class Fruit: Codable {
public let fruits: [Fruit]
enum CodingKeys: String, CodingKey {
case fruits
}
}
public class Fruit: Codable {
public let name: String
public let color: String
public let recipes: [Recipes]
enum CodingKeys: String, CodingKey {
case name
case color
case recipes
}
}
public class Recipes: Codable {
public let title: String
public let description: String
public let videoTutorial: String
enum CodingKeys: String, CodingKey {
case title
case description
case videoTutorial
}
}
In order to obtain the recipes data from the fruit, we added coding keys and a decode if present to get our array of Recipes. However, GRDB seems to want a column for recipes to be present when recipes is part of Coding Keys. Is there a way to get around this so that I only store recipes in its table without needing the column?
Currently, I have both and it is redundant so we're trying to get rid of the column as a table is preferable since recipes contains different elements that would be better stored in their own respective columns in the recipe table as opposed to a single column.