Lightweight 0.20250904.0
Loading...
Searching...
No Matches
Entities.hpp
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include <Lightweight/Lightweight.hpp>
5
6#include <ostream>
7
8struct Person
9{
12 Light::Field<bool> is_active { true };
14
15 std::weak_ordering operator<=>(Person const& other) const = default;
16};
17
18// This is a test to only partially query a table row (a few columns)
19struct PersonName
20{
23
24 static constexpr std::string_view TableName = Light::RecordTableName<Person>;
25};
26
27inline std::ostream& operator<<(std::ostream& os, Person const& value)
28{
29 return os << std::format("Person {{ id: {}, name: {}, is_active: {}, age: {} }}",
30 value.id.Value(),
31 value.name.Value(),
32 value.is_active.Value(),
33 value.age.Value().value_or(-1));
34}
35
36struct User;
37struct Email;
38
39struct User
40{
43
44 Light::HasMany<Email> emails {};
45};
46
47struct Email
48{
51 Light::BelongsTo<Member(User::id), Light::SqlRealName { "user_id" }> user {};
52
53 constexpr std::weak_ordering operator<=>(Email const& other) const = default;
54};
55
56struct Physician;
57struct Appointment;
58struct Patient;
59
60struct Physician
61{
64 Light::HasMany<Appointment> appointments;
66
67 constexpr std::weak_ordering operator<=>(Physician const& other) const
68 {
69 if (auto result = id.Value() <=> other.id.Value(); result != std::weak_ordering::equivalent)
70 return result;
71
72 if (auto result = name.Value() <=> other.name.Value(); result != std::weak_ordering::equivalent)
73 return result;
74
75 return std::weak_ordering::equivalent;
76 }
77};
78
79struct Patient
80{
84 Light::HasMany<Appointment> appointments;
86
87 constexpr std::weak_ordering operator<=>(Patient const& other) const
88 {
89 if (auto result = id.Value() <=> other.id.Value(); result != std::weak_ordering::equivalent)
90 return result;
91
92 if (auto result = name.Value() <=> other.name.Value(); result != std::weak_ordering::equivalent)
93 return result;
94
95 if (auto result = comment.Value() <=> other.comment.Value(); result != std::weak_ordering::equivalent)
96 return result;
97
98 return std::weak_ordering::equivalent;
99 }
100};
101
102struct Appointment
103{
107 Light::BelongsTo<Member(Physician::id), Light::SqlRealName { "physician_id" }> physician;
108 Light::BelongsTo<Member(Patient::id), Light::SqlRealName { "patient_id" }> patient;
109
110 constexpr std::weak_ordering operator<=>(Appointment const& other) const
111 {
112 if (auto const result = id.Value() <=> other.id.Value(); result != std::weak_ordering::equivalent)
113 return result;
114
115 if (auto const result = date.Value() <=> other.date.Value(); result != std::weak_ordering::equivalent)
116 return result;
117
118 if (auto const result = comment.Value() <=> other.comment.Value(); result != std::weak_ordering::equivalent)
119 return result;
120
121 if (auto const result = physician.Value() <=> other.physician.Value(); result != std::weak_ordering::equivalent)
122 return result;
123
124 if (auto const result = patient.Value() <=> other.patient.Value(); result != std::weak_ordering::equivalent)
125 return result;
126
127 return std::weak_ordering::equivalent;
128 }
129};
Represents a one-to-one relationship.
Definition BelongsTo.hpp:46
This API represents a many-to-many relationship between two records through a third record.
This HasMany<OtherRecord> represents a simple one-to-many relationship between two records.
Definition HasMany.hpp:34
Represents a single column in a table.
Definition Field.hpp:84
constexpr T const & Value() const noexcept
Returns the value of the field.
Definition Field.hpp:298
Helper class, used to represent a real SQL column names as template arguments.