easy-mongo-database
v1.0.1
Published
MongoDB wrapper for begginers using Mongoose and simple syntax made with JavaScript.
Downloads
12
Maintainers
Readme
easy-mongo-database
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
Installation
npm install easy-mongo-database --production
yarn add easy-mongo-database
Support
Create a issue or join my Discord server click here (support only in english and portuguese)
Example
const DatabaseManager = require('easy-mongo-database');
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();
Documentation
Database#connection
| Mongoose connection objectDatabase#db
| Mongoose connection objectDatabase#model
| Mongoose model objectDatabase#mongoose
| Mongoose model objectDatabase#schema
| Mongoose schema objectDatabase#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