DB Kong - Database connections simplified
DB Kong is an Android library that allows you to connect, interact and test any type of cloud database on Android apps without the need to setup a dedicated backend. The library simplifies the process of connecting to a database and performing operations on it to just a few lines of code.
As of the current release DB Kong supports the following databases (future releases are aimed at including many more):
MongoDB
I really can't wait for y'all to try it out, so let's dive into the setup and features!
Getting started
- Firstly add the dependencies to your
app/build.gradle
(module level gradle file):
...
dependencies {
...
implementation 'com.1407arjun.libs:dbkong:1.1.0'
//Volley dependencies (required by the library as of the current version)
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.android.volley:volley:1.2.0'
...
}
- Create a
github.properties
file in the root of your project and add in the code as follows (skip this step if you want to use system environment variables as in step 2):
gpr.usr = < your github username >
gpr.key = < your personal access token >
The personal access token should have read packages scope.
- In the
app/build.gradle
(module level gradle file) include the following code to access the credentials from thegithub.properties
file:
...
android {
...
repositories {
maven {
url = "https://maven.pkg.github.com/1407arjun/DBKong"
credentials {
def githubProperties = new Properties()
githubProperties.load(new FileInputStream(rootProject.file("github.properties")))
username = githubProperties['gpr.usr'] ?: System.getenv("GPR_USER")
password = githubProperties['gpr.key'] ?: System.getenv("GPR_API_KEY")
}
}
}
...
}
Steps 2 and 3 are required by github to access any package published on Github Packages.
- Finally the repositories to your project level
build.gradle
file (depends on the Gradle version, do not add these to thebuildscript
):
...
allprojects {
repositories {
google()
mavenCentral()
}
}
...
-
Sync the project with the build files.
-
Add the permission to access the internet and allow clear-text traffic in the
AndroidManifest.xml
:
... <uses-permission android:name="android.permission.INTERNET"/> ... <application android:usesCleartextTraffic="true" ... ...
Using the library
- Firstly initialize the library in the
onCreate()
of yourMainActivity.java
or any other Java file:
new DBKong(getApplicationContext(), timeout, new OnInitListener() {
@Override
public void onInit(boolean init, Error error) {
if (init && error == null) {
...
} else {
if (error != null)
...
}
}
});
Here, timeout
is the time in milliseconds within which the connection should be established. You can set any timeout value depending on your use-case. On initialization, this method invokes a callback giving a boolean
for the initialization status and an Error
object for any error encountered (if any).
- If there is no error and the initialization status
init == true
then the intermediate server connection has been established and now all the connections to the database can be executed. Here, an example is shown for the MongoDB database:
... new DBKong(getApplicationContext(), timeout, new OnInitListener() { @Override public void onInit(boolean init, Error error) { if (init && error == null) { String uri = "mongodb+srv://admin:@ ; MongoDBConnect.getInstance(uri, db, collection) .insertOne(query) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(JSONObject response) { ... } @Override public void onFailure(Error error) { ... } }); } ... } }); ....1oqwt.mongodb.net/ ?retryWrites=true&w=majority "
Here, uri
is the connection string of MongoDB, db
and collection
is the name of the database (string) and collection (string) you want to connect to. This instance has several methods which you can use to perform CRUD or interact with the collection (for eg. insertOne()
, find()
, deleteMany()
, etc.). Also you can add an OnSuccessListener
callback to know if the operation is a success or whether it encountered an error, since all these tasks are carried out asynchronously.
Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the project.
git clone https://github.com/1407arjun/DBKong.git
- Create your feature branch.
git checkout -b feature/AmazingFeature
- Commit your changes.
git commit -m 'Add some AmazingFeature'
- Push to the branch.
git push origin feature/AmazingFeature
- Open a Pull Request.