feat: Database model & configuration

This commit is contained in:
Piotr Dec 2024-05-21 22:40:44 +02:00
parent c087081d25
commit ee6c6eda02
Signed by: stawros
GPG key ID: F89F27AD8F881A91
10 changed files with 209 additions and 0 deletions

View file

@ -0,0 +1,54 @@
package eu.ztsh.wymiana.data.config;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.hsqldb.persist.HsqlProperties;
import org.hsqldb.server.Server;
import org.hsqldb.server.ServerAcl;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
@Configuration
public class DatabaseConfiguration {
/**
* Create HSQL Database instance that is accessible from outside of application
*
* @return server instance
* @throws ServerAcl.AclFormatException when instantiating ServerAcl
* @throws IOException when instantiating ServerAcl
*/
@Bean
public Server hsqlServer(HsqlDbProperties properties) throws ServerAcl.AclFormatException, IOException {
var server = new Server();
var props = new HsqlProperties();
props.setProperty("server.database.0", String.format("mem:%s", properties.name()));
props.setProperty("server.dbname.0", properties.name());
props.setProperty("server.no_system_exit", true);
props.setProperty("server.port", properties.port());
server.setProperties(props);
server.start();
return server;
}
/**
* As we manually create database instance, we need also create datasource instance.
* HSQL instance is passed to force spring to create {@link Server} before pool.
*
* @param dataSourceProperties spring.datasource.* properties
* @return datasource instance
*/
@Bean
public HikariDataSource hikariDataSource(DataSourceProperties dataSourceProperties, Server ignore) {
var config = new HikariConfig();
config.setJdbcUrl(dataSourceProperties.getUrl());
config.setUsername(dataSourceProperties.getUsername());
config.setPassword(dataSourceProperties.getPassword());
config.setDriverClassName(dataSourceProperties.getDriverClassName());
return new HikariDataSource(config);
}
}

View file

@ -0,0 +1,8 @@
package eu.ztsh.wymiana.data.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("hsqldb")
public record HsqlDbProperties(String name, int port) {
}