Unique Features
Unique features but in reality Mongodb Model features are the features you are most likely alread familiar with. There really is nothing new. If you are familiar with Javascript (ES6), NodeJs Standard Library, and Mongodb then you are already familiar with every feature of Mongodb Model.
How it works
Simple and easy! On average you need no more than 3 lines of code for each of your queries or CRUD operations, even with the most complex ones. For event base queries, model emits two events: a success event and an error event.
Success event: The success event name is the name of model method executed in the query.
Error event: The error event name is the name of model method executed in the query plus the string "-error".
Explanation: If you use the create method in your query, model will emit success event create with the created object or error event create-error with the error object.
const Model = require('@mongodb-model/model');
const User = new Model({collection: 'users'});
// query (create query)
const userData = {firstname: 'John', lastname: 'Doe', email: 'john.doe@mail.com'};
User.create(userData);
// Listen for the 'create' event because model emitted 'create' event when the 'create' (User.create(userData)) method was called and successully created a new user with the userData.
User.on('create', user => console.log('new user created', user));
// Listen for the 'create-error' event because model emitted 'create-error' event when the 'create' (User.create(userData)) method was called and failed to create a new user with the userData.
User.on('create-error', error => console.log('new user creation error', error));
Model Class or Object
The model object is an ES6 Class with an unlimited constructor parameters. All of the constructor parameters are optional! However, the most important constructor parameter is the first parameter, because it an object that defines your database connection parameters.
Model constructor first parameter object
The first parameter object is optional. Event when provided, its keys are optional. In its complete form, it is an object with three keys:
db: The database name. If not provided it defaults to your .env DATABASE_NAME .
url: The database url. If not provided it defaults to your .env DATABASE_URL or 'mongodb://localhost:27017' .
collection: The collection name. If not provided it defaults to 'users' .
*** This also allows multiple database (local and/or sever) connections.
A detailed explanation
const Model = require('@mongodb-model/model');
// Usage
const YourCustomModel = new Model({db: 'your_database_name', collection: 'your_collection_name', url: 'your_database_url'})
// No constructor Parameter provided:
const User = new Model;
// Default collection is 'users'
// Default database name is your .env DATABASE_NAME
// Default database url is your .env DATABASE_URL or 'mongodb://localhost:27017'
// Constructor first parameter object with only collection key
const User = new Model({collection: 'users'});
// Default database name is your .env DATABASE_NAME
// Default database url is your .env DATABASE_URL or 'mongodb://localhost:27017'
// Connecting to multiple databases
const BlogUser = new Model({db: 'blog', collection: 'users'})
const WorkChat = new Model({db: 'work', collection: 'chats'})
const ForumUser = new Model({db: 'forum', collection: 'users'})
// query (create query using ForumUser)
const userData = {firstname: 'John', lastname: 'Doe', email: 'john.doe@mail.com'};
ForumUser.create(userData);
ForumUser.on('create', user => console.log('new user created', user));
ForumUser.on('create-error', error => console.log('new user creation error', error));
Again, model constructor can take from zero to unlimited parameter objects. Besides the first parameter object, for all the other parameter objects, each key of each the parameter objects is added or bounded as property or method to the prototype of the correponding model instance.
All other Model constructor parameter objects
All model constructor parameters are optional. Event when provided, each key for each of the additional parameter objects are optional. In its complete form, each parameter object can have zero to unlimited keys and the value of a key can be any javascript data type.
*** This also allows to add properties, methods, or classes to the instance.
A detailed explanation
const Model = require('@mongodb-model/model');
const User = new Model({},{title: 'Cool Users', age: 25, fullName: () => 'User Full Name', Post: class Post {}});
// The User model now has the following added to its prototype and they are bounded to it: title,age, fullName, post
// So now we can do things like the following:
const title = User.title
const age = User.age
const fullname = User.fullName();
const FirstPost = new User.Post
// Or using object destructuring
const {title, age, fullName, Post} = User
Event Base Example CRUD
Model emits its method names as success events and its method names plus the string "-error" as error events.
const Model = require('@mongodb-model/model');
const User = new Model({collection: 'users'});
// Read (reall all)
User.all();
User.on('all', users => console.log('all users', users));
User.on('all-error', error => console.log('error in getting all users', error));
// Create
const userData = {firstname: 'John', lastname: 'Doe', email: 'john.doe@mail.com'};
User.create(userData);
User.on('create', user => console.log('new user created', user));
User.on('create-error', error => console.log('new user creation error', error));
// find, takes exact same parametters as the pure mongodb find method.
User.find({_id: '633050cf3a5f6ed0d6c482e7'});
User.on('find', user => console.log('found user', user));
User.on('find-error', error => console.log('error in finding user', error));
// update, takes exact same parametters as the pure mongodb updateOne method.
User.update({_id: '633050cf3a5f6ed0d6c482e7'},{firstname: 'New First Name'});
User.on('update', user => console.log('updated user', user));
User.on('update-error', error => console.log('error in updating user', error));
// delete, takes exact same parametters as the pure mongodb deleteOne method.
User.delete({_id: '633050cf3a5f6ed0d6c482e7'});
User.on('delete', user => console.log('deleted user', user));
User.on('delete-error', error => console.log('error in deleting user', error));
Promise Base Example CRUD
Model provides promise based queries.
const Model = require('@mongodb-model/model');
const User = new Model({collection: 'users'});
// Read (reall all)
User.awaitAll()
.then(users => console.log('all users', users))
.catch(error => console.log('finding all users eror', error));
// Create
const userData = {firstname: 'John', lastname: 'Doe', email: 'john.doe@mail.com'};
User.awaitCreate(userData)
.then(user => console.log('new user created', user));
.catch(error => console.log('new user creation error', error));
// awaitFind, takes exact same parametters as the pure mongodb find method.
User.awaitFind({_id: '633050cf3a5f6ed0d6c482e7'})
.then(user => console.log('found user', user));
.catch(error => console.log('error in finding user', error));
// awaitUpdate, takes exact same parametters as the pure mongodb updateOne method.
User.awaitUpdate({_id: '633050cf3a5f6ed0d6c482e7'},{firstname: 'New First Name'})
.then( user => console.log('updated user', user));
.catch(error => console.log('error in updating user', error));
// awaitDelete, takes exact same parametters as the pure mongodb deleteOne method.
User.awaitDelete({_id: '633050cf3a5f6ed0d6c482e7'});
.then( user => console.log('deleted user', user));
.catch(error => console.log('error in deleting user', error));
Give it a try
CLI in terminal screenshot
// Install Model
yarn add @mongodb-model/model
// Type the following command in your terminal to initiate the cli:
model
