user_attribute_value.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package schema
  2. import (
  3. "github.com/Wei-Shaw/sub2api/ent/schema/mixins"
  4. "entgo.io/ent"
  5. "entgo.io/ent/dialect/entsql"
  6. "entgo.io/ent/schema"
  7. "entgo.io/ent/schema/edge"
  8. "entgo.io/ent/schema/field"
  9. "entgo.io/ent/schema/index"
  10. )
  11. // UserAttributeValue holds a user's value for a specific attribute.
  12. //
  13. // This entity stores the actual values that users have for each attribute definition.
  14. // Values are stored as strings and converted to the appropriate type by the application.
  15. type UserAttributeValue struct {
  16. ent.Schema
  17. }
  18. func (UserAttributeValue) Annotations() []schema.Annotation {
  19. return []schema.Annotation{
  20. entsql.Annotation{Table: "user_attribute_values"},
  21. }
  22. }
  23. func (UserAttributeValue) Mixin() []ent.Mixin {
  24. return []ent.Mixin{
  25. // Only use TimeMixin, no soft delete - values are hard deleted
  26. mixins.TimeMixin{},
  27. }
  28. }
  29. func (UserAttributeValue) Fields() []ent.Field {
  30. return []ent.Field{
  31. // user_id: References the user this value belongs to
  32. field.Int64("user_id"),
  33. // attribute_id: References the attribute definition
  34. field.Int64("attribute_id"),
  35. // value: The actual value stored as a string
  36. // For multi_select, this is a JSON array string
  37. field.Text("value").
  38. Default(""),
  39. }
  40. }
  41. func (UserAttributeValue) Edges() []ent.Edge {
  42. return []ent.Edge{
  43. // user: The user who owns this attribute value
  44. edge.From("user", User.Type).
  45. Ref("attribute_values").
  46. Field("user_id").
  47. Required().
  48. Unique(),
  49. // definition: The attribute definition this value is for
  50. edge.From("definition", UserAttributeDefinition.Type).
  51. Ref("values").
  52. Field("attribute_id").
  53. Required().
  54. Unique(),
  55. }
  56. }
  57. func (UserAttributeValue) Indexes() []ent.Index {
  58. return []ent.Index{
  59. // Unique index on (user_id, attribute_id)
  60. index.Fields("user_id", "attribute_id").Unique(),
  61. index.Fields("attribute_id"),
  62. }
  63. }