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
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
}
}
],
"no-use-before-define": "warn",
"sort-keys": "warn"
}
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
}
8 changes: 8 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": "0.2",
"language": "en",
"words": [
"bson",
"papr"
]
}
86 changes: 43 additions & 43 deletions docs/api/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Calls the MongoDB [`countDocuments()`](https://mongodb.github.io/node-mongodb-na

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

**Returns:**
Expand All @@ -114,10 +114,10 @@ Calls the MongoDB [`deleteMany()`](https://mongodb.github.io/node-mongodb-native

**Parameters:**

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

**Returns:**

Expand All @@ -135,10 +135,10 @@ Calls the MongoDB [`deleteOne()`](https://mongodb.github.io/node-mongodb-native/

**Parameters:**

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

**Returns:**

Expand All @@ -156,11 +156,11 @@ Calls the MongoDB [`distinct()`](https://mongodb.github.io/node-mongodb-native/5

**Parameters:**

| Name | Type | Attribute |
| --------- | ----------------------- | --------- |
| `key` | `keyof TSchema` | required |
| `filter` | `StrictFilter<TSchema>` | optional |
| `options` | `DistinctOptions` | optional |
| Name | Type | Attribute |
| --------- | --------------------- | --------- |
| `key` | `keyof TSchema` | required |
| `filter` | `PaprFilter<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` | `StrictFilter<TSchema>` | required |
| `filter` | `PaprFilter<TSchema>` | required |
| `options` | `Omit<FindOptions<TSchema>, ("projection" \| "limit" \| "sort" \| "skip")>` | optional |

**Returns:**
Expand All @@ -205,10 +205,10 @@ The result type (`TProjected`) takes into account the projection for this query

**Parameters:**

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

**Returns:**

Expand Down Expand Up @@ -265,10 +265,10 @@ The result type (`TProjected`) takes into account the projection for this query

**Parameters:**

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

**Returns:**

Expand Down Expand Up @@ -296,7 +296,7 @@ The result type (`TProjected`) takes into account the projection for this query

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

**Returns:**
Expand All @@ -317,11 +317,11 @@ The result type (`TProjected`) takes into account the projection for this query

**Parameters:**

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

**Returns:**

Expand Down Expand Up @@ -397,11 +397,11 @@ Calls the MongoDB [`updateMany()`](https://mongodb.github.io/node-mongodb-native

**Parameters:**

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

**Returns:**

Expand All @@ -419,11 +419,11 @@ Calls the MongoDB [`updateOne()`](https://mongodb.github.io/node-mongodb-native/

**Parameters:**

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

**Returns:**

Expand All @@ -441,10 +441,10 @@ Calls the MongoDB [`findOneAndUpdate()`](https://mongodb.github.io/node-mongodb-

**Parameters:**

| Name | Type | Attribute |
| -------- | ----------------------------- | --------- |
| `filter` | `StrictFilter<TSchema>` | required |
| `update` | `StrictUpdateFilter<TSchema>` | required |
| Name | Type | Attribute |
| -------- | --------------------------- | --------- |
| `filter` | `PaprFilter<TSchema>` | required |
| `update` | `PaprUpdateFilter<TSchema>` | required |

**Returns:**

Expand Down
9 changes: 5 additions & 4 deletions docs/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,16 @@ Starting with `mongodb` v4.3.0, these types were enhanced to support dot notatio

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.
Papr v11 has adopted and enhanced these 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`.
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 `PaprFilter`.

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

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

Expand Down
11 changes: 11 additions & 0 deletions example/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@ import papr from './papr';
const userSchema = schema(
{
active: types.boolean(),
address: types.object({
country: types.string({ required: true }),
zip: types.number({ required: true }),
}),
age: types.number(),
firstName: types.string({ required: true }),
lastName: types.string({ required: true }),
orders: types.array(
types.object({
product: types.string({ required: true }),
quantity: types.number({ required: true }),
})
),
tags: types.array(types.string()),
},
{
defaults: { active: true },
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"standard-version": "9.5.0",
"ts-expect": "1.3.0",
"ts-node": "10.9.1",
"typescript": "4.9.3"
"typescript": "4.9.5"
},
"peerDependencies": {
"mongodb": "^5.0.0"
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Collection, MongoError, ObjectId } from 'mongodb';
import { expectType } from 'ts-expect';
import { Hooks } from '../hooks';
import { abstract, build, Model } from '../model';
import { PaprBulkWriteOperation } from '../mongodbTypes';
import schema from '../schema';
import Types from '../types';
import { BulkWriteOperation } from '../utils';

describe('model', () => {
let collection: Collection;
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('model', () => {

describe('bulkWrite', () => {
test('simple schema', async () => {
const operations: BulkWriteOperation<SimpleDocument, SimpleOptions>[] = [
const operations: PaprBulkWriteOperation<SimpleDocument, SimpleOptions>[] = [
{
insertOne: {
document: {
Expand Down Expand Up @@ -273,7 +273,7 @@ describe('model', () => {
});

test('schema with defaults', async () => {
const operations: BulkWriteOperation<SimpleDocument, SimpleOptions>[] = [
const operations: PaprBulkWriteOperation<SimpleDocument, SimpleOptions>[] = [
{
insertOne: {
document: {
Expand Down
Loading