This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cef43d1
commit d6beaf8
Showing
7 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type OmitSecrets<T> = Omit<Omit<T, 'password'>, 'twoFactorSecret'>; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PrismaService } from './prisma.service'; | ||
|
||
@Module({ | ||
providers: [PrismaService], | ||
exports: [PrismaService], | ||
}) | ||
export class PrismaModule {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common'; | ||
import { PrismaClient } from '@prisma/client'; | ||
import { OmitSecrets } from 'src/modules/prisma/prisma.interface'; | ||
|
||
@Injectable() | ||
export class PrismaService extends PrismaClient | ||
implements OnModuleInit, OnModuleDestroy { | ||
async onModuleInit() { | ||
await this.$connect(); | ||
} | ||
|
||
async onModuleDestroy() { | ||
await this.$disconnect(); | ||
} | ||
|
||
expose<T>(item: T): OmitSecrets<T> { | ||
if (!item) return null; | ||
return item; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { users } from '@prisma/client'; | ||
import { OmitSecrets } from 'src/modules/prisma/prisma.interface'; | ||
import { UsersService } from './user.service'; | ||
|
||
@Controller('users') | ||
export class UserController { | ||
constructor(private usersService: UsersService) {} | ||
|
||
@Get() | ||
async getAll(): Promise<OmitSecrets<users>[]> { | ||
return this.usersService.users({}); | ||
} | ||
|
||
// @Get('post/:id') | ||
// async getPostById(@Param('id') id: string): Promise<PostModel> { | ||
// return this.postService.post({ id: Number(id) }); | ||
// } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { PrismaModule } from '../prisma/prisma.module'; | ||
import { UserController } from './user.controller'; | ||
import { UsersService } from './user.service'; | ||
|
||
@Module({ | ||
imports: [PrismaModule], | ||
controllers: [UserController], | ||
providers: [UsersService], | ||
}) | ||
export class UsersModule {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { | ||
usersUpdateInput, | ||
users, | ||
usersCreateInput, | ||
usersWhereUniqueInput, | ||
usersWhereInput, | ||
usersOrderByInput, | ||
} from '@prisma/client'; | ||
import { OmitSecrets } from 'src/modules/prisma/prisma.interface'; | ||
|
||
@Injectable() | ||
export class UsersService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async user( | ||
userWhereUniqueInput: usersWhereUniqueInput, | ||
): Promise<OmitSecrets<users> | null> { | ||
const user = await this.prisma.users.findOne({ | ||
where: userWhereUniqueInput, | ||
}); | ||
return this.prisma.expose<users>(user); | ||
} | ||
|
||
async users(params: { | ||
skip?: number; | ||
take?: number; | ||
cursor?: usersWhereUniqueInput; | ||
where?: usersWhereInput; | ||
orderBy?: usersOrderByInput; | ||
}): Promise<OmitSecrets<users>[]> { | ||
const { skip, take, cursor, where, orderBy } = params; | ||
const users = await this.prisma.users.findMany({ | ||
skip, | ||
take, | ||
cursor, | ||
where, | ||
orderBy, | ||
}); | ||
return users.map(user => this.prisma.expose<users>(user)); | ||
} | ||
|
||
async createUser(data: usersCreateInput): Promise<users> { | ||
return this.prisma.users.create({ | ||
data, | ||
}); | ||
} | ||
|
||
async updateUser(params: { | ||
where: usersWhereUniqueInput; | ||
data: usersUpdateInput; | ||
}): Promise<OmitSecrets<users>> { | ||
const { where, data } = params; | ||
const user = await this.prisma.users.update({ | ||
data, | ||
where, | ||
}); | ||
return this.prisma.expose<users>(user); | ||
} | ||
|
||
async deleteUser(where: usersWhereUniqueInput): Promise<OmitSecrets<users>> { | ||
const user = await this.prisma.users.delete({ | ||
where, | ||
}); | ||
return this.prisma.expose<users>(user); | ||
} | ||
} |