#How to make includes dry?

5 messages · Page 1 of 1 (latest)

leaden hemlock
#

Hey! I would like to know how to not repeat the includes in prisma queries.

thanks. 😉

leaden hemlock
#

look here:

export default class UserRepository {
  static async findById(id: string): Promise<User> {
    return await database.user.findUnique({
      where: { id },
      include: {
        blogs: {
          orderBy: { slug: 'asc' }
        },
        posts: {
          orderBy: { publishedAt: 'desc' }
        },
        comments: {
          orderBy: { createdAt: 'asc' }
        }
      }
    })
  }
  static async findByBlogId(blogId: string) {}
  static async findByPostId(postId: string) {}
  static async findByCommentId(commentId: string) {}
}
#

Heyho!

#

I've got it.

#
const include: UserInclude = {
  blogs: {
    orderBy: { slug: 'asc' }
  },
  posts: {
    orderBy: { publishedAt: 'desc' }
  },
  comments: {
    orderBy: { createdAt: 'asc' }
  }
}