Mongo DB Operations Framework to be used with jay-schema
npm install ia-mongo
var jay-schema = require('jay-schema');
var Jay-Mongo = require('jay-mongo');
var connUrl = 'mongodb://localhost/jaymongo';
Jay-Mongo
.pConnect(connUrl)
.then(function() {
console.log('Connected Correctly to DB');
});
}, function(err) {
console.log(err);
});
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);
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
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);
});
var Jay = Jay-Mongo.model('Jay');
Jay.pMakeStruct({
name: 'Jose',
lastname: 'Rodriguez',
email: 'j@gmail.com'
}).then(function(result) {
console.log('struct', result);
});
Jay.pCreate({
name: 'Jose',
lastname: 'Rodriguez',
email: 'j@gmail.com'
})
.then(function(result) {
console.log('new Jay', result[0]);
});
Jay.pFindByEmail('j@gmail.com').then(function(result) {
console.log('Found Jay', result);
});
Check jay-Schema documentation for Schema Details
Jay.pMakeStruct(data,options,extention)
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
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
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
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
}
}
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']]
}
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
}
Jay.pCount(query)
npm test
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.
- 1.0.0 Init fork