-
-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Types for query methods #247
Conversation
this is more or less exactly what typegoose tries to "prevent" (making interfaces for mongoose schemas/models) otherwise, the only think i would like to change is to swap the generics of |
Yes, I know and I don't really like it either but otherwise there are no types at all for people who'd like to have them. I'll investigate it a bit further to see if it's possible to have types without defining interfaces explicitly. |
because if we would make the functions in the class again, we would need an |
I have two easier solutions/proposals and assume we have the following in both. function findByName(this: ReturnModelType<typeof MyClass, MyClass, SchemaQueries>, name: string) {
return this.find({ name });
}
function findByLastname(this: ReturnModelType<typeof MyClass, MyClass, SchemaQueries>, lastname: string) {
return this.find({ lastname });
}
@queryMethod(findByName)
@queryMethod(findByLastname)
class MyClass {
@prop()
public name: string;
@prop()
public lastname: string;
} Usage: const MyModel = getModelForClass<MyClass, typeof MyClass, SchemaQueries>(MyClass);
// Will be placed in `types.ts`
type QueryMethod<T extends (...args: any) => any> = (...args: Parameters<T>) => ReturnType<T>;
// In model's file
interface SchemaQueries {
findByName: QueryMethod<typeof findByName>;
findByLastname: QueryMethod<typeof findByLastname>;
}
// Will be placed in `types.ts`
type QueryMethodTypes<T extends {}> = {
[F in keyof T]: T[F] extends (...args: any) => any ? (...args: Parameters<T[F]>) => ReturnType<T[F]> : never;
}
// In model's file
const MyMethods = { findByName, findByLastname };
type SchemaQueries = QueryMethodTypes<typeof MyMethods>; |
i think solution 1 is better and easier to understand & modify (and without any runtime extras) |
I agree. Do you think we should make |
yes, but this would be an breaking change (maybe even major) but on the other side, the types that would need to be before it would need to be defined - which is not good because they should work automatically |
@hasezoey are you planning to merge this PR or should we make it specific to updating the related types in |
i still need to check out this pr locally, and if all works, then i have some changes in mind i want to test & integrate |
remove unnecessary generics actually export "queryMethod"
@ggurkal are you ok with the changes i made? |
Since types are handled correctly, comments related to query methods are removed.
Yes |
This is the only thing I could think of for query methods types. Using mongoose's own types for
Model
'sQueryHelpers
, which is added as the third type argument to thegetModelForClass
function.