#How to use nestjs with regular mongoose schema

1 messages · Page 1 of 1 (latest)

toxic lake
#

I see that I can use the regular mongoose schema syntax without the schema decorator or classes,

But I am wondering how I can now inject that schema into my service?

with the class and decorator based approach I noticed that I get a regular class as type, a schema and a document.

How do I get a type and document from the regular mongoose schema?

scenic anvil
#

You'd still use @InjectModel(nameYouGaveToMongooseModuleForFeature) to inject the schema. Not sure about the typings, just not familiar with them enough to say

rose narwhal
#

How do I get a type and document from the regular mongoose schema?
If you use regular Mongoose, you have to create TS interfaces to match your schema to get the entity types. Document types comes from casting the Document interface on returns or, by extending your interfaces with the Document interface, which isn't recommended. At any rate, the whole point of using Nest's Mongoose module, is so you don't have to do all that "double work" of creating both interfaces for types and schema definitions. Typegoose explains this in their docs as Typegoose has the same purpose and Nest's Mongoose module: https://typegoose.github.io/typegoose/docs/guides/quick-start-guide/

Quick Overview of Typegoose