Prisma implementation...
This commit is contained in:
15
backend/prisma/prisma.service.ts
Normal file
15
backend/prisma/prisma.service.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService extends PrismaClient implements OnModuleInit {
|
||||
async onModuleInit() {
|
||||
await this.$connect();
|
||||
}
|
||||
|
||||
async enableShutdownHooks(app: INestApplication) {
|
||||
this.$on('beforeExit', async () => {
|
||||
await app.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { AuthModule } from './auth/auth.module';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { GraphQLISODateTime, GraphQLModule } from '@nestjs/graphql';
|
||||
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
|
||||
import { ApolloServerPluginLandingPageLocalDefault } from 'apollo-server-core';
|
||||
// import { ApolloServerPluginLandingPageLocalDefault } from 'apollo-server-core';
|
||||
import { UsersModule } from './users/users.module';
|
||||
|
||||
@Module({
|
||||
|
||||
@@ -20,15 +20,15 @@ export class User {
|
||||
}
|
||||
|
||||
export abstract class IQuery {
|
||||
abstract users(): Nullable<User>[] | Promise<Nullable<User>[]>;
|
||||
abstract users(): Nullable<Nullable<User>[]> | Promise<Nullable<Nullable<User>[]>>;
|
||||
|
||||
abstract user(id: number): Nullable<User> | Promise<Nullable<User>>;
|
||||
abstract user(id: string): Nullable<User> | Promise<Nullable<User>>;
|
||||
}
|
||||
|
||||
export abstract class IMutation {
|
||||
abstract createUser(createUserInput: CreateUserInput): User | Promise<User>;
|
||||
|
||||
abstract removeUser(id: number): Nullable<User> | Promise<Nullable<User>>;
|
||||
abstract removeUser(id: string): Nullable<User> | Promise<Nullable<User>>;
|
||||
}
|
||||
|
||||
export type DateTime = any;
|
||||
|
||||
@@ -2,6 +2,7 @@ import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
import supertokens from 'supertokens-node';
|
||||
import { SupertokensExceptionFilter } from './auth/filters/auth.filter';
|
||||
import { PrismaService } from 'prisma/prisma.service';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
@@ -11,6 +12,8 @@ async function bootstrap() {
|
||||
credentials: true,
|
||||
});
|
||||
app.useGlobalFilters(new SupertokensExceptionFilter());
|
||||
const prismaService = app.get(PrismaService);
|
||||
await prismaService.enableShutdownHooks(app);
|
||||
|
||||
await app.listen(process.env.APP_PORT);
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
export class CreateUserInput {}
|
||||
@@ -1,6 +0,0 @@
|
||||
import { CreateUserInput } from './create-user.input';
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
|
||||
export class UpdateUserInput extends PartialType(CreateUserInput) {
|
||||
id: number;
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export class User {}
|
||||
@@ -12,16 +12,16 @@ input CreateUserInput {
|
||||
}
|
||||
|
||||
# input UpdateUserInput {
|
||||
# id: Int!
|
||||
# id: String!
|
||||
# }
|
||||
|
||||
type Query {
|
||||
users: [User]!
|
||||
user(id: Int!): User
|
||||
users: [User]
|
||||
user(id: String!): User
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
createUser(createUserInput: CreateUserInput!): User!
|
||||
# updateUser(updateUserInput: UpdateUserInput!): User!
|
||||
removeUser(id: Int!): User
|
||||
removeUser(id: String!): User
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UsersService } from './users.service';
|
||||
import { UsersResolver } from './users.resolver';
|
||||
import { PrismaService } from 'prisma/prisma.service';
|
||||
|
||||
@Module({
|
||||
providers: [UsersResolver, UsersService]
|
||||
providers: [PrismaService, UsersResolver, UsersService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
|
||||
@@ -1,25 +1,23 @@
|
||||
import { Resolver, Query, Mutation, Args } from '@nestjs/graphql';
|
||||
import { CreateUserInput } from 'src/graphql/graphql.typings';
|
||||
import { UsersService } from './users.service';
|
||||
import { CreateUserInput } from './dto/create-user.input';
|
||||
import { UpdateUserInput } from './dto/update-user.input';
|
||||
|
||||
@Resolver('User')
|
||||
export class UsersResolver {
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
|
||||
@Mutation('createUser')
|
||||
create(@Args('createUserInput') createUserInput: CreateUserInput) {
|
||||
return this.usersService.create(createUserInput);
|
||||
}
|
||||
|
||||
// create(@Args('createUserInput') createUserInput: CreateUserInput) {
|
||||
// return this.usersService.create(createUserInput);
|
||||
// }
|
||||
@Query('users')
|
||||
findAll() {
|
||||
return this.usersService.findAll();
|
||||
return this.usersService.users({});
|
||||
}
|
||||
|
||||
@Query('user')
|
||||
findOne(@Args('id') id: number) {
|
||||
return this.usersService.findOne(id);
|
||||
findOne(@Args('id') id: string) {
|
||||
return this.usersService.user({ id });
|
||||
}
|
||||
|
||||
// @Mutation('updateUser')
|
||||
@@ -27,8 +25,8 @@ export class UsersResolver {
|
||||
// return this.usersService.update(updateUserInput.id, updateUserInput);
|
||||
// }
|
||||
|
||||
@Mutation('removeUser')
|
||||
remove(@Args('id') id: number) {
|
||||
return this.usersService.remove(id);
|
||||
}
|
||||
// @Mutation('removeUser')
|
||||
// remove(@Args('id') id: string) {
|
||||
// return this.usersService.remove(id);
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,26 +1,43 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { CreateUserInput } from './dto/create-user.input';
|
||||
import { UpdateUserInput } from './dto/update-user.input';
|
||||
import { CreateUserInput } from 'src/graphql/graphql.typings';
|
||||
import { Prisma, User } from '@prisma/client';
|
||||
import { PrismaService } from 'prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
create(createUserInput: CreateUserInput) {
|
||||
return 'This action adds a new user';
|
||||
constructor(private readonly prismaService: PrismaService) {}
|
||||
|
||||
async user(uniqueInput: Prisma.UserWhereUniqueInput) {
|
||||
return await this.prismaService.user.findUnique({ where: uniqueInput });
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return `This action returns all users`;
|
||||
async users(params: {
|
||||
skip?: number;
|
||||
take?: number;
|
||||
cursor?: Prisma.UserWhereUniqueInput;
|
||||
where?: Prisma.UserWhereInput;
|
||||
orderBy?: Prisma.UserOrderByWithRelationInput;
|
||||
}) {
|
||||
return await this.prismaService.user.findMany(params);
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
return `This action returns a #${id} user`;
|
||||
}
|
||||
// create(createUserInput: CreateUserInput) {
|
||||
// return 'This action adds a new user';
|
||||
// }
|
||||
|
||||
// update(id: number, updateUserInput: UpdateUserInput) {
|
||||
// findAll() {
|
||||
// return `This action returns all users`;
|
||||
// }
|
||||
|
||||
// findOne(id: string) {
|
||||
// return `This action returns a #${id} user`;
|
||||
// }
|
||||
|
||||
// update(id: string, updateUserInput: UpdateUserInput) {
|
||||
// return `This action updates a #${id} user`;
|
||||
// }
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} user`;
|
||||
}
|
||||
// remove(id: string) {
|
||||
// return `This action removes a #${id} user`;
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user