From de8fd0e6d12e3833ffd280388e38bcd2f811cf7b Mon Sep 17 00:00:00 2001 From: Artem-Darius Atlas Date: Wed, 26 Jul 2023 09:41:35 +0300 Subject: [PATCH 1/2] docs: fix backend prisma generate types command in the README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 986abd2..c8b0448 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ Just run `nx build demoapp` to build the application. The build artifacts will b # Backend ## Prisma Types ```bash -nx generate @nx/plugin:generator prisma-generator --project=prisma-generator +nx g prisma-generator ``` # Frontend From 08da8f8247056e211dcf24b42e9c03fe287ccbba Mon Sep 17 00:00:00 2001 From: Artem-Darius Atlas Date: Wed, 26 Jul 2023 10:38:03 +0300 Subject: [PATCH 2/2] feat: created and register new prisma client for user-data service --- .env | 1 + README.md | 16 ++++++++++++ apps/user-data/.env.example | 8 ------ apps/user-data/prisma/.gitignore | 1 - apps/user-data/serving/docker-compose.yaml | 12 +-------- apps/user-data/src/email/email.controller.ts | 16 ++++++------ .../src/email/service/email.service.ts | 20 ++++++++------ .../phone-number/phone-number.controller.ts | 20 ++++++++------ .../service/phone-number.service.ts | 19 ++++++++------ .../src/prisma/service/prisma.service.ts | 5 ++-- .../src/user/service/user.service.ts | 19 ++++++++------ apps/user-data/src/user/user.controller.ts | 19 ++++++++------ libs/prisma-clients/index.ts | 1 + libs/prisma-clients/user-data/.gitignore | 3 +++ libs/prisma-clients/user-data/index.ts | 2 ++ .../prisma/error/record-not-found.error.ts | 1 + .../error/unique-constraint-failed.error.ts | 1 + .../interceptor/prisma-error.interceptor.ts | 26 +++++++++++++++++++ .../user-data/prisma/schema.prisma | 12 ++++++--- tsconfig.base.json | 1 + 20 files changed, 129 insertions(+), 74 deletions(-) delete mode 100644 apps/user-data/.env.example delete mode 100644 apps/user-data/prisma/.gitignore create mode 100644 libs/prisma-clients/user-data/.gitignore create mode 100644 libs/prisma-clients/user-data/index.ts create mode 100644 libs/prisma-clients/user-data/prisma/error/record-not-found.error.ts create mode 100644 libs/prisma-clients/user-data/prisma/error/unique-constraint-failed.error.ts create mode 100644 libs/prisma-clients/user-data/prisma/interceptor/prisma-error.interceptor.ts rename {apps => libs/prisma-clients}/user-data/prisma/schema.prisma (89%) diff --git a/.env b/.env index d0b9db4..4f37bbe 100644 --- a/.env +++ b/.env @@ -6,3 +6,4 @@ POSTGRES_DB=root AUTH_SOURCE_URL=postgresql://postgres:postgres@localhost:5444/user-authentication PUBLIC_KEY_PATH=tokens/jwt.key.pub PRIVATE_KEY_PATH=tokens/jwt.key +USER_DATA_SOURCE_URL=postgresql://postgres:postgres@localhost:5445/sc-user-data diff --git a/README.md b/README.md index c8b0448..c85b3ea 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,14 @@ copy .env.example .env cd apps/auth/serving && copy database.env.example database.env && sudo docker-compose up -d ``` +```bash +cd apps/auth/serving && copy database.env.example database.env && sudo docker-compose up -d && cd ../../../ +``` + +```bash +cd apps/user-data/serving && copy database.env.example database.env && sudo docker-compose up -d && cd ../../../ +``` +auth ```bash prisma generate --schema="./libs/prisma-clients/auth/prisma/schema.prisma" ``` @@ -52,6 +60,14 @@ prisma generate --schema="./libs/prisma-clients/auth/prisma/schema.prisma" ```bash prisma db push --schema="./libs/prisma-clients/auth/prisma/schema.prisma" ``` +user data + +```bash +prisma generate --schema="./libs/prisma-clients/user-data/prisma/schema.prisma" +``` +```bash +prisma db push --schema="./libs/prisma-clients/user-data/prisma/schema.prisma" +``` Run all services: ```bash diff --git a/apps/user-data/.env.example b/apps/user-data/.env.example deleted file mode 100644 index 6c89035..0000000 --- a/apps/user-data/.env.example +++ /dev/null @@ -1,8 +0,0 @@ -# Environment variables declared in this file are automatically made available to Prisma. -# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema - -# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. -# See the documentation for all the connection string options: https://pris.ly/d/connection-strings - -DATABASE_URL=postgresql://postgres:postgres@sc-user-data-database/sc-user-data -PORT=3002 diff --git a/apps/user-data/prisma/.gitignore b/apps/user-data/prisma/.gitignore deleted file mode 100644 index 24a8e4d..0000000 --- a/apps/user-data/prisma/.gitignore +++ /dev/null @@ -1 +0,0 @@ -migrations diff --git a/apps/user-data/serving/docker-compose.yaml b/apps/user-data/serving/docker-compose.yaml index 0e8a651..ab5ad40 100644 --- a/apps/user-data/serving/docker-compose.yaml +++ b/apps/user-data/serving/docker-compose.yaml @@ -11,18 +11,8 @@ services: - user-data-database:/var/lib/postgresql env_file: - ./database.env - user-data-database-admin: - container_name: user-data-database-admin - image: bitnami/phppgadmin:latest - restart: always - networks: - - user-data - depends_on: - - user-data-database - environment: - - DATABASE_HOST=user-data-database ports: - - "8083:8080" + - 5445:5432 networks: user-data: diff --git a/apps/user-data/src/email/email.controller.ts b/apps/user-data/src/email/email.controller.ts index f4dade4..4cdddb4 100644 --- a/apps/user-data/src/email/email.controller.ts +++ b/apps/user-data/src/email/email.controller.ts @@ -1,5 +1,5 @@ import { Body, Controller, HttpCode, Inject, Post, UseInterceptors } from "@nestjs/common"; -import { Prisma, Email } from "@prisma/client"; +import { EmailFindUniqueArgs, EmailFindFirstArgs, EmailFindManyArgs, EmailUpdateArgs, EmailCountArgs, EmailCreateArgs, EmailDeleteArgs, Email } from "@platform/prisma-clients/user-data"; import { EmailService } from "./service/email.service"; import { PrismaErrorInterceptor } from "../prisma/interceptor/prisma-error.interceptor"; @@ -11,43 +11,43 @@ export class EmailController { @Post("find/unique") @HttpCode(200) - async FindUnique(@Body() emailFindUniqueArgs: Prisma.EmailFindUniqueArgs): Promise | null> { + async FindUnique(@Body() emailFindUniqueArgs: EmailFindUniqueArgs): Promise | null> { return await this.email.FindUnique(emailFindUniqueArgs); } @Post("find/first") @HttpCode(200) - async FindFirst(@Body() emailFindFirstArgs: Prisma.EmailFindFirstArgs): Promise | null> { + async FindFirst(@Body() emailFindFirstArgs: EmailFindFirstArgs): Promise | null> { return await this.email.FindFirst(emailFindFirstArgs); } @Post("find/many") @HttpCode(200) - async FindMany(@Body() emailFindManyArgs: Prisma.EmailFindManyArgs): Promise[]> { + async FindMany(@Body() emailFindManyArgs: EmailFindManyArgs): Promise[]> { return await this.email.FindMany(emailFindManyArgs); } @Post("update") @HttpCode(200) - async Update(@Body() emailUpdateArgs: Prisma.EmailUpdateArgs): Promise> { + async Update(@Body() emailUpdateArgs: EmailUpdateArgs): Promise> { return await this.email.Update(emailUpdateArgs); } @Post("count") @HttpCode(200) - async Count(@Body() emailCountArgs: Prisma.EmailCountArgs): Promise { + async Count(@Body() emailCountArgs: EmailCountArgs): Promise { return await this.email.Count(emailCountArgs); } @Post("create") @HttpCode(200) - async Create(@Body() emailCreateArgs: Prisma.EmailCreateArgs): Promise> { + async Create(@Body() emailCreateArgs: EmailCreateArgs): Promise> { return await this.email.Create(emailCreateArgs); } @Post("delete") @HttpCode(200) - async Delete(@Body() emailDeleteArgs: Prisma.EmailDeleteArgs): Promise> { + async Delete(@Body() emailDeleteArgs: EmailDeleteArgs): Promise> { return await this.email.Delete(emailDeleteArgs); } } diff --git a/apps/user-data/src/email/service/email.service.ts b/apps/user-data/src/email/service/email.service.ts index b64799c..6bf6711 100644 --- a/apps/user-data/src/email/service/email.service.ts +++ b/apps/user-data/src/email/service/email.service.ts @@ -1,6 +1,10 @@ import { Inject, Injectable } from "@nestjs/common"; import { PrismaService } from '../../prisma/service/prisma.service'; -import { Email, Prisma } from "@prisma/client"; +import { UserDataClient as Prisma } from "@platform/prisma-clients"; +import { Email, EmailFindUniqueArgs, + EmailFindFirstArgs, EmailFindManyArgs, EmailUpdateArgs, + EmailCountArgs, EmailCreateArgs, EmailDeleteArgs + } from "@platform/prisma-clients/user-data"; import { PrismaClientKnownRequestError } from "@prisma/client/runtime"; import { UniqueConstraintFailedError } from "../../prisma/error/unique-constraint-failed.error"; import { RecordNotFoundError } from "../../prisma/error/record-not-found.error"; @@ -10,19 +14,19 @@ export class EmailService { constructor(@Inject(PrismaService) private prisma: PrismaService) { } - async FindUnique(emailFindUniqueArgs: Prisma.EmailFindUniqueArgs): Promise | null> { + async FindUnique(emailFindUniqueArgs: EmailFindUniqueArgs): Promise | null> { return await this.prisma.email.findUnique(emailFindUniqueArgs); } - async FindFirst(emailFindFirstArgs: Prisma.EmailFindFirstArgs): Promise | null> { + async FindFirst(emailFindFirstArgs: EmailFindFirstArgs): Promise | null> { return await this.prisma.email.findFirst(emailFindFirstArgs); } - async FindMany(emailFindManyArgs: Prisma.EmailFindManyArgs): Promise[]> { + async FindMany(emailFindManyArgs: EmailFindManyArgs): Promise[]> { return await this.prisma.email.findMany(emailFindManyArgs); } - async Update(emailUpdateArgs: Prisma.EmailUpdateArgs): Promise> { + async Update(emailUpdateArgs: EmailUpdateArgs): Promise> { try { return await this.prisma.email.update(emailUpdateArgs); } catch (e) { @@ -34,11 +38,11 @@ export class EmailService { } } - async Count(emailCountArgs: Prisma.EmailCountArgs): Promise { + async Count(emailCountArgs: EmailCountArgs): Promise { return await this.prisma.email.count(emailCountArgs); } - async Create(emailCreateArgs: Prisma.EmailCreateArgs): Promise> { + async Create(emailCreateArgs: EmailCreateArgs): Promise> { try { return await this.prisma.email.create(emailCreateArgs); } catch (e) { @@ -49,7 +53,7 @@ export class EmailService { } } - async Delete(emailDeleteArgs: Prisma.EmailDeleteArgs): Promise> { + async Delete(emailDeleteArgs: EmailDeleteArgs): Promise> { try { return await this.prisma.email.delete(emailDeleteArgs); } catch (e) { diff --git a/apps/user-data/src/phone-number/phone-number.controller.ts b/apps/user-data/src/phone-number/phone-number.controller.ts index a4fae62..185d5a2 100644 --- a/apps/user-data/src/phone-number/phone-number.controller.ts +++ b/apps/user-data/src/phone-number/phone-number.controller.ts @@ -1,5 +1,9 @@ import { Body, Controller, HttpCode, Inject, Post, UseInterceptors } from "@nestjs/common"; -import { PhoneNumber, Prisma } from "@prisma/client"; +import { UserDataClient as Prisma } from "@platform/prisma-clients"; +import { PhoneNumber, PhoneNumberFindUniqueArgs, + PhoneNumberFindFirstArgs, PhoneNumberFindManyArgs, PhoneNumberUpdateArgs, + PhoneNumberCountArgs, PhoneNumberCreateArgs, PhoneNumberDeleteArgs +} from "@platform/prisma-clients/user-data"; import { PhoneNumberService } from "./service/phone-number.service"; import { PrismaErrorInterceptor } from "../prisma/interceptor/prisma-error.interceptor"; @@ -11,43 +15,43 @@ export class PhoneNumberController { @Post("find/unique") @HttpCode(200) - async FindUnique(@Body() phoneNumberFindUniqueArgs: Prisma.PhoneNumberFindUniqueArgs): Promise | null> { + async FindUnique(@Body() phoneNumberFindUniqueArgs: PhoneNumberFindUniqueArgs): Promise | null> { return await this.phoneNumber.FindUnique(phoneNumberFindUniqueArgs); } @Post("find/first") @HttpCode(200) - async FindFirst(@Body() phoneNumberFindFirstArgs: Prisma.PhoneNumberFindFirstArgs): Promise | null> { + async FindFirst(@Body() phoneNumberFindFirstArgs: PhoneNumberFindFirstArgs): Promise | null> { return await this.phoneNumber.FindFirst(phoneNumberFindFirstArgs); } @Post("find/many") @HttpCode(200) - async FindMany(@Body() phoneNumberFindManyArgs: Prisma.PhoneNumberFindManyArgs): Promise[]> { + async FindMany(@Body() phoneNumberFindManyArgs: PhoneNumberFindManyArgs): Promise[]> { return await this.phoneNumber.FindMany(phoneNumberFindManyArgs); } @Post("update") @HttpCode(200) - async Update(@Body() phoneNumberUpdateArgs: Prisma.PhoneNumberUpdateArgs): Promise> { + async Update(@Body() phoneNumberUpdateArgs: PhoneNumberUpdateArgs): Promise> { return await this.phoneNumber.Update(phoneNumberUpdateArgs); } @Post("count") @HttpCode(200) - async Count(@Body() phoneNumberCountArgs: Prisma.PhoneNumberCountArgs): Promise { + async Count(@Body() phoneNumberCountArgs: PhoneNumberCountArgs): Promise { return await this.phoneNumber.Count(phoneNumberCountArgs); } @Post("create") @HttpCode(200) - async Create(@Body() phoneNumberCreateArgs: Prisma.PhoneNumberCreateArgs): Promise> { + async Create(@Body() phoneNumberCreateArgs: PhoneNumberCreateArgs): Promise> { return await this.phoneNumber.Create(phoneNumberCreateArgs); } @Post("delete") @HttpCode(200) - async Delete(@Body() phoneNumberDeleteArgs: Prisma.PhoneNumberDeleteArgs): Promise> { + async Delete(@Body() phoneNumberDeleteArgs: PhoneNumberDeleteArgs): Promise> { return await this.phoneNumber.Delete(phoneNumberDeleteArgs); } } diff --git a/apps/user-data/src/phone-number/service/phone-number.service.ts b/apps/user-data/src/phone-number/service/phone-number.service.ts index 150bf3d..9c396f9 100644 --- a/apps/user-data/src/phone-number/service/phone-number.service.ts +++ b/apps/user-data/src/phone-number/service/phone-number.service.ts @@ -1,6 +1,9 @@ import { Inject, Injectable } from "@nestjs/common"; import { PrismaService } from "../../prisma/service/prisma.service"; -import { PhoneNumber, Prisma } from "@prisma/client"; +import { PhoneNumber, PhoneNumberFindUniqueArgs, + PhoneNumberFindFirstArgs, PhoneNumberFindManyArgs, PhoneNumberUpdateArgs, + PhoneNumberCountArgs, PhoneNumberCreateArgs, PhoneNumberDeleteArgs +} from "@platform/prisma-clients/user-data"; import { PrismaClientKnownRequestError } from "@prisma/client/runtime"; import { UniqueConstraintFailedError } from "../../prisma/error/unique-constraint-failed.error"; import { RecordNotFoundError } from "../../prisma/error/record-not-found.error"; @@ -10,19 +13,19 @@ export class PhoneNumberService { constructor(@Inject(PrismaService) private prisma: PrismaService) { } - async FindUnique(phoneNumberFindUniqueArgs: Prisma.PhoneNumberFindUniqueArgs): Promise | null> { + async FindUnique(phoneNumberFindUniqueArgs: PhoneNumberFindUniqueArgs): Promise | null> { return await this.prisma.phoneNumber.findUnique(phoneNumberFindUniqueArgs); } - async FindFirst(phoneNumberFindFirstArgs: Prisma.PhoneNumberFindFirstArgs): Promise | null> { + async FindFirst(phoneNumberFindFirstArgs: PhoneNumberFindFirstArgs): Promise | null> { return await this.prisma.phoneNumber.findFirst(phoneNumberFindFirstArgs); } - async FindMany(phoneNumberFindManyArgs: Prisma.PhoneNumberFindManyArgs): Promise[]> { + async FindMany(phoneNumberFindManyArgs: PhoneNumberFindManyArgs): Promise[]> { return await this.prisma.phoneNumber.findMany(phoneNumberFindManyArgs); } - async Update(phoneNumberUpdateArgs: Prisma.PhoneNumberUpdateArgs): Promise> { + async Update(phoneNumberUpdateArgs: PhoneNumberUpdateArgs): Promise> { try { return await this.prisma.phoneNumber.update(phoneNumberUpdateArgs); } catch (e) { @@ -34,11 +37,11 @@ export class PhoneNumberService { } } - async Count(phoneNumberCountArgs: Prisma.PhoneNumberCountArgs): Promise { + async Count(phoneNumberCountArgs: PhoneNumberCountArgs): Promise { return await this.prisma.phoneNumber.count(phoneNumberCountArgs); } - async Create(phoneNumberCreateArgs: Prisma.PhoneNumberCreateArgs): Promise> { + async Create(phoneNumberCreateArgs: PhoneNumberCreateArgs): Promise> { try { return await this.prisma.phoneNumber.create(phoneNumberCreateArgs); } catch (e) { @@ -49,7 +52,7 @@ export class PhoneNumberService { } } - async Delete(phoneNumberDeleteArgs: Prisma.PhoneNumberDeleteArgs): Promise> { + async Delete(phoneNumberDeleteArgs: PhoneNumberDeleteArgs): Promise> { try { return await this.prisma.phoneNumber.delete(phoneNumberDeleteArgs); } catch (e) { diff --git a/apps/user-data/src/prisma/service/prisma.service.ts b/apps/user-data/src/prisma/service/prisma.service.ts index aaa764a..ad01efa 100644 --- a/apps/user-data/src/prisma/service/prisma.service.ts +++ b/apps/user-data/src/prisma/service/prisma.service.ts @@ -1,8 +1,9 @@ import { INestApplication, Injectable, OnModuleInit } from "@nestjs/common"; -import { PrismaClient } from "@prisma/client"; +import {UserDataClient} from "@platform/prisma-clients"; + @Injectable() -export class PrismaService extends PrismaClient implements OnModuleInit { +export class PrismaService extends UserDataClient implements OnModuleInit { async onModuleInit() { await this.$connect(); } diff --git a/apps/user-data/src/user/service/user.service.ts b/apps/user-data/src/user/service/user.service.ts index 413d64a..05fafd3 100644 --- a/apps/user-data/src/user/service/user.service.ts +++ b/apps/user-data/src/user/service/user.service.ts @@ -1,6 +1,9 @@ import { Inject, Injectable } from "@nestjs/common"; import { PrismaService } from "../../prisma/service/prisma.service"; -import { Prisma, User } from "@prisma/client"; +import { User, UserFindUniqueArgs, + UserFindUniqueArgs, UserFindFirstArgs, UserFindManyArgs, + UserUpdateArgs, UserCountArgs, UserCreateArgs, UserDeleteArgs +} from "@platform/prisma-clients/user-data"; import { PrismaClientKnownRequestError } from "@prisma/client/runtime"; import { UniqueConstraintFailedError } from "../../prisma/error/unique-constraint-failed.error"; import { RecordNotFoundError } from "../../prisma/error/record-not-found.error"; @@ -10,19 +13,19 @@ export class UserService { constructor(@Inject(PrismaService) private prisma: PrismaService) { } - async FindUnique(userFindUniqueArgs: Prisma.UserFindUniqueArgs): Promise | null> { + async FindUnique(userFindUniqueArgs: UserFindUniqueArgs): Promise | null> { return await this.prisma.user.findUnique(userFindUniqueArgs); } - async FindFirst(userFindFirstArgs: Prisma.UserFindFirstArgs): Promise | null> { + async FindFirst(userFindFirstArgs: UserFindFirstArgs): Promise | null> { return await this.prisma.user.findFirst(userFindFirstArgs); } - async FindMany(userFindManyArgs: Prisma.UserFindManyArgs): Promise[]> { + async FindMany(userFindManyArgs: UserFindManyArgs): Promise[]> { return await this.prisma.user.findMany(userFindManyArgs); } - async Update(userUpdateArgs: Prisma.UserUpdateArgs): Promise> { + async Update(userUpdateArgs: UserUpdateArgs): Promise> { try { return await this.prisma.user.update(userUpdateArgs); } catch (e) { @@ -34,11 +37,11 @@ export class UserService { } } - async Count(userCountArgs: Prisma.UserCountArgs): Promise { + async Count(userCountArgs: UserCountArgs): Promise { return await this.prisma.user.count(userCountArgs); } - async Create(userCreateArgs: Prisma.UserCreateArgs): Promise> { + async Create(userCreateArgs: UserCreateArgs): Promise> { try { return await this.prisma.user.create(userCreateArgs); } catch (e) { @@ -49,7 +52,7 @@ export class UserService { } } - async Delete(userDeleteArgs: Prisma.UserDeleteArgs): Promise> { + async Delete(userDeleteArgs: UserDeleteArgs): Promise> { try { return await this.prisma.user.delete(userDeleteArgs); } catch (e) { diff --git a/apps/user-data/src/user/user.controller.ts b/apps/user-data/src/user/user.controller.ts index 66dbd28..110ddd0 100644 --- a/apps/user-data/src/user/user.controller.ts +++ b/apps/user-data/src/user/user.controller.ts @@ -1,5 +1,8 @@ import { Body, Controller, HttpCode, Inject, Post, UseInterceptors } from "@nestjs/common"; -import { Prisma, User } from "@prisma/client"; +import { User, UserFindUniqueArgs, + UserFindUniqueArgs, UserFindFirstArgs, UserFindManyArgs, + UserUpdateArgs, UserCountArgs, UserCreateArgs, UserDeleteArgs +} from "@platform/prisma-clients/user-data"; import { UserService } from "./service/user.service"; import { PrismaErrorInterceptor } from "../prisma/interceptor/prisma-error.interceptor"; @@ -11,43 +14,43 @@ export class UserController { @Post("find/unique") @HttpCode(200) - async FindUnique(@Body() userFindUniqueArgs: Prisma.UserFindUniqueArgs): Promise | null> { + async FindUnique(@Body() userFindUniqueArgs: UserFindUniqueArgs): Promise | null> { return await this.user.FindUnique(userFindUniqueArgs); } @Post("find/first") @HttpCode(200) - async FindFirst(@Body() userFindFirstArgs: Prisma.UserFindFirstArgs): Promise | null> { + async FindFirst(@Body() userFindFirstArgs: UserFindFirstArgs): Promise | null> { return await this.user.FindFirst(userFindFirstArgs); } @Post("find/many") @HttpCode(200) - async FindMany(@Body() userFindManyArgs: Prisma.UserFindManyArgs): Promise[]> { + async FindMany(@Body() userFindManyArgs: UserFindManyArgs): Promise[]> { return await this.user.FindMany(userFindManyArgs); } @Post("update") @HttpCode(200) - async Update(@Body() userUpdateArgs: Prisma.UserUpdateArgs): Promise> { + async Update(@Body() userUpdateArgs: UserUpdateArgs): Promise> { return await this.user.Update(userUpdateArgs); } @Post("count") @HttpCode(200) - async Count(@Body() userCountArgs: Prisma.UserCountArgs): Promise { + async Count(@Body() userCountArgs: UserCountArgs): Promise { return await this.user.Count(userCountArgs); } @Post("create") @HttpCode(200) - async Create(@Body() userCreateArgs: Prisma.UserCreateArgs): Promise> { + async Create(@Body() userCreateArgs: UserCreateArgs): Promise> { return await this.user.Create(userCreateArgs); } @Post("delete") @HttpCode(200) - async Delete(@Body() userDeleteArgs: Prisma.UserDeleteArgs): Promise> { + async Delete(@Body() userDeleteArgs: UserDeleteArgs): Promise> { return await this.user.Delete(userDeleteArgs); } } diff --git a/libs/prisma-clients/index.ts b/libs/prisma-clients/index.ts index 086aa34..c6ed4ba 100644 --- a/libs/prisma-clients/index.ts +++ b/libs/prisma-clients/index.ts @@ -1 +1,2 @@ export { AuthClient } from './auth'; +export { UserDataClient } from './user-data'; diff --git a/libs/prisma-clients/user-data/.gitignore b/libs/prisma-clients/user-data/.gitignore new file mode 100644 index 0000000..11ddd8d --- /dev/null +++ b/libs/prisma-clients/user-data/.gitignore @@ -0,0 +1,3 @@ +node_modules +# Keep environment variables out of version control +.env diff --git a/libs/prisma-clients/user-data/index.ts b/libs/prisma-clients/user-data/index.ts new file mode 100644 index 0000000..3fe0530 --- /dev/null +++ b/libs/prisma-clients/user-data/index.ts @@ -0,0 +1,2 @@ +export { PrismaClient as UserDataClient } from '.prisma/user-data-client'; +export * from '.prisma/user-data-client'; diff --git a/libs/prisma-clients/user-data/prisma/error/record-not-found.error.ts b/libs/prisma-clients/user-data/prisma/error/record-not-found.error.ts new file mode 100644 index 0000000..e1c9cb1 --- /dev/null +++ b/libs/prisma-clients/user-data/prisma/error/record-not-found.error.ts @@ -0,0 +1 @@ +export class RecordNotFoundError extends Error {} diff --git a/libs/prisma-clients/user-data/prisma/error/unique-constraint-failed.error.ts b/libs/prisma-clients/user-data/prisma/error/unique-constraint-failed.error.ts new file mode 100644 index 0000000..647b582 --- /dev/null +++ b/libs/prisma-clients/user-data/prisma/error/unique-constraint-failed.error.ts @@ -0,0 +1 @@ +export class UniqueConstraintFailedError extends Error {} diff --git a/libs/prisma-clients/user-data/prisma/interceptor/prisma-error.interceptor.ts b/libs/prisma-clients/user-data/prisma/interceptor/prisma-error.interceptor.ts new file mode 100644 index 0000000..d61d135 --- /dev/null +++ b/libs/prisma-clients/user-data/prisma/interceptor/prisma-error.interceptor.ts @@ -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 { + 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; + }) + ); + } +} diff --git a/apps/user-data/prisma/schema.prisma b/libs/prisma-clients/user-data/prisma/schema.prisma similarity index 89% rename from apps/user-data/prisma/schema.prisma rename to libs/prisma-clients/user-data/prisma/schema.prisma index 4b6ad66..114c608 100644 --- a/apps/user-data/prisma/schema.prisma +++ b/libs/prisma-clients/user-data/prisma/schema.prisma @@ -1,10 +1,14 @@ -datasource db { - provider = "postgresql" - url = env("DATABASE_URL") -} +// 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" + output = "../../../../node_modules/.prisma/user-data-client" +} + +datasource db { + provider = "postgresql" + url = env("USER_DATA_SOURCE_URL") } model User { diff --git a/tsconfig.base.json b/tsconfig.base.json index 0d2bff7..0a44649 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -17,6 +17,7 @@ "paths": { "@pconfig": ["config/index.ts"], "@platform/prisma-clients": ["libs/prisma-clients/index.ts"], + "@platform/prisma-clients/*": ["libs/prisma-clients/*"], "@platform/prisma-generator": ["libs/prisma-generator/src/index.ts"], "@platform/site-layouts": ["libs/site-layouts/src/index.ts"], "@platform/site-pages": ["libs/site-pages/src/index.ts"],