```model User { id String @id ...
# orm-help
d
Copy code
model User {
  id                 String   @id @default(uuid()) /// @zod.uuid({ message: "Invalid UUID" })
  name               String
  email              String   @unique /// @zod.email({ message: "Invalid email address" })
  username           String   @unique /// @zod.min(3)
  phone              String? /// @zod.min(10).max(12)
  passwordHash       String
  emailVerified      Boolean  @default(false)
  acceptedTermsOfUse Boolean  @default(false)
  points             Points[]
  wish               String?
  createdAt          DateTime @default(now())
  updatedAt          DateTime @updatedAt
}

model Points {
  id         Int          @id @default(autoincrement()) /// @zod.int({ message: "Invalid UUID" })
  userId     String
  User       User         @relation(fields: [userId], references: [id])
  points     Int          @default(0)
  totalAfter Int          @default(0)
  desc       String
  action     PointsAction
  createdAt  DateTime     @default(now())
}
How do I store the total of points in the user table, please?
a
I think you have two options. First, you don't store the total of points, and instead aggregate the points when you query a user. Second, whenever a point is created/modified/deleted, you also increment or decrement the pointsTotal for users. In this option, you'd be able to read userPoints more quickly, but would have to be more thorough with your code that inserts/updates, and would need to be aware of possible race conditions if multiple points are added at the same time.
w
You might also consider using a materialized view for the aggregation count.
d
@Austin Zentz after aggregating the points, how do I add it to the user query?
@Wil Moore III what do you mean by materialized view?