-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Usage with TypeScript and typegoose #83
Comments
You have to use the instantiation. Try to change your code like this import mongoose from 'mongoose';
import typegoose from '@typegoose/typegoose';
const { prop, getModelForClass, defaultClasses, plugin } = typegoose;
// Is this import right?
import AutoIncrementFactory from 'mongoose-sequence';
// AutoIncrement now is the instance
const AutoIncrement = AutoIncrementFactory(mongoose);
@plugin(AutoIncrement, { inc_field: 'id', start_seq: 200 })
export class Foo extends defaultClasses.TimeStamps {
...
}
...
export const FooModel = getModelForClass(Foo);
...
mongoose.connect(mongoUrl, { ... }); To answer this question
Yes, sure, just call
after you initialized your models, but since the plugin, in mongoose, must be instantiated alongside model definition, I don't get your point. |
Thanks @ramiel, that worked and I've added an example to the typegoose docs. |
Cool! |
How is it possible to use |
@alimoli from what i can tell, you are using the wrong function, see Requiring also, you have |
Awesome thank you @hasezoey ! :D |
typegoose (at least assuming you run the current latest version (9.5.0)), it does not automatically run plugins, but they are run by mongoose - and mongoose should only call plugins when specific events happen (like TL;DR: i dont know enough about |
hi, I have this problem to.
@type checks failed, it throws the exception:
now I have to changed the code to:
any idea? |
it seems like the current types for this package ( (as i can see you already filed a bug at DefinitelyTyped/DefinitelyTyped#64251) in case it works for your workflow, there is also the alternative of |
I'm trying to use the plugin with typegoose, but I'm not sure if I need to supply the connection, at what point, how, and how to import properly using TypeScript.
Here's what I have:
The error I get comes from the
getModelForClass
line:I understand why the connection is needed, but in classes of this sort, I'd like to only define the class and the model, and initialize the plugin later, when the script starts. Would this deferred initialization be possible? Ideally until the first insertion, but if not, just after defining the models.
If not possible, what do I need to pass to the plugin or typegoose?
Thanks!
CC @typegoose / @hasezoey
The text was updated successfully, but these errors were encountered: