Giter Site home page Giter Site logo

fortytwoapps / kstitch Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 134 KB

An object oriented toolkit for MongoDB Stitch in KotlinJS

License: Apache License 2.0

Kotlin 100.00%
mongodb-stitch javascript-stitch-api odm kotlin kotlinjs mongodb-odm mongodb

kstitch's Introduction

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

kstitch's People

Contributors

robert-cronin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.