Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-3804

Investigate NODE-3803 - Typescript errors on _id after update to 4.2.1

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • 1
    • Not Needed

      NODE-3803 Description

      What problem are you facing?

      After updating to 4.2.1 from 4.1.4 we are having issues managing types where the _id is a string instead of ObjectId.

      If `_id` is not required (i.e. allow undefined) InferIdType fails and infers the id to be ObjectId.

      If we make `_id` required, InferIdType infers the type correctly, but then insertOne and similar functions require the `_id` to be present, even if we defined the proper pkFactory on the client

      What driver and relevant dependency versions are you using?

      We are using nodes driver version 4.2.1

      Steps to reproduce?

       

       

      import { randomUUID } from 'crypto' import { MongoClient, InferIdType } from 'mongodb'
       
      interface TestDocument1 {
       _id: string
       name: string
      }
      interface TestDocument2 {
       _id?: string
       name: string
      }
      type id1 = InferIdType<TestDocument1> // string
      type id2 = InferIdType<TestDocument2> // object id
      const client = await MongoClient.connect(url, {
       pkFactory: {
       createPk: randomUUID
       }
       })
      client.db().collection<TestDocument1>('test_collection').insertOne({ name: 'test1'}) // compilation error
       client.db().collection<TestDocument2>('test_collection').insertOne({ name: 'test2'}) // compilation error
      

       

            Assignee:
            Unassigned Unassigned
            Reporter:
            dbeng-pm-bot PM Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: