Skip to content

An object oriented toolkit for MongoDB Stitch in KotlinJS

License

Notifications You must be signed in to change notification settings

robert-cronin/kstitch

Repository files navigation

Awesome Kotlin Badge Download Build Status License

KStitch

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

Getting Started

Development

You can explore the source code either by browsing this repo or downloading KStitch from GitHub:

git clone https://github.com/fortytwoapps/kstitch.git

Installing

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"
}

Usage

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:

Initializing and logging in:
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}")
        }
}
Calling a function:
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};
};

BSON and Extended JSON

KStitch now comes with BSON support:

import com.fortytwoapps.kstitch.bson.ObjectID

val newId = ObjectID()

ODM Functionaltiy

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()

Built With

Contributing

Feel free to contribute if you feel this toolkit is lacking for your application. Contributions are open.

Authors

License

This project is licensed under the Apache 2.0 License - see the LICENSE.md file for details

Acknowledgments

  • Hat tip goes to developers at MongoDB for developing the JavaScript Stitch API
  • Template for this readme came from PurpleBooth

Releases

No releases published

Packages

No packages published

Languages