@bhc/mongo-modeler
v0.0.2
Published
Use database model with simplicity ! Private package developed by **BHC-IT** # Summary * [Getting Started](#GettingStarted) * [Prerequisites](#Prerequisites) * [Instaling](#Instaling) * [Usages](#Usages) * [Running tests](#RunningTests) * [Classic usa
Downloads
5
Readme
MongoModeler
Use database model with simplicity ! Private package developed by BHC-IT
Summary
- Getting Started
- Usages
- License
Getting Started
Prerequisites
You must install mongodb package with
npm install mongodb --save
If you want to run unitary tests, you must have
- chai
- chai-as-promised
- request
install them with
npm install --save-dev chai
npm install --save-dev chai-as-promised
npm install --save-dev request
Instaling
To install package, just do
npm i @bhc/mongo-modeler
Usages
Running tests
For running tests, you just have to do
npm test
Classic usages
Inclusion
To include the module, use :
const MongoModeler = require('../index.js');
Create a model for your objects
For create a model, make a new class wich extends "Module.base" and set parts
attribut as your object must be.
An exemple of good class may be like this :
class coefModel extends MongoModeler.base {
constructor(props){
super(props);
this.parts = {
name:String,
mod:Array(Object({
kev:Number,
h:Number,
})),
test:{lol:{mdr:Number}},
}
}
}
Don't forget to use super()
in constructor of your class.
Last step is to export your class. To do that, export a new instance of it. Good way to do that can be :
module.exports = (mongo) => {
return new coefModel({colone:"coefs", dbConnector:mongo});
}
You have to pass a JSON parameter to super()
function. It must contain :
colone
: It must be a string, it represents the name of database table in wich will be stock the instance of your class.dbConnector
: It's an instance of database, use a parameter recived when a file import this class
Initialize the dataBase
In your "main" file, include this module :
const MongoModeler = require('../index.js');
and use it to initialize mongo with mongoIniter()
function :
const mongo = MongoModeler.mongoIniter("mongodb://localhost:27017", { useNewUrlParser: true, useUnifiedTopology: true });
mongoIniter()
function take 2 parameters
- First parameter must be a string wich represents the url to access the database
- Second parameter must be a JSON, use
{ useNewUrlParser: true, useUnifiedTopology: true }
Use the module again to create an instance of database with dbMiddleware()
function :
let db = new MongoModeler.dbMiddleware({dbName:"mongo-modeler", dbConnector:mongo, techno:'mongo'});
dbMiddleware()
function take a JSON parameter which must contain :
dbName
: it represents the name of your database (must be a string)dbConnector
: use the result ofmongoIniter()
function which has been used previouslytechno
: it represents the technology which is used (actualy support only mongo) (must be a string)
Last step is to initialize the instance of database freshly created with the init()
function : db.init()
You can start your main function like this :
db.init().then(() => main(db));
async function main(db){
...
}
Use your models
Before using
To use a model, you have to Create it.
Once it's done, start by importing it with a classic require()
:
const coefModel = require('./coefTest.js')(db);
The (db)
parameter send to the require()
is the instance of your database. (How to have it?)
Create your objects
Now, you can use it. To create an object of your class, use the create()
function. Exemple :
async function main(db){
try{
let myObject = await coefModel.create({ name:"test1",
mod:[{kev:1, h:2}],
test:{lol:{mdr:30}}});
} catch(e){console.log(e)}
return;
}
The create()
function take a JSON parameter which take all property that your object may have.
Use your objects
The created object have an attribute named fields
, use it to access values.
Read value of an object attribut
Read it like a normal object but be careful to don't forget fields
before your attribut.
For exemple, to access at the name
field of your object, you have to do :
let nameOfMyObject = myObject.fields.name;
Warning : if the field you want to read is an array, use the getValues()
function. Just like this :
let modOfMyObject = myObject.fields.mod.getValues();
Write value of an object attribut
Write it like a normal object but be careful to don't forget fields
before your attribut.
For exemple, to change the name
field of your object, you have to do :
myObject.fields.name = "aNewName";
Warning : if the field you want to change is an array, use the add()
/ remove()
/ replace()
functions. Just like this :
- add(param) :
param
is a value to add to the array. Exemple of use :myObject.fields.mod.add({kev:31, h:31});
- remove(param) :
param
is a value to remove to the array. Exemple of use :myObject.fields.mod.remove({kev:1, h:2});
- replace(param1, param2) :
param1
will be replace byparam2
in the array. Exemple of use :myObject.fields.mod.replace({kev:1, h:2}, {kev:31, h:31});
Erase your object
To erase an object, just call the erase()
function like this :
myObject.erase();
Clone your object
To clone an object, just call the clone()
function like this :
myObject.clone();
Use database
Find objects
Find one object
To find an object, use find(colone, json)
on an instance of database. Parameters are :
colone :
It must be a string, it represents the name of database table in wich will be find the object.json :
It must be a JSON, defining the conditions the object need to apply
Exemple of use :
let resultOfFind = await db.find("coefs", {name:"test1"});
The db
in front of find
is the instance of your database. (How to have it?)
Find many objects
To find many objects, use findMany(colone, json)
on an instance of database. Parameters are :
colone :
It must be a string, it represents the name of database table in wich will be find objects.json :
It must be a JSON, defining the conditions the objects need to apply
Exemple of use :
let resultOfFind = await db.findMany("coefs", {name:"test1"});
The db
in front of findMany
is the instance of your database. (How to have it?)
Clone objects
To clone objects, use clone(colone, json)
on an instance of database. Parameters are :
colone :
It must be a string, it represents the name of database table in wich will be find and clone your objects.json :
It must be a JSON, defining the conditions the objects need to apply
Exemple of use :
db.clone("coefs", {name:"test1"});
The db
in front of clone
is the instance of your database. (How to have it?)
Update objects
Update an object
To clone objects, use update(colone, oldObject, newObject)
on an instance of database. Parameters are :
colone :
It must be a string, it represents the name of database table in wich will be find and update your object.oldObject :
It must be a JSON, defining the conditions the object need to applynewObject :
It must be a JSON, it's the content which replace the old values in your object
Exemple of use :
db.update("coefs", {name:"test1"}, {name:"newName"});
The db
in front of update
is the instance of your database. (How to have it?)
Update many objects
To clone objects, use updateMany(collection, filter, newValues)
on an instance of database. Parameters are :
collection :
It must be a string, it represents the name of database table in wich will be find and update your object.filter :
It must be a JSON, defining the conditions the objects need to applynewValues :
It must be a JSON, it's the content which replace the old values in your objects
Exemple of use :
db.updateMany("coefs", {name:"test1"}, {name:"newName"});
The db
in front of updateMany
is the instance of your database. (How to have it?)
License
This package is developed for BHC-IT