Skip to content

Simple and async java-library for mysql and mongodb database-connections

License

Notifications You must be signed in to change notification settings

Heliumdioxid/database-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

database-api

GitHub release (latest by date including pre-releases) GitHub GitHub issues GitHub pull requests

a simple and async java-library for mysql and mongodb database-connections

It is an easy-to-use library that (until now) can connect to mysql and mongodb databases.
In addition, interactions with databases are simplified. Feel free to leave a ⭐ if you appreciate the work.

getting startedreport a bugfeature request


Table of Contents


🧪 Installation

You can integrate the database-api into your project with Maven or Gradle.

Maven

Maven repository:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Maven dependencies:

<dependency>
    <groupId>com.github.Heliumdioxid.database-api</groupId>
    <artifactId>api</artifactId>
    <version>1.0.0-rc1</version>
</dependency>

<dependency>
    <groupId>com.github.Heliumdioxid.database-api</groupId>
    <artifactId>mysql</artifactId>
    <version>1.0.0-rc1</version>
</dependency>

<dependency>
    <groupId>com.github.Heliumdioxid.database-api</groupId>
    <artifactId>mongo</artifactId>
    <version>1.0.0-rc1</version>
</dependency>

Gradle

Gradle repository:

maven {
    name 'jitpack.io'
    url 'https://jitpack.io'
}

Gradle dependency:

dependencies {
    implementation 'com.github.Heliumdioxid.database-api:api:1.0.0-rc1'
}

dependencies {
    implementation 'com.github.Heliumdioxid.database-api:mysql:1.0.0-rc1'
}

dependencies {
    implementation 'com.github.Heliumdioxid.database-api:mongo:1.0.0-rc1'
}

📖 Code examples

First, you have to create a ConnectionData, either for a mongo or mysql connection. You need to provide a host, username, password, database-name and port (mostly 3306 for mysql and 27017 for mongodb)

ConnectionData connectionData = new ConnectionData("127.0.0.1", "username", "password", "database", 3306);

mysql-database-connection

🤖 The connection to mysql-databases should work properly. If there are any errors, it would be a great help to report them.

Moreover, you have to create some properties, which are then passed on during the connection:

MySQLConnectionConfig mySQLConnectionConfig = new MySQLConnectionConfig(connectionData);
mySQLConnectionConfig.applyDefaultHikariConfig() // recommended setting for HikariCP

After that, the connection to the database can be established like this:

MySQLDatabaseConnection mySQLDatabaseConnection = new MySQLDatabaseConnection(mySQLConnectionConfig);

MySQLConnectionHandler mySQLConnectionHandler;
// connect and check for a present value in the optional:
mySQLConnectionHandler = mySQLDatabaseConnection.connect().join().get();
// get the MySQLConnectionHandler with this method:
mySQLConnectionHandler = mySQLDatabaseConnection.getConnectionHandler().get();

The MySQLConnectionHandler can now be used for communicating with the connected mysql-database:

UpdateResult updateResult = mySQLConnectionHandler.executeUpdate("query"); // execute an update

// executes a query and applies the result to a functional interface
boolean included = mySQLConnectionHandler.executeQuery(resultSet -> resultSet.next(), false, "query");

The connection can finally be closed by calling this method:

mySQLDatabaseConnection.disconnect();

mongo-database-connection

🤖 The connection to mongo-databases was not testet yet. If you have tried it, I would be very grateful if you would contact me.

Moreover, you have to create some properties, which are then passed on during the connection:

MongoConnectionConfig mongoConnectionConfig = new MongoConnectionConfig(connectionData);
mongoConnectionConfig.applyDefaultMongoClientSettings(); // applies default properties like the uri

After that, the connection to the database can be established like this:

MongoDatabaseConnection mongoDatabaseConnection = new MongoDatabaseConnection(mongoConnectionConfig);

MongoConnectionHandler mongoConnectionHandler;
// connect and check for a present value in the optional:
mongoConnectionHandler = mongoDatabaseConnection.connect().join().get();
// get the MongoConnectionHandler with this method:
mongoConnectionHandler = mongoDatabaseConnection.getConnectionHandler().get();

The MongoConnectionHandler can now be used for communicating with the connected mongo-database (few examples):

Optional<MongoCollection<Document>> mongoCollection = mongoConnectionHandler.getCollection("collection").join();
Optional<Document> document = mongoConnectionHandler.getDocument("collection", "fieldName", "value").join();

The connection can finally be closed by calling this method:

mongoDatabaseConnection.disconnect();

⌛ Pending tasks

🤖 Please suggest features that we should include in the api. Read Contributing for details.


🗺️ Contributing

Before submitting issues and pull requests please read the CONTRIBUTING.md file. Please follow these guidelines to make life easier for all of us.


📜 License

database-api is distributed under the terms of the MIT License. A complete version of the license is available in the LICENSE.md in this repository. Any contribution made to this project will be licensed under the MIT License.
All files are Copyright © 2023 Florian H. and all contributors.


☎️ Contact

You can contact me via Discord: Heliumdioxid#3963