-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reference model from within validator on nested schema #3589
Comments
First of all, I'd strongly advise against using mongoose-unique-validator, it has a fundamental race condition that makes it virtually useless for enforcing unique constraints. That being said, try |
I've taken over maintenance of that very plugin from its original owner, what race condition are you referring to? I've never noticed any race conditions, at least with my rewrite of it. |
Because mongoose-unique-validator relies on async operations like The only way to work around this is to use only one process and set your mongodb connection pool size to 1, but that limits the amount of app servers you can run and will only work so long as mongodb retains its legacy behavior of blocking operations on a per-connection basis. |
It sounds like there's no feasible way around it. I think for the majority of our users use-cases this will be an edge case, I've personally never had a scenario in which duplicate data was written to the collection while an existing count/query was returning 0. I'll be sure to make this clear in readme though, thanks. |
|
The fact that |
It seems like this bug was reintroduced, or that it was never resolved for sub-subdocuments, see example below: mongoose = require 'mongoose'
mongoose.connect 'mongodb://localhost/mongotest'
print = -> console.log 'name:', @name, 'ownerDocument:', @ownerDocument().name
grandChildSchema = new mongoose.Schema
name: String
grandChildSchema.methods =
print: print
childSchema = new mongoose.Schema
name: String
child: grandChildSchema
childSchema.methods =
print: print
parentSchema = new mongoose.Schema
name: String
children: [childSchema]
Parent = mongoose.model 'Parent', parentSchema
p = new Parent
name: 'Parent Parentson'
children: [
{
name: 'Child Parentson'
child:
name: 'Grandchild Parentson'
}
]
p.children[0].print()
p.children[0].child.print()
Received output:
Expected output
Edit |
I managed to fix it (I think) for the first case where parent has an array of childSchema by changing : Subdocument.prototype.ownerDocument = function() {
if (this.$__.ownerDocument) {
return this.$__.ownerDocument;
}
var parent = this.$parent;
if (!parent) {
return this;
}
while (parent.$parent) {
parent = parent.$parent;
}
this.$__.ownerDocument = parent;
return this.$__.ownerDocument;
}; to Subdocument.prototype.ownerDocument = function() {
if (this.$__.ownerDocument) {
return this.$__.ownerDocument;
}
var parent = this.$parent;
if (!parent) {
return this;
}
while (parent.$parent || parent.__parent) {
parent = parent.$parent || parent.__parent;
}
this.$__.ownerDocument = parent;
return this.$__.ownerDocument;
}; I have not run the tests with this fix. |
I'm trying to find a way to get a reference to the parent model of a nested schema from inside the
validator
.I maintain a plugin which queries a collection for unique records before mongo returns a duplicate key error, so these validators are built dynamically.
I have a feeling it's not possible, and I need to check
schema.nested
and use some custom logic for these nested documents.When the validator is called on a nested schema, the validator's
this
isDocument
:Yet for a validator directly on the parent, it's a
model
:An example schema:
The text was updated successfully, but these errors were encountered: