francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
105 stars 24 forks source link

Look for schema object, not only models #111

Closed xDelph closed 1 year ago

xDelph commented 1 year ago

Hi,

Currently i'm using @nestjs/mongoose in my project I have the need to define a mongoose schema, but not the the mongoose model associated, as it's injected with nestjs injection system

So far i can't sheet doing something like

export UserSchema = mongoose.Schema...

export const mongooseGen: any = { modelName: 'User' , schema: UserSchema };

Instead, can you add the possibility to directly use the already exported schema obj ? (so far i don't know if you can define the name of the future type with the schema only)

francescov1 commented 1 year ago

Hi @xDelph, if I understand correctly, you need to be able to use the generated Schema type from mongoose-tsgen.

To do so, all you need to do is import it from the generated file. So if your generated file is called mongoose.gen.ts, and your model is called User, you would do the following:

import { UserSchema } from './mongoose.gen.ts'`

// pass `UserSchema` to nestjs

Does that help? Let me know if I'm not understanding correctly

xDelph commented 1 year ago

Hello, thanks for the quick reply And sorry, but that's not the point With my NestJS project, i've already define of the mongoose schema with nestjs/mongoose

Now, i want to use your package to generate the types from this schema in order to share them with another api But in this case i don't have the models, only the schema And as you use the schema underground to do the generation, i wonder if you can add on options to use direclty schema objects instead of model

francescov1 commented 1 year ago

Ah I understand now. We use the model to gather certain metadata as well, so we would need a bit of a refactor to support using the schema directly.

Are you able to instantiate a Mongoose model anyways, even if NestJS doesnt need it? This would allow the generator to use it as needed. Or alternatively, I assume NestJS would provide a way to reference the underlying Mongoose model, since they would need to instantiate it themselves. But I may be wrong, dont have any experience with NestJS.

xDelph commented 1 year ago

Hello sorry for the delay yes we have like auto model for example something like this

@Schema()
export class User {
  @Prop({ type: mongoose.Schema.Types.ObjectId, auto: true })
  _id: string;

  @Prop({ type: Date, required: true })
  createdAt: string;
}

export const UserSchema: mongoose.Schema<User> = SchemaFactory.createForClass(User);

if possible, i don't wanna modify every file that create a schema in order to extract the model define by the class that's why i ask to gather any metadata from the schema to do the generation

francescov1 commented 1 year ago

Hi @xDelph ,

Apologies, I think you will need to extract the model yourself. It would require too large of a refactor to only depend on the schema here and this is the first use case Ive heard of someone needing it.

If there is more traction for this, I will consider it in the future. Feel free to re-open this if thats the case.