Skip to content
Discussion options

You must be logged in to vote

Hi @Jerland2, modeling currency as an integer is a common way to do things, and provides a completely lossless representation in the database (as long as you don't need fractional cents). You should even be able to bundle both the subunit and currency into a single @Selection type that can be reused in many places:

@Selection struct Amount {
  let subunit: Int
  let currency: String
}

@Table struct Transaction {
  let id: UUID 
  let amount: Amount}

That allows you to treat Transaction.amount as a single unit, while secretly it is powered by two columns in the table. This means you can select, filter, order and even join on it:

Transaction
  .select { $0.amount.subunit }

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@Jerland2
Comment options

@mbrandonw
Comment options

@Jerland2
Comment options

@mbrandonw
Comment options

Answer selected by Jerland2
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