Database Helper

The project provides a database helper to easily generate HikariDataSource to be used in the Exposed Database.

First for get a HikariDataSource we will need a DatabaseTypeConfig that will have all the configurations need to KotlinBukkitKIT generate the proper HikariDataSource.

val config = DatabaseTypeConfig(
        type = "H2",
        hostname = "localhost",
        port = 3306,
        database = "kbapi_database",
        user = "root",
        password = "12345"
)

The following database types are supported out of the box with KotlinBukkitKIT.

  • SQLite

  • H2

  • MySQL

  • PostgreSQL

  • SQLServer

All the drivers from the non shipped with Bukkit is downloaded and provided at runtime by KotlinBukkitKIT, you don't need to ship it yourself (H2, PostgreSQL and SQLServer).

Now, we will extract a DatabaseType based on that config, with the DatabaseType we will be able to get the HikariDataSource.

val databaseType = databaseTypeFrom(dataFolder = yourPluginDataFodler, config = config)

val dataSource: HikariDataSource = databaseType.dataSource()

You can also get the HikariConfig if you want to do an optimization by your own: databaseType.config().

Now that we have the DataSource we can work with the Exposed.

val database = Database.connect(dataSource)

transaction(database) {
  SchemaUtils.create(YourTable)
}

Last updated