From d152820cb0bf2fec6310ecbe8654f635ef5130ba Mon Sep 17 00:00:00 2001 From: hasezoey Date: Fri, 19 Aug 2022 11:56:48 +0200 Subject: [PATCH 1/2] fix(types): remove "U" generic added in Schema from "Connection.model" re #12125 --- types/connection.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/connection.d.ts b/types/connection.d.ts index 28d2dd73dbb..c47f7be3f81 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -150,7 +150,7 @@ declare module 'mongoose' { ): Model, ObtainSchemaGeneric, ObtainSchemaGeneric, {}, TSchema> & ObtainSchemaGeneric; model( name: string, - schema?: Schema, + schema?: Schema, collection?: string, options?: CompileModelOptions ): U; From 3bce1060aed7a91a09c1a490cdd383fd364ba12f Mon Sep 17 00:00:00 2001 From: hasezoey Date: Fri, 19 Aug 2022 11:57:03 +0200 Subject: [PATCH 2/2] test(types): add test from #12125 for Connection re #12125 --- test/types/connection.test.ts | 41 ++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/test/types/connection.test.ts b/test/types/connection.test.ts index e2d86efee12..28c941c8786 100644 --- a/test/types/connection.test.ts +++ b/test/types/connection.test.ts @@ -1,4 +1,4 @@ -import { createConnection, Schema, Collection, Connection, ConnectionSyncIndexesResult, Model, connection } from 'mongoose'; +import { createConnection, Schema, Collection, Connection, ConnectionSyncIndexesResult, Model, connection, HydratedDocument, Query } from 'mongoose'; import * as mongodb from 'mongodb'; import { expectAssignable, expectError, expectType } from 'tsd'; import { AutoTypedSchemaType, autoTypedSchema } from './schema.test'; @@ -142,3 +142,42 @@ export function autoTypedModelConnection() { })(); return AutoTypedModel; } + +function schemaInstanceMethodsAndQueryHelpersOnConnection() { + type UserModelQuery = Query, UserQueryHelpers> & UserQueryHelpers; + interface UserQueryHelpers { + byName(this: UserModelQuery, name: string): this + } + interface User { + name: string; + } + interface UserInstanceMethods { + doSomething(this: HydratedDocument): string; + } + interface UserStaticMethods { + findByName(name: string): Promise>; + } + type UserModel = Model & UserStaticMethods; + + const userSchema = new Schema({ + name: String + }, { + statics: { + findByName(name: string) { + return connection.model('User').findOne({ name }).orFail(); + } + }, + methods: { + doSomething() { + return 'test'; + } + }, + query: { + byName(this: UserModelQuery, name: string) { + return this.where({ name }); + } + } + }); + + const TestModel = connection.model('User', userSchema); +}