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