Skip to content

Instantly share code, notes, and snippets.

@Romakita
Created May 24, 2021 15:52
Show Gist options
  • Save Romakita/5027e31bfaf3981ead17030aeb09bf69 to your computer and use it in GitHub Desktop.
Save Romakita/5027e31bfaf3981ead17030aeb09bf69 to your computer and use it in GitHub Desktop.
Prisma - TsED UsersRepository
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