Skip to content

Commit

Permalink
feat(server): add prisma & user module
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Nov 17, 2022
1 parent 7ca4807 commit fca199c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
29 changes: 29 additions & 0 deletions server/prisma/schema.prisma
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?
}
15 changes: 15 additions & 0 deletions server/src/prisma.service.ts
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()
})
}
}
9 changes: 9 additions & 0 deletions server/src/users/users.module.ts
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 {}
18 changes: 18 additions & 0 deletions server/src/users/users.service.spec.ts
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()
})
})
48 changes: 48 additions & 0 deletions server/src/users/users.service.ts
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,
})
}
}

0 comments on commit fca199c

Please sign in to comment.