node-mongodb-native icon indicating copy to clipboard operation
node-mongodb-native copied to clipboard

feat(NODE-6350): add typescript support to client bulkWrite API

Open nbbeeken opened this issue 4 months ago • 0 comments

Description

What is changing?

  • Adds namespace to schema mapping type

  • Adds tests/examples to our tsd tests

  • Some important choices:

    • We should default only where needed ("near the top"), the Models now require an input schema, Document can be used when none is needed.
    • ClientBulkWriteModel is exported on its own and is not an array type. We can offer this seperate from the client API so annotations can be used to get this string to schema behavior (but I would advocate for putting it directly into the API)
Is there new documentation needed for these changes?

Yes, I think the API doc on client.bulkWrite should explain how to use the TS a bit. I started in the TS doc for ClientBulkWriteModel

We mostly use "inline" models to demo the API, however, this TS change does not cause friction in wrapping this API, it does have some considerations though, if you make a helper that returns InsertOneModels you will want the return type to still preserve the type of "namespace" where appropriate to get autocomplete and assertions. -- we should write a small example of this.

What is the motivation for this change?

Users of this method can get thier schema asserted on the various bulkWrite models making sure input data is not adjusted accidentally or that filters aren't written in a way that doees not match the data format at all.

Release Highlight

Schema assertion support added to client.bulkWrite

type MongoDBSchemas = {
  'db.books': Book;
  'db.authors': Author;
}

const model: ClientBulkWriteModel<MongoDBSchemas> = {
  namespace: 'db.books'
  name: 'insertOne',
  document: { title: 'Practical MongoDB Aggregations', authorName: 3 } 
  // error `authorName` cannot be number
};

Notice how authorName is type checked against the Book type because namespace is set to "db.books".

... WIP ...

Double check the following

  • [x] Ran npm run check:lint script
  • [x] Self-review completed using the steps outlined here
  • [x] PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • [x] Changes are covered by tests
  • [x] New TODOs have a related JIRA ticket

nbbeeken avatar Sep 30 '24 16:09 nbbeeken