Skip to content

Commit

Permalink
Add options property to virtual path to be able to apply passed optio…
Browse files Browse the repository at this point in the history
…ns while creating virtual path.
  • Loading branch information
mohammad0-0ahmad committed Jul 19, 2022
1 parent 7df135b commit b0f4912
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
3 changes: 2 additions & 1 deletion test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ export function autoTypedSchema() {
},
set() {
expectType<Document<any, any, AutoTypedSchemaType['schema']> & AutoTypedSchemaType['schema']>(this);
}
},
options: {}
}
}
});
Expand Down
8 changes: 4 additions & 4 deletions types/schemaoptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare module 'mongoose' {
type TypeKeyBaseType = string;

type DefaultTypeKey = 'type';
interface SchemaOptions<PathTypeKey extends TypeKeyBaseType = DefaultTypeKey, DocType = unknown, InstanceMethods = {}, QueryHelpers = {}, TVirtuals = {}, StaticMethods = {}> {
interface SchemaOptions<PathTypeKey extends TypeKeyBaseType = DefaultTypeKey, DocType = unknown, TInstanceMethods = {}, QueryHelpers = {}, TVirtuals = {}, TStaticMethods = {}> {
/**
* By default, Mongoose's init() function creates all the indexes defined in your model's schema by
* calling Model.createIndexes() after you successfully connect to MongoDB. If you want to disable
Expand Down Expand Up @@ -191,12 +191,12 @@ declare module 'mongoose' {
/**
* Model Statics methods.
*/
statics?: Record<any, (this: Model<DocType>, ...args: any) => unknown> | StaticMethods,
statics?: Record<any, (this: Model<DocType>, ...args: any) => unknown> | TStaticMethods,

/**
* Document instance methods.
*/
methods?: Record<any, (this: HydratedDocument<DocType>, ...args: any) => unknown> | InstanceMethods,
methods?: Record<any, (this: HydratedDocument<DocType>, ...args: any) => unknown> | TInstanceMethods,

/**
* Query helper functions.
Expand All @@ -212,6 +212,6 @@ declare module 'mongoose' {
/**
* Virtual paths.
*/
virtuals?: VirtualsSchemaOptionsPropertyType<DocType, TVirtuals>,
virtuals?: VirtualsSchemaOptionsPropertyType<DocType, TVirtuals, TInstanceMethods>,
}
}
9 changes: 5 additions & 4 deletions types/virtuals.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/// <reference path="./utility.d.ts" />

declare module 'mongoose' {
type VirtualPathFunctions<DocType = {}, pathType = unknown> = {
get?: (this: Document<any, any, DocType> & DocType) => pathType;
type VirtualPathFunctions<DocType = {}, PathType = unknown, TInstanceMethods = {}> = {
get?: (this: Document<any, any, DocType> & DocType) => PathType;
set?: (this: Document<any, any, DocType> & DocType, ...args: any) => unknown;
options?: VirtualTypeOptions<HydratedDocument<DocType, TInstanceMethods>, DocType>;
};

type VirtualsSchemaOptionsPropertyType<DocType = any, virtualPaths = Record<any, unknown>> = {
[K in keyof virtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, virtualPaths[K]>
type VirtualsSchemaOptionsPropertyType<DocType = any, virtualPaths = Record<any, unknown>, TInstanceMethods = {}> = {
[K in keyof virtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, virtualPaths[K], TInstanceMethods>
};
}

0 comments on commit b0f4912

Please sign in to comment.