Skip to content

can GRDB handle union types? #1751

Answered by groue
EricWVGG asked this question in Q&A
Mar 26, 2025 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

Hello @EricWVGG,

Can anyone tell me if recording and retrieving this in GRDB is relatively straightforward?

It is straightforward if the involved types conform to the standard Codable protocol. See Codable Records.

In this case, user and embed will be stored as JSON in the database (not in their own tables):

struct Post: Codable {
  var postId: String
  var user: User // User is Codable
  var date: Date
  var text: String
  var embed: Embed // Embed is Codable
}

enum Embed: Codable {
  case image (EmbeddedImage) // EmbeddedImage is Codable
  case video (EmbeddedVideo) // EmbeddedVideo is Codable
}

If users should be stored in their own table, then I suggest you read Recommended Practic…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@EricWVGG
Comment options

Answer selected by EricWVGG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants