From 8b31d4db524466195572dd2d88394327617c0c0c Mon Sep 17 00:00:00 2001 From: Geordie Jay Date: Mon, 16 Aug 2021 13:56:55 +0200 Subject: [PATCH 1/4] test(NODE-3468): Add tests for a strongly-typed Db This allows users who re-use a single db object to define which collections are available on that Db, as well as the types those collections contain. NODE-3468 --- .../community/collection/findX.test-d.ts | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/test/types/community/collection/findX.test-d.ts b/test/types/community/collection/findX.test-d.ts index 16c596807d2..37f3c1c2dba 100644 --- a/test/types/community/collection/findX.test-d.ts +++ b/test/types/community/collection/findX.test-d.ts @@ -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'; @@ -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(fields); }); @@ -22,7 +22,7 @@ interface TestModel { } const collectionT = db.collection('testCollection'); -await collectionT.find({ +collectionT.find({ $and: [{ numberField: { $gt: 0 } }, { numberField: { $lt: 100 } }], readonlyFruitTags: { $all: ['apple', 'pear'] } }); @@ -74,7 +74,7 @@ const collectionBag = db.collection('bag'); const cursor: FindCursor = collectionBag.find({ color: 'black' }); -cursor.toArray((err, bags) => { +cursor.toArray((_err, bags) => { expectType(bags); }); @@ -198,3 +198,32 @@ expectType(findOptions); // This is just to check that we still export these type symbols expectAssignable({}); expectAssignable({}); + +// 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; + collection(name: 'things'): Collection; +} + +const typedDb = client.db('test2') as TypedDb; + +const person = typedDb.collection('people').findOne({}); +expectType>(person); + +typedDb.collection('people').findOne({}, function (_err, person) { + expectType(person); +}); + +typedDb.collection('things').findOne({}, function (_err, thing) { + expectType(thing); +}); From a2f82e13c2c282a75f470dc5e4a132279feaff2c Mon Sep 17 00:00:00 2001 From: Geordie J Date: Mon, 16 Aug 2021 16:08:18 +0200 Subject: [PATCH 2/4] Update test/types/community/collection/findX.test-d.ts Co-authored-by: Daria Pardue <81593090+dariakp@users.noreply.github.com> --- test/types/community/collection/findX.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/community/collection/findX.test-d.ts b/test/types/community/collection/findX.test-d.ts index 37f3c1c2dba..ed1d8365c00 100644 --- a/test/types/community/collection/findX.test-d.ts +++ b/test/types/community/collection/findX.test-d.ts @@ -218,7 +218,7 @@ interface TypedDb extends Db { const typedDb = client.db('test2') as TypedDb; const person = typedDb.collection('people').findOne({}); -expectType>(person); +expectType>(person); typedDb.collection('people').findOne({}, function (_err, person) { expectType(person); From 6691b3eff458bda8aebae2ac4893968416b02a94 Mon Sep 17 00:00:00 2001 From: Geordie J Date: Mon, 16 Aug 2021 16:08:22 +0200 Subject: [PATCH 3/4] Update test/types/community/collection/findX.test-d.ts Co-authored-by: Daria Pardue <81593090+dariakp@users.noreply.github.com> --- test/types/community/collection/findX.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/community/collection/findX.test-d.ts b/test/types/community/collection/findX.test-d.ts index ed1d8365c00..53f68c2ddb8 100644 --- a/test/types/community/collection/findX.test-d.ts +++ b/test/types/community/collection/findX.test-d.ts @@ -225,5 +225,5 @@ typedDb.collection('people').findOne({}, function (_err, person) { }); typedDb.collection('things').findOne({}, function (_err, thing) { - expectType(thing); + expectType(thing); }); From a2a43e5a9e639e2236411f62476ad27585ba1865 Mon Sep 17 00:00:00 2001 From: Geordie J Date: Mon, 16 Aug 2021 16:08:26 +0200 Subject: [PATCH 4/4] Update test/types/community/collection/findX.test-d.ts Co-authored-by: Daria Pardue <81593090+dariakp@users.noreply.github.com> --- test/types/community/collection/findX.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/community/collection/findX.test-d.ts b/test/types/community/collection/findX.test-d.ts index 53f68c2ddb8..9fb33a75f4b 100644 --- a/test/types/community/collection/findX.test-d.ts +++ b/test/types/community/collection/findX.test-d.ts @@ -221,7 +221,7 @@ const person = typedDb.collection('people').findOne({}); expectType>(person); typedDb.collection('people').findOne({}, function (_err, person) { - expectType(person); + expectType(person); }); typedDb.collection('things').findOne({}, function (_err, thing) {