Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
],
"@typescript-eslint/no-unnecessary-type-arguments": "error",
"@typescript-eslint/no-unnecessary-type-constraint": "error",
"@typescript-eslint/no-unsafe-argument": "warn",
"@typescript-eslint/no-unsafe-assignment": "warn",
"@typescript-eslint/no-unsafe-member-access": "warn",
"@typescript-eslint/no-unsafe-return": "warn",
Comment on lines +105 to +108
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some weird reason, the ESLint TS plugin now thinks the type of the update filter variables are any, so let's temporarily downgrade these to warning.

"@typescript-eslint/no-useless-constructor": "warn",
"@typescript-eslint/object-curly-spacing": ["error", "always"],
"@typescript-eslint/parameter-properties": "error",
Expand Down
128 changes: 64 additions & 64 deletions docs/api/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const User = papr.model('users', userSchema);

## `aggregate`

Calls the MongoDB [`aggregate()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#aggregate) method.
Calls the MongoDB [`aggregate()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#aggregate) method.

The MongoDB aggregation pipeline syntax is very rich and powerful, however providing full typed support for the results is out of the scope of `papr`.

Expand Down Expand Up @@ -50,7 +50,7 @@ const results = await User.aggregate<{ age: number }>([

## `bulkWrite`

Calls the MongoDB [`bulkWrite()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#bulkWrite) method.
Calls the MongoDB [`bulkWrite()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#bulkWrite) method.

**Parameters:**

Expand All @@ -61,7 +61,7 @@ Calls the MongoDB [`bulkWrite()`](https://mongodb.github.io/node-mongodb-native/

**Returns:**

[`Promise<BulkWriteResult>`](https://mongodb.github.io/node-mongodb-native/4.1/classes/BulkWriteResult.html)
[`Promise<BulkWriteResult>`](https://mongodb.github.io/node-mongodb-native/5.0/classes/BulkWriteResult.html)

**Example:**

Expand All @@ -88,13 +88,13 @@ const results = await User.bulkWrite([

## `countDocuments`

Calls the MongoDB [`countDocuments()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#countDocuments) method.
Calls the MongoDB [`countDocuments()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#countDocuments) method.

**Parameters:**

| Name | Type | Attribute |
| --------- | ----------------------- | --------- |
| `filter` | `Filter<TSchema>` | required |
| `filter` | `StrictFilter<TSchema>` | required |
| `options` | `CountDocumentsOptions` | optional |

**Returns:**
Expand All @@ -110,18 +110,18 @@ const countWicks = await User.countDocuments({ lastName: 'Wick' });

## `deleteMany`

Calls the MongoDB [`deleteMany()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#deleteMany) method.
Calls the MongoDB [`deleteMany()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#deleteMany) method.

**Parameters:**

| Name | Type | Attribute |
| --------- | ----------------- | --------- |
| `filter` | `Filter<TSchema>` | required |
| `options` | `DeleteOptions` | optional |
| Name | Type | Attribute |
| --------- | ----------------------- | --------- |
| `filter` | `StrictFilter<TSchema>` | required |
| `options` | `DeleteOptions` | optional |

**Returns:**

[`Promise<DeleteResult>`](https://mongodb.github.io/node-mongodb-native/4.1/interfaces/DeleteResult.html)
[`Promise<DeleteResult>`](https://mongodb.github.io/node-mongodb-native/5.0/interfaces/DeleteResult.html)

**Example:**

Expand All @@ -131,18 +131,18 @@ await User.deleteMany({ lastName: 'Wick' });

## `deleteOne`

Calls the MongoDB [`deleteOne()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#deleteOne) method.
Calls the MongoDB [`deleteOne()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#deleteOne) method.

**Parameters:**

| Name | Type | Attribute |
| --------- | ----------------- | --------- |
| `filter` | `Filter<TSchema>` | required |
| `options` | `DeleteOptions` | optional |
| Name | Type | Attribute |
| --------- | ----------------------- | --------- |
| `filter` | `StrictFilter<TSchema>` | required |
| `options` | `DeleteOptions` | optional |

**Returns:**

[`Promise<DeleteResult>`](https://mongodb.github.io/node-mongodb-native/4.1/interfaces/DeleteResult.html)
[`Promise<DeleteResult>`](https://mongodb.github.io/node-mongodb-native/5.0/interfaces/DeleteResult.html)

**Example:**

Expand All @@ -152,15 +152,15 @@ await User.deleteOne({ lastName: 'Wick' });

## `distinct`

Calls the MongoDB [`distinct()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#distinct) method.
Calls the MongoDB [`distinct()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#distinct) method.

**Parameters:**

| Name | Type | Attribute |
| --------- | ----------------- | --------- |
| `key` | `keyof TSchema` | required |
| `filter` | `Filter<TSchema>` | optional |
| `options` | `DistinctOptions` | optional |
| Name | Type | Attribute |
| --------- | ----------------------- | --------- |
| `key` | `keyof TSchema` | required |
| `filter` | `StrictFilter<TSchema>` | optional |
| `options` | `DistinctOptions` | optional |

**Returns:**

Expand All @@ -180,7 +180,7 @@ Performs an optimized `find` to test for the existence of any document matching

| Name | Type | Attribute |
| --------- | --------------------------------------------------------------------------- | --------- |
| `filter` | `Filter<TSchema>` | required |
| `filter` | `StrictFilter<TSchema>` | required |
| `options` | `Omit<FindOptions<TSchema>, ("projection" \| "limit" \| "sort" \| "skip")>` | optional |

**Returns:**
Expand All @@ -199,16 +199,16 @@ const isAlreadyActive = await User.exists({

## `find`

Calls the MongoDB [`find()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#find) method.
Calls the MongoDB [`find()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#find) method.

The result type (`TProjected`) takes into account the projection for this query and reduces the original `TSchema` type accordingly. See also [`ProjectionType`](api/utils.md#ProjectionType).

**Parameters:**

| Name | Type | Attribute |
| --------- | ---------------------- | --------- |
| `filter` | `Filter<TSchema>` | required |
| `options` | `FindOptions<TSchema>` | optional |
| Name | Type | Attribute |
| --------- | ----------------------- | --------- |
| `filter` | `StrictFilter<TSchema>` | required |
| `options` | `FindOptions<TSchema>` | optional |

**Returns:**

Expand All @@ -228,7 +228,7 @@ usersProjected[0]?.lastName; // valid

## `findById`

Calls the MongoDB [`findOne()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#findOne) method.
Calls the MongoDB [`findOne()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#findOne) method.

The result type (`TProjected`) takes into account the projection for this query and reduces the original `TSchema` type accordingly. See also [`ProjectionType`](api/utils.md#ProjectionType).

Expand Down Expand Up @@ -259,16 +259,16 @@ userProjected.lastName; // valid

## `findOne`

Calls the MongoDB [`findOne()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#findOne) method.
Calls the MongoDB [`findOne()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#findOne) method.

The result type (`TProjected`) takes into account the projection for this query and reduces the original `TSchema` type accordingly. See also [`ProjectionType`](api/utils.md#ProjectionType).

**Parameters:**

| Name | Type | Attribute |
| --------- | ---------------------- | --------- |
| `filter` | `Filter<TSchema>` | required |
| `options` | `FindOptions<TSchema>` | optional |
| Name | Type | Attribute |
| --------- | ----------------------- | --------- |
| `filter` | `StrictFilter<TSchema>` | required |
| `options` | `FindOptions<TSchema>` | optional |

**Returns:**

Expand All @@ -288,15 +288,15 @@ userProjected.lastName; // valid

## `findOneAndDelete`

Calls the MongoDB [`findOneAndDelete()`](http://mongodb.github.io/node-mongodb-native/4.1/classes/collection.html#findoneanddelete) method and returns the document found before removal.
Calls the MongoDB [`findOneAndDelete()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#findOneAndDelete) method and returns the document found before removal.

The result type (`TProjected`) takes into account the projection for this query and reduces the original `TSchema` type accordingly. See also [`ProjectionType`](api/utils.md#ProjectionType).

**Parameters:**

| Name | Type | Attribute |
| --------- | ------------------------- | --------- |
| `filter` | `Filter<TSchema>` | required |
| `filter` | `StrictFilter<TSchema>` | required |
| `options` | `FindOneAndUpdateOptions` | optional |

**Returns:**
Expand All @@ -311,17 +311,17 @@ const user = await User.findOneAndDelete({ firstName: 'John' });

## `findOneAndUpdate`

Calls the MongoDB [`findOneAndUpdate()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#findOneAndUpdate) method.
Calls the MongoDB [`findOneAndUpdate()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#findOneAndUpdate) method.

The result type (`TProjected`) takes into account the projection for this query and reduces the original `TSchema` type accordingly. See also [`ProjectionType`](api/utils.md#ProjectionType).

**Parameters:**

| Name | Type | Attribute |
| --------- | ------------------------- | --------- |
| `filter` | `Filter<TSchema>` | required |
| `update` | `UpdateFilter<TSchema>` | required |
| `options` | `FindOneAndUpdateOptions` | optional |
| Name | Type | Attribute |
| --------- | ----------------------------- | --------- |
| `filter` | `StrictFilter<TSchema>` | required |
| `update` | `StrictUpdateFilter<TSchema>` | required |
| `options` | `FindOneAndUpdateOptions` | optional |

**Returns:**

Expand All @@ -345,7 +345,7 @@ userProjected.lastName; // valid

## `insertMany`

Calls the MongoDB [`insertMany()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#insertMany) method.
Calls the MongoDB [`insertMany()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#insertMany) method.

**Parameters:**

Expand All @@ -369,7 +369,7 @@ const users = await User.insertMany([

## `insertOne`

Calls the MongoDB [`insertOne()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#insertOne) method.
Calls the MongoDB [`insertOne()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#insertOne) method.

**Parameters:**

Expand All @@ -393,19 +393,19 @@ const users = await User.insertOne([

## `updateMany`

Calls the MongoDB [`updateMany()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#updateMany) method.
Calls the MongoDB [`updateMany()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#updateMany) method.

**Parameters:**

| Name | Type | Attribute |
| --------- | ----------------------- | --------- |
| `filter` | `Filter<TSchema>` | required |
| `update` | `UpdateFilter<TSchema>` | required |
| `options` | `UpdateOptions` | optional |
| Name | Type | Attribute |
| --------- | ----------------------------- | --------- |
| `filter` | `StrictFilter<TSchema>` | required |
| `update` | `StrictUpdateFilter<TSchema>` | required |
| `options` | `UpdateOptions` | optional |

**Returns:**

[`Promise<UpdateResult>`](https://mongodb.github.io/node-mongodb-native/4.1/interfaces/UpdateResult.html)
[`Promise<UpdateResult>`](https://mongodb.github.io/node-mongodb-native/5.0/interfaces/UpdateResult.html)

**Example:**

Expand All @@ -415,19 +415,19 @@ const result = await User.updateMany({ firstName: 'John' }, { $set: { age: 40 }

## `updateOne`

Calls the MongoDB [`updateOne()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#updateOne) method.
Calls the MongoDB [`updateOne()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#updateOne) method.

**Parameters:**

| Name | Type | Attribute |
| --------- | ----------------------- | --------- |
| `filter` | `Filter<TSchema>` | required |
| `update` | `UpdateFilter<TSchema>` | required |
| `options` | `UpdateOptions` | optional |
| Name | Type | Attribute |
| --------- | ----------------------------- | --------- |
| `filter` | `StrictFilter<TSchema>` | required |
| `update` | `StrictUpdateFilter<TSchema>` | required |
| `options` | `UpdateOptions` | optional |

**Returns:**

[`Promise<UpdateResult>`](https://mongodb.github.io/node-mongodb-native/4.1/interfaces/UpdateResult.html)
[`Promise<UpdateResult>`](https://mongodb.github.io/node-mongodb-native/5.0/interfaces/UpdateResult.html)

**Example:**

Expand All @@ -437,14 +437,14 @@ const result = await User.updateOne({ firstName: 'John' }, { $set: { age: 40 } }

## `upsert`

Calls the MongoDB [`findOneAndUpdate()`](https://mongodb.github.io/node-mongodb-native/4.1/classes/Collection.html#findOneAndUpdate) method with the `upsert` option enabled.
Calls the MongoDB [`findOneAndUpdate()`](https://mongodb.github.io/node-mongodb-native/5.0/classes/Collection.html#findOneAndUpdate) method with the `upsert` option enabled.

**Parameters:**

| Name | Type | Attribute |
| -------- | ----------------------- | --------- |
| `filter` | `Filter<TSchema>` | required |
| `update` | `UpdateFilter<TSchema>` | required |
| Name | Type | Attribute |
| -------- | ----------------------------- | --------- |
| `filter` | `StrictFilter<TSchema>` | required |
| `update` | `StrictUpdateFilter<TSchema>` | required |

**Returns:**

Expand Down
25 changes: 25 additions & 0 deletions docs/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,28 @@ const postSchema = schema(
}
);
```

## Filter Types

`mongodb` exposes two important TypeScript types for the most common operations you can do with the driver: the `Filter` (used for querying documents) and `UpdateFilter` (used for updating documents) types.

Starting with `mongodb` v4.3.0, these types were enhanced to support dot notation field access and type checking.

However, `mongodb` v5.0.0 [removed these types](https://github.com/mongodb/node-mongodb-native/blob/main/etc/notes/CHANGES_5.0.0.md#dot-notation-typescript-support-removed-by-default) as the default ones used in their methods and reverted to the old ones without dot notation support. The previous enhanced types were not removed, instead they were renamed to `StrictFilter` and `StrictUpdateFilter`, but they aren't referenced in any of their methods.

Papr is using the strict types to provide type safety for all query and update filters.

This comes with a caveat: whenever you need to interact with the `mongodb` driver collections, you need to cast filter types to their simple counterparts, since `Filter` is not compatible with `StrictFilter`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit of the docs is very nicely written, well done!


```ts
import { Filter, StrictFitler } from 'mongodb';
import User, { UserDocument } from './user';

const filter: StrictFilter<UserDocument> = {
firstName: 'John',
};

await User.find(filter);

await User.collection.find(filter as Filter<UserDocument>);
```
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"jsdoc-api": "8.0.0",
"jsdoc-parse": "6.2.0",
"lint-staged": "13.1.0",
"mongodb": "4.13.0",
"mongodb": "5.0.1",
"mongodb-memory-server": "8.11.0",
"mongoose": "6.3.5",
"pinst": "3.0.0",
Expand All @@ -89,7 +89,7 @@
"typescript": "4.9.3"
},
"peerDependencies": {
"mongodb": ">=4.11.0"
"mongodb": "^5.0.0"
},
"commitlint": {
"extends": [
Expand Down Expand Up @@ -125,4 +125,4 @@
"yarn": "1.22.19"
},
"packageManager": "[email protected]"
}
}
1 change: 0 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ describe('index', () => {
});

test('existing collection', async () => {
// @ts-expect-error Ignore collection type
(db.collections as jest.Mocked<Db['collections']>).mockResolvedValue([collection]);

const papr = new Papr();
Expand Down
Loading