MyGit

v4.1.1

mongodb/node-mongodb-native

版本发布时间: 2021-08-25 04:42:38

mongodb/node-mongodb-native最新发布版本:v6.9.0(2024-09-13 01:12:41)

The MongoDB Node.js team is pleased to announce version 4.1.1 of the mongodb package!

Release Highlights

Error handling

We introduced better organization and consistency to our existing errors in an effort to provide more detailed error types that can help identify issues clearly and quickly. Our readme has a new section that describes how to handle errors thrown by the driver and defines our approach to semver in the context of errors. Notably, we recommend only using instanceof checks to filter for a given error class as we do not guarantee error messages or names will be preserved between patch releases, only the subclass hierarchy.

Thanks so much to our summer interns @andymina and @W-A-James for undertaking this effort!

Notable fixes

This release also addresses some Typescript issues that require further explanation, let's dive in:

TypeScript support

Projections

Starting in MongoDB 4.4 projections can accept aggregation expressions and aggregation syntax. This empowers users to create some pretty amazing and complex data model transformations on the database side. Unfortunately, our initial release of typescript typing for projections was too narrow to allow these use cases and still pass the compiler checks. Now projections are generic objects and the result of a cursor with a projection is typed as a generic object by default.

The recommended usage for projections alongside typescript is as follows:

interface Pet {
    name: string;
    buddies: Pet[];
}
interface PetBuddyCount {
    name: string;
    buddyCount: number;
}

const pets = db.collection<Pet>('pets');

const petBuddyCounts = await pets.find().project<PetBuddyCount>({
    name: 1,
    buddyCount: { $size: '$buddies' },
}).toArray();

By using a parameterized .project call you can now get the correct type information on the petBuddyCounts array. You will need to build the projection type yourself based on the projection you define for your query, but this has the benefit of constraining your results to precisely your type expectations.

Generics in find/findOne

In our initial typescript release the find and findOne methods accepted a generic parameter that was passed to the filter argument of the API.

find<T>(f: Filter<T>): FindCursor<T>

Due to how typescript automatically resolves the types of generics, one could run into an issue when specifying a filter that was incorrectly typed. The code below should be a Typescript error, TS hints to us the name is a string so it should only allow an array of string for $in.

// (using the same pets collection from the last example)
pets.find({ name: { $in: [1, 2] } });
// instead of the expected FindCursor<Pet> type TS was resolving to:
const res: FindCursor<{name: {$in: number[]}}> = pets.find(/* same arg as above */);

It uses the incorrectly typed filter that does not match the schema of Filter<TSchema> to automatically resolve a crazy return type. The function definition has now been updated to be:

find<T>(f: Filter<TSchema>): FindCursor<T>

So the Filter argument will no longer be automatically resolved to the passed in type, giving us the typescript compiler errors we love so much!

Bug Fixes

Refactoring

Documentation

We invite you to try the mongodb library immediately, and report any issues to the NODE project.

相关地址:原始地址 下载(tar) 下载(zip)

查看:2021-08-25发行的版本