Last active
September 28, 2022 09:13
-
-
Save Rafik-Belkadi-malou/9c55da5fb1b0a3081129ae39d7ccf305 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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