SDA Commons Server Spring Data Mongo¶
The module sda-commons-server-spring-data-mongo is used to work with MongoDB using Spring Data Mongo.
Initialization¶
The SpringDataMongoBundle
should be added as a field in the application class instead of being anonymously added in the initialize
method like other bundles of this library.
The Dropwizard application's config class needs to provide a
MongoConfiguration
.
Please refer to the official documentation how to annotate your entity classes correctly, e.g. by
adding @Document
, @MongoId
or @Indexed
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Configuration¶
The database connection is configured in the config.yaml
of the application, specifically the
connectionString
.
1 2 |
|
Example config for developer machines using local-infra:
1 2 |
|
In tests the config is derived from the MongoDbClassExtension
. See
sda-commons-server-mongo-testing
for details.
Inheritance in Entities¶
It is strongly recommended to annotate all types that are used in a field that does not exactly
match the type with @TypeAlias
.
Using @TypeAlias
will replace the default class name as discriminator with the given value in the
annotation and gives you the ability for refactoring of the model classes.
This rule applies for all types that are a subclass of an (abstract) super class, types that are
stored in a field defined as Object
and all types that are stored in a shared collection.
The latter are usually a subtype of an abstract class to support a common repository.
It is important to register each class that is annotated with @TypeAlias
by using withEntities
in the builder of the bundle.
If not registered there, the mapping is unknown when reading entities.
Health check¶
A health check with the name mongo is automatically registered to test the mongo connection. A simple ping command to the database is used.
Index creation¶
The bundle will create indexes automatically by default. You can change the configuration using the builder:
1 2 3 4 5 6 |
|
Spring Data Mongo Repositories¶
The bundle support creating Spring Data Mongo repositories that are defined by an interface. You
can create an instance of your repository using the bundle's createRepository
method that
accepts the interface.
1 2 3 4 |
|
1 |
|
CA Certificates support¶
Instead of providing caCertificate
as an environment variable, mount the CA certificates in PEM format
in the directory /var/trust/certificates
. Certificates available in subdirectories will also be loaded.
Note that this directory is also configurable through the Dropwizard config class. The config class should then provide a
CaCertificateConfiguration
to the bundle builder. See sda-commons-shared-certificates
for details.