feature: configured prisma database for auth service

feature/auth-configure
Artem-Darius Weber 1 year ago
parent 444a5b345a
commit 3355b9bf25

@ -3,3 +3,6 @@ PGADMIN_DEFAULT_PASSWORD: fakepassword123!
POSTGRES_USER=root POSTGRES_USER=root
POSTGRES_PASSWORD=root POSTGRES_PASSWORD=root
POSTGRES_DB=root 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

@ -28,8 +28,33 @@
To start the development server run `nx serve frontend`. Open your browser and navigate to http://localhost:4200/. Happy coding! To start the development server run `nx serve frontend`. Open your browser and navigate to http://localhost:4200/. Happy coding!
Run all services:
```bash
npm i -g prisma nx
```
```bash
yarn install
```
```bash
copy .env.example .env
```
```bash
cd apps/auth/serving && copy database.env.example database.env && sudo docker-compose up -d
```
```bash
prisma generate --schema="./libs/prisma-clients/auth/prisma/schema.prisma"
``` ```
```bash
prisma db push --schema="./libs/prisma-clients/auth/prisma/schema.prisma"
```
Run all services:
```bash
nx run-many --parallel --target=serve --projects=agw,user-data,auth,frontend nx run-many --parallel --target=serve --projects=agw,user-data,auth,frontend
``` ```
<a style="padding: 10px; background: black; color: white; border-radius: 25px;" href="https://l.djft.ru/r/72/p/itlabplatform1/a/p/a/"> <a style="padding: 10px; background: black; color: white; border-radius: 25px;" href="https://l.djft.ru/r/72/p/itlabplatform1/a/p/a/">

@ -1,6 +0,0 @@
PORT=3005
DATABASE_URL=postgresql://postgres:postgres@localhost:5433/user-authentication
PUBLIC_KEY_PATH=tokens/jwt.key.pub
PRIVATE_KEY_PATH=tokens/jwt.key

@ -1,32 +0,0 @@
# syntax=docker/dockerfile:1
FROM node:18
WORKDIR /app
COPY --chown=node:node . .
RUN rm -f *.env *.env.*
RUN apt-get update -y && apt-get install -y dumb-init
RUN npm install
ENV NODE_ENV=production
RUN npm run prefullbuild && npm run prebuild && npm run build
RUN mkdir temp temp/.prisma temp/@prisma temp/prisma && cp -r ./node_modules/.prisma/* ./temp/.prisma/ && cp -r ./node_modules/@prisma/* ./temp/@prisma/ && cp -r ./node_modules/prisma/* ./temp/prisma/
RUN rm -rdf node_modules
RUN npm install --production
RUN cp -r ./temp/* ./node_modules/ && rm -rdf temp
RUN ls | grep -v node_modules | grep -v dist | xargs rm -rfv
RUN cp -r ./dist/* ./ && rm -rdf dist
USER node
CMD ["dumb-init", "node", "./main.js"]

@ -1 +0,0 @@
migrations

@ -5,24 +5,14 @@ services:
image: postgres:latest image: postgres:latest
container_name: user-authentication-database container_name: user-authentication-database
restart: always restart: always
ports:
- 5444:5432
networks: networks:
- user-authentication - user-authentication
volumes: volumes:
- user-authentication-database:/var/lib/postgresql - user-authentication-database:/var/lib/postgresql
env_file: env_file:
- ./database.env - ./database.env
user-authentication-database-admin:
container_name: user-authentication-database-admin
image: bitnami/phppgadmin:latest
restart: always
networks:
- user-authentication
depends_on:
- user-authentication-database
environment:
- DATABASE_HOST=user-authentication-database
ports:
- "8083:8080"
networks: networks:
user-authentication: user-authentication:

@ -1,11 +1,11 @@
import { BadRequestException, ConflictException, Injectable } from "@nestjs/common"; import { BadRequestException, ConflictException, Injectable } from "@nestjs/common";
import { PrismaService } from "../../Prisma/service/prisma.service";
import { UserDto } from "../dto/user.dto"; import { UserDto } from "../dto/user.dto";
import { PrismaClientValidationError } from "@prisma/client/runtime"; import { PrismaClientValidationError } from "@prisma/client/runtime";
import { AuthClient } from "@platform/prisma-clients";
@Injectable() @Injectable()
export class DeviceService { export class DeviceService {
constructor(private readonly prisma: PrismaService) { constructor(private readonly prisma: AuthClient) {
} }
public getUserUuidAsPrismaPromise(params: { fingerprint: string }) { public getUserUuidAsPrismaPromise(params: { fingerprint: string }) {

@ -1,11 +1,11 @@
import { BadRequestException, Injectable } from "@nestjs/common"; import { BadRequestException, Injectable } from "@nestjs/common";
import { PrismaService } from "../../Prisma/service/prisma.service"; import { AuthClient } from "@platform/prisma-clients";
import { DevicesDto } from "../dto/devices.dto"; import { DevicesDto } from "../dto/devices.dto";
import { PrismaClientValidationError } from "@prisma/client/runtime"; import { PrismaClientValidationError } from "@prisma/client/runtime";
@Injectable() @Injectable()
export class UserService { export class UserService {
constructor(private readonly prisma: PrismaService) { constructor(private readonly prisma: AuthClient) {
} }
public async devices(params: DevicesDto): Promise<{ name?: string, fingerprint?: string, isBlocked?: boolean }[]> { public async devices(params: DevicesDto): Promise<{ name?: string, fingerprint?: string, isBlocked?: boolean }[]> {

@ -0,0 +1,3 @@
node_modules
# Keep environment variables out of version control
.env

@ -0,0 +1,2 @@
export { PrismaClient as AuthClient } from '.prisma/auth-client';
export * from '.prisma/auth-client';

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

@ -0,0 +1 @@
export class UniqueConstraintFailedError 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;
})
);
}
}

@ -3,11 +3,12 @@
generator client { generator client {
provider = "prisma-client-js" provider = "prisma-client-js"
output = "../../../../node_modules/.prisma/auth-client"
} }
datasource db { datasource db {
provider = "postgresql" provider = "postgresql"
url = env("DATABASE_URL") url = env("AUTH_SOURCE_URL")
} }
model User { model User {

@ -0,0 +1 @@
export { AuthClient } from './auth';

@ -17,6 +17,7 @@
"paths": { "paths": {
"@pconfig": ["config/index.ts"], "@pconfig": ["config/index.ts"],
"@platform/prisma-generator": ["libs/prisma-generator/src/index.ts"], "@platform/prisma-generator": ["libs/prisma-generator/src/index.ts"],
"@platform/prisma-clients": ["libs/prisma-clients/index.ts"],
"@platform/ui": ["libs/ui/src/index.ts"] "@platform/ui": ["libs/ui/src/index.ts"]
} }
}, },

Loading…
Cancel
Save