-
Notifications
You must be signed in to change notification settings - Fork 0
Create Repository and Database
A repository provides CRUD operations on a given entity.
To create a new repository for an entity, the interface CrudRepository<T, K>
must be extended.
The type parameter T
refers to the entity, and K
to the identifier type.
Consider the following example:
@Entity("persons")
public class Person {
@Identifier
@MaxLength(128)
@Attribute("email")
private String email;
}
public interface PersonRepository extends CrudRepository<Person, String> {
}
To instantiate the repository, the following snippet must be used:
PersonRepository repository = Repository.create(PersonRepository.class, Person.class, database);
The variable database
refers to a specific database implementation described in the following.
Now, the repository PersonRepository
provides the following CRUD operations without implementing them manually:
Collection<Person> findAll();
Optional<Person> findById(String email);
void delete(Person person);
Person save(Person person);
Next to these operations, it is possible to declare custom methods for more specific functionality.
Worm offers an in-memory "database" (a simple Map
) and a MySQL implementation.
In-Memory Database
Database database = new InMemoryDatabase();
MySQL Database
The MySQL database implementation uses Hikari as a connection pool.
Database database = new MySQLDatabase(HOST, PORT, DATABASE, USERNAME, PASSWORD);
If your application is dockerized and runs in the same Docker network of the database, the service name of the database application can be used as host (e.g., "database").
If your application runs locally on your host and you want to connect to a dockerized database, use the host host.docker.internal
.