Skip to content

Using a database

Alexander edited this page Nov 12, 2019 · 6 revisions

Simple example on how to use a database

NOTE We currently only support MongoDB & MYSQL (DRIVER)

MongoDB Example

function PlayerJoinEvent(event){
    var player = event.getPlayer();

    //Creating an database url
    var databaseUrl = database.newUrl("mongodb://user:password@ip:port");
    //Creating an mongodb client
    var dbClient = database.newClient(databaseUrl);
    //get a datbase
    var db = dbClient.getDB("database");
    //get a collection inside of our database
    var collection = db.getCollection("collection");

    //You can either, make an object and add data or use the database.convertJSON to use JSON directly
    /* 
    var testObj = database.newObj();
    testObj.put("username", "Hundeklemmen");

    var playerINFO = collection.findOne(testObj);
    */
    //Converting json to DBObject
    var obj = database.convertJSON( {username: "Hundeklemmen"} )
    //Finding an document inside of our collection
    var playerINFO = collection.findOne(obj);
    //Getting an value from our playerinfo object and then we cast it to a string.
    var rank = cast.asString(playerINFO.get("rank"))
    //here we will send the rank to our player
    player.sendMessage("rank: " + rank);
}

MySQL Example Click Here