dannyelo
dannyelo•5mo ago

Insert a record, then another record with the id of the first one in the same transaction

Hello, I have some tables orders and order_products This is the schema:
orders: defineTable({
organizationId: v.id('organizations'),
customerId: v.id('customers'),
salesChannelId: v.id('sales_channels'),
status: v.string(),
folio_number: v.number(),
}),
order_products: defineTable({
orderId: v.id('orders'),
productId: v.id('products'),
quantity: v.number(),
price: v.number(),
discount: v.number(),
taxes: v.array(v.id('taxes')),
})
orders: defineTable({
organizationId: v.id('organizations'),
customerId: v.id('customers'),
salesChannelId: v.id('sales_channels'),
status: v.string(),
folio_number: v.number(),
}),
order_products: defineTable({
orderId: v.id('orders'),
productId: v.id('products'),
quantity: v.number(),
price: v.number(),
discount: v.number(),
taxes: v.array(v.id('taxes')),
})
I'm trying to construct a create function but I'm not sure how to get the orderId first to assign it to all the order_products Is this the correct approach?
export const createOrder = mutation({
args: {
organizationId: v.id('organizations'),
salesChannelId: v.id('sales_channels'),
customerId: v.id('customers'),
products: v.array(
v.object({
productId: v.id('products'),
quantity: v.number(),
price: v.number(),
discount: v.number(),
taxes: v.array(v.id('taxes')),
})
),
},
handler: async (ctx, args) => {
const { organizationId, salesChannelId, customerId } = args
const orderId = await ctx.db.insert('orders', {
organizationId,
salesChannelId,
customerId,
folio_number: 1,
status: 'pending',
})

args.products.forEach(async (product) => {
await ctx.db.insert('order_products', {
orderId,
productId: product.productId,
quantity: product.quantity,
price: product.price,
discount: product.discount,
taxes: product.taxes || [],
})
})
},
})
export const createOrder = mutation({
args: {
organizationId: v.id('organizations'),
salesChannelId: v.id('sales_channels'),
customerId: v.id('customers'),
products: v.array(
v.object({
productId: v.id('products'),
quantity: v.number(),
price: v.number(),
discount: v.number(),
taxes: v.array(v.id('taxes')),
})
),
},
handler: async (ctx, args) => {
const { organizationId, salesChannelId, customerId } = args
const orderId = await ctx.db.insert('orders', {
organizationId,
salesChannelId,
customerId,
folio_number: 1,
status: 'pending',
})

args.products.forEach(async (product) => {
await ctx.db.insert('order_products', {
orderId,
productId: product.productId,
quantity: product.quantity,
price: product.price,
discount: product.discount,
taxes: product.taxes || [],
})
})
},
})
Thanks!
8 Replies
lee
lee•5mo ago
yes that looks right (you should await your promises though)
dannyelo
dannyeloOP•5mo ago
Thanks @lee! I'm awaiting them, aren't I?
lee
lee•5mo ago
args.products.forEach(async (product) => { this will create a lot of promises that don't get awaited
dannyelo
dannyeloOP•5mo ago
How can I await them? Like this?
const productPromises = args.products.map(async (product) => {
ctx.db.insert('order_products', {
orderId,
productId: product.productId,
quantity: product.quantity,
price: product.price,
discount: product.discount,
taxes: product.taxes || [],
})
})

await Promise.all(productPromises)
const productPromises = args.products.map(async (product) => {
ctx.db.insert('order_products', {
orderId,
productId: product.productId,
quantity: product.quantity,
price: product.price,
discount: product.discount,
taxes: product.taxes || [],
})
})

await Promise.all(productPromises)
lee
lee•5mo ago
that's close
No description
dannyelo
dannyeloOP•5mo ago
Oh I forgot to return each insertion
const productPromises = args.products.map(async (product) => {
return ctx.db.insert('order_products', {
orderId,
productId: product.productId,
quantity: product.quantity,
price: product.price,
discount: product.discount,
taxes: product.taxes || [],
})
})

await Promise.all(productPromises)
const productPromises = args.products.map(async (product) => {
return ctx.db.insert('order_products', {
orderId,
productId: product.productId,
quantity: product.quantity,
price: product.price,
discount: product.discount,
taxes: product.taxes || [],
})
})

await Promise.all(productPromises)
lee
lee•5mo ago
yep that's good 🙂
dannyelo
dannyeloOP•5mo ago
Thank you @lee!!

Did you find this page helpful?