Creating a related record with Prisma returning all records / how to get just one?

Hi folks! I'm pretty new to prisma so i have something a little annoying happening, love your guidance. I'm running some reporting - one batch has many reports... so first i create the batch
let batch;
try {
batch = await prisma.reportingBatch.create({
data: {
status: "RUNNING",
},
});
}
let batch;
try {
batch = await prisma.reportingBatch.create({
data: {
status: "RUNNING",
},
});
}
Then i create a report:
const report = await prisma.reportingBatch.update({
where: { id: batch.id },
data: {
report: {
create: { url: "www.google.com" },
},
},
include: { report: true },
});
const report = await prisma.reportingBatch.update({
where: { id: batch.id },
data: {
report: {
create: { url: "www.google.com" },
},
},
include: { report: true },
});
The first time this runs, the report is returned like this:
{
id: 20n,
createdAt: 2023-01-19T23:24:28.000Z,
status: 'RUNNING',
report: [
{
id: 27n,
reportingBatchId: 20n,
url: 'https://google.com',
}
]
{
id: 20n,
createdAt: 2023-01-19T23:24:28.000Z,
status: 'RUNNING',
report: [
{
id: 27n,
reportingBatchId: 20n,
url: 'https://google.com',
}
]
which is a little annoying cause i just need the report to come back, but in a situation where there are 10000 reports, the last one would return with report.length of 10000 Which is annoying, and not performant (too much data getting passed around), and also hard to know which item i created (use Array.pop?) Is there a way to do it the other way? to create the the report, and have that report created as a relation in one go? ive found connect operator but it looks like it can only be used if i create the report first? so you have to do it in two queries?
1 Reply
fotoflo
fotofloOP2y ago
report = await prisma.report.create({
data: {
reportingBatch: {
connect: {
id: batch.id,
},
},
url: "www.google.com"
},
});
report = await prisma.report.create({
data: {
reportingBatch: {
connect: {
id: batch.id,
},
},
url: "www.google.com"
},
});
worked!
Want results from more Discord servers?
Add your server