-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
19 changed files
with
1,033 additions
and
102 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DATABASE_URL="postgresql://mk:mk@localhost:5432/postgres?schema=public" |
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 @@ | ||
|
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 @@ | ||
|
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 @@ | ||
|
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
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/nest-cli", | ||
"collection": "@nestjs/schematics", | ||
"sourceRoot": "src" | ||
"sourceRoot": "src", | ||
"compilerOptions": { | ||
"plugins": ["@nestjs/graphql"] | ||
} | ||
} |
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,11 @@ | ||
-- CreateTable | ||
CREATE TABLE "Users" ( | ||
"id" SERIAL NOT NULL, | ||
"first_name" TEXT NOT NULL, | ||
"last_name" TEXT NOT NULL, | ||
"gender" TEXT, | ||
"created_at" TEXT NOT NULL, | ||
"updated_at" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Users_pkey" PRIMARY KEY ("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,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "postgresql" |
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 @@ | ||
// This is your Prisma schema file, | ||
// learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model Users { | ||
id Int @default(autoincrement()) @id | ||
first_name String | ||
last_name String | ||
gender String? | ||
created_at String | ||
updated_at String | ||
} |
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,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() | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { Module } from '@nestjs/common' | ||
import { UsersService } from './users.service' | ||
import { UsersResolver } from './users.resolver' | ||
import { PrismaService } from 'src/prisma.service' | ||
|
||
@Module({ | ||
providers: [UsersService, UsersResolver] | ||
providers: [UsersService, UsersResolver, PrismaService] | ||
}) | ||
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 |
---|---|---|
@@ -1,18 +1,23 @@ | ||
import { Args, ID, Query, Resolver } from '@nestjs/graphql' | ||
import { Args, ID, Mutation, Query, Resolver } from '@nestjs/graphql' | ||
import { Users } from './users.models' | ||
import { UsersService } from './users.service' | ||
|
||
@Resolver() | ||
@Resolver((of) => Users) | ||
export class UsersResolver { | ||
constructor(private usersService: UsersService) {} | ||
|
||
@Query(() => [Users], { nullable: 'items' }) | ||
findAll() { | ||
@Query(() => [Users], { nullable: true }) | ||
users() { | ||
return this.usersService.findAll() | ||
} | ||
|
||
@Query(() => Users) | ||
findOneById(@Args('id', { type: () => ID }) id: string) { | ||
@Query(() => Users, { nullable: true }) | ||
user(@Args('id', { type: () => ID }) id: string) { | ||
return this.usersService.findOneById(id) | ||
} | ||
|
||
@Mutation(() => Users, { nullable: true }) | ||
async createUser(@Args('firstName') firstName: string, @Args('lastName') lastName: string) { | ||
return this.usersService.create(firstName, lastName) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,20 +1,20 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common' | ||
import { Status, Users } from './users.models' | ||
import { Injectable } from '@nestjs/common' | ||
import dayjs from 'dayjs' | ||
import { PrismaService } from 'src/prisma.service' | ||
|
||
@Injectable() | ||
export class UsersService { | ||
private users: Users[] = [ | ||
{ id: '1', gender: '', firstName: 'aaa', lastName: 'bbb', status: Status.ACTIVE, createdAt: new Date(), updatedAt: new Date() }, | ||
{ id: '2', gender: '', firstName: 'aaa2', lastName: 'bbb2', status: Status.ACTIVE, createdAt: new Date(), updatedAt: new Date() } | ||
] | ||
constructor(private prisma: PrismaService) {} | ||
|
||
findAll(): Users[] { | ||
return this.users | ||
async findAll() { | ||
return await this.prisma.users.findMany() | ||
} | ||
|
||
findOneById(id: string): Users { | ||
const result = this.users.find((todo) => id === todo.id) | ||
if (!result) throw new NotFoundException() | ||
return result | ||
async findOneById(id: string) { | ||
return await this.prisma.users.findUnique({ where: { id: Number(id) } }) | ||
} | ||
|
||
async create(firstName: string, lastName: string) { | ||
return this.prisma.users.create({ data: { first_name: firstName, last_name: lastName, created_at: dayjs().toString(), updated_at: dayjs().toString() } }) | ||
} | ||
} |
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
Oops, something went wrong.