mongoose-schema-extend
Advanced tools
Comparing version 0.1.7 to 0.2.0
41
index.js
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var mongoose = require('mongoose'), | ||
@@ -23,2 +25,11 @@ owl = require('owl-deepcopy'); | ||
// Fix validators RegExps | ||
Object.keys(this.paths).forEach(function(k) { | ||
this.paths[k].validators.forEach(function (validator, index) { | ||
if (validator.validator instanceof RegExp) { | ||
newSchema.paths[k].validators[index].validator = validator.validator; | ||
} | ||
}); | ||
}, this); | ||
// Override the existing options with any newly supplied ones | ||
@@ -29,2 +40,24 @@ for(var k in options) { | ||
// Change the unique fields to compound discriminator/field unique indexes | ||
// using a 2dSphere [HACK ALERT] to allow duplicates or missing fields on subSchemas where | ||
// the option has not been specified | ||
var uniqueFields = []; | ||
for(var k in obj) { | ||
if(obj[k].unique) { | ||
obj[k].unique = false; | ||
uniqueFields.push(k); | ||
} | ||
} | ||
uniqueFields.forEach(function (field) { | ||
obj[field + '_unique'] = { type: {type: String, enum: "Point", default: "Point"}, coordinates: { type: [Number], default: [0,0] } }; | ||
var index = {}; | ||
index[newSchema.options.discriminatorKey] = 1; | ||
index[field] = 1; | ||
index[field + '_unique'] = '2dsphere'; | ||
newSchema.index(index, { unique: true }); | ||
}); | ||
// This adds all the new schema fields | ||
@@ -56,3 +89,3 @@ newSchema.add(obj); | ||
*/ | ||
var oldInit = Model.prototype.init; | ||
var _init = Model.prototype.init; | ||
Model.prototype.init = function(doc, query, fn) { | ||
@@ -70,3 +103,3 @@ var key = this.schema.options['discriminatorKey']; | ||
process.nextTick(function() { | ||
fn.apply(this, arguments); | ||
if(fn && typeof fn === 'function') fn.apply(this, arguments); | ||
}); | ||
@@ -76,3 +109,3 @@ } | ||
this.schema = model.schema; | ||
var obj = oldInit.call(this, doc, query, newFn); | ||
var obj = _init.call(this, doc, query, newFn); | ||
obj.__proto__ = model.prototype; | ||
@@ -84,3 +117,3 @@ return obj; | ||
// If theres no discriminatorKey we can just call the original method | ||
return oldInit.apply(this, arguments); | ||
return _init.apply(this, arguments); | ||
} |
{ | ||
"name": "mongoose-schema-extend", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"description": "mongoose schema inheritance and discriminator key extension", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "node ./node_modules/.bin/grunt test" | ||
}, | ||
@@ -25,5 +25,6 @@ "homepage": "https://github.com/briankircho/mongoose-schema-extend", | ||
{ "name": "Ajay Sabhaney", "email": "ajay@mothercreative.com" }, | ||
{ "name": "Nicholas Calugar", "email" : "njcalugar@gmail.com" } | ||
{ "name": "Nicholas Calugar", "email" : "njcalugar@gmail.com" }, | ||
{ "name": "Luis Sieira", "email" : "luis.sieira@spacebar.fr" } | ||
], | ||
"license": "BSD", | ||
"license": "BSD-3-Clause", | ||
"dependencies": { | ||
@@ -33,6 +34,10 @@ "owl-deepcopy": "~0.0.1" | ||
"devDependencies": { | ||
"mocha": "~1.3.2", | ||
"should": "~1.1.0", | ||
"mongoose": "~3.5.7" | ||
"glob": "^5.0.15", | ||
"grunt": "^0.4.5", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-mocha-test": "^0.12.7", | ||
"mocha": "^2.3.3", | ||
"mongoose": "^4.2.4", | ||
"should": "~1.1.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
17036
9
0
414
7
1