-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add prisma & user module
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 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,29 @@ | ||
// 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 User { | ||
id Int @default(autoincrement()) @id | ||
email String @unique | ||
phone String @unique | ||
name String? | ||
posts Post[] | ||
} | ||
|
||
model Post { | ||
id Int @default(autoincrement()) @id | ||
title String | ||
content String? | ||
published Boolean? @default(false) | ||
author User? @relation(fields: [authorId], references: [id]) | ||
authorId Int? | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common' | ||
import { PrismaService } from 'src/prisma.service' | ||
import { UsersService } from './users.service' | ||
|
||
@Module({ | ||
providers: [UsersService, PrismaService], | ||
exports: [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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { UsersService } from './users.service' | ||
|
||
describe('UsersService', () => { | ||
let service: UsersService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [UsersService], | ||
}).compile() | ||
|
||
service = module.get<UsersService>(UsersService) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
}) | ||
}) |
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,48 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { Prisma, User } from '@prisma/client' | ||
import { PrismaService } from '../prisma.service' | ||
|
||
@Injectable() | ||
export class UsersService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async user(input: Prisma.UserWhereUniqueInput) { | ||
return this.prisma.user.findUnique({ | ||
where: input, | ||
}) | ||
} | ||
|
||
async users(params: { | ||
skip?: number | ||
take?: number | ||
cursor?: Prisma.UserWhereUniqueInput | ||
where?: Prisma.UserWhereInput | ||
orderBy?: Prisma.UserOrderByWithRelationInput | ||
}): Promise<User[]> { | ||
const { skip, take, cursor, where, orderBy } = params | ||
return this.prisma.user.findMany({ | ||
skip, | ||
take, | ||
cursor, | ||
where, | ||
orderBy, | ||
}) | ||
} | ||
|
||
async updateUser(params: { | ||
where: Prisma.UserWhereUniqueInput | ||
data: Prisma.UserUpdateInput | ||
}) { | ||
const { where, data } = params | ||
return this.prisma.user.update({ | ||
data, | ||
where, | ||
}) | ||
} | ||
|
||
async deleteUser(where: Prisma.UserWhereUniqueInput) { | ||
return this.prisma.user.delete({ | ||
where, | ||
}) | ||
} | ||
} |