feat: added prisma for the auth service

feature/auth
Artem-Darius Weber 2 years ago
parent 3d0d30a24f
commit 24885340d9

@ -0,0 +1,27 @@
// 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 {
uuid String @id @default(uuid()) @db.Uuid
isBlocked Boolean @default(false)
devices UserDevice[] @relation("userDevice")
}
model UserDevice {
fingerprint Bytes @id @db.ByteA
name String @db.VarChar(100)
userUuid String @db.Uuid
isBlocked Boolean @default(false)
user User @relation("userDevice", fields: [userUuid], references: [uuid])
}

@ -0,0 +1 @@
export class RecordNotFoundError extends Error {}

@ -0,0 +1,26 @@
import {
CallHandler,
ConflictException,
ExecutionContext,
Injectable,
NestInterceptor,
NotFoundException
} from "@nestjs/common";
import { catchError, Observable } from "rxjs";
import { RecordNotFoundError } from "../error/record-not-found.error";
import { UniqueConstraintFailedError } from "../error/unique-constraint-failed.error";
@Injectable()
export class PrismaErrorInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next
.handle()
.pipe(
catchError(err => {
if (err instanceof RecordNotFoundError) throw new NotFoundException(err.message);
if (err instanceof UniqueConstraintFailedError) throw new ConflictException(err.message);
throw err;
})
);
}
}

@ -0,0 +1,10 @@
import { Global, Module } from "@nestjs/common";
import { PrismaService } from "./service/prisma.service";
@Global()
@Module({
providers: [PrismaService],
exports: [PrismaService]
})
export class PrismaModule {
}

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from './prisma.service';
describe('PrismaService', () => {
let service: PrismaService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PrismaService],
}).compile();
service = module.get<PrismaService>(PrismaService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

@ -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();
});
}
}
Loading…
Cancel
Save