|
1 |
| -import { createConnection, Schema, Collection, Connection, ConnectionSyncIndexesResult, Model, connection } from 'mongoose'; |
| 1 | +import { createConnection, Schema, Collection, Connection, ConnectionSyncIndexesResult, Model, connection, HydratedDocument, Query } from 'mongoose'; |
2 | 2 | import * as mongodb from 'mongodb';
|
3 | 3 | import { expectAssignable, expectError, expectType } from 'tsd';
|
4 | 4 | import { AutoTypedSchemaType, autoTypedSchema } from './schema.test';
|
@@ -142,3 +142,42 @@ export function autoTypedModelConnection() {
|
142 | 142 | })();
|
143 | 143 | return AutoTypedModel;
|
144 | 144 | }
|
| 145 | + |
| 146 | +function schemaInstanceMethodsAndQueryHelpersOnConnection() { |
| 147 | + type UserModelQuery = Query<any, HydratedDocument<User>, UserQueryHelpers> & UserQueryHelpers; |
| 148 | + interface UserQueryHelpers { |
| 149 | + byName(this: UserModelQuery, name: string): this |
| 150 | + } |
| 151 | + interface User { |
| 152 | + name: string; |
| 153 | + } |
| 154 | + interface UserInstanceMethods { |
| 155 | + doSomething(this: HydratedDocument<User>): string; |
| 156 | + } |
| 157 | + interface UserStaticMethods { |
| 158 | + findByName(name: string): Promise<HydratedDocument<User>>; |
| 159 | + } |
| 160 | + type UserModel = Model<User, UserQueryHelpers, UserInstanceMethods> & UserStaticMethods; |
| 161 | + |
| 162 | + const userSchema = new Schema<User, UserModel, UserInstanceMethods, UserQueryHelpers, any, UserStaticMethods>({ |
| 163 | + name: String |
| 164 | + }, { |
| 165 | + statics: { |
| 166 | + findByName(name: string) { |
| 167 | + return connection.model('User').findOne({ name }).orFail(); |
| 168 | + } |
| 169 | + }, |
| 170 | + methods: { |
| 171 | + doSomething() { |
| 172 | + return 'test'; |
| 173 | + } |
| 174 | + }, |
| 175 | + query: { |
| 176 | + byName(this: UserModelQuery, name: string) { |
| 177 | + return this.where({ name }); |
| 178 | + } |
| 179 | + } |
| 180 | + }); |
| 181 | + |
| 182 | + const TestModel = connection.model<User, UserModel, UserQueryHelpers>('User', userSchema); |
| 183 | +} |
0 commit comments