Suggested way to do nested inserts

In prisma, it is possible to create nested tables (for instance create posts with a user at the same time) like so:
await prisma.user.create({
name: 'John',
posts: { createMany: newPosts }
});
await prisma.user.create({
name: 'John',
posts: { createMany: newPosts }
});
Is it correct that the only way to do this in drizzle would be to do something like:
cosnt [{id: userId}] = await db.insert(users).values({ name: 'John' }).returning();

await db.insert(posts).values(newPosts.map(post => ({ ...post, userId })));
cosnt [{id: userId}] = await db.insert(users).values({ name: 'John' }).returning();

await db.insert(posts).values(newPosts.map(post => ({ ...post, userId })));
unless you generate userId ahead of time manually
1 Reply
Angelelz
Angelelz12mo ago
I guess that's how you should do it I would do it in a transaction though
Want results from more Discord servers?
Add your server