A KotlinJS Toolkit for the MongoDB Stitch API. This tool kit will also support an ODM (Object Document Mapper) in the future, watch this space!
Heavily inspired by KMongo: https://github.com/Litote/kmongo
You can explore the source code either by browsing this repo or downloading KStitch from GitHub:
git clone https://github.com/fortytwoapps/kstitch.git
To setup KStitch and start using it in your project, add these lines to build.gradle:
repositories {
maven { url = "https://dl.bintray.com/robert-cronin/fortytwoapps" }
}
dependencies {
implementation "fortytwoapps:kstitch:4.9.0"
}
To start using KStitch in your project, simply import like this:
import com.fortytwoapps.kstitch.*
If you are familiar with the MongoDB Stitch JavaScript API, you can still use the Kotlin in much the same way:
import com.fortytwoapps.kstitch.browser.*
fun main(args: Array<String>){
// Initialize default app client
Stitch.initializeDefaultAppClient('<your-client-app-id>')
// Login anonymously
val client = Stitch.initializeDefaultAppClient('<your-client-app-id>')
client.auth.loginWithCredential(AnonymousCredential())
.then {user ->
console.log("Logged in as anonymous user with id ${user.id}")
}
}
client.callFunction("echoArg", ["Hello world!"])
.then { echoedResult ->
console.log("Echoed result: $echoedResult")
}
Note: this requires a function called echoArg
to be defined in the Stitch UI that looks like this:
exports = function(arg) {
return {arg: arg};
};
KStitch now comes with BSON support:
import com.fortytwoapps.kstitch.bson.ObjectID
val newId = ObjectID()
Define a Kotlin class and use it in Stitch!
class User(
var _id: ObjectID = "",
var adminUser: Boolean = false,
var email: String = "",
var fullName: String = "",
var age: Int = 18,
var customFunction: Code = Code("console.log('Hello World')")
)
val mongoClient = client.getRemoteServiceClient("<your-service-name>")
val userDatabase = mongoClient.db("user")
val userDataCollection = userDatabase.collection<User>("userData")
val specificUser: User = userDataCollection.find(UserData::_id eq ObjectID("5e13f16fb3de4f44ccb386e8")).await().firstOrNull()
val adminUserList: List<User> = userDataCollection.find(UserData::adminUser eq true).await()
- stitch-js-sdk - The underlying JavaScript library used
- Travis CI - Continuous Integration
- Gradle - Dependency Management
Feel free to contribute if you feel this toolkit is lacking for your application. Contributions are open.
- Robert Cronin - Initial work - robert-cronin
This project is licensed under the Apache 2.0 License - see the LICENSE.md file for details
- Hat tip goes to developers at MongoDB for developing the JavaScript Stitch API
- Template for this readme came from PurpleBooth