MCPcopy Create free account
hub / github.com/Automattic/mongoose / schemaInstanceMethodsAndQueryHelpers

Function schemaInstanceMethodsAndQueryHelpers

test/types/models.test.ts:543–580  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

541}
542
543function schemaInstanceMethodsAndQueryHelpers() {
544 type UserModelQuery = Query<any, HydratedDocument<User>, UserQueryHelpers> & UserQueryHelpers;
545 interface UserQueryHelpers {
546 byName(this: UserModelQuery, name: string): this
547 }
548 interface User {
549 name: string;
550 }
551 interface UserInstanceMethods {
552 doSomething(this: HydratedDocument<User>): string;
553 }
554 interface UserStaticMethods {
555 findByName(name: string): Promise<HydratedDocument<User>>;
556 }
557 type UserModel = Model<User, UserQueryHelpers, UserInstanceMethods> & UserStaticMethods;
558
559 const userSchema = new Schema<User, UserModel, UserInstanceMethods, UserQueryHelpers, any, UserStaticMethods>({
560 name: String
561 }, {
562 statics: {
563 findByName(name: string) {
564 return model('User').findOne({ name }).orFail();
565 }
566 },
567 methods: {
568 doSomething() {
569 return 'test';
570 }
571 },
572 query: {
573 byName(this: UserModelQuery, name: string) {
574 return this.where({ name });
575 }
576 }
577 });
578
579 const TestModel = model<User, UserModel, UserQueryHelpers>('User', userSchema);
580}
581
582function gh12100() {
583 const schema = new Schema();

Callers

nothing calls this directly

Calls 1

modelFunction · 0.85

Tested by

no test coverage detected