Using a database

The framework comes with integrated Hibernate support for plugins to work with databases.

Connecting to a database

Normally, connecting Hibernate to a database requires setting up a persistence unit.
This is not required for the Jeak framework as the initializing code takes care of most options that need to be set.

In order to connect a database to the framework, all you need to do is adding a <connection-name>.json file inside config/databases.
On the next startup, the database service will try to load the settings and provide the persistence unit to plugins that request it.

Most configuration options are set using a sensible default. The administrator only has to provide the following three options that the framework cannot guess:

{
  "host": "localhost",
  "port": "3306",
  "driver": "mariadb",
  "user": "jeakbot",
  "pass": "secret",
  "schema": "jeakdb",
  "dataSourceOpts": {
  }
}

The applied defaults are overwritten by any configuration in the properties file.

❗️

Attention

Overwriting the wrong options can have unforeseeable consequences during runtime.
If you don't know what you're doing, stick to the defaults!

Using a database from a plugin

A plugin can annotate injection fields of type EntityManager with @PersistenceUnit(String name) which will tell the injection service to retrieve that connection from the database service.

The entity manager can then be used to store / retrieve objects from the database.
As Hibernate is a JPA provider, plugins can now use the Java Persistence API for reference.

The following field types are supported:

  • EntityManager
  • DataSource
  • IPersistenceUnit
  • Boolean

📘

Keep in mind

The Hibernate integration works fairly stable but still has space for enhancements.
If you have suggestions, feel free to get in touch.