Skip to content
Merged
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
37 changes: 33 additions & 4 deletions test/types/community/collection/findX.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expectAssignable, expectNotType, expectType } from 'tsd';
import { FindCursor, FindOptions, MongoClient, Document } from '../../../../src';
import { FindCursor, FindOptions, MongoClient, Document, Collection, Db } from '../../../../src';
import type { Projection, ProjectionOperators } from '../../../../src';
import type { PropExists } from '../../utility_types';

Expand All @@ -9,7 +9,7 @@ const db = client.db('test');
const collection = db.collection('test.find');

// Locate all the entries using find
collection.find({}).toArray((err, fields) => {
collection.find({}).toArray((_err, fields) => {
expectType<Document[] | undefined>(fields);
});

Expand All @@ -22,7 +22,7 @@ interface TestModel {
}

const collectionT = db.collection<TestModel>('testCollection');
await collectionT.find({
collectionT.find({
$and: [{ numberField: { $gt: 0 } }, { numberField: { $lt: 100 } }],
readonlyFruitTags: { $all: ['apple', 'pear'] }
});
Expand Down Expand Up @@ -74,7 +74,7 @@ const collectionBag = db.collection<Bag>('bag');

const cursor: FindCursor<Bag> = collectionBag.find({ color: 'black' });

cursor.toArray((err, bags) => {
cursor.toArray((_err, bags) => {
expectType<Bag[] | undefined>(bags);
});

Expand Down Expand Up @@ -198,3 +198,32 @@ expectType<FindOptions>(findOptions);
// This is just to check that we still export these type symbols
expectAssignable<Projection>({});
expectAssignable<ProjectionOperators>({});

// Ensure users can create a custom Db type that only contains specific
// collections (which are, in turn, strongly typed):
type Person = {
name: 'alice' | 'bob';
age: number;
};

type Thing = {
location: 'shelf' | 'cupboard';
};

interface TypedDb extends Db {
collection(name: 'people'): Collection<Person>;
collection(name: 'things'): Collection<Thing>;
}

const typedDb = client.db('test2') as TypedDb;

const person = typedDb.collection('people').findOne({});
expectType<Promise<Person | undefined>>(person);

typedDb.collection('people').findOne({}, function (_err, person) {
expectType<Person | undefined>(person);
});

typedDb.collection('things').findOne({}, function (_err, thing) {
expectType<Thing | undefined>(thing);
});