🔬
Moonlight.DB
GitHubNPM
  • Introduction
  • Getting Started
  • Documentation
    • Database
      • createCollection()
      • deleteCollection()
      • collections
    • Collection
      • find()
      • findOne()
      • update()
      • updateOne()
      • insertOne()
      • insertMany()
      • delete()
      • deleteOne()
      • length
Powered by GitBook
On this page
  • Setting up the Moonlight.DB instance
  • Example Usage

Was this helpful?

Getting Started

Setting up the Moonlight.DB instance

database.js
const DatabaseManager = require("moonlight.db");

module.exports = {
    connection: new DatabaseManager.MoonlightDB({
        dbpath: __dirname + "/database"
    })
};

Example Usage

test.js
const database = require("./database.js");
const db = database.connection;

if(db.collections.includes("users") === false) db.createCollection("users");

/* ------- insert item into db ------- */
db.collection("users").insertOne({
    name: "TestUser",
    age: 26,
    banned: false
}).then(() => {
    //ok user created;
}).catch(error => {
    //error;
    console.log(error);
});

/* ------- find item ------- */
db.collection("users").findOne({name: "TestUser"}).then(item => {
    //ok found the user;
    console.log(item);
    /*
        console output => {
          _id: "3bgfvx99l1t99j0pxouv3u8y00fy6n49",
          name: "TestUser",
          age: 26,
          banned: false
        }
    */
}).catch(() => {
    //error not found;
});

/* ------- update item ------- */
db.collection("users").updateOne({_id: "3bgfvx99l1t99j0pxouv3u8y00fy6n49"}, {
    "$set": {
        banned: true
    }
}).then(() => {
    //ok user updated;
}).catch(error => {
    //error not found;
    console.log(error);
});

/* ------- delete item ------- */
db.collection("users").deleteOne({name: "TestUser", banned: true}).then(() => {
    //ok user removed from db;
}).catch(error => {
    console.log(error);
});
PreviousIntroductionNextDatabase

Last updated 2 years ago

Was this helpful?