prisma-client-go icon indicating copy to clipboard operation
prisma-client-go copied to clipboard

how can i exclude a field while finding model.

Open egely1337 opened this issue 2 years ago • 9 comments

how can i do this? i can do this in javascript using select field. how can i do this in go client???

egely1337 avatar Jul 14 '23 20:07 egely1337

It's not possible yet in the Go client. Can you please describe why you want this feature? Is there a big field which you want to exclude for performance?

also see #346 for selecting fields

steebchen avatar Jul 14 '23 22:07 steebchen

i dont want to user see hashedpassword

egely1337 avatar Jul 15 '23 10:07 egely1337

Good point! I'll put this into the 'soon' list, it would be indeed cool if specific fields could be ignored. However, note that specifically excluding fields might a bit dangerous as the default is returning all, so you might be better off to just selecting the fields you want. This is also not supported yet, but you can work around this by defining a new struct which just returns the fields you want:

user, err := db.User.FindUnique(...).Exec(ctx)

u := struct{
  Name string `json:"string"`
}{
  Name: user.Name,
}

// do something with u
return u

or

user, err := db.User.FindUnique(...).Exec(ctx)

newUser := db.UserModel{
  ID: user.ID,
  Name: user.Name,
}

return newUser

steebchen avatar Jul 15 '23 12:07 steebchen

thank you for suggestion

egely1337 avatar Jul 17 '23 20:07 egely1337

Good point! I'll put this into the 'soon' list, it would be indeed cool if specific fields could be ignored. However, note that specifically excluding fields might a bit dangerous as the default is returning all, so you might be better off to just selecting the fields you want. This is also not supported yet, but you can work around this by defining a new struct which just returns the fields you want:

user, err := db.User.FindUnique(...).Exec(ctx)

u := struct{
  Name string `json:"string"`
}{
  Name: user.Name,
}

// do something with u
return u

or

user, err := db.User.FindUnique(...).Exec(ctx)

newUser := db.UserModel{
  ID: user.ID,
  Name: user.Name,
}

return newUser

This works but if i want to return a nested json with some fields that might be null in the database how should i approach that?

Chalwe19 avatar Sep 03 '23 12:09 Chalwe19

@Chalwe19 Can you please create a new discussion and elaborate on your problem? Thanks!

steebchen avatar Sep 18 '23 21:09 steebchen

@steebchen any news on this feature? even with your suggestions, its a pain when you have to use FindMany as it adds a lot of unnecessary operations

pikanezi avatar Nov 08 '23 16:11 pikanezi

@pikanezi

Not sure what you mean by "have to use FindMany". Using structs is indeed not ideal

I'm currently working on a few Go client things, feel free to sponsor me (or ask your employer) to potentially speed things up ;)

steebchen avatar Nov 08 '23 18:11 steebchen

Regarding the implementation, it would most likely implementing select instead of implementing an exclude function (similar to what the JS client does).

steebchen avatar Nov 08 '23 18:11 steebchen