How to use multiple data sources in micronaut

Viewed 40

I have two schemas for my application. One for regular customer data and another for analytics purpose.

I want to use one data source for customer data related domain objects & other data source for analytics related domain objects.

I am new to micronaut. I see micronaut application.yml file supports multiple data sources to configure. Micronuat uses default data source by default.

Is there a way to configure datasource at a domain level like grails entity.

I'm using frontier schema for Person domain.

class Person {
    String guid
    String name

    static mapping = {
        datasource 'frontier'
        table 'person'
    }
}

Using analytics schema for Event domain.

class Event {
    String guid
    String action

    static mapping = {
        datasource 'analytics'
        table 'person_event'
    }
}

In Grails, operations on each domain are happened based on the given datasource.

How to achieve the same functionality in micronaut using multiple data sources ?

1 Answers

Is there a way to configure datasource at a domain level like grails entity.

There is a way to configure datasource at a domain level, but it isn't the way you would do it in Grails. In Micronaut Data the datasource may be specified using @Repository or @TransactionalAdvice.

An example from micronaut-projects.github.io/micronaut-data/3.7.3/guide/#dbcRepositories:

@JdbcRepository(dialect = Dialect.ORACLE, dataSource = "inventoryDataSource") 
@TransactionalAdvice("inventoryDataSource") 
public interface PhoneRepository extends CrudRepository<Phone, Integer> {
    Optional<Phone> findByAssetId(@NotNull Integer assetId);
}
Related