Skip to content

Instantly share code, notes, and snippets.

@Rafik-Belkadi-malou
Last active September 28, 2022 10:05
Show Gist options
  • Save Rafik-Belkadi-malou/b95ee929b91d9d376a8ffce387ccd643 to your computer and use it in GitHub Desktop.
Save Rafik-Belkadi-malou/b95ee929b91d9d376a8ffce387ccd643 to your computer and use it in GitHub Desktop.
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
const orderPayload: IOrder = {
// Relation
productsIds: products.map(p => p._id),
quantity: 2,
}
// Create the order and then populate products, you can not use populate directly on the create function
const order: IOrder = await Order.create(orderPayload).then(order => order.populate('products'))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment