1.0.1 • Published 1 year ago

moonlight.db v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Table of contents

About

Moonlight.DB is a lightweight local database with no dependencies and easy to use.

Installation

Node.js v14.0.0 or newer is recommended.

Install: npm install moonlight.db

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);
});

Links

Help

If you are experiencing problems, or you just need a nudge in the right direction, please do not hesitate to create a New Issue on Github repository.