rykuno
rykuno
Explore posts from servers
DTDrizzle Team
Created by rykuno on 8/26/2023 in #help
Optional/Default value for Relational `Extras`
SQL default mechanism ?? Odd, is this the latest drizzle orm version? It works for me on the latest typescript/drizzle version but still yields a type error. I just set an ignore for it atm
9 replies
DTDrizzle Team
Created by rykuno on 8/26/2023 in #help
Optional/Default value for Relational `Extras`
const event = await db.query.events.findFirst({
where: eq(events.id, params.eventId),
extras(fields, operators) {
return {
authedUserTicketId:
sql<string>`(SELECT (ticket_id) from ${members} where event_id = ${
fields.id
} and user_id = ${authedUser?.user.id || null})`.as(
'authed_user_membership'
)
};
},
with: {
tickets: {
extras(fields, operators) {
return {
memberCount:
sql<number>`(SELECT count(*)::int from ${members} where ticket_id = ${fields.id})`.as(
'member_count'
)
};
}
}
}
});
const event = await db.query.events.findFirst({
where: eq(events.id, params.eventId),
extras(fields, operators) {
return {
authedUserTicketId:
sql<string>`(SELECT (ticket_id) from ${members} where event_id = ${
fields.id
} and user_id = ${authedUser?.user.id || null})`.as(
'authed_user_membership'
)
};
},
with: {
tickets: {
extras(fields, operators) {
return {
memberCount:
sql<number>`(SELECT count(*)::int from ${members} where ticket_id = ${fields.id})`.as(
'member_count'
)
};
}
}
}
});
9 replies
DTDrizzle Team
Created by rykuno on 8/26/2023 in #help
Optional/Default value for Relational `Extras`
Tried that. Actually returns a TS error Type 'string' is not assignable to type 'Aliased<unknown>'.
9 replies
DTDrizzle Team
Created by rykuno on 7/14/2023 in #help
Soft Delete Strategy
My god, this library is maintained by absolute Chads
8 replies
DTDrizzle Team
Created by rykuno on 5/28/2023 in #help
Relation Query - Get likes in post
I've completed it like this at the moment, but man, im struggling to turn this into a relational query 😦
const author = alias(users, "author");
const hasLiked = alias(likes, "hasLiked");
const postRows = await db
.select({
likes: sql<number>`COUNT(${likes.id})`.as("likes"),
hasLiked: hasLiked,
post: posts,
author
})
.from(posts)
.where(eq(posts.eventId, params.id))
.leftJoin(likes, eq(likes.postId, posts.id))
.leftJoin(
hasLiked,
and(
eq(hasLiked.postId, posts.id),
eq(hasLiked.userId, session?.user?.id || "")
)
)
.innerJoin(author, eq(author.id, posts.authorId))
.orderBy(desc(posts.createdAt))
.groupBy(posts.id);
const author = alias(users, "author");
const hasLiked = alias(likes, "hasLiked");
const postRows = await db
.select({
likes: sql<number>`COUNT(${likes.id})`.as("likes"),
hasLiked: hasLiked,
post: posts,
author
})
.from(posts)
.where(eq(posts.eventId, params.id))
.leftJoin(likes, eq(likes.postId, posts.id))
.leftJoin(
hasLiked,
and(
eq(hasLiked.postId, posts.id),
eq(hasLiked.userId, session?.user?.id || "")
)
)
.innerJoin(author, eq(author.id, posts.authorId))
.orderBy(desc(posts.createdAt))
.groupBy(posts.id);
7 replies