1.0.2 • Published 1 year ago

mongofy v1.0.2

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

mongofy

An NPM package to simplify the use of MongoDB

Getting started

  • Install the package
npm install mongofy
  • Import the installed package
const { Database } = require("mongofy");
  • Init the database
const mongo = new Database("mongodb://localhost", {
  name: "Mongofy", // The name of the database
  separator: ".", // The separator symbol that you will use to split the document
  client: {
    ...
  }, // The MongoClient options
});

Database options

KeyValue typeDescriptionDefault valueOptional?
nameStringThe name of the databaseMongofyYes
separatorStringThe separator symbol that you will use to split the document.Yes
clientMongoClientOptionsThe MongoClient options{}Yes

All methods

WARNING

All methods are asynchronous

set(key, value)

Set a value to a key

get(key)

Get the value of a key

delete(key)

Delete the value of a key

has(key)

Check if a key exists

all()

Get the document

clear()

Clear the document expect the filter

push(key, item)

Push an item into an array

pull(key, item)

Pull an item from an array

add(key, number)

Add a number to a key value

remove(key, number)

Remove a number from a key value

Example

const { Database } = require("mongofy");
const mongo = new Database(
  "mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017/",
  {
    name: "Database",
    separator: "_",
    client: {
      authSource: "admin",
    },
  }
);
const db = mongo.collection("doc").find({
  id: "1",
});

async function run() {
  await db.set("a_b_c", "value"); // { "id": "1", "a": { "b": { "c": "value" }}}
  await db.get("a"); // { "b": { "c": "value" }}
  await db.delete("a_b_c"); // { "id": "1", "a": { "b": {}}}
  await db.has("a_b_c"); // false
  await db.all(); // { "id": "1", "a": { "b": {}}}
  await db.clear(); // { "id": "1" }
  await db.push("a_b", "item"); // { "id": "1", "a": { "b": ["item"] }}
  await db.pull("a_b", "item"); // { "id": "1", "a": { "b": [] }}
  await db.add("a_c", 4); // { "id": "1", "a": { "b": [] }, { "c": 4 }}
  await db.remove("a_c", 2); // { "id": "1", "a": { "b": [] }, { "c": 2 }}
}

run();