-
Notifications
You must be signed in to change notification settings - Fork 27
Database Settings
y-yoshinoya edited this page Feb 15, 2013
·
23 revisions
A running mode is defined by setting system property run.mode
, and you can separate database configuration.
Note : default value is
dev
.
Configuration file named application.conf
, located on classpath (e.g. src/main/resources/application.conf
),
and written in HOCON format.
The format is [running mode].[setting key] = [setting value]
.
dev {
schema = "models.Tables"
driver = "org.postgresql.Driver"
jdbcurl = "jdbc:postgresql://hostname:5432/dbname"
username = "user"
password = "xxxxxxxx"
partitionCount = 5
maxConnectionsPerPartition = 1
minConnectionsPerPartition = 5
}
test {
schema = "models.Tables"
driver = "org.h2.Driver"
jdbcurl = "jdbc:h2:mem:test"
}
If you set system property "run.mode" test, H2 JDBC driver is used.
This is a list of available configuration items.
Note : JDBC datasource is managed by BoneCP.
Key name | Type | Description | Default value |
---|---|---|---|
schema | String | Schema definition class | "models.Tables" |
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 | BoneCP partitionCount | (none) |
maxConnectionsPerPartition | Integer | BoneCP maxConnectionsPerPartition | (none) |
minConnectionsPerPartition | Integer | BoneCP minConnectionsPerPartition | (none) |
Example Model:
case class SomeModel(someAttribute: String) extends ActiveRecord {
val someBelongsToModelId: Option[Long] = None
lazy val belongsTo[SomeBelongsToModel]
lazy val hasAndBelongsTo[SomeManyToManyModel]
}
Type | Description | Default |
---|---|---|
table name | underscore and plural form | ModelClass => "model_classes" |
intermediate table name | sorted table names are joined with underscore | User, Group => groups_users |
column name | underscore | fieldName => field_name |
foreign key field name | camelize + "Id" | ModelClass => modelClassId |
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:
object Tables extends ActiveRecordTables {
// Convention of table name
// ModelClass => "MODEL_CLASS"
override def tableNameFromClass(c: Class[_]): String =
c.getSimpleName.underscore.toUpperCase
// Convention of intermediate table name
// ModelClass1, ModelClass2 => "MODEL_CLASS1___MODEL_CLASS2"
override def tableNameFromClasses(c1: Class[_], c2: Class[_]): String =
Seq(c1, c2).map(tableNameFromClass).sorted.mkString("___")
// Convention of column name
// ModelField => "MODEL_FIELD"
override def columnNameFromPropertyName(propertyName: String): String =
propertyName.underscore.toUpperCase
// Convention of foreign key field name
// ModelClass => "ModelClass_id"
override def foreignKeyFromClass(c: Class[_]): String =
c.getSimpleName.takeWhile(_ != '$').camelize + "_id"
}