Database

Database tables can be created with the file browser (with /DB/* rights), multi-column indices must be created manually be the server admin. Following function exist to access the database. See DB Reference for a full list of available functions.

Example insert:

function onUpdated(pl) {
    DB.save("supportticket", {
        userid: pl.id,
        date_created: new Date(),
        nickname: pl.name,
        category: "general",
        subject: "Test2",
        description: "Test " + uuid()
    });
}  

Example load and update:

async function onUpdated(pl) {
    let tickets = await DB.load("supportticket", {});
    this.say("Load tickets from DB: " + tickets.length + " - " + tickets[0].description);

    tickets[0].description = "Update Test " + uuid();
    DB.save("supportticket", tickets[0]);
}

Notice the above async in-front of function and the await keywords before DB.load. This is similar to using the old callback way to load entries, as seen below. DB.save and DB.delete also has an third optional parameter for callback when entries has been saved/deleted from database, incase you want to wait for it to properly be saved/deleted.

function onUpdated(pl) {
    let self = this;
    DB.load("supportticket", {}, function(tickets) {
        self.say("Load tickets from DB: " + tickets.length + " - " + tickets[0].description);

        tickets[0].description = "Update Test " + uuid();
        DB.save("supportticket", tickets[0]);
    });
}


Example delete:

function onUpdated(pl) {
    DB.delete("supportticket", {ticket_id: 2});
}

Joins & More

With the options parameter you can also specify limit & more:

Example of limit, offset, and join:

async function onUpdated() {
    let clanpolls = await DB.load("clanpolls", {
        clanid: 1,
        limit: 10,
        offset: 0,
        join:[
            {table:"clanpolltypes", "clanpolltypes.polltype":"clanpolls.polltype"},
            {table:"clanvotes", "clanvotes.pollid":"clanpolls.pollid", userid:pl.id, required:false},
            {table:"game.players", "players.userid":"clanpolls.userid"}
        ]
    };
    this.say("Clan polls from DB: " + clanpolls.length);
}

Example with orderby and join, see the use of "as" for joining the same table twice:

async function onUpdated() {
    let matches = await DB.load("tournament_match", {
        orderby: "id ASC",
        join:[
            {table:"game.players", as:"participant1", "participant1.userid":"tournament_match.participant1"},
            {table:"game.players", as:"participant2", "participant2.userid":"tournament_match.participant2"}
        ]
    });
    this.say("Tournament matches from DB: " + matches.length);
}