Skip to content

Commit ef10fb8

Browse files
committed
Add enum documentation.
Relates to #14
1 parent 70c9360 commit ef10fb8

1 file changed

Lines changed: 50 additions & 14 deletions

File tree

README.adoc

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ Storm entities are defined using Java record classes. By default, Storm automati
6464
entity fields directly to corresponding database columns. This approach simplifies development, as explicit annotations
6565
or mappings are not required for standard naming conventions.
6666

67-
Consider the following example, where two entities — City and User — are defined:
67+
Consider the following example, where two entities are defined:
6868

69-
* The City entity maps directly to the columns: id, name, and population.
70-
* The User entity maps directly to the columns: id, email, birth_date, street, postal_code and city_id.
69+
* The `City` entity maps directly to the columns: `id`, `name`, and `population`.
70+
* The `User` entity maps directly to the columns: `id`, `email`, `birth_date`, `street`, `postal_code` and `city_id`.
7171

7272
When an entity references another entity, Storm applies a default naming convention to handle foreign keys. For
7373
instance, the city field in the User entity automatically maps to the city_id column in the database, creating a foreign
@@ -95,29 +95,67 @@ operations.
9595
==== Nullability
9696

9797
In Storm, all fields are nullable by default, except for the primary key field, which is always non-nullable. To
98-
explicitly mark a field as non-nullable, use the `@Nonnull` annotation. Storm automatically validates nullability
99-
constraints when reading from or writing to the database, throwing an exception if a non-nullable field is found to be
100-
`null`.
98+
explicitly mark a field as non-nullable, use the `@Nonnull` annotation. When a non-nullable field is of a primitive
99+
wrapper type (e.g., `Integer`, `Long`), it can alternatively be specified as its primitive counterpart (`int`, `long`),
100+
inherently enforcing non-nullability.
101101

102-
Additionally, it's beneficial to use the optional `@Nullable` annotation, as it enables automatic null-checking support
102+
Storm automatically validates nullability constraints when interacting with the database, throwing an exception if a
103+
non-nullable field is found to be `null`.
104+
105+
Additionally, it is beneficial to use the optional `@Nullable` annotation, as it enables automatic null-checking support
103106
within your IDE.
104107

105-
[source,java,indent=0]
108+
[source,java]
106109
----
107110
record User(@PK int id,
108111
@Nonnull String email,
109112
@Nonnull LocalDate birthDate,
110113
@Nonnull String street,
111-
@Nullable String postalCode,
114+
String postalCode,
112115
@Nullable @FK City city
113116
) implements Entity<User, Integer> {}
114117
----
115118

116119
In this example, the fields `id`, `email`, `birthDate`, and `street` are marked as non-nullable, whereas the
117-
`postalCode` and `city` fields remain nullable. Consequently, the relationship between the `User` and `City` entities
120+
`postalCode` and `city` fields are nullable. Consequently, the relationship between the `User` and `City` entities
118121
results in a left join, accommodating potential `null` values for the `city` field. Conversely, a non-nullable foreign
119122
key would lead to an inner join, ensuring the referenced entity must always exist.
120123

124+
==== Enumerations
125+
126+
Storm provides built-in support for enumerations, simplifying how predefined sets of values are handled in entities. By
127+
default, enumerations (`enum` types) are stored in the database using their names. However, Storm allows you to customize
128+
this persistence behavior using the `@DbEnum` annotation, enabling storage as either the enum's `NAME` (default) or
129+
`ORDINAL` (integer index). This flexibility facilitates seamless integration with existing database schemas or preferred
130+
storage formats.
131+
132+
[source,java]
133+
----
134+
enum RoleType {
135+
USER,
136+
ADMIN
137+
}
138+
139+
record Role(@PK int id,
140+
@Nonnull String name,
141+
@Nonnull RoleType type
142+
) implements Entity<Integer> {}
143+
----
144+
145+
In this example, the RoleType enumeration values (`USER` and `ADMIN`) are persisted directly by their names
146+
("USER" and "ADMIN") in the database.
147+
148+
[source,java]
149+
----
150+
record Role(@PK int id,
151+
@Nonnull String name,
152+
@Nonnull @DbEnum(ORDINAL) RoleType type
153+
) implements Entity<Integer> {}
154+
----
155+
156+
In the second example, `@DbEnum(ORDINAL)` instructs Storm to persist the RoleType enumeration using its ordinal value
157+
(integer index) instead of its name.
158+
121159
==== Naming Conventions
122160

123161
While Storm's default naming conventions simplify entity definitions, custom column names or foreign key mappings can
@@ -233,13 +271,11 @@ the relationship can be defined using the `@FK` annotation. For example, conside
233271

234272
[source,java,indent=0]
235273
----
236-
record Role(@PK Integer id, String name) implements Entity<Integer> {}
237-
238274
record UserRolePk(int userId, int roleId) {}
239275
240276
record UserRole(@PK UserRolePk userRolePk,
241-
@FK User user,
242-
@FK Role role
277+
@Nonnull @FK User user,
278+
@Nonnull @FK Role role
243279
) implements Entity<UserRolePk> {}
244280
----
245281

0 commit comments

Comments
 (0)