EzyMongo Configuration
1. Configuration List
EzyMongo provides a list of configurations in bellow table:
# | Property | Default Value | Description |
1 | database.mongo.uri | null | MongoDB URI |
2 | database.mongo.host | null | MongoDB host |
3 | database.mongo.port | 0 | MongoDB port |
4 | database.mongo.username | null | MongoDB username |
5 | database.mongo.password | null | MongoDB password |
6 | database.mongo.database | null | MongoDB database name |
7 | database.mongo.collection.naming.case | NATURE | Collection name style |
8 | database.mongo.collection.naming.ignored_suffix | null | Suffix to remove from collection's name |
You can encrypt sensitive configuration like username, password if you want.
2. An Example
You can put bellow configuration to your
application.yaml
file: # for application.yaml
database:
mongo:
uri: mongodb://ezydata_mongo:ezydata_mongo@localhost:27017/ezydata-mongo
database: ezydata-mongo
collection:
naming:
case: UNDERSCORE
ignored_suffix: Entity
Or for
application.properties
file: # for application.properties
database.mongo.uri=mongodb://ezydata_mongo:ezydata_mongo@localhost:27017/ezydata-mongo
database.mongo.database=ezydata-mongo
database.mongo.collection.naming.case=UNDERSCORE
database.mongo.collection.naming.ignored_suffix=Entity
3. Auto Configuration
To configure automatically, you just need add configuration and add
ezyfox-boot-autoconfigure
to your project dependency like this: <dependency>
<groupId>com.tvd12</groupId>
<artifactId>ezyfox-boot-autoconfigure</artifactId>
<version>1.0.4</version>
</dependency>
4. Configure Manually
For some reason, you can not use
ezyfox-boot-autoconfigure
you can config like this: import com.mongodb.MongoClient;
import com.tvd12.ezydata.database.EzyDatabaseContext;
import com.tvd12.ezydata.mongodb.EzyMongoDatabaseContextBuilder;
import com.tvd12.ezydata.mongodb.loader.EzySimpleMongoClientLoader;
import com.tvd12.ezyfox.annotation.EzyProperty;
import com.tvd12.ezyfox.bean.EzyBeanConfig;
import com.tvd12.ezyfox.bean.EzyPackagesToScanAware;
import com.tvd12.ezyfox.bean.EzySingletonFactory;
import com.tvd12.ezyfox.bean.EzySingletonFactoryAware;
import com.tvd12.ezyfox.bean.annotation.EzyConfigurationBefore;
import com.tvd12.ezyfox.util.EzyPropertiesAware;
import lombok.Setter;
import java.util.Map;
import java.util.Properties;
@Setter
@EzyConfigurationBefore
public class EzyMongoConfiguration implements
EzyBeanConfig,
EzyPropertiesAware,
EzySingletonFactoryAware {
@EzyProperty("database.mongo.database")
private String databaseName;
private Properties properties;
private EzySingletonFactory singletonFactory;
@Override
public void config() {
EzyDatabaseContext databaseContext = newMongodbDatabaseContext();
Map<String, Object> repos = databaseContext.getRepositoriesByName();
for (String repoName : repos.keySet()) {
singletonFactory.addSingleton(repoName, repos.get(repoName));
}
}
private EzyDatabaseContext newMongodbDatabaseContext() {
EzyMongoDatabaseContextBuilder builder = new EzyMongoDatabaseContextBuilder()
.properties(properties)
.scan("your_package_to_scan")
.mongoClient(newMongoClient())
.databaseName(databaseName);
return builder.build();
}
protected MongoClient newMongoClient() {
return EzySimpleMongoClientLoader.load(properties);
}
}
Next
You can take a look a list of default functions