From 86d9e882124b281d560b78d070b6bc99dcbfda68 Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Wed, 20 Jul 2022 12:19:15 +0200 Subject: [PATCH 1/7] fix: better type support for nested _objects_ in query & update --- src/mongo_types.ts | 2 +- test/types/community/collection/filterQuery.test-d.ts | 3 +++ test/types/community/collection/updateX.test-d.ts | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/mongo_types.ts b/src/mongo_types.ts index 9891e0abf5..124e9ce9e3 100644 --- a/src/mongo_types.ts +++ b/src/mongo_types.ts @@ -531,7 +531,7 @@ export type NestedPaths = Type extends : // child is an array, but it's not a recursive array [Key, ...NestedPaths] : // child is not structured the same as the parent - [Key, ...NestedPaths]; + [Key, ...NestedPaths] | [Key]; }[Extract] : []; diff --git a/test/types/community/collection/filterQuery.test-d.ts b/test/types/community/collection/filterQuery.test-d.ts index 2f4d7f7e53..f0c12efb0a 100644 --- a/test/types/community/collection/filterQuery.test-d.ts +++ b/test/types/community/collection/filterQuery.test-d.ts @@ -178,6 +178,7 @@ expectNotType>({ 'regex.dotAll': true }); collectionT.find({ 'meta.updatedAt': new Date() }); collectionT.find({ 'meta.deep.nested.level': 123 }); collectionT.find({ meta: { deep: { nested: { level: 123 } } } }); // no impact on actual nesting +collectionT.find({ 'meta.deep': { nested: { level: 123 } } }); // no impact on actual nesting collectionT.find({ 'friends.0.name': 'John' }); collectionT.find({ 'playmates.0.name': 'John' }); // supports arrays with primitive types @@ -203,6 +204,8 @@ expectNotType>({ 'friends.0.name': 123 }); expectNotType>({ 'playmates.0.name': 123 }); expectNotType>({ 'laps.foo': 'string' }); expectNotType>({ 'treats.0': 123 }); +expectNotType>({ meta: { deep: { nested: { level: 'string' } } } }); // no impact on actual nesting +expectNotType>({ 'meta.deep': { nested: { level: 'string' } } }); // no impact on actual nesting /// it should not accept wrong types for nested document array fields expectError>({ diff --git a/test/types/community/collection/updateX.test-d.ts b/test/types/community/collection/updateX.test-d.ts index 6d269bc320..cb59182905 100644 --- a/test/types/community/collection/updateX.test-d.ts +++ b/test/types/community/collection/updateX.test-d.ts @@ -74,6 +74,10 @@ interface SubTestModel { field1: string; field2?: string; field3?: number; + nestedObject?: { + a: string; + b: string; + }; } type FruitTypes = 'apple' | 'pear'; @@ -204,6 +208,13 @@ expectAssignable>({ $set: { longField: Long.fromString(' expectAssignable>({ $set: { stringField: 'a' } }); expectError(buildUpdateFilter({ $set: { stringField: 123 } })); expectAssignable>({ $set: { 'subInterfaceField.field2': '2' } }); +expectAssignable>({ $set: { 'subInterfaceField.nestedObject.a': '2' } }); +expectAssignable>({ + $set: { 'subInterfaceField.nestedObject': { a: '1', b: '2' } } +}); +expectError>({ + $set: { 'subInterfaceField.nestedObject': { a: '1' } } +}); expectError(buildUpdateFilter({ $set: { 'subInterfaceField.field2': 2 } })); expectError(buildUpdateFilter({ $set: { 'unknown.field': null } })); expectAssignable>({ $set: { 'numberArray.$': 40 } }); From 04acc52dfbbccecc98fe697c8959ad0ed0d42da0 Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Wed, 20 Jul 2022 14:25:18 +0200 Subject: [PATCH 2/7] =?UTF-8?q?=E2=9C=85=20Add=20test=20with=20`Record`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/types/community/collection/updateX.test-d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/types/community/collection/updateX.test-d.ts b/test/types/community/collection/updateX.test-d.ts index cb59182905..dc5bce6ba8 100644 --- a/test/types/community/collection/updateX.test-d.ts +++ b/test/types/community/collection/updateX.test-d.ts @@ -101,6 +101,7 @@ interface TestModel { subInterfaceField: SubTestModel; subInterfaceArray: SubTestModel[]; timestampField: Timestamp; + extras: Record; } const collectionTType = db.collection('test.update'); @@ -205,6 +206,7 @@ expectAssignable>({ expectAssignable>({ $set: { doubleField: new Double(1.23) } }); expectAssignable>({ $set: { int32Field: new Int32(10) } }); expectAssignable>({ $set: { longField: Long.fromString('999') } }); +expectAssignable>({ $set: { extras: { someExtras: { id: 'someId' } } } }); expectAssignable>({ $set: { stringField: 'a' } }); expectError(buildUpdateFilter({ $set: { stringField: 123 } })); expectAssignable>({ $set: { 'subInterfaceField.field2': '2' } }); From 493722159b17cb1552fd17f4e5e42eb2b4042760 Mon Sep 17 00:00:00 2001 From: Eliott C Date: Wed, 20 Jul 2022 17:02:51 +0200 Subject: [PATCH 3/7] Update test/types/community/collection/filterQuery.test-d.ts Co-authored-by: Daria Pardue --- test/types/community/collection/filterQuery.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/community/collection/filterQuery.test-d.ts b/test/types/community/collection/filterQuery.test-d.ts index f0c12efb0a..2e96efeeed 100644 --- a/test/types/community/collection/filterQuery.test-d.ts +++ b/test/types/community/collection/filterQuery.test-d.ts @@ -178,7 +178,7 @@ expectNotType>({ 'regex.dotAll': true }); collectionT.find({ 'meta.updatedAt': new Date() }); collectionT.find({ 'meta.deep.nested.level': 123 }); collectionT.find({ meta: { deep: { nested: { level: 123 } } } }); // no impact on actual nesting -collectionT.find({ 'meta.deep': { nested: { level: 123 } } }); // no impact on actual nesting +collectionT.find({ 'meta.deep': { nested: { level: 123 } } }); collectionT.find({ 'friends.0.name': 'John' }); collectionT.find({ 'playmates.0.name': 'John' }); // supports arrays with primitive types From 25b884b26cc4bf435ad9ff3a1f7814c46e264fdc Mon Sep 17 00:00:00 2001 From: Eliott C Date: Wed, 20 Jul 2022 17:02:59 +0200 Subject: [PATCH 4/7] Update test/types/community/collection/filterQuery.test-d.ts Co-authored-by: Daria Pardue --- test/types/community/collection/filterQuery.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/community/collection/filterQuery.test-d.ts b/test/types/community/collection/filterQuery.test-d.ts index 2e96efeeed..873c6b2392 100644 --- a/test/types/community/collection/filterQuery.test-d.ts +++ b/test/types/community/collection/filterQuery.test-d.ts @@ -204,7 +204,7 @@ expectNotType>({ 'friends.0.name': 123 }); expectNotType>({ 'playmates.0.name': 123 }); expectNotType>({ 'laps.foo': 'string' }); expectNotType>({ 'treats.0': 123 }); -expectNotType>({ meta: { deep: { nested: { level: 'string' } } } }); // no impact on actual nesting +expectNotType>({ meta: { deep: { nested: { level: 'string' } } } }); expectNotType>({ 'meta.deep': { nested: { level: 'string' } } }); // no impact on actual nesting /// it should not accept wrong types for nested document array fields From 9f2a0db196f332d1c4078ae563b2ae7ceda941a5 Mon Sep 17 00:00:00 2001 From: Eliott C Date: Wed, 20 Jul 2022 17:03:05 +0200 Subject: [PATCH 5/7] Update test/types/community/collection/filterQuery.test-d.ts Co-authored-by: Daria Pardue --- test/types/community/collection/filterQuery.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/community/collection/filterQuery.test-d.ts b/test/types/community/collection/filterQuery.test-d.ts index 873c6b2392..dd17868514 100644 --- a/test/types/community/collection/filterQuery.test-d.ts +++ b/test/types/community/collection/filterQuery.test-d.ts @@ -205,7 +205,7 @@ expectNotType>({ 'playmates.0.name': 123 }); expectNotType>({ 'laps.foo': 'string' }); expectNotType>({ 'treats.0': 123 }); expectNotType>({ meta: { deep: { nested: { level: 'string' } } } }); -expectNotType>({ 'meta.deep': { nested: { level: 'string' } } }); // no impact on actual nesting +expectNotType>({ 'meta.deep': { nested: { level: 'string' } } }); /// it should not accept wrong types for nested document array fields expectError>({ From 6f75c1298313a390e7ddf3f2e68a1cff67edc278 Mon Sep 17 00:00:00 2001 From: Eliott C Date: Wed, 20 Jul 2022 17:03:19 +0200 Subject: [PATCH 6/7] Update test/types/community/collection/updateX.test-d.ts Co-authored-by: Daria Pardue --- test/types/community/collection/updateX.test-d.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/types/community/collection/updateX.test-d.ts b/test/types/community/collection/updateX.test-d.ts index dc5bce6ba8..6ec9584ebb 100644 --- a/test/types/community/collection/updateX.test-d.ts +++ b/test/types/community/collection/updateX.test-d.ts @@ -217,6 +217,9 @@ expectAssignable>({ expectError>({ $set: { 'subInterfaceField.nestedObject': { a: '1' } } }); +expectError>({ + $set: { 'subInterfaceField.nestedObject': { a: 1, 'b': '2' } } +}); expectError(buildUpdateFilter({ $set: { 'subInterfaceField.field2': 2 } })); expectError(buildUpdateFilter({ $set: { 'unknown.field': null } })); expectAssignable>({ $set: { 'numberArray.$': 40 } }); From ce3ddcea9af5ed3f2461643df57940293a42e77f Mon Sep 17 00:00:00 2001 From: Daria Pardue Date: Wed, 20 Jul 2022 11:30:05 -0400 Subject: [PATCH 7/7] lint: Update test/types/community/collection/updateX.test-d.ts --- test/types/community/collection/updateX.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/community/collection/updateX.test-d.ts b/test/types/community/collection/updateX.test-d.ts index 6ec9584ebb..01b7ccfc6e 100644 --- a/test/types/community/collection/updateX.test-d.ts +++ b/test/types/community/collection/updateX.test-d.ts @@ -218,7 +218,7 @@ expectError>({ $set: { 'subInterfaceField.nestedObject': { a: '1' } } }); expectError>({ - $set: { 'subInterfaceField.nestedObject': { a: 1, 'b': '2' } } + $set: { 'subInterfaceField.nestedObject': { a: 1, b: '2' } } }); expectError(buildUpdateFilter({ $set: { 'subInterfaceField.field2': 2 } })); expectError(buildUpdateFilter({ $set: { 'unknown.field': null } }));