Main Class
The Documentation for the main class of abc.db
The main class of abc.db is where you connect to your MongoDB and pass any options you wish to passed into the connections (these are optional)
Requiring/Importing
//Use only one of these (es6 usually only is used in ts)
//es6
import AbcDB from "abc.db";
//commonjs
const AbcDB = require("abc.db")
Constructing the Class
To begin using abc.db, you need a mongo db cluster which you can get for free here or host locally.
const DB = new AbcDB("Mongo connection uri", "your database name");
/*
you can put a database name inside the second option
but it is not mandatory, by default it uses the name "test"
*/
v1
const DB = new AbcDB("Mongo connection uri", {});
//you can put mongoose options as the second part
//but they are not needed. The {} is not needed either
//i have just left it to show where to put the options.
Class Events
Since v2, Events have been removed, but will be left here as a reference
The main class has some events as it extends the event emitter, below you can find them
Connected
This event is triggered when the db connects successfully, also includes when the connection reconnects after being closed.
DB.on("connected", () => console.log("Database Connected"));
Disconnected
This event is triggered when the db disconnects and includes when the connection is closed.
DB.on("disconnected", () => console.log("Database Reconnected"));
Reconnected
This event is triggered when the db reconnects after being disconnected, excludes when the connection is closed
DB.on("reconnected", () => console.log("Database Reconnected"));
Methods
Connect - Promise
To use any methods without an error, you must call this method and make sure it is run with await or .then (treated as a promise essentially)
await DB.connect()
Collections
To make a new collection (you can have as many of these as you want) type:
const myCollection = DB.Collection("your collection name");
v1
const myCollection = new DB.Collection("your collection name");
Refer to the Collection documentation for more info
Last updated
Was this helpful?