Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Filter query root operators support for partials #3136

Closed
Closed
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
8 changes: 5 additions & 3 deletions src/mongo_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ export type EnhancedOmit<TRecordOrUnion, KeyUnion> = string extends keyof TRecor
export type WithoutId<TSchema> = Omit<TSchema, '_id'>;

/** A MongoDB filter can be some portion of the schema or a set of operators @public */
export type Filter<TSchema> =
export type Filter<TSchema> = (
| Partial<TSchema>
| ({
| {
[Property in Join<NestedPaths<WithId<TSchema>>, '.'>]?: Condition<
PropertyType<WithId<TSchema>, Property>
>;
} & RootFilterOperators<WithId<TSchema>>);
}
) &
RootFilterOperators<WithId<TSchema>>;

/** @public */
export type Condition<T> = AlternativeType<T> | FilterOperators<AlternativeType<T>>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectError } from 'tsd';

import type { Collection } from '../../../../src';
import type { Collection, Filter } from '../../../../src';

/**
* mutually recursive types are not supported and will not get type safety
Expand Down
10 changes: 10 additions & 0 deletions test/types/community/collection/findX.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,16 @@ const fooObj: Foo = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const fooFilter: Filter<Foo> = fooObj;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const fooFilterBuiltNow: Filter<Foo> = {
a: 'foo',
$or: [{ a: 'bar' }]
};

const fooFilterBuiltLater: Filter<Foo> = {};
fooFilterBuiltLater.a = 'foo';
fooFilterBuiltLater.$or = [{ a: 'bar' }];

// Specifically test that arrays can be included as a part of an object
// ensuring that a bug reported in https://jira.mongodb.org/browse/NODE-3856 is addressed
interface FooWithArray {
Expand Down