FNDEVVE
Explore posts from serversDTDrizzle Team
•Created by FNDEVVE on 8/2/2024 in #help
findMany conditional select one-to-one relation
Hello. I am switching to Drizzle from Supabase ORM and it's very good. However I got some issues, in findMany query like this:
return this.drizzleService.db.query.Client.findMany({
columns: CLIENT_QUERY,
with: {
Documents: {
columns: DOCUMENT_QUERY,
},
SharedClients: {
columns: SHARED_CLIENT_QUERY,
with: {
User_sharedWithId: {
columns: PARTIAL_USER_QUERY,
},
},
where: eq(SharedClient.ownerId, userId),
},
User: {
columns: PARTIAL_USER_QUERY,
// where: ne(Client.ownerId, userId),
},
},
where: or(
eq(Client.ownerId, userId),
inArray(
Client.id,
this.drizzleService.db
.select({ clientId: SharedClient.clientId })
.from(SharedClient)
.where(eq(SharedClient.sharedWithId, userId)),
),
),
});
I need to return User only when owner !== current user. I can't find a way to do it. This is how relations look:
export const ClientRelations = relations(Client, ({ one, many }) => ({
Documents: many(Document),
User: one(User, {
fields: [Client.ownerId],
references: [User.id],
}),
SharedClients: many(SharedClient),
}));
I found someone with similar issue here: https://discord.com/channels/1043890932593987624/11771898925859635602 replies