Skip to content

Mongo DB Operations Framework to be used with JaySchema. JaySchema+JayMongo were built as alternative to Mongoose.

Notifications You must be signed in to change notification settings

zombitos/jay-mongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jay-Mongo

Mongo DB Operations Framework to be used with jay-schema

Installation

npm install ia-mongo

Basic Usage

Vars

var jay-schema = require('jay-schema');
var Jay-Mongo = require('jay-mongo');
var connUrl = 'mongodb://localhost/jaymongo';

Connect to Database

Jay-Mongo
  .pConnect(connUrl)
  .then(function() {
    console.log('Connected Correctly to DB');
    });
  }, function(err) {
    console.log(err);
  });

Create a Schema with jay-schema

var JaySchema = new jay-schema({
  name: {
    required: true,
  },
  lastname: {
    required: true
  },
  email: {
    type: String,
    required: true,
    index: 1,
    indexOptions: {
      unique: true
    },
    validator: function(val) {
      var regex = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
      if (!regex.test(val)) {
        return false;
      }
      return val;
    }
  },
  createdAt: {
    type: Date,
    required: true,
    index: 1,
    default: function() {
      return new Date();
    }
  },
  deletedAt: {
    type: Date,
    default: null
  }
}, schemaOptions);

Register the model to Jay-Mongo

Basic registration

Jay-Mongo.register('Jay', JaySchema);

You can register custom methods to add extra operations, or operation wrappers to the models

var methods = {
  sayHello: function() {
    return 'Hello';
  },
  pFindByEmail: function(email) {
    return this.pFindOne({
      email: email
    }).then(function(result) {
      return result;
    }, function() {
      return 'ERROR!!!!';
    })
  }
};
Jay-Mongo.register('Jay', JaySchema,methods);

Methods parameter needs to be an object. Methods properties names need to different form Jay-MONGO reserved operation names: pMakeStruct pFindAndModify pUpdate pDestroy pCreate pFindOne pFindMany pCount

Load Models

You can use loadModels method to require the files of a folder where your models are, that way you can make sure when you call a model it has already been registered

Jay-Mongo.loadModels(__dirname + '/models/');

###Express app.js example

Jay-Mongo.pConnect(config.db)
  .then(function() {
    Jay-Mongo.loadModels(__dirname + '/_app/models/');
    var app = express();
    //EXPRESS CONFIGURATION WOULD BE HERE
    module.exports = app;
  }, function(err) {
    console.error('Jay-MONGO CONNECTION ERROR:', err);
  });

Get the registered Model

var Jay = Jay-Mongo.model('Jay');

Create an Struct Manually

Jay.pMakeStruct({
    name: 'Jose',
    lastname: 'Rodriguez',
    email: 'j@gmail.com'
  }).then(function(result) {
      console.log('struct', result);
    });

Make an insertion to DB

Jay.pCreate({
    name: 'Jose',
    lastname: 'Rodriguez',
    email: 'j@gmail.com'
  })
  .then(function(result) {
      console.log('new Jay', result[0]);
    });

Use a custom operation

Jay.pFindByEmail('j@gmail.com').then(function(result) {
  console.log('Found Jay', result);
});

Posible Operations To DB (Doc in progress)

All operations will return a promise

Check jay-Schema documentation for Schema Details

Jay.pMakeStruct(data,options,extention)

Find and Modify

Jay.pFindAndModify(query, data, options)

Query: the search criteria the document needs to meet to be updated

Data: Update Object, needs to be key = Valid Mongodb update operator(Such as set, push, inc. Check mongodb documentation for operators) value = object with poperties to update **Note: The operators can with or without the '$' sign

//Example
 Model.pFindAndModify({
      email: 'test@email.com'
    }, {
      set: {
        email: 'test2@email.cr'
      },
      $inc:{
        timesLogged: 1
      }
    }, {
      new: true
    })

Options: Options available to mongodb for findAndModify, such as {new:true}. Search mongoDB documentation for available options **Note: results sorting needs to be defined withing options paramenter

{
  sort:{createdAt:1}
}

pFindAndModify will run jay-schema pMakeStruct operation with option omitUndefined set to true to validate the data parameter. Check jay-Schema documentation for Schema Details

Update

Jay.pUpdate(query, data, options)

Query: the search criteria the document needs to meet to be updated

Data: Update Object, needs to be key = Valid Mongodb update operator(Such as set, push, inc. Check mongodb documentation for operators) value = object with poperties to update **Note: The operators can with or without the '$' sign

//Example
 Model.pUpdate({
      email: 'test@test.cr'
    }, {
      set: {
        name: 'Hola',
        lastname: 'Mundo'
      },
      $setOnInsert: {
        createdAt: new Date()
      }
    }, {
      upsert: true
    })

Options: Options available to mongodb for update, such as {upsert:true}. Search mongoDB documentation for available options

pUpdate will run jay-schema pMakeStruct operation with option omitUndefined set to true to validate the data parameter. Check jay-Schema documentation for Schema Details

Create

Jay.pCreate(data, options, schemaOptions)

Data parameter can be an object or an array of objects. Either way the result will be an array of objects with the new documents.

Options object, check mongodb documentation for inserting options.

schemaOptions object, jay-schema option, omitUndefined : true is required for index in unique and sparse to work.

pCreate will run jay-schema pMakeStruct operation to validate the data parameter. Check jay-Schema documentation for Schema Details

Find One

Jay.pFindOne(query, options)

Query: the search criteria the document needs to meet to be found

Options: fields to be returned or excluded, mongo does not allow excluding and including fields definition.

//INCLUDING FIELDS
{
  fields:{
    createdAt:1
  }
}
//EXCLUDING FIELDS
{
  fields:{
    _id:0,
    email:0
  }
}

Find Many

Jay.pFindMany(query, options)

Query: the search criteria the documents need to meet to be found

Options: fields to be returned or excluded, mongo does not allow excluding and including fields definition. Also other options such as sorting, check mongo documentation for available options.

//INCLUDING FIELDS
{
  fields:{
    createdAt:1
  }
}
//EXCLUDING FIELDS
{
  fields:{
    _id:0,
    email:0
  }
}
//SORTING NEEDS TO BE DONE IN ARRAY NOTATION
{
  fields:{
    createdAt:1
  }
  sort:[['createdAt','desc'],['name','asc']]
}

Destroy

Jay.pDestroy(query, options)

Query: the search criteria the documents need to meet to be destroyed

Options: justOne: Boolean, default true. If true only destroys the first document found, if false it deletes all documents that meet the criteria

//DESTROYS FIRST FOUND
{
  justOne: true //default
}
//DESTROYS ALL THAT MEET CRITERIA
{
  justOne: false
}

Count

Jay.pCount(query)

Tests

npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Release History

  • 1.0.0 Init fork

About

Mongo DB Operations Framework to be used with JaySchema. JaySchema+JayMongo were built as alternative to Mongoose.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published