nosql-dao library is an enabler for NoSQL databases such as MongoDB and Azure CosmosDB. It allows to bootstrap the project with following functionalities -
- Provides base DAO interface to perform MongoDB related queries.
- Provides base DAO interface to perform CosmosDB related queries.
- Centralizes all database-related code within dedicated DAO implementation classes.
- Provides implementations of create, read, update, delete (CRUD), and many other database operation.
- Provides a configurable way to fetch MongoDB and CosmosDB properties.
- Provides a way to extend custom interfaces for implementing custom Mongo query operations.
- Provides a configurable way to choose the NoSQL database to be used, between CosmosDB and MongoDB.
- Getting Started
- Usage
- How to contribute
- Built with Dependencies
- Code of Conduct
- Authors
- Security Contact Information
- Support
- Troubleshooting
- License
- Announcements
To build the project in the local working directory after the project has been cloned/forked, run:
mvn clean install
from the command line interface.
- Maven
- Java 17
mvn test
Or run a specific test
mvn test -Dtest="TheFirstUnitTest"
To run a method from within a test
mvn test -Dtest="TheSecondUnitTest#whenTestCase2_thenPrintTest2_1"
nosql-dao project serves as a library for the services. It is not meant to be deployed as a service in any cloud
environment.
Add the following dependency in the target project
<dependency>
<groupId>org.eclipse.ecsp</groupId>
<artifactId>nosql-dao</artifactId>
<version>1.x.x</version>
</dependency>
NoSQL-DAO library provides support for integration with Azure CosmosDB as well as MongoDB. Integrating services can define the type of database they need to connect to, with current supported database being Mongo DB and Cosmos DB. Ignite DAO library supports RU, as well as vCore, database instance types for CosmosDB. You can configure the name of NoSQL database service against the property - "no.sql.database.type". Supported values for no.sql.database.type are - mongoDB cosmosDB
no.sql.database.type=mongoDB or cosmosDBDefault connection would be attempted to mongo DB, if no database type defined. If, MongoDB is set as database type, respective configuration for mongo DB need to be configured by the integrating service. Details for the same can be found in the sections below.
Following properties have been exposed for Azure CosmosDB configuration -
cosmos.db.connection.string=Complete connection string for vCore or RU instance of CosmosDB
cosmosdb.name=Name of the databaseThe class MongoPropsCondition enables @Configuration class which allows to fetch mongodb properties when vault is
disabled.
To represent a custom interface for implementing custom Mongo query operations, the services needs to implement
CustomDao interface.
Example:
public interface CustomDao<K, E extends IgniteEntity> extends IgniteBaseDAO<K, E> {}To Perform translation of query QueryTranslatorMorphiaImpl class in implemented which consists translate method
for performing on basis of ignite query and collectionName.
Example:
public class QueryTranslatorMorphiaImpl<E extends IgniteEntity> implements QueryTranslator<Query<E>> {
public Query<E> translate(IgniteQuery igniteQuery, Optional<String> collectionName) {}
}To Perform various basic database operations IgniteBaseDAOMongoImpl class is implemented which contains
below methods:
| Method Names | Purpose |
|---|---|
| findAll | To find all entities. |
| save | To save an entity. |
| saveAll | To save a list of entities. |
| findById | To find an entity by primary key. |
| findByIds | To find list of entities by primary keys. |
| upsert | To insert and update the value in any operation. |
| find | To find an entity using IgniteQuery. |
| findWithPagingInfo | To find with pagination. |
| deleteById | To delete an entity by primary key. |
| deleteByIds | To delete multiple entities by primary keys. |
| deleteByQuery | To delete entities by Query. |
| countByQuery | To find the count of entities by Query. |
| update | To update an existing entity. |
| getAndUpdate | To fetch and update the same entity. |
| updateAll | To update multiple entities. |
| deleteAll | To delete all the entities. |
| removeAll | To delete entities by Query. |
| delete | To delete and entity. |
| distinct | To find distinct entities. |
| streamFindAll | To fetch the reactive stream of all entries. |
| streamFind | To fetch the reactive stream using Query. |
| countAll | To get the count of all existing entries. |
| collectionExists | To check if a collection exists. |
To perform any query operation in mongo you need to create igniteQuery.
Example:
@Repository
public class CustomDaoImpl extends IgniteBaseDAOMongoImpl<String, CollectionName>
implements CustomDao {
@Override
public Optional<List<CollectionName>> findById(String id) {
IgniteCriteria idCriteria = new IgniteCriteria("id", "=", id);
IgniteCriteriaGroup criteriaGroup = new IgniteCriteriaGroup();
criteriaGroup.and(idCriteria);
IgniteQuery query = new IgniteQuery(criteriaGroup);
return Optional.ofNullable(super.find(query));
}
}To Perform translation of query UpdatesTranslatorMorphiaImpl class in implemented which consists translate method
for performing on basis of updates and collectionName.
public class UpdatesTranslatorMorphiaImpl<E extends IgniteEntity> implements UpdatesTranslator<List<UpdateOperator>> {
public List<UpdateOperator> translate(Updates updates , Optional<String> collectionName) {}
}| Dependency | Purpose |
|---|---|
| Morphia Core | ODM for MongoDB |
| Mongo DB Driver Sync | MongoDB Java Driver |
| Mongo DB Driver legacy | MongoDB Support for the Legacy API |
| Junit | Testing framework |
| Mockito | Test Mocking framework |
| Reflections | Scans the classpath, indexes the metadata, allows to query it on runtime and may save and collect that information for many modules within the project. |
| Ignite utils | Centralized logging, Health checks and Diagnostic data reporting library. |
| Vault Apis | Secrets Storage Provider. |
| Reactor Core | Reactive Programming Framework. |
| Flapdoodle Embbed Mongo | Platform for running mongodb in unit tests. |
| Apache Commons | Library focused on algorithms working on strings. |
Please read CONTRIBUTING.md for details on our contribution guidelines, and the process for submitting pull requests to us.
Please read CODE_OF_CONDUCT.md for details on our code of conduct.
![]() Kaushal Arora ๐ ๐ |
See also the list of contributors who participated in this project.
Please read SECURITY.md to raise any security related issues.
Please write to us at csp@harman.com
If, CosmosDB is set as database type, there could be a possible issue where spring tries to autoconfigure a mongo client with localhost, depending upon the spring version. This could lead to below error being observed in the application - Updating cluster description to {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}] To avoid this, please exclude MongoAutoConfiguration class from the spring boot application launcher class.
Example - Excluding MongoAutoConfiguration
@SpringBootApplication(scanBasePackages = {"org.eclipse.ecsp"}, exclude={MongoAutoConfiguration.class})
public class ApiGatewayTestApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayTestApplication.class, args);
}
}Or, Excluding MongoAutoConfiguration
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
public class Application {
// ...
}Please read CONTRIBUTING.md for details on how to raise an issue and submit a pull request to us.
This project is licensed under the Apache-2.0 License - see the LICENSE file for details.
All updates to this library are documented in our Release Notes and releases. For the versions available, see the tags on this repository.

