Skip to content

Instantly share code, notes, and snippets.

View Rafik-Belkadi-malou's full-sized avatar

Rafik Belkadi Rafik-Belkadi-malou

  • Malou
  • Paris
View GitHub Profile
import {Product, IProduct} from './product'
import {Order, IOrder} from './order'
const main = async () => {
// products to insert
const productsPayload: IProduct[] = [{name: 'Pokemon', price: 20, stock: 40}, {name: 'Socks', price: 10, stock: 50}]
// Inserted products result typed with the interface IProduct
const products: IProduct[] = await Product.insertMany(productsPayload)
// Order to insert
import { Schema, model } from 'mongoose'
import { IProduct } from './product'
// Utility type which you can put in your utils file
type ID = Schema.Types.ObjectId
export interface IOrder {
_id?: ID
// Represents the relation between the order and the product
productsIds: ID[]
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