rykuno
rykuno
Explore posts from servers
DTDrizzle Team
Created by rykuno on 8/26/2023 in #help
Optional/Default value for Relational `Extras`
I have a couple cases where a default/optional value for "extras" in the relational query builder could come in handy. I only really want to run this if the user is logged in. ATM I have the following solution
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'
)
};
},
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'
)
};
},
If there is no auth user it just sets the user_id to null but its not exactly ideal. Is there any plans or current way to optionally add this statement within the same return block?
9 replies
DTDrizzle Team
Created by rykuno on 7/2/2023 in #help
PGEnum -> Typescript Enum
Hey there. Is there any convenient way people have found to conveniently turn a pgEnum into a typescript enum such as below? Currently I'm creating DTO's but i figured coupling them would be much easier :). I figure ZOD might come into play but we are utilizing NestJS w/ class-validator atm.
const typeEnum = pgEnum('type', [
'short_text',
'long_text',
'single_choice',
'multiple_choice',
]);

export enum SurveyQuestionType {
SHORT_TEXT = 'short_text',
LONG_TEXT = 'long_text',
SINGLE_CHOICE = 'single_choice',
MULTIPLE_CHOICE = 'multiple_choice',
}
const typeEnum = pgEnum('type', [
'short_text',
'long_text',
'single_choice',
'multiple_choice',
]);

export enum SurveyQuestionType {
SHORT_TEXT = 'short_text',
LONG_TEXT = 'long_text',
SINGLE_CHOICE = 'single_choice',
MULTIPLE_CHOICE = 'multiple_choice',
}
5 replies
DTDrizzle Team
Created by rykuno on 5/28/2023 in #help
Relation Query - Get likes in post
Playing around with relational queries and I'm not quite getting how I'd retrieve the count of likes on a given post. I'm able to accomplish this in sql, but I'm having a hard time translating or failing to understand why i cant translate this to drizzle.
const postResults = await db.query.posts.findMany({
where: eq(posts.eventId, params.id),
with: {
author: true,
likes: true
},
orderBy: desc(posts.createdAt)
});
const postResults = await db.query.posts.findMany({
where: eq(posts.eventId, params.id),
with: {
author: true,
likes: true
},
orderBy: desc(posts.createdAt)
});
export const likes = mysqlTable(
"likes",
{
id: cuid2("id").primaryKey().notNull(),
postId: cuid2("post_id").notNull(),
userId: cuid2("user_id").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedat: timestamp("updated_at").notNull().defaultNow().onUpdateNow()
},
like => ({
postUserIndex: uniqueIndex("likes__post_id__user_id__idx").on(
like.postId,
like.userId
),
postIndex: index("likes__post_id").on(like.postId)
})
);

export const posts = mysqlTable("posts", {
id: cuid2("id").primaryKey().notNull(),
text: varchar("text", { length: 750 }).notNull(),
authorId: cuid2("author_id").notNull(),
eventId: cuid2("event_id").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow()
});

export const likesRelations = relations(likes, ({ one }) => ({
post: one(posts, {
fields: [likes.postId],
references: [posts.id]
}),
user: one(users, {
fields: [likes.userId],
references: [users.id]
})
}));

export const postsRelations = relations(posts, ({ one, many }) => ({
author: one(users, {
fields: [posts.authorId],
references: [users.id]
}),
event: one(events, {
fields: [posts.eventId],
references: [events.id]
}),
likes: many(likes)
}));
export const likes = mysqlTable(
"likes",
{
id: cuid2("id").primaryKey().notNull(),
postId: cuid2("post_id").notNull(),
userId: cuid2("user_id").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedat: timestamp("updated_at").notNull().defaultNow().onUpdateNow()
},
like => ({
postUserIndex: uniqueIndex("likes__post_id__user_id__idx").on(
like.postId,
like.userId
),
postIndex: index("likes__post_id").on(like.postId)
})
);

export const posts = mysqlTable("posts", {
id: cuid2("id").primaryKey().notNull(),
text: varchar("text", { length: 750 }).notNull(),
authorId: cuid2("author_id").notNull(),
eventId: cuid2("event_id").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow().onUpdateNow()
});

export const likesRelations = relations(likes, ({ one }) => ({
post: one(posts, {
fields: [likes.postId],
references: [posts.id]
}),
user: one(users, {
fields: [likes.userId],
references: [users.id]
})
}));

export const postsRelations = relations(posts, ({ one, many }) => ({
author: one(users, {
fields: [posts.authorId],
references: [users.id]
}),
event: one(events, {
fields: [posts.eventId],
references: [events.id]
}),
likes: many(likes)
}));
7 replies
DTDrizzle Team
Created by rykuno on 5/13/2023 in #help
Custom `Select` object returns type `any`
Databases like Planetscale do not support FK constraints. As I understand I can manipulate the returned data structure from within the select clause. The data structure returned is correct but there is a typescript error casting it to any. Is there a way to fix this or am I misusing select?
const [event] = await db
.select({
...events,
createdBy: {
...users
}
})
.from(events)
.innerJoin(users, eq(events.createdByUserId, users.id))
.where(eq(events.id, params.id));
const [event] = await db
.select({
...events,
createdBy: {
...users
}
})
.from(events)
.innerJoin(users, eq(events.createdByUserId, users.id))
.where(eq(events.id, params.id));
export const events = mysqlTable("events", {
id: cuid2("id").primaryKey().notNull(),
name: text("name").notNull(),
description: text("description"),
image: text("image").notNull(),
privacy: text("privacy", { enum: ["public", "private"] }).notNull(),
startDate: timestamp("startDate").notNull(),
endDate: timestamp("endDate").notNull(),
createdByUserId: cuid2("created_by_user_id").notNull(),
created_at: timestamp("created_at").notNull().defaultNow(),
updated_at: timestamp("updated_at").notNull().defaultNow().onUpdateNow()
});
export const events = mysqlTable("events", {
id: cuid2("id").primaryKey().notNull(),
name: text("name").notNull(),
description: text("description"),
image: text("image").notNull(),
privacy: text("privacy", { enum: ["public", "private"] }).notNull(),
startDate: timestamp("startDate").notNull(),
endDate: timestamp("endDate").notNull(),
createdByUserId: cuid2("created_by_user_id").notNull(),
created_at: timestamp("created_at").notNull().defaultNow(),
updated_at: timestamp("updated_at").notNull().defaultNow().onUpdateNow()
});
1 replies