Last active
September 28, 2022 10:05
-
-
Save Rafik-Belkadi-malou/b95ee929b91d9d376a8ffce387ccd643 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 {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