Mongoose JS is the most popular Mongodb client library in Nodejs world. And Typescript is one of the finest tools for JS when it comes to DX. It brings type-safety, type inference, that helps in detecting the bugs much early while giving good control.
The most loved part about TS is definitely the autosuggestions is brings while programming. This boosts the speed and we don’t need to check jump various files just to check types.
However, the Mongoose library does still rely on older JS code, hence it struggles with typescript. As of writing this article, out of the box TS support hasn’t been its feature.
But the lib allows playing with its own types, and lets devs configure it so that typescript takes control nicely. Lets discuss the steps for modifying mongoose models to obey typescript.
I assume you already know how to create mongoose models in JS. Lets go.
Which are generally passed in the
codeconst myobj = new MyModel({attriX: valX, attriY: valY})
part of our JS code
interface TicketAttrs {
title: string;
price: number;
userId: string;
}
interface TicketDoc extends mongoose.Document {
title: string;
price: number;
userId: string;
orderId? :string // not used when instantiating new object
}
Which accepts args of TickectAttrs
we defined above.
interface TicketModel extends mongoose.Model<TicketDoc> {
build(attrs: TicketAttrs): TicketDoc;
}
const ticketSchema = new mongoose.Schema(
{
title: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
userId: {
type: String,
required: true,
},
orderId: {
type: String,
},
}
);
build()
method doesticketSchema.statics.build = (attrs: TicketAttrs) => {
return new Ticket(attrs);
};
Pass the above interfaces as the generic types.
const Ticket = mongoose.model<TicketDoc, TicketModel>('Ticket', ticketSchema);
export {Ticket}
Coming soon...