@mongodb-model/template

0.0.11 • Public • Published

Template

Man is the man page for the mongodb model. If you are not building a cli application, then you most likely do not need it. Man is a duplex stream, specifically a Transform stream with some functionalities added. Primarily, it uses full power of the NodeJs Transform Stream API. In other words, everything you can do with NodeJs Transform API you can do with cli! Cli is centrally very highly event driven. Its common use is by extension or by using object destruction to get the instance methods needed.

Installation

$ yarn add @mongodb-model/template

or

$ npm i @mongodb-model/template

Simple Usage Examples

Making api request (http request)

const CLI = require('@mongodb-model/template');
const cli = new CLI();
cli.apiGet(); //base.apiGet(your api endpoint)
cli.on('apiGet', data => console.log(data));
cli.on('apiGet-error', error => console.error(error));
 

By extension

class MyWonderfulClass extends require('@mongodb-model/template') {

    constructor(...arrayOfObjects) {

    super({ objectMode: true, encoding: "utf-8", autoDestroy: true });

    arrayOfObjects.forEach(option => {
        if(Object.keys(option).length > 0){
            Object.keys(option).forEach((key) => { if(!this[key]) this[key] = option[key];})
        }
    });

    this.autobind(MyWonderfulClass);
    this.autoinvoker(MyWonderfulClass);
    this.setMaxListeners(Infinity);
  }
};

Schema template creation example: userSchemaTemplate

const Template = require('@mongodb-model/template');

const {schema} = new Template

const userSchemaTemplate = schema({name: 'UserSchema', type: 'object'});

// The resulting userSchemaTemplate contains: 

'use strict';
/*
|--------------------------------------------------------------------------
| UserSchema Schema
|--------------------------------------------------------------------------
|
| Here we you may add more options (keys) to you schema.
|
|
*/
const Schema  = require('@mongodb-model/db-schema');

const {makeSchema}  = new Schema;

 module.exports = makeSchema("UserSchema",{

  property: "string|min:2|max:10",

 }, "object") ;

Model template creation example: userModelTemplate

const Template = require('@mongodb-model/template');

const {model} = new Template

const userModelTemplate = model({model: 'User', collection: 'users'});

// The resulting userModelTemplate contains: 


'use strict';
/*
|--------------------------------------------------------------------------------
| User Model
|--------------------------------------------------------------------------------
|
| User extends the base model (Model) class and thus has everything
| the base model has including all the basic CRUD methods or operations.
|
|
*/
const Model = require('@mongodb-model/model');

class User extends Model{

    /*
    |----------------------------------------------------------------------------------
    |                                   constructor
    |----------------------------------------------------------------------------------
    |
    | dbOptions: default database options: collection, database url, and database name.
    | options: default model options: any other option  for the model.
    |
    |
    */
    constructor(dbOptions = {collection: 'users', url: 'mongodb://localhost:27017', db: 'app'},...options){
   
    /*
    |-------------------------------------------------------------------------------------
    |                                       super
    |-------------------------------------------------------------------------------------
    |
    | dbOptions: default database options: collection, database url, and database name.
    |
    |
    */

    super(dbOptions);

    /*
    |--------------------------------------------------------------------------------------
    | default database options: in case dbOptions is set but collection and url 
    | keys on the dbOptions are not provided.
    |--------------------------------------------------------------------------------------
    |
    */

    if(!this['hasOwnProperty']['collection']) this.collection = 'users';
    if(!this['hasOwnProperty']['url']) this.url = 'mongodb://localhost:27017';

    /*
    |---------------------------------------------------------------------------------------
    |                                      model options
    |---------------------------------------------------------------------------------------
    | Any other optional options passed to the model.
    |
    */
        options.forEach(option => {
            if(Object.keys(option).length > 0){
                Object.keys(option).forEach(key => {
                    if(!this[key]) this[key] = option[key];
                })
            }
        })
    }

    /*
    |---------------------------------------------------------------------------------------
    |                   Bellow, you may add properties and methods to your model. 
    |---------------------------------------------------------------------------------------
    |
    */


    /**
     * @name sayHello
     * @function
     *
     * @param {Object|String} toMyProject Project name or project object.
     *
     * @description says hello to my project
     *
     * @return does not return anything
     *
     */

    async sayHello(toMyProject) {
        console.log('Hello there', toMyProject, '! I understand you are the new wonderful chick in the neighborhood!');
    }

 }


 /*
 |-----------------------------------------------------------------------------------------------
 |                                       exports model 
 |-----------------------------------------------------------------------------------------------
 |
 */
 module.exports = User;

Author's Info

Website NPM Github Gitlab Blog LinkedIn Facebook Twitter Instagram
Website NPM Github Gitlab Blog LinkedIn Facebook Twitter Instagram

Package Sidebar

Install

npm i @mongodb-model/template

Weekly Downloads

1

Version

0.0.11

License

MIT

Unpacked Size

43.9 kB

Total Files

10

Last publish

Collaborators

  • eweah
  • afrosintech