Skip to content

Commit

Permalink
Merge pull request #61 from amosproj/feat/xd-43
Browse files Browse the repository at this point in the history
feat: Setup Prisma (XD-43)
  • Loading branch information
Persists authored May 15, 2024
2 parents 24ed79d + 0899d99 commit f49f95d
Show file tree
Hide file tree
Showing 16 changed files with 40,833 additions and 31 deletions.
24 changes: 18 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
APP_PORT=3000
APP_HOST=localhost
APP_NAME=MyApp
POSTGRES_HOST=db.example.com
# Port number where the backend server will run
BACKEND_PORT=3333
# Hostname for the backend server
BACKEND_HOST=localhost
# Name of the backend application
BACKEND_NAME=MyApp
# Hostname for the PostgreSQL database
POSTGRES_HOST=localhost
# Port number for the PostgreSQL database
POSTGRES_PORT=5432
POSTGRES_USER=myuser
POSTGRES_PASSWORD=mypassword
# Username for accessing the PostgreSQL database
POSTGRES_USER=amos
# Password for the PostgreSQL database user
POSTGRES_PASSWORD=amos
# Name of the PostgreSQL database to be used
POSTGRES_DB=amos

# Full database connection URL constructed from the above PostgreSQL variables used for Prisma
DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}
2 changes: 1 addition & 1 deletion apps/backend/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { validateConfig } from './config/validation';
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: ['apps/backend/.env'],
envFilePath: ['.env'],
validate: validateConfig,
}),
],
Expand Down
6 changes: 3 additions & 3 deletions apps/backend/src/app/config/classes/environment.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class EnvironmentVariables implements IEnvironmentVariables {
*/
@IsDefined()
@IsNumber()
APP_PORT: number;
BACKEND_PORT?: number;

/**
* Application host for the backend
Expand All @@ -26,7 +26,7 @@ export class EnvironmentVariables implements IEnvironmentVariables {
@IsDefined()
@IsString()
@MinLength(1)
APP_HOST: string;
BACKEND_HOST?: string;

/**
* Application name for the backend
Expand All @@ -36,7 +36,7 @@ export class EnvironmentVariables implements IEnvironmentVariables {
@IsDefined()
@IsString()
@MinLength(1)
APP_NAME: string;
BACKEND_NAME?: string;

/* DATABASE */

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export interface IEnvironmentVariables {
/* APP */
APP_PORT?: number;
APP_HOST?: string;
APP_NAME?: string;
BACKEND_PORT?: number;
BACKEND_HOST?: string;
BACKEND_NAME?: string;
/* DATABASE */
POSTGRES_HOST?: string;
POSTGRES_PORT?: number;
Expand Down
6 changes: 3 additions & 3 deletions apps/backend/src/app/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export const validateConfig = (config: IEnvironmentVariables): BackendConfig =>
const generateConfig = (config: IEnvironmentVariables): BackendConfig =>
plainToInstance(BackendConfig, {
app: {
port: config.APP_PORT || 3000,
host: config.APP_HOST || 'localhost',
name: config.APP_NAME,
port: config.BACKEND_PORT || 3333,
host: config.BACKEND_HOST || 'localhost',
name: config.BACKEND_NAME,
},
database: {
host: config.POSTGRES_HOST,
Expand Down
12 changes: 7 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ services:
volumes:
- postgres:/var/lib/postgresql/data
restart: unless-stopped
env_file:
- .env
ports:
- '5432:5432'
environment:
POSTGRES_USER: amos
POSTGRES_PASSWORD: amos
POSTGRES_DB: amos
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
networks:
- db_network
- backend_network
Expand All @@ -36,11 +38,11 @@ services:
dockerfile: apps/backend/Dockerfile
restart: unless-stopped
ports:
- '3000:3000'
- '${BACKEND_PORT}:${BACKEND_PORT}'
networks:
- backend_network
env_file:
- apps/backend/.env
- .env

frontend:
depends_on:
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@nestjs/platform-express": "^10.0.2",
"@nx/angular": "18.3.3",
"@siemens/ix": "^2.2.1",
"@prisma/client": "^5.13.0",
"@siemens/ix-angular": "^2.2.1",
"@siemens/ix-icons": "^2.1.0",
"axios": "^1.6.8",
Expand All @@ -36,7 +37,6 @@
"dotenv": "^16.4.5",
"lodash": "^4.17.21",
"postgres": "^3.4.4",
"prisma": "^5.13.0",
"reflect-metadata": "^0.1.13",
"rxjs": "~7.8.0",
"ts-enum-util": "^4.1.0",
Expand Down Expand Up @@ -97,6 +97,7 @@
"lint-staged": "^15.2.2",
"nx": "18.3.4",
"prettier": "^3.2.5",
"prisma": "^5.13.0",
"stylelint": "^16.4.0",
"stylelint-config-recess-order": "^5.0.1",
"stylelint-config-standard": "^36.0.0",
Expand All @@ -107,5 +108,8 @@
"typescript": "~5.4.5",
"webpack-cli": "^5.1.4"
},
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
"packageManager": "[email protected]+sha512.831cf4c5f8b8374af71521d4d153db49d7086de615c2af7cb5e9d7eb8ba630ddac46fea495d643e552ef2e68a3aa99a3e5e9fbee8696702967504df5c59cb273"
}
28 changes: 19 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions prisma/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Docs

### Setup 🛠

Install Prisma

```sh
pnpm add -D @prisma/cli
```

Install the Prisma Client

```sh
pnpm add prisma
```

Initialize Prisma

```sh
pnpx prisma init
```

generating the Prisma Client

```sh
pnpx prisma generate
```

### Usage 🚀

First you need to apply the migrations to your database with the following command:

```sh
pnpx prisma migrate dev
```

Then u can use the Prisma Client in your code to access your database:

```ts
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
const allUsers = await prisma.user.findMany();
console.log(allUsers);
}
```

### Seeding the database 🌱

If you want to seed the database with some initial data, you can use the following command:

```sh
pnpx prisma db seed
```

### Inspecting the database 🕵️‍♂️

If you want to inspect the database, you can use Prisma Studio. You can start it with the following command:

```sh
pnpx prisma studio
```

Or you can also use alternative tools like [DataGrip](https://www.jetbrains.com/datagrip/) ...
Loading

0 comments on commit f49f95d

Please sign in to comment.