is it possible to do conditional relational queries?

for example, i only want to retrieve the author object if their profile is public (a boolean on a user object)
export const users_relations = relations(users_table, ({ many }) => ({
posts: many(posts_table),
}));

export const posts_relations = relations(posts_table, ({ one }) => ({
author: one(users_table, {
fields: [posts_table.owner_id],
references: [users_table.id],
}),
}));
export const users_relations = relations(users_table, ({ many }) => ({
posts: many(posts_table),
}));

export const posts_relations = relations(posts_table, ({ one }) => ({
author: one(users_table, {
fields: [posts_table.owner_id],
references: [users_table.id],
}),
}));
2 Replies
netwrx
netwrxOP7mo ago
if i do something like this
const post = await db.query.posts_table.findFirst({
where: eq(posts_table.id, post_id),
with: {
author: {
where: eq(users_table.public, true),
columns: {
username: true,
display_name: true,
public: true,
},
},
},
});
const post = await db.query.posts_table.findFirst({
where: eq(posts_table.id, post_id),
with: {
author: {
where: eq(users_table.public, true),
columns: {
username: true,
display_name: true,
public: true,
},
},
},
});
it works. but im getting typescript errors. is this a bug with drizzle? bump :/
agonist
agonist7mo ago
same question here. the query works but typescript isnt happy

const t = await db.query.campaigns.findMany({
where: isNotNull(campaigns.userId),
with: {
user: {
// @ts-ignore
where: eq(users.clerkId, userId),
},
},
});

const t = await db.query.campaigns.findMany({
where: isNotNull(campaigns.userId),
with: {
user: {
// @ts-ignore
where: eq(users.clerkId, userId),
},
},
});
Want results from more Discord servers?
Add your server