Added Swagger and TypeORM, added Hello World

This commit is contained in:
R40fendt
2026-01-29 20:42:07 +01:00
parent be19571e30
commit 1948ce58d3
7 changed files with 679 additions and 98 deletions

704
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -19,18 +19,21 @@
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json" "test:e2e": "jest --config ./test/jest-e2e.json"
}, },
"dependencies": { "dependencies": {
"@nestjs/common": "^11.0.1", "@nestjs/common": "^11.0.1",
"@nestjs/core": "^11.0.1", "@nestjs/core": "^11.0.1",
"@nestjs/graphql": "^13.2.3", "@nestjs/graphql": "^13.2.3",
"@nestjs/mercurius": "^13.2.3", "@nestjs/mercurius": "^13.2.3",
"@nestjs/platform-fastify": "^11.1.12", "@nestjs/platform-fastify": "^11.1.12",
"@nestjs/swagger": "^11.2.5",
"fastify": "^5.7.2", "fastify": "^5.7.2",
"graphiql": "^5.2.2", "graphiql": "^5.2.2",
"graphql": "^16.12.0", "graphql": "^16.12.0",
"mercurius": "^16.7.0", "mercurius": "^16.7.0",
"mysql2": "^3.16.2",
"reflect-metadata": "^0.2.2", "reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1" "rxjs": "^7.8.1",
"typeorm": "^0.3.28"
}, },
"devDependencies": { "devDependencies": {
"@eslint/eslintrc": "^3.2.0", "@eslint/eslintrc": "^3.2.0",
@@ -56,8 +59,7 @@
"typescript": "^5.7.3", "typescript": "^5.7.3",
"typescript-eslint": "^8.20.0" "typescript-eslint": "^8.20.0"
}, },
"jest": {
"jest": {
"moduleFileExtensions": [ "moduleFileExtensions": [
"js", "js",
"json", "json",

View File

@@ -1,12 +1,15 @@
import { Controller, Get } from '@nestjs/common'; import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { Query, Resolver } from '@nestjs/graphql';
@Controller() @Controller()
@Resolver()
export class AppController { export class AppController {
constructor(private readonly appService: AppService) {} constructor(private readonly appService: AppService) {}
@Get() @Query(()=>String)
getHello(): string { @Get("/")
hello(): string {
return this.appService.getHello(); return this.appService.getHello();
} }
} }

View File

@@ -3,19 +3,18 @@ import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { GraphQLModule } from '@nestjs/graphql'; import { GraphQLModule } from '@nestjs/graphql';
import { MercuriusDriver, MercuriusDriverConfig } from '@nestjs/mercurius'; import { MercuriusDriver, MercuriusDriverConfig } from '@nestjs/mercurius';
import { HelloResolver } from './hello.resolver'; import { databaseProviders } from './database.providers';
@Module({ @Module({
imports: [ imports: [
GraphQLModule.forRoot<MercuriusDriverConfig>({ GraphQLModule.forRoot<MercuriusDriverConfig>({
driver: MercuriusDriver, driver: MercuriusDriver,
path: '/api/graphql',
graphiql: true, graphiql: true,
autoSchemaFile: true autoSchemaFile: true
} as MercuriusDriverConfig), } as MercuriusDriverConfig),
], ],
controllers: [AppController], controllers: [AppController],
providers: [AppService,HelloResolver], providers: [AppService, AppController, ...databaseProviders],
}) })
export class AppModule {} export class AppModule {}

23
src/database.providers.ts Normal file
View File

@@ -0,0 +1,23 @@
import { DataSource } from 'typeorm';
export const databaseProviders = [
{
provide: 'DATA_SOURCE',
useFactory: async () => {
const dataSource = new DataSource({
type: 'mysql',
host: process.env.DB_HOST || 'database',
port: parseInt(process.env.DB_PORT || "3006"),
username: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || 'root',
database: process.env.DB_NAME || 'ritzenbergen',
entities: [
__dirname + '/**/*.entity{.ts,.js}',
],
synchronize: true,
});
return dataSource.initialize();
},
},
];

View File

@@ -1,10 +0,0 @@
import { Resolver, Query } from '@nestjs/graphql';
@Resolver()
export class HelloResolver {
@Query(() => String)
hello(): string {
return 'hello world';
}
}

View File

@@ -1,9 +1,23 @@
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
import { NestFastifyApplication, FastifyAdapter } from '@nestjs/platform-fastify'; import { NestFastifyApplication, FastifyAdapter } from '@nestjs/platform-fastify';
import { FastifyContextConfig } from 'fastify';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter()); const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter({
await app.listen(process.env.PORT ?? 3000); listeningOrigin: "0.0.0.0"
} as FastifyContextConfig));
const config = new DocumentBuilder()
.setTitle('Ritzenbergen API')
.setDescription('The Ritzenbergen API description')
.setVersion('1.0')
.addTag('ritzenbergen')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('/api/docs', app, document);
await app.listen(process.env.PORT ?? 3000, "0.0.0.0");
} }
bootstrap(); bootstrap();