MongoDB wrapper for beginners using Mongoose and simple syntax made with JavaScript.
A simple promise-based wrapper made for beginners with focus in Performance and Simplicity.
Inspired in quick.db, Enmap and denky-database
npm install easy-mongodb.js --production
yarn add easy-mongodb.js
Create a issue or join my Discord server click here (support only in english and portuguese)
const DatabaseManager = require('easy-mongodb.js');
const db = new DatabaseManager('mongodb url here', 'name');
db.on('ready', async () => {
console.log('Database connected successfully.');
// Create key 'companies' with an array as value.
await db.set('companies', ['Facebook', 'Apple', 'Amazon', 'Netflix', 'Google'];
// Get the companies list from database
const companiesList = await db.get('companies');
console.log(companiesList); // Should return an array.
});
db.on('error', (mongooseError) => {
console.log('Unexpected error:', mongooseError);
});
db.on('disconnect', (mongooseError) => {
console.log('Disconnected from the database. Trying to reconnect automatically...');
});
db.connect();
-
Database#connection
| Mongoose connection object -
Database#db
| Mongoose connection object -
Database#model
| Mongoose model object -
Database#mongoose
| Mongoose model object -
Database#schema
| Mongoose schema object -
Database#set(key, value)
| Create or changes a key with the specified valueDatabase.set('website', 'www.github.com/DenkyLabs');
- Types:
- Key name: any (*)
- Value: any (*)
- Types:
-
Database#get(key)
| Returns the value of the specified keyDatabase.get('website');
- Types:
- Key name: any (*)
- Returns: undefined or any (*)
- Types:
-
Database#delete(key)
| Delete a key from the databaseDatabase.delete('website');
- Types:
- Key name: any (*)
- Returns: Object or null
- Types:
-
Database#exists(key)
| Check if a key existsDatabase.exists('website');
- Types:
- Key name: any (*)
- Returns: boolean
- Types:
-
Database#inc(key)
| Increment a number to the key (addition)await Database.set('number', 15); Database.inc('number', 30) // Sets the value to 45
- Types:
- Key name: any (*)
- Returns: Object or number
- Types:
-
Database#dec(key)
| Decrease a number to the key (addition)await Database.set('number', 15); Database.dec('number', 10) // Sets the value to 5
- Types:
- Key name: any (*)
- Returns: Object or number
- Types:
-
Database#deleteAll()
| Deletes everything from the database
Database.deleteAll()
-
Types:
- Returns: Array
-
Database#keyArray()
| Get all keys from the database
Database.keyArray();
-
Types:
- Returns: Array
-
Database#getAll()
| Get all objects from the database
Database.getAll();
-
Types:
- Returns: Array
-
Database#pull(key, value)
| Remove an item from an array
await Database.set('companies', ['Google', 'Facebook'])
Database.pull('companies', 'Google'); // ['Facebook']
-
Types:
- Returns: Object
-
Database#push(key, value)
| Add an item from an array
await Database.set('companies', ['Facebook'])
Database.pull('companies', 'Google'); // ['Facebook', 'Google']
-
Types:
- Returns: Object
-
Database#push(key, value)
| Add an item from an array
await Database.set('companies', ['Amazon'])
Database.includes('companies', 'Amazon'); // true
- Types:
- Returns: boolean