Adding database storage #
You can add data storage for a custom service using a custom Data Access Object (DAO) service. To add data storage, extend one of the DAO Node Services classes.
Pragma Engine uses a MySQL database to maintain service data. To remain consistent with the new custom service and Pragma Engine, create a DAO Node Service class.
Your custom DAO can extend from two different kinds of DaoNodeService
classes:
UnpartitionedDaoNodeService
- Represents a single database.
- Useful for centralized data that doesn’t need a lot of scaling.
PartitionedDaoNodeService
- Uses a collection of databases to distribute data for scaling.
Extend a Dao Node service #
To extend an existing DaoNodeService
class, you must overwrite the following methods in the parent class: changelogFilepath
(a
Liquibase changelog file) and getDatabaseConfigFrom
(a config file). You must also include the SharedDatabaseConfigServiceConfig
dependency in the service using the DAO Node Service. Partitioned services also need an override with a key to hash on, such as playerId
.
For a detailed example of an unpartitioned Dao Node service, see the Arbiter leaderboard service.
The following procedure walks you through how to set up a service that extends the partitioned and unpartitioned classes:
Override the change log filepath
To override
changelogFilepath
, your service must return a path to a SQL migration script.- Create a SQL script in
5-ext/ext/src/main/resources/db-changelogs/
. Pragma Engine packages files in 5-ext into a .jar, and uses them to apply database migrations on startup. - Add any relevant change sets to the script.
- For partitioned services, add a hash key column.
- Create a SQL script in
- Override
getDatabaseConfigFrom
and add theDaoConfig
To override getDatabaseConfigFrom
, add the DaoConfig
Kotlin file to your custom service directory. For partitioned services, you
must include multiple hostPortSchemas
. If you are following along in the Arbiter leaderboard service this is found in the
ArbiterLeaderboardDaoConfig.kt file.
- Create the custom Dao Node service.
If you are following along in the Arbiter leaderboard service this is found in the ArbiterLeaderboardDaoNodeService.kt file.
- Create a Kotlin file in the custom service directory.
- Inherit the
UnpartitionedDaoNodeService
orPartitionedDaoNodeService
class. - Add the required
changelogFilepath
andgetDatabaseConfigFrom
overrides.
- Implement the database service in your custom service.
For more information about setting up a custom service see, Creating a custom service. If you are following along in the Arbiter leaderboard service this step is found in the ArbiterLeaderboardService.kt file.
@PragmaService(
backendTypes = [BackendType.GAME],
dependencies = [MyCustomServiceDaoNodeService::class]
)
internal class MyCustomService(
pragmaNode: PragmaNode,
instanceId: UUID
) : DistributedService(pragmaNode, instanceId) {
private lateinit var myCustomServiceDaoNodeService: MyCustomServiceDaoNodeService
override fun run() {
myCustomServiceDaoNodeService = nodeServicesContainer[MyCustomServiceDaoNodeService::class]
}
- Add your database details to you service configuration.
serviceConfigs:
# Collections of database configurations
SharedDatabaseConfigServiceConfig:
databaseConfigsByIdentifier:
testIdentifier:
username: "superuser"
password: "password"
host: "${databaseHost}"
# PartitionedDaoNodeService configuration
MyCustomServiceDaoConfig:
databaseConfig:
identifierSchemas:
1:
identifier: "testIdentifier"
schema: "test_customservice1"
2:
identifier: "testIdentifier"
schema: "test_customservice2"
# UnpartitionedDaoNodeService configuration
MyCustomServiceDaoConfig:
databaseConfig:
identifierSchema:
identifier: "testIdentifier"
schema: "test_customservice"