Created
May 24, 2021 15:52
-
-
Save Romakita/5027e31bfaf3981ead17030aeb09bf69 to your computer and use it in GitHub Desktop.
Prisma - TsED UsersRepository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { isArray } from "@tsed/core"; | |
import { deserialize } from "@tsed/json-mapper"; | |
import { Injectable, Inject } from "@tsed/di"; | |
import { PrismaService } from "../services/PrismaService"; | |
import { Prisma, User } from "../client"; | |
import { UserModel } from "../models"; | |
@Injectable() | |
export class UsersRepository { | |
@Inject() | |
protected prisma: PrismaService; | |
get collection() { | |
return this.prisma.user | |
} | |
get groupBy() { | |
return this.collection.groupBy.bind(this.collection) | |
} | |
protected deserialize<T>(obj: null | User | User[]): T { | |
return deserialize<T>(obj, { type: UserModel, collectionType: isArray(obj) ? Array : undefined }) | |
} | |
async findUnique(args: Prisma.UserFindUniqueArgs): Promise<UserModel | null> { | |
const obj = await this.collection.findUnique(args); | |
return this.deserialize<UserModel | null>(obj); | |
} | |
async findFirst(args: Prisma.UserFindFirstArgs): Promise<UserModel | null> { | |
const obj = await this.collection.findFirst(args); | |
return this.deserialize<UserModel | null>(obj); | |
} | |
async findMany(args?: Prisma.UserFindManyArgs): Promise<UserModel[]> { | |
const obj = await this.collection.findMany(args); | |
return this.deserialize<UserModel[]>(obj); | |
} | |
// etc ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment