-
Notifications
You must be signed in to change notification settings - Fork 1.8k
<fix>: _id may not be required in create operation #3100
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
## Issue When schema includes `_id: ObjectId`, `insertOne` should not force `_id` to be a required property. ## Example ```ts export interface Test { _id: ObjectId, data: string } db.collection<Test>('Test').insertOne({data: 'XXXX'}) ``` ### Expected Compiled successfully. ### Actual Got error, because missing `_id` This PR let `_id` is still optional when `_id: ObjectId` in schema
Hi @k8w, thank you for reaching out. The intended relationship between the schema and the _id in the typings is as follows:
We made this choice in order to provide a consistent experience for the different use cases our users have for the collection schema specified at the driver level and the behavior expectations that come with those schemas. The driver has to rely on the input schema to provide its own type enforcement, where the difference between optional and required properties of the driver-side collection schema are meaningful on document insertion and document retrieval. Our APIs ensure that the return type of operations such as On the other hand, on inserts, we have to guess the use case based on the schema, keeping in mind that the Making an exception for just For your use case, if the collection type is something that is used independently in other parts of the code base as a document model, we recommend simply wrapping it in our We are aware that this may be different from the intuitive notion of a collection document type; unfortunately, since it is impossible to reverse engineer the desired insert shape from the final document shape, only vice versa, we cannot use the final shape of the document to fully define the collection for all driver operations. We appreciate your understanding and intend to add this guidance to our official documentation soon. |
Thanks~
|
@alias-mac We're looking at putting out the 4.3.1 patch today! |
The problem is |
@justinmchase I am sorry that this is causing friction for your use case. However, we would not have made this change if we did not have real customers with real code bases that needed their use cases enabled. To address your specific concern though, I'm not sure what you mean that the types do not accurately reflect the reality in the case of specifying your collection type with an OptionalId. Regardless of whether you mark the id as optional or required in the collection schema type, it will ALWAYS be returned as required (i.e., guaranteed to be present and not optional) in any read operation by default. There is no inconsistency, since the collection schema for the driver specifies the insert shape of the document, which is then correctly transformed in the input and output types of the various driver methods as needed. If your assertion is that the _id should always be optional on inserts because it's optional server-side on inserts, it is worth remembering that all fields are optional server-side on inserts unless you also use the same schema to configure server-side validation. And taking that approach would unfortunately leave our users who rely on a custom _id with no recourse for type safety where forgetting to specify an _id would result in data inconsistency. So, as I mentioned in my comment above,
|
Description
When schema includes
_id: ObjectId
,insertOne
should not force_id
to be a required property.What is changing?
Let
_id
is still optional ininsertOne
when_id: ObjectId
in schemaIs there new documentation needed for these changes?
What is the motivation for this change?
Expected
Compiled successfully.
Actual
Got error, because missing
_id
Double check the following
npm run check:lint
script<type>(NODE-xxxx)<!>: <description>