-
Notifications
You must be signed in to change notification settings - Fork 155
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
Question: Provide _id
in create request?
#326
Comments
This is a feature I've had in mind too. There a few edge cases though.
Here's a test that documents all of this behavior:
Here's what I'm thinking (for object creation via POST/PUT)
Then we document the "unset @Zertz what do you think about this? |
I'd rather not tinker with const schema = new mongoose.Schema({
// Apply the validation rules you need
uid: { type: String, unique: true, isRequired: true, minlength: 32, maxlength: 32 }
})
erm.serve(mongoose.model('Hello', Hello), {
idProperty: 'uid'
}) |
In my opinion, lines 140-142 of operations.js are already tinkering with the Unless I'm misunderstanding, and by not tinkering you mean you'd like to leave functionality as-is? Otherwise, what @b-gran is proposing would be perfect for our situation, at least. Changing the |
I'm kind of on the fence about this. On the one hand, messing around with On the other, we're already messing with How about adding an option? The option would be pretty straightforward:
The |
@b-gran, +1, allowing _id to be passed in seems like a great solution to keep backwards compatibility while also allowing generation of _id externally. |
Indeed, that tinkering is an old bit of code that was kept for backward compatibility. Ideally we'd simply remove that bit of code, but I fear in this case it might be too much of a breaking change as it could surface weird bugs in user apps that depend on this behavior. Looking at it now, I'm not even sure what the purpose of that was. That said, while I'm not a fan of piling up options, you brought up valid points so feel free to create a PR. Keep it backward compatible for now and perhaps add a warning when the option is off to warn the user that their Basically, add the feature in v4.x and completely remove |
I definitely agree, the fewer options the better. That seems like a reasonable path forward, we just need to make sure the docs track the addition and removal of the option. |
Correct. |
Implementation question for you guys. We're building a product that will require client-server synchronization with offline capabilities. In order to do that, we need to be able to generate unique ids on the client side that we can be sure will be the same after they are sent to the server.
Is there a way to provide
_id
in the request body for creates without having them removed? I believe the lines here are undo-ing what we are trying to do.We can sort of apply a work around by adding a schema like this:
This allows us to post in
_id
fields, but then whenever they aren't supplied (and they won't always be, in our case -- sometimes we want the server to generate the ids), thedefault
doesn't seem to get triggered, and we get an error from mongoose. So that unfortunately doesn't work 100% for us. Feels a bit scary to supply an_id
and then mark_id: false
in the options, too.I can confirm that commenting out the lines noted above works as expected, at least for supplying
_id
as anObjectId
and leaving itundefined
.Here are a few outcomes based on
_id
inputs when those lines are commented out:_id
string
representation ofObjectId
ObjectId
is usedundefined
ObjectId
is generatednull
_id: null
, but I believe mongoose still generates an_id
and creates the record''
"Cast to ObjectID failed for value \"\" at path \"_id\""
How do you feel about not deleting
req.body._id
when it is a validObjectId
? Thatnull
thing seems like a problem, but everything else seems potentially reasonable.The text was updated successfully, but these errors were encountered: