npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@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

10

Readme

MongoModeler

Use database model with simplicity ! Private package developed by BHC-IT

Summary

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 of mongoIniter() function which has been used previously
  • techno : 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 by param2 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 apply
  • newObject : 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 apply
  • newValues : 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