zenstackhq / zenstack

Fullstack TypeScript toolkit that enhances Prisma ORM with flexible Authorization layer for RBAC/ABAC/PBAC/ReBAC, offering auto-generated type-safe APIs and frontend hooks.
https://zenstack.dev
MIT License
2.07k stars 88 forks source link

[BUG] Query Discrepancy with Selected Relation Fields #1427

Closed azoom-dang-van-hoi closed 5 months ago

azoom-dang-van-hoi commented 5 months ago

Description and expected behavior When querying data with ZenStack and selecting fields with a value of false or undefined within relation models, ZenStack includes these fields in the query results, contrary to the expected behavior observed with Prisma. Prisma correctly excludes fields with these values from the returned data.

// Query with ZenStack (incorrect behavior)
const data = await enhanced.user.findFirst({
  select: {
    id: true,
    name: true,
    profile: false // This field should not be included in the result
  }
})

// Actual Result 
{
  id: 1,
  name: 'John Doe',
  profile: {
      id: 1,
  }
}

// Expected result (observed with Prisma)
{
  id: 1,
  name: 'John Doe'
}

Environment:

Additional context The bug was identified within the code responsible for the injectForRead check. The automatic addition of the where clause to the field causes the extra data to be retrieved during the get operation. I suggest filtering out fields with values of false or undefined in the select option before executing the query.

ymc9 commented 5 months ago

Thanks for reporting this @azoom-dang-van-hoi ! Yes, there seems to be a problem when handling relation fields. I'll make a fix and include it in the upcoming 2.1.0 release.

ymc9 commented 5 months ago

Fixed in 2.1.0