Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add Prisma CRUD endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 22, 2020
1 parent cef43d1 commit d6beaf8
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaModule } from './modules/prisma/prisma.module';
import { UsersModule } from './modules/user/user.module';

@Module({
imports: [],
imports: [UsersModule, PrismaModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
1 change: 1 addition & 0 deletions src/modules/prisma/prisma.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type OmitSecrets<T> = Omit<Omit<T, 'password'>, 'twoFactorSecret'>;
8 changes: 8 additions & 0 deletions src/modules/prisma/prisma.module.ts
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 {}
20 changes: 20 additions & 0 deletions src/modules/prisma/prisma.service.ts
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;
}
}
19 changes: 19 additions & 0 deletions src/modules/user/user.controller.ts
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) });
// }
}
11 changes: 11 additions & 0 deletions src/modules/user/user.module.ts
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 {}
68 changes: 68 additions & 0 deletions src/modules/user/user.service.ts
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);
}
}

0 comments on commit d6beaf8

Please sign in to comment.