Skip to content

Instantly share code, notes, and snippets.

@Rafik-Belkadi-malou
Last active September 28, 2022 09:13
Show Gist options
  • Save Rafik-Belkadi-malou/9c55da5fb1b0a3081129ae39d7ccf305 to your computer and use it in GitHub Desktop.
Save Rafik-Belkadi-malou/9c55da5fb1b0a3081129ae39d7ccf305 to your computer and use it in GitHub Desktop.
import { Schema, model } from 'mongoose'
// Utility type which you can put in your utils file
type ID = Schema.Types.ObjectId
export interface IProduct {
// Optional so it is easier to use when trying to create a new document
_id?: ID
name: string
price: number
stock: number
}
// Here we use ProductSchema as a type generic for the schema definition, this will check the structure of the schema but not the types of the schema
const productSchema = new Schema<IProduct>({
name: String,
price: Number,
stock: Number
})
// Here we use ProductSchema as a type generic for the model to type the resulted documents of the queries on the model.
export const Product = model<IProduct>('Product', productSchema)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment