iamclaytonray
02/09/2021, 2:04 AMMeiri Anto
02/09/2021, 2:23 AMselect?
iamclaytonray
02/09/2021, 2:23 AMiamclaytonray
02/09/2021, 2:24 AMpassword
from User
unless I explicitly select itMeiri Anto
02/09/2021, 2:25 AMYeonHoPark
02/09/2021, 5:41 AMMeiri Anto
02/09/2021, 5:45 AMBård
02/09/2021, 6:56 AMBård
02/09/2021, 6:56 AMimport userSchema from '@schemas/user';
const getUser = async (id, select = {})=> {
const user = await prisma.users.findUnique({
where: {
id: id
},
select: {
...userSchema, // Default schema
...select // Overwrites / adds keys on the default schema
}
});
return user;
}
Bård
02/09/2021, 6:56 AM// @schemas/user.js
export default {
name: true,
email: true,
};
Bård
02/09/2021, 6:58 AMBård
02/09/2021, 6:58 AM// No password
const user = await getUser(id);
// With password
const user = await getUser(id, { password: true });
Martïn
02/09/2021, 10:11 AMselect
only retrieves only the fields you want (: true
) ignoring every other fields not included in `select`:
const userWithoutPassword = await context.prisma.user.findUnique({
where: {
email: “<mailto:you@yourcompany.com|you@yourcompany.com>”
},
select: {
email: true,
country: true,
isProfileActive: true
// password: false
canReceiveNewsletter: true,
}
})
console.log(userWithoutPassword.password) // => null
@Bård Your solution looks neat! ⚡️Bård
02/10/2021, 9:54 AM