-
Notifications
You must be signed in to change notification settings - Fork 137
Using Mongoose Plugins ? #15
Comments
Thanks for your interest in Typegoose. Plugins are currently not supported however can be easily implemented (which I will try to do in sometime in the near future) as class decorators. I will change this to enhancement and create a pull request for this when ready. If I am correct your other question is that is it possible to reuse properties? I haven't tested this but if you create a base class 'A' which extends Typegoose and you extend class 'B' and 'C' from class 'A' then B and C will have A's properties. Is this what you want to do? |
Thanks for the plugin annotation works great! For some plugins like mongoose-pagination it would be great to initialize static as an empty object. The extension of classes seems not possible at the moment. |
Hey, awesome work on Typegoose. I'm having issues using the plugin functionality. Tried to implement it using the docs, but can't seem to get it to work. https://stackoverflow.com/questions/45775134/using-plugins-with-typegoose. |
Hey @stuartkuentzel, thanks for your interest in Typegoose! Unfortunately when applying a Mongoose plugin, TypeScript has no information about the plugins "class signature" e.g. which new properties and methods will the plugin add to the schema. This is why the additional typing infos must be provided by hand, as the Typegoose docs states:
So in your case you must add import * as passportLocalMongoose from 'passport-local-mongoose';
import { prop, Typegoose, plugin } from 'typegoose';
@plugin(passportLocalMongoose)
class User extends Typegoose {
@prop()
email: string;
@prop()
username: string;
@prop()
password: string;
// method return values are not represented here, you'll have to look for the plugin's docs to see what they actually return
static authenticate();
static serializeUser();
static deserializeUser();
}
export const UserModel = new User().getModelForClass(User); And basically that is it! Hope it helps. I am going to attach this link to stackoverflow too. |
The issue I'm having is, TypeScript doesn't like if I follow with an instanceMethod. If I follow anything after, for example |
@stuartkuentzel okay i will try to reproduce the problem, meanwhile can you attach what TypeScript / Node / NPM versions are you using? Thanks! |
@szokodiakos Sure, I'm using Node 8.2.1, TypeScript 2.4.2, and NPM 5.3.0. |
@szokodiakos any solution to this problem |
Is there any way to use the very useful Plugins from mongoose ? Or anything allowing me to re-use a bunch of properties in several models ?
Re-using the a sub schema could be a solution but it does not allow custom pre-validation on on the added properties. I've also tried using typescript mixins, but this method needs to redeclare the variables in the implementing class.
Any ideas about this ?
Thanks in advance.
The text was updated successfully, but these errors were encountered: