mongolight

0.0.2 • Public • Published

Mongolight

This project is a wrapper for mongoskin.

Compatible versions

mongoskin >= 0.6.1.

You should use mongodb version 1.3.3. Prior mongodb versions might not work.

# Mongolight documentation

Install

$ npm install mongolight

Quick start

Get db collection


See mongoskin

var db = mongo.db("localhost:27017/?auto_reconnect=false",{
    database: 'test',
    safe: true
}, {
    connectArbiter: false,
    socketOptions: {
        timeout: 2000
    }
});

Single file require


// file path need full path
var path = require('path');
var mongolight = require('mongolight');
//db collection
mongolight.loadModel(path.join(__dirname,'./model/your_model.js'), {db: db});

Skip db parameter:

mongolight need the db collection to add some dynamic methods ,you can use the global to skip the db parameter.

var path = require('path');
// it will check global.db when you require a model
global._option.db = db ;
var mongolight = require('mongolight');
mongolight.loadModel(path.join(__dirname,'./model/your_model.js');

Multi-file require


if lazy loading is true , loadFolder method only set the path to global.

var path = require('path');
var mongolight = require('mongolight');
mongolight.loadFolder(path.join(__dirname,'./model'),{db:db,lazy:true});
//set the loadModel to global
global.loadModel = mongolight.loadModel;
/**
 *  when you use model class in first time,you need use loadModel method
 **/
//in index.js
loadModel('model');
module.exports = function(app){    
  app.get('/',function(req,res){
    var model = new Model();
    res.render('index', { title: 'Express' });
  });
}; 
 

when lazy loading is false,you can skip the loadModel method

var path = require('path');
var mongolight = require('mongolight');
mongolight.loadFolder(path.join(__dirname,'./model'),{db:db,lazy:false});
 
/**
 *  skip the loadModel method
 **/
//in index.js
module.exports = function(app){
  app.get('/',function(req,res){
    var model = new Model();
    res.render('index', { title: 'Express' });
  });
};
 

Default added methods

Model.findById(id,callback) :

   //use folder require
   loadModel('model');
   Model.findById('your_mongo_id',function(err,model)){
        if(err != null){
            throw err;
        }
        console.info(model);
   };

Model.findByOpt(opt,callback) :

    //use folder require
    loadModel('model');
    Model.findByOpt({key: 'search_content'},function(err,models){
        if(err != null){
            throw err;
        }
        console.info(models);
    });

Model.list(callback) :

the same method as Model.findByOpt({},callback):

    //use folder require
    loadModel('model');
    Model.list(function(err,models){
        if(err != null){
            throw err;
        }
        console.info(models);
    });

Model.modify(id,opt,callback) :

    //use folder require
    loadModel('model');
    Model.modify('mongo_id',{key: 'search_content'},function(err,result){
        if(err != null){
            throw err;
        }
        //updated record amounts
        console.info(result);
    });

Model.delById(id,callback) :

    //use folder require
    loadModel('model');
    Model.delById('mongo_id',function(err,result){
        if(err != null){
            throw err;
        }
        //deleted record amounts
        console.info(result);
    });

model.save(callback) :

save method will save the object to mongo.

     //use folder require
    loadModel('model');
    var model = new Model();
    model.save(function(err,model){
        if(err != null){
            throw err;
        }
        console.info(model);
    });

License

(The MIT License)

Copyright (c) 2014 gimmu

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Dependencies (1)

Dev Dependencies (2)

Package Sidebar

Install

npm i mongolight

Weekly Downloads

0

Version

0.0.2

License

none

Last publish

Collaborators

  • gim