Kai Revona
Kai Revona
DTDrizzle Team
Created by Kai Revona on 3/7/2024 in #help
The optimise likes table
I am beginner, i have lealearned about social media design pattern, i created posts, likes, comments tabltable. But likes table only have userId and postId to relation. I don't know how to create likes for comments. Please help me
1 replies
DTDrizzle Team
Created by Kai Revona on 2/2/2024 in #help
Nested comment
How to create a nested comment model like prisma
model Post {
id String @id @default(uuid())
title String
body String
comments Comment[]
}

model User {
id String @id @default(uuid())
name String
comments Comment[]
likes Like[]
}

model Comment {
id String @id @default(uuid())
message String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
postId String
parent Comment? @relation("ParentChild", fields: [parentId], references: [id], onDelete: Cascade)
children Comment[] @relation("ParentChild")
parentId String?
likes Like[]
}

model Like {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
comment Comment @relation(fields: [commentId], references: [id], onDelete: Cascade)
userId String
commentId String

@@id([userId, commentId])
}
model Post {
id String @id @default(uuid())
title String
body String
comments Comment[]
}

model User {
id String @id @default(uuid())
name String
comments Comment[]
likes Like[]
}

model Comment {
id String @id @default(uuid())
message String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
postId String
parent Comment? @relation("ParentChild", fields: [parentId], references: [id], onDelete: Cascade)
children Comment[] @relation("ParentChild")
parentId String?
likes Like[]
}

model Like {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
comment Comment @relation(fields: [commentId], references: [id], onDelete: Cascade)
userId String
commentId String

@@id([userId, commentId])
}
20 replies
DTDrizzle Team
Created by Kai Revona on 2/1/2024 in #help
Auto delete record using timestamp
How to auto delete a record using timestamp and do a function when delete
9 replies
DTDrizzle Team
Created by Kai Revona on 1/18/2024 in #help
many-to-many selection as array
const data = await db
.select({
id: posts.id,
content: posts.content,
imageUrl: posts.imageUrl,
placeholder: posts.placeholder,
allowComments: posts.allowComments,
createdAt: posts.createdAt,
author: {
id: users.id,
username: users.username,
firstName: users.firstName,
lastName: users.lastName,
imageUrl: users.imageUrl,
},
likeCount: count(likes.postId),
likedByMe: countDistinct(likes.userId),
})
.from(posts)
.leftJoin(users, eq(posts.authorId, users.id))
.leftJoin(hashtagsToPosts, eq(hashtagsToPosts.postId, posts.id))
.leftJoin(hashtags, eq(hashtags.id, hashtagsToPosts.hashtagId))
.leftJoin(likes, eq(posts.id, likes.postId))

.where(cursor ? lte(posts.createdAt, new Date(cursor)) : undefined)
.limit(limit + 1)
.orderBy(desc(posts.createdAt))
.groupBy(posts.id)
const data = await db
.select({
id: posts.id,
content: posts.content,
imageUrl: posts.imageUrl,
placeholder: posts.placeholder,
allowComments: posts.allowComments,
createdAt: posts.createdAt,
author: {
id: users.id,
username: users.username,
firstName: users.firstName,
lastName: users.lastName,
imageUrl: users.imageUrl,
},
likeCount: count(likes.postId),
likedByMe: countDistinct(likes.userId),
})
.from(posts)
.leftJoin(users, eq(posts.authorId, users.id))
.leftJoin(hashtagsToPosts, eq(hashtagsToPosts.postId, posts.id))
.leftJoin(hashtags, eq(hashtags.id, hashtagsToPosts.hashtagId))
.leftJoin(likes, eq(posts.id, likes.postId))

.where(cursor ? lte(posts.createdAt, new Date(cursor)) : undefined)
.limit(limit + 1)
.orderBy(desc(posts.createdAt))
.groupBy(posts.id)
How to join hashtagsToPosts with hashtags to data selection
5 replies
DTDrizzle Team
Created by Kai Revona on 1/13/2024 in #help
Autogenerated default approach
How to return a default generated id like the isssue "Does Drizzle support MySQL's LAST_INSERT_ID function?" of Andrew Sherman: with autogenerated default approach(after we will add this functionality) I guess we will send you back this generated id from insert call, so you can do second select yourself
20 replies
DTDrizzle Team
Created by Kai Revona on 1/7/2024 in #help
Relation many record MySQL
How to create many record using forEach in array and relrelation it to parent table in MySQL like Prisma
await ctx.prisma.post.create({
        data: {
          content: input.content,
          imageUrl: input.imageUrl,
          authorId: userId,
          hashtags: {
            connectOrCreate: input.tags?.map((tag) => ({
              where: { name: tag },
              create: { name: tag },
            })),
          },
        },
      })
await ctx.prisma.post.create({
        data: {
          content: input.content,
          imageUrl: input.imageUrl,
          authorId: userId,
          hashtags: {
            connectOrCreate: input.tags?.map((tag) => ({
              where: { name: tag },
              create: { name: tag },
            })),
          },
        },
      })
14 replies
DTDrizzle Team
Created by Kai Revona on 12/17/2023 in #help
MySQL Relational CRUD
I am the own of issue #1395, it was closed but i don't know way to follow this suggestion: MySql doesn't support returning() you have to do it in a migration and send 2 different queries. Please create a help post in discord or a question in a discussion if you need further help.
16 replies