Created
September 2, 2011 12:16
-
-
Save nekaab/1188475 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* proposed changes for DBRef support | |
* | |
*/ | |
// /lib/schema.js:528 | |
/** | |
* DBRef schema identifier. Not an actual DBRef, only used for Schemas. | |
* | |
* @api public | |
*/ | |
function DBRef () { | |
throw new Error('This is an abstract interface. Its only purpose is to mark ' | |
+ 'fields as DBRef in the schema creation.'); | |
} | |
// /lib/schema.js:551 | |
exports.DBRef = DBRef; | |
// /lib/schema/array.js:14 | |
, DBRef: require('./dbref') | |
// /lib/schema/index.js:22 | |
exports.ObjectId = require('./dbref'); | |
// /lib/schema/dbref.js (new file) | |
// TODO add "$db" support ($db is optional, should be ok in 90% of all use cases) | |
/** | |
* Module dependencies. | |
*/ | |
var SchemaType = require('../schematype') | |
, CastError = SchemaType.CastError | |
, driver = global.MONGOOSE_DRIVER_PATH || './../drivers/node-mongodb-native' | |
, oid = require('../types/objectid'); | |
/** | |
* DBRef SchemaType constructor. | |
* | |
* @param {String} key | |
* @param {Object} options | |
* @api private | |
*/ | |
function DBRef (key, options) { | |
SchemaType.call(this, key, options); | |
}; | |
/** | |
* Inherits from SchemaType. | |
*/ | |
DBRef.prototype.__proto__ = SchemaType.prototype; | |
/** | |
* Check required | |
* | |
* @api private | |
*/ | |
DBRef.prototype.checkRequired = function (value) { | |
// TODO check if instance is DBRef | |
return !!value && value instanceof oid; | |
}; | |
/** | |
* Overrides the getters application for the population special-case | |
* | |
* @param {Object} value | |
* @param {Object} scope | |
* @api private | |
*/ | |
DBRef.prototype.applyGetters = function (value, scope) { | |
if (this.options.ref && value && value._id && value._id instanceof oid) { | |
// means the object id was populated | |
return value; | |
} | |
return SchemaType.prototype.applyGetters.call(this, value, scope); | |
}; | |
/** | |
* Overrides the getters application for the population special-case | |
* | |
* @param {Object} value | |
* @param {Object} scope | |
* @api private | |
*/ | |
DBRef.prototype.applySetters = function (value, scope) { | |
if (this.options.ref && value && value._id && value._id instanceof oid) { | |
// means the object id was populated | |
return value; | |
} | |
return SchemaType.prototype.applySetters.call(this, value, scope); | |
}; | |
/** | |
* Casts to DBRef | |
* | |
* @param {Object} value | |
* @param {Object} scope | |
* @param {Boolean} whether this is an initialization cast | |
* @api private | |
*/ | |
DBRef.prototype.cast = function (value, scope, init) { | |
if (value === null) | |
return value; | |
// | |
if (value && value.oid instanceof oid && typeof value.namespace == 'string') { | |
return { | |
"$ref" : this.options.ref, | |
"$id" : value.oid | |
}; | |
} | |
if (value && value instanceof oid) { | |
return { | |
"$ref" : this.options.ref, | |
"$id" : value | |
}; | |
} | |
if (typeof value == 'string') { | |
return { | |
"$ref" : this.options.ref, | |
"$id" : oid.fromString(value) | |
}; | |
} | |
if (value && value._id instanceof oid) { | |
return { | |
"$ref" : value.collection.name, | |
"$id" : value._id | |
}; | |
} | |
throw new CastError('db ref', value); | |
}; | |
function handleSingle (val) { | |
return this.cast(val); | |
} | |
function handleArray (val) { | |
var self = this; | |
return val.map(function (m) { | |
return self.cast(m); | |
}); | |
} | |
DBRef.prototype.$conditionalHandlers = { | |
'$ne': handleSingle | |
, '$in': handleArray | |
, '$nin': handleArray | |
, '$gt': handleSingle | |
, '$lt': handleSingle | |
, '$gte': handleSingle | |
, '$lte': handleSingle | |
}; | |
DBRef.prototype.castForQuery = function ($conditional, val) { | |
var handler; | |
if (arguments.length === 2) { | |
handler = this.$conditionalHandlers[$conditional]; | |
if (!handler) | |
throw new Error("Can't use " + $conditional + " with DBRef."); | |
return handler.call(this, val); | |
} else { | |
val = $conditional; | |
return this.cast(val); | |
} | |
}; | |
/** | |
* Adds an auto-generated DBRef default if turnOn is true. | |
* @param {Boolean} turnOn auto generated DBRef defaults | |
* @api private | |
*/ | |
DBRef.prototype.auto = function (turnOn) { | |
if (turnOn) { | |
this.default(function(){ | |
console.log(this); | |
return { | |
"$ref" : this.options.ref, | |
"$id" : new oid() | |
}; | |
}); | |
} | |
}; | |
/** | |
* Module exports. | |
*/ | |
module.exports = DBRef; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment