Skip to content

Commit 78f26e2

Browse files
committed
Added base sample with db work
1 parent 58b5956 commit 78f26e2

26 files changed

+2442
-1879
lines changed

onlineshop/.env

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ NODE_ENV=development
44
SERVER_HOST=localhost
55
SERVER_PORT=3000
66
SERVER_WEB_HOST=localhost
7-
SERVER_WEB_PORT=4000
7+
SERVER_WEB_PORT=4000
8+
DB_USER=postgres
9+
DB_PASSWORD=postgres
10+
DB_PORT=5432
11+
DB_NAME=sample

onlineshop/docker-compose.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ services:
99
- POSTGRES_PASSWORD=${DB_PASSWORD}
1010
- POSTGRES_DB=${DB_NAME}
1111
volumes:
12-
- ./pgdata:/var/lib/postgresql/data
12+
- sample:/var/lib/postgresql/data
1313
ports:
1414
- "${DB_PORT}:5432"
1515

@@ -21,4 +21,5 @@ services:
2121
links:
2222
- db
2323

24-
24+
volumes:
25+
sample:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { DataSource } from 'typeorm';
2+
3+
export const AppDataSource = new DataSource({
4+
type: 'postgres',
5+
host: 'localhost',
6+
port: 5432,
7+
username: 'postgres',
8+
password: 'postgres',
9+
database: 'sample',
10+
entities: ['src/**/*.entity.{ts,js}'],
11+
migrations: [`${__dirname}/migrations/*{.ts,.js}`],
12+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class Initial1743834051977 implements MigrationInterface {
4+
name = 'Initial1743834051977'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`CREATE TABLE "dog" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, "age" integer NOT NULL, CONSTRAINT "PK_187826f37fd8e592a5711350db1" PRIMARY KEY ("id"))`);
8+
}
9+
10+
public async down(queryRunner: QueryRunner): Promise<void> {
11+
await queryRunner.query(`DROP TABLE "dog"`);
12+
}
13+
14+
}

onlineshop/source/server/package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@
2121
"test:watch": "jest --watch",
2222
"test:cov": "jest --coverage",
2323
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
24-
"test:e2e": "jest --config ./test/jest-e2e.json"
24+
"test:e2e": "jest --config ./test/jest-e2e.json",
25+
"typeorm": "typeorm-ts-node-commonjs",
26+
"migration:generate": "yarn typeorm -- migration:generate -d ./data-source.ts",
27+
"migration:run": "yarn typeorm -- migration:run -d ./data-source.ts"
2528
},
2629
"dependencies": {
2730
"@nestjs/common": "^10.4.4",
2831
"@nestjs/core": "^10.4.4",
32+
"@nestjs/mapped-types": "*",
2933
"@nestjs/platform-express": "^10.4.4",
34+
"@nestjs/typeorm": "^11.0.0",
3035
"npm-check-updates": "^17.1.3",
36+
"pg": "^8.14.1",
3137
"reflect-metadata": "^0.2.2",
32-
"rxjs": "^7.8.1"
38+
"rxjs": "^7.8.1",
39+
"typeorm": "^0.3.22"
3340
},
3441
"devDependencies": {
3542
"@eslint/compat": "^1.2.0",
@@ -51,7 +58,7 @@
5158
"source-map-support": "^0.5.21",
5259
"supertest": "^7.0.0",
5360
"ts-jest": "^29.2.5",
54-
"ts-loader": "^9.5.1",
61+
"ts-loader": "^9.5.2",
5562
"ts-node": "^10.9.2",
5663
"tsconfig-paths": "^4.2.0",
5764
"typescript": "^5.6.2"
@@ -72,5 +79,6 @@
7279
],
7380
"coverageDirectory": "../coverage",
7481
"testEnvironment": "node"
75-
}
82+
},
83+
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
7684
}

onlineshop/source/server/src/app.module.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
import { Module } from '@nestjs/common';
2+
import { TypeOrmModule } from '@nestjs/typeorm';
23

34
import { AppController } from './app.controller';
45
import { AppService } from './app.service';
6+
import { CatsModule } from './cats/cats.module';
7+
import { DogsModule } from './dogs/dogs.module';
8+
import { Dog } from './dogs/entities/dog.entity';
59

610
@Module({
7-
imports: [],
11+
imports: [
12+
TypeOrmModule.forRoot({
13+
type: 'postgres',
14+
host: 'localhost',
15+
port: 5432,
16+
username: 'postgres',
17+
password: 'postgres',
18+
database: 'sample',
19+
entities: [Dog],
20+
synchronize: true,
21+
}),
22+
CatsModule,
23+
DogsModule,
24+
],
825
controllers: [AppController],
926
providers: [AppService],
1027
})

onlineshop/source/server/src/app.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
33
@Injectable()
44
export class AppService {
55
getHello(): string {
6-
return 'Hello World!';
6+
return 'Hello World! 12345';
77
}
88
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { CatsController } from './cats.controller';
3+
import { CatsService } from './cats.service';
4+
5+
describe('CatsController', () => {
6+
let controller: CatsController;
7+
8+
beforeEach(async () => {
9+
const module: TestingModule = await Test.createTestingModule({
10+
controllers: [CatsController],
11+
providers: [CatsService],
12+
}).compile();
13+
14+
controller = module.get<CatsController>(CatsController);
15+
});
16+
17+
it('should be defined', () => {
18+
expect(controller).toBeDefined();
19+
});
20+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Controller, Get, Post, Body, Patch, Param, Delete, NotFoundException } from '@nestjs/common';
2+
import { CatsService } from './cats.service';
3+
import { CreateCatDto } from './dto/create-cat.dto';
4+
import { UpdateCatDto } from './dto/update-cat.dto';
5+
6+
@Controller('cats')
7+
export class CatsController {
8+
constructor(private readonly catsService: CatsService) {}
9+
10+
@Post()
11+
create(@Body() createCatDto: CreateCatDto) {
12+
return this.catsService.create(createCatDto);
13+
}
14+
15+
@Get()
16+
findAll() {
17+
return this.catsService.findAll();
18+
}
19+
20+
@Get(':id')
21+
findOne(@Param('id') id: string) {
22+
const cat = this.catsService.findOne(+id);
23+
if (cat) {
24+
return cat
25+
}
26+
throw new NotFoundException(`cat with id: ${id} was not found`);
27+
}
28+
29+
@Patch(':id')
30+
update(@Param('id') id: string, @Body() updateCatDto: UpdateCatDto) {
31+
return this.catsService.update(+id, updateCatDto);
32+
}
33+
34+
@Delete(':id')
35+
remove(@Param('id') id: string) {
36+
return this.catsService.remove(+id);
37+
}
38+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Module } from '@nestjs/common';
2+
import { CatsService } from './cats.service';
3+
import { CatsController } from './cats.controller';
4+
5+
@Module({
6+
controllers: [CatsController],
7+
providers: [CatsService],
8+
})
9+
export class CatsModule {}

0 commit comments

Comments
 (0)