Skip to content
Yuki Yoshinoya edited this page Aug 30, 2020 · 23 revisions

Running modes

A running mode is defined by setting system property run.mode, and you can separate database configuration.

Note : default value is dev.

Configuration file

Configuration file named application.conf, located on classpath (e.g. src/main/resources/application.conf), and written in HOCON format.

application.conf example

The format is [running mode].[setting key] = [setting value].

dev {
  driver = "org.postgresql.Driver"
  jdbcurl = "jdbc:postgresql://hostname:5432/dbname"
  username = "user"
  password = "xxxxxxxx"
  partitionCount = 5
  maxConnectionsPerPartition = 1
  minConnectionsPerPartition = 5
  autoCreate = false
  autoDrop = false
}

test {
  driver = "org.h2.Driver"
  jdbcurl = "jdbc:h2:mem:test"
  autoCreate = true
  autoDrop = true
}

If you set system property "run.mode" is test, H2 JDBC driver is used.

Changing Schema class

By default, schema class is models.Tables.

If you would be change the schema class, please describe in some.packages.SchemaTables.[running mode].[setting key] = [setting value] format.

some.packages.SchemaTables {
  dev {
    driver = "org.postgresql.Driver"
    jdbcurl = "jdbc:postgresql://hostname:5432/dbname"
    username = "user"
    password = "xxxxxxxx"
    partitionCount = 5
    maxConnectionsPerPartition = 1
    minConnectionsPerPartition = 5
    autoCreate = false
    autoDrop = false
  }

  test {
    driver = "org.h2.Driver"
    jdbcurl = "jdbc:h2:mem:test"
    autoCreate = true
    autoDrop = true
  }
}

Using multiple Schema class

If you want to use multiple database connections (defines some schema classes) please set as follows:

models.schema1.Schema1Tables {
  dev {
    driver = "org.postgresql.Driver"
    jdbcurl = "jdbc:postgresql://hostname:5432/dbname"
    username = "user"
    password = "xxxxxxxx"
    partitionCount = 5
    maxConnectionsPerPartition = 1
    minConnectionsPerPartition = 5
    autoCreate = false
    autoDrop = false
  }

  test {
    driver = "org.h2.Driver"
    jdbcurl = "jdbc:h2:mem:test"
    autoCreate = true
    autoDrop = true
  }
}

models.schema2.Schema2Tables {
  dev {
    driver = "org.h2.Driver"
    jdbcurl = "jdbc:h2:mem:schema2db"
    autoCreate = true
    autoDrop = true
    user=sa
    password=""
  }

  test {
    driver = "org.h2.Driver"
    jdbcurl = "jdbc:h2:mem:test"
    autoCreate = true
    autoDrop = true
  }
}

Configuration items

This is a list of available configuration items.

Note : JDBC datasource is managed by HikariCP.

Key name Type Description Default value
driver String JDBC driver "org.h2.Driver"
jdbcurl String JDBC URL "jdbc:h2:mem:activerecord"
username String Username for connection (none)
password String Password for connection (none)
partitionCount Integer HikariCP partitionCount (none)
maxConnectionsPerPartition Integer HikariCP maxConnectionsPerPartition (none)
minConnectionsPerPartition Integer HikariCP minConnectionsPerPartition (none)
autoCreate Boolean Create tables automatically on Tables#initialize true
autoDrop Boolean Drop tables automatically on Tables#cleanup false

Settings for table configurations

Default naming convention

Type Description Default
table name model class name is underscored and pluralized ModelClass => "model_classes"
intermediate table name table names are sorted and joined with underscore User, Group => groups_users
column name field name is underscored fieldName => field_name
foreign key field name camelized model class name + "Id" ModelClass => modelClassId

Override settings

Tables for Scala ActiveRecord objects are named in plural form by default. If you need to change the table name, you can override the default behavior in com.github.aselab.activerecord.ActiveRecordTables.

For example:

import com.github.aselab.activerecord._
import com.github.aselab.activerecord.dsl._

object Tables extends ActiveRecordTables {
  // change the convention of table name into ModelClass => "MODEL_CLASS"
  override def tableNameFromClass(c: Class[_]): String =
    c.getSimpleName.underscore.toUpperCase

  // change the convention of intermediate table name into (ModelClass1, ModelClass2) => "MODEL_CLASS1___MODEL_CLASS2"
  override def tableNameFromClasses(c1: Class[_], c2: Class[_]): String =
    Seq(c1, c2).map(tableNameFromClass).sorted.mkString("___")

  // change the convention of column name into ModelField => "MODEL_FIELD"
  override def columnNameFromPropertyName(propertyName: String): String  =
     propertyName.underscore.toUpperCase

  // change the convention of foreign key field name into ModelClass => "modelClass_id"
  override def foreignKeyFromClass(c: Class[_]): String =
    c.getSimpleName.camelize + "_id"
}
Clone this wiki locally