From c130c86b11f54244f0d32e25d908e42274cd180a Mon Sep 17 00:00:00 2001 From: Valentin Agachi Date: Mon, 22 Nov 2021 15:27:27 +0100 Subject: [PATCH 1/3] feat: Filter type uses WithId on the schema --- src/mongo_types.ts | 4 ++-- test/types/union_schema.test-d.ts | 22 +--------------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/src/mongo_types.ts b/src/mongo_types.ts index e911aade8c..cb221ab78e 100644 --- a/src/mongo_types.ts +++ b/src/mongo_types.ts @@ -56,8 +56,8 @@ export type WithoutId = Omit; /** A MongoDB filter can be some portion of the schema or a set of operators @public */ export type Filter = { - [P in keyof TSchema]?: Condition; -} & RootFilterOperators; + [P in keyof WithId]?: Condition[P]>; +} & RootFilterOperators>; /** @public */ export type Condition = AlternativeType | FilterOperators>; diff --git a/test/types/union_schema.test-d.ts b/test/types/union_schema.test-d.ts index 4db6bbaaa9..7bd8d488ec 100644 --- a/test/types/union_schema.test-d.ts +++ b/test/types/union_schema.test-d.ts @@ -2,7 +2,7 @@ import { expectType, expectError, expectNotType, expectNotAssignable, expectAssi import type { Collection } from '../../src/collection'; import { ObjectId } from '../../src/bson'; -import type { Filter, WithId } from '../../src/mongo_types'; +import type { WithId } from '../../src/mongo_types'; type InsertOneFirstParam = Parameters['insertOne']>[0]; @@ -44,23 +44,3 @@ interface B { type Data = A | B; expectAssignable>({ _id: 2 }); expectAssignable>({ _id: 'hi' }); - -// Ensure Exclusive Union Type doesn't break inside our collection methods -type Without = { [P in Exclude]?: never }; -// eslint-disable-next-line @typescript-eslint/ban-types -type XOR = T | U extends object ? (Without & U) | (Without & T) : T | U; - -interface Dog { - bark: string; -} -interface Cat { - meow: string; -} -type Pet = XOR; -expectNotAssignable>({ meow: '', bark: '' }); -expectAssignable>({ meow: '' }); -expectAssignable>({ bark: '' }); -expectAssignable>({ bark: '', _id: new ObjectId() }); -expectNotAssignable>({ meow: '', bark: '' }); // find -expectAssignable>({ bark: '' }); -expectAssignable>({ meow: '' }); From 0580654eabf48484110d456762ed55c12692a387 Mon Sep 17 00:00:00 2001 From: Daria Pardue Date: Mon, 29 Nov 2021 15:29:53 -0500 Subject: [PATCH 2/3] refactor: remove unnecessary WithId on Filter in find --- src/collection.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/collection.ts b/src/collection.ts index 598af08d3b..7aae0901c3 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -730,9 +730,9 @@ export class Collection { * @param filter - The filter predicate. If unspecified, then all documents in the collection will match the predicate */ find(): FindCursor>; - find(filter: Filter>, options?: FindOptions): FindCursor>; - find(filter: Filter>, options?: FindOptions): FindCursor; - find(filter?: Filter>, options?: FindOptions): FindCursor> { + find(filter: Filter, options?: FindOptions): FindCursor>; + find(filter: Filter, options?: FindOptions): FindCursor; + find(filter?: Filter, options?: FindOptions): FindCursor> { if (arguments.length > 2) { throw new MongoInvalidArgumentError( 'Method "collection.find()" accepts at most two arguments' From 58c5396266a2e1f40883917f7dc64db780b39114 Mon Sep 17 00:00:00 2001 From: Daria Pardue Date: Mon, 29 Nov 2021 16:41:31 -0500 Subject: [PATCH 3/3] test: add type tests around filter --- test/types/community/collection/findX.test-d.ts | 9 +++++++++ test/types/schema_helpers.test-d.ts | 2 ++ 2 files changed, 11 insertions(+) diff --git a/test/types/community/collection/findX.test-d.ts b/test/types/community/collection/findX.test-d.ts index d84940ed87..fe6a38d72b 100644 --- a/test/types/community/collection/findX.test-d.ts +++ b/test/types/community/collection/findX.test-d.ts @@ -270,6 +270,9 @@ interface SchemaWithTypicalId { const schemaWithTypicalIdCol = db.collection('a'); expectType | null>(await schemaWithTypicalIdCol.findOne()); expectAssignable(await schemaWithTypicalIdCol.findOne()); +// should allow _id as an ObjectId +await schemaWithTypicalIdCol.findOne({ _id: new ObjectId() }); +schemaWithTypicalIdCol.find({ _id: new ObjectId() }); interface SchemaWithOptionalTypicalId { _id?: ObjectId; @@ -278,6 +281,9 @@ interface SchemaWithOptionalTypicalId { const schemaWithOptionalTypicalId = db.collection('a'); expectType | null>(await schemaWithOptionalTypicalId.findOne()); expectAssignable(await schemaWithOptionalTypicalId.findOne()); +// should allow _id as an ObjectId +await schemaWithTypicalIdCol.findOne({ _id: new ObjectId() }); +await schemaWithTypicalIdCol.find({ _id: new ObjectId() }); interface SchemaWithUserDefinedId { _id: number; @@ -290,3 +296,6 @@ if (result !== null) { expectType(result._id); } expectAssignable(await schemaWithUserDefinedId.findOne()); +// should allow _id as a number +await schemaWithUserDefinedId.findOne({ _id: 5 }); +await schemaWithUserDefinedId.find({ _id: 5 }); diff --git a/test/types/schema_helpers.test-d.ts b/test/types/schema_helpers.test-d.ts index 9d177306ee..780b21dbe7 100644 --- a/test/types/schema_helpers.test-d.ts +++ b/test/types/schema_helpers.test-d.ts @@ -19,6 +19,8 @@ expectAssignable>(1 + 1); // WithId expectAssignable>({ _id: new ObjectId() }); expectAssignable>({ _id: new ObjectId(), a: 3 }); +expectAssignable>({ _id: new ObjectId() }); +expectAssignable>({ _id: 5 }); expectNotType>({ _id: 3 }); // Changing _id to a type other than ObjectId makes it required: