francescov1 / mongoose-tsgen

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

Use this tool to have a single source of truth for backend and frontend #7

Closed unsignedmind closed 3 years ago

unsignedmind commented 3 years ago

Hi, I would like to suggest a new cli option. In order to use the typescript interfaces in a frontend I would need a seperate index.d.ts were all interfaces are exported and not in a declared module. In addition to that the _id field must be availabe in the base interface(e.g. IUser). Also extends mongoose.Types.Subdocument and extends Document mustn't be added and all mongoose types need be exchanged with TS Types (e.g. Types.DocumentArray => Array)

exported interface IUser {
    _id: string;
    name: string;
    email: string;
    password: string;
    isAdmin: boolean;
    updatedAt?: Date;
    createdAt?: Date;
}

Before starting the backend mtgen must run and before the frontend starts a script runs which copies the index-frontend.d.ts. A option to remove the "I" prefix would be very nice since it's not necessary nowadays.

francescov1 commented 3 years ago

Great ideas! I've been meaning to implement something along the lines of this example for the being able to use documents more easily in both a backend or frontend codebase. Would this satify your use case?

Happy to also add an option to remove the "I" prefix (might just remove it all together) and an option to generate exported interfaces rather than in a declared module. Will try to get to this by the end of the weekend!

unsignedmind commented 3 years ago

Yes thanks. That sounds great.

francescov1 commented 3 years ago

Just published a new version. Few breaking changes here:

Haven't been able to add the module/export option yet. I need to sort out one issue before - with the new type system the mongoose-typed document interfaces now use TS types (this is because they now use an intersection type to combine mongoose.Document and the barebones interface). Consequently, they cannot reference themselves for any Schema ref and therefore must reference the barebones interface (instead of a ref pointing to UserDocument, it points to User. Because of this constraint, I've typed the barebones interface _ids as mongoose.Types.ObjectId so that the edge case here is minimized. See the "Development" section of the README for a bit more clarification.

If you have any ideas here, would love to hear them 🙌 Likely won't get back to this until end of week or next.

unsignedmind commented 3 years ago

Hello, sorry for the late reply. I was busy as well. First of all, thank you very much for the effort.

Can't we add a string as second possible type for the _id attribute. If the interfaces are also exported, then I have everything for my use case. Unfortunaly I can't help with the deeper logic behind generating these interfaces.

francescov1 commented 3 years ago

FYI, interfaces are exported by default now. Old behaviour can be achieved using --augment.

The issue of _id: string is still blocked (see the Development section of the README). I don't want to add string as a second option because this property is commonly used in Mongoose projects and making it a union type would require consistently specifying which version (objectId or string) the user is referring to.

Note that I also need this feature in another project of mine so I will likely try to find a better solution in the coming month or so.

francescov1 commented 3 years ago

Closed with 8.1.0, #52 🚀