Reactive-MongoDB
Reactive-MongoDB is An ODM for MongoDB with very strict data types/stucture. It is Based on Observable (with RxJS).
Why Observables
You can read about all the benefits of the Reactive approach (Reactive Architectures) here : RxJS5
Use RxJS's Operators you can work easily on your mongodb documents and define complexe behaviors in an easy to reason about way.
What's New
Currently working on data models so you can specifie the structure of your JSON objects and validate them before inserting them into your database. I made a module hyvalidator specificly for data validation, I plan on using it for this module to validate the data models.
Suggestions
Always Open For Suggestions. you can leave them as Issues
Bugs
Please report your bugs here: Issues
Installation
Add the project to your dependencies
npm install --save reactive-mongodb
To properly setup you need to connect to your mongo database with the following commands:
const connect = require('reactive-mongodb').connect;
connect('YOUR_DATABASE_URL');
You only need to do this once.
Collections
Reactive-MongoDB allows you to interact with your collections after you connect to your database.
New Collection
const Collection = require('../index.js').Collection;
const Users = new Collection('users');
Insert A Document
const Collection = require('../index.js').Collection;
const Users = new Collection('users');
var user;
function onItemReceived(item) {
}
function errorHandler(err) {
}
function completionHandler() {
}
Users.insert(user).subscribe(onItemReceived, errorHandler, completionHandler);
Update A Document
const Collection = require('../index.js').Collection;
const Users = new Collection('users');
var oldUser;
var newUser;
function errorHandler(err) {
}
function completionHandler() {
}
Users.updateOne(oldUser,newUser).subscribe(null, errorHandler, completionHandler);
Update will replace the oldUser by the newOne. If you want to update specifique field you have to use {$set:{fields:values}} instead of newUser:
Users.updateOne(oldUser,{$set:{fields:values}}).subscribe(null, errorHandler, completionHandler);
Update A Document By It's Id
const Collection = require('../index.js').Collection;
const Users = new Collection('users');
var newUser;
function errorHandler(err) {
}
function completionHandler() {
}
Users.updateById(id,newUser).subscribe(null, errorHandler, completionHandler);
Update Multiple Documents
const Collection = require('../index.js').Collection;
const Users = new Collection('users');
var query;
function errorHandler(err) {
}
function completionHandler() {
}
Users.update(query,{$set:{fields:values}}).subscribe(null, errorHandler, completionHandler);
Delete Multiple Documents
const Collection = require('../index.js').Collection;
const Users = new Collection('users');
var query;
function errorHandler(err) {
}
function completionHandler() {
}
Users.delete(query).subscribe(null, errorHandler, completionHandler);
Delete A Documents By Id
const Collection = require('../index.js').Collection;
const Users = new Collection('users');
function errorHandler(err) {
}
function completionHandler() {
}
Users.deleteById(id).subscribe(null, errorHandler, completionHandler);
The Id is the MongoDB ObjectID, you just need to pass in the String value Example :
Users.deleteById("590db2cc375bcc2cddc450a5").subscribe(onItemReceived, errorHandler, completionHandler);
Finding Documents
const Collection = require('../index.js').Collection;
const Users = new Collection('users');
const query = {
};
function onItemReceived(item) {
}
function errorHandler(err) {
}
function completionHandler() {
}
Users.find(query).subscribe(onItemReceived, errorHandler, completionHandler);
Finding A Document
const Collection = require('../index.js').Collection;
const Users = new Collection('users');
const query = {
};
function onItemReceived(item) {
}
function errorHandler(err) {
}
function completionHandler() {
}
Users.findOne(query).subscribe(onItemReceived, errorHandler, completionHandler);
Finding A Document By Id
const Collection = require('../index.js').Collection;
const Users = new Collection('users');
function onItemReceived(item) {
}
function errorHandler(err) {
}
function completionHandler() {
}
Users.findById(id).subscribe(onItemReceived, errorHandler, completionHandler);
About the Author
I'm Khaled Romdhane but mostly known as heiyuki.
My handle is : @heiyukidev.
I Work at this amazing Company @redcarpetsolutions don't hesitate to go check us out.
This project is backed By redcarpetsolutions