P
Prisma6mo ago
Nawwa

How can I use plain object instead of composite types ?

Hi, We were using Prisma v4.10.0 in ou project and we upgraded to 4.16.0 in order to use the new $extends() features Problem : When we updated to 4.16, all of our code broke because now, our mongo's embedded documents are defined as "composite types" with the following struct : { objects : {}, scalars: {}, composites: {} } It's a huge pain because before we had the plain object directly and we could do something like : const user = await prisma.user.find(); console.log(user.profile.name) now : const user = await prisma.user.find(); console.log(user.profile.scalars.name) Any idea if there is an option to go back to old way ?
Solution:
ty ! just checked and the composites disapeared on v5...
Jump to solution
2 Replies
Olyno
Olyno6mo ago
Hi :vmathi: Sorry to hear that! You can try to upgrade to a fixed version, v4.16.2-dev.2, which should fix some issues with composites:
Another option is to try to have explicit type selection by explicitly select the fields you need in your queries:
const user = await prisma.user.find({
select: {
profile: {
select: {
name: true,
},
},
},
});
console.log(user.profile.name);
const user = await prisma.user.find({
select: {
profile: {
select: {
name: true,
},
},
},
});
console.log(user.profile.name);
You can also use Prisma's $extends feature to create custom extensions that simplify access to your nested fields, allowing you to define getter methods to access fields directly without navigating through scalars:
const prisma = new PrismaClient().$extends({
result: {
user: {
profile: {
needs: { profile: true },
compute(user) {
return user.profile.scalars;
},
},
},
},
});
const prisma = new PrismaClient().$extends({
result: {
user: {
profile: {
needs: { profile: true },
compute(user) {
return user.profile.scalars;
},
},
},
},
});
The latest option i would suggest would be to downgrade to v4.15.0 if none of the options above work
Solution
Nawwa
Nawwa6mo ago
ty ! just checked and the composites disapeared on v5
Want results from more Discord servers?
Add your server