chore: add typeorm config
This commit is contained in:
Vendored
+15
-4
@@ -21,11 +21,15 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nestjs/common": "^10.4.15",
|
"@nestjs/common": "^10.4.15",
|
||||||
|
"@nestjs/config": "^4.0.0",
|
||||||
"@nestjs/core": "^10.4.15",
|
"@nestjs/core": "^10.4.15",
|
||||||
"@nestjs/platform-express": "^10.4.15",
|
"@nestjs/platform-fastify": "^10.4.15",
|
||||||
"@nestjs/swagger": "^11.0.2",
|
"@nestjs/swagger": "^8.1.1",
|
||||||
|
"@nestjs/typeorm": "^10.0.2",
|
||||||
|
"pg": "^8.13.1",
|
||||||
"reflect-metadata": "^0.2.2",
|
"reflect-metadata": "^0.2.2",
|
||||||
"rxjs": "^7.8.1"
|
"rxjs": "^7.8.1",
|
||||||
|
"typeorm": "^0.3.20"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@commitlint/cli": "^19.6.1",
|
"@commitlint/cli": "^19.6.1",
|
||||||
@@ -70,6 +74,13 @@
|
|||||||
"**/*.(t|j)s"
|
"**/*.(t|j)s"
|
||||||
],
|
],
|
||||||
"coverageDirectory": "../coverage",
|
"coverageDirectory": "../coverage",
|
||||||
"testEnvironment": "node"
|
"testEnvironment": "node",
|
||||||
|
"lint-staged": {
|
||||||
|
"**/*.ts": [
|
||||||
|
"pnpm run format",
|
||||||
|
"pnpm run lint:fix",
|
||||||
|
"git add ."
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+701
-452
File diff suppressed because it is too large
Load Diff
+15
-1
@@ -1,7 +1,21 @@
|
|||||||
import { Module } from "@nestjs/common";
|
import { Module } from "@nestjs/common";
|
||||||
|
import { ConfigModule } from "@nestjs/config";
|
||||||
|
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||||
|
|
||||||
|
import { DatabaseConfigs } from "./configs/typeorm.config";
|
||||||
|
import { UsersModule } from './modules/users/users.module';
|
||||||
|
import { TicketsModule } from './modules/tickets/tickets.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [],
|
imports: [
|
||||||
|
ConfigModule.forRoot({
|
||||||
|
cache: true,
|
||||||
|
isGlobal: true,
|
||||||
|
}),
|
||||||
|
TypeOrmModule.forRootAsync(DatabaseConfigs()),
|
||||||
|
UsersModule,
|
||||||
|
TicketsModule,
|
||||||
|
],
|
||||||
controllers: [],
|
controllers: [],
|
||||||
providers: [],
|
providers: [],
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { CreateDateColumn, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
|
||||||
|
|
||||||
|
export abstract class BaseEntity {
|
||||||
|
@PrimaryGeneratedColumn("uuid")
|
||||||
|
id: string;
|
||||||
|
|
||||||
|
@CreateDateColumn()
|
||||||
|
createdAt: Date;
|
||||||
|
|
||||||
|
@UpdateDateColumn()
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { NestFastifyApplication } from "@nestjs/platform-fastify";
|
||||||
|
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
|
||||||
|
|
||||||
|
export function getSwaggerDocument(app: NestFastifyApplication) {
|
||||||
|
const swaggerConfig = new DocumentBuilder()
|
||||||
|
.setTitle("The Danak dsc api document")
|
||||||
|
.setDescription("The Danak dsc API description")
|
||||||
|
.setVersion("1.0.0")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
const swaggerDocument = SwaggerModule.createDocument(app, swaggerConfig);
|
||||||
|
SwaggerModule.setup("api-docs", app, swaggerDocument);
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { ConfigService } from "@nestjs/config";
|
||||||
|
import { TypeOrmModuleAsyncOptions } from "@nestjs/typeorm";
|
||||||
|
|
||||||
|
export function DatabaseConfigs(): TypeOrmModuleAsyncOptions {
|
||||||
|
return {
|
||||||
|
inject: [ConfigService],
|
||||||
|
useFactory(configService: ConfigService) {
|
||||||
|
return {
|
||||||
|
type: "postgres",
|
||||||
|
host: configService.getOrThrow<string>("DB_HOST"),
|
||||||
|
port: configService.getOrThrow<number>("DB_PORT"),
|
||||||
|
database: configService.getOrThrow<string>("DB_NAME"),
|
||||||
|
username: configService.getOrThrow<string>("DB_USER"),
|
||||||
|
password: configService.getOrThrow<string>("DB_PASS"),
|
||||||
|
synchronize: true,
|
||||||
|
logging: configService.getOrThrow<string>("NODE_ENV") == "production" ? false : true,
|
||||||
|
migrationsTableName: "typeorm_migrations",
|
||||||
|
migrationsRun: false,
|
||||||
|
autoLoadEntities: true,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
+18
-4
@@ -1,8 +1,22 @@
|
|||||||
import { NestFactory } from '@nestjs/core';
|
import { Logger } from "@nestjs/common";
|
||||||
import { AppModule } from './app.module';
|
import { ConfigService } from "@nestjs/config";
|
||||||
|
import { NestFactory } from "@nestjs/core";
|
||||||
|
import { FastifyAdapter, NestFastifyApplication } from "@nestjs/platform-fastify";
|
||||||
|
|
||||||
|
import { AppModule } from "./app.module";
|
||||||
|
import { getSwaggerDocument } from "./configs/swagger.config";
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const logger = new Logger("APP");
|
||||||
await app.listen(process.env.PORT ?? 3000);
|
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
|
||||||
|
const configService = app.get<ConfigService>(ConfigService);
|
||||||
|
|
||||||
|
getSwaggerDocument(app);
|
||||||
|
|
||||||
|
const PORT = configService.getOrThrow<number>("PORT");
|
||||||
|
await app.listen(PORT, () => {
|
||||||
|
logger.log(`Server running on http://localhost:${PORT}`);
|
||||||
|
logger.log(`Swagger running on http://localhost:${PORT}/api-docs`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
bootstrap();
|
bootstrap();
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import { Controller } from '@nestjs/common';
|
||||||
|
|
||||||
|
@Controller('tickets')
|
||||||
|
export class TicketsController {}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TicketsService } from './tickets.service';
|
||||||
|
import { TicketsController } from './tickets.controller';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [TicketsService],
|
||||||
|
controllers: [TicketsController]
|
||||||
|
})
|
||||||
|
export class TicketsModule {}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class TicketsService {}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
export class User {
|
||||||
|
@PrimaryGeneratedColumn("uuid")
|
||||||
|
id: string;
|
||||||
|
|
||||||
|
@Column({ type: "varchar", length: 150, unique: true })
|
||||||
|
email: string;
|
||||||
|
|
||||||
|
@Column({ type: "varchar", length: 11, unique: true })
|
||||||
|
phoneNumber: string;
|
||||||
|
|
||||||
|
@Column({ type: "varchar", length: 150 })
|
||||||
|
password: string;
|
||||||
|
|
||||||
|
@Column({ type: "varchar", length: 150 })
|
||||||
|
fistName: string;
|
||||||
|
|
||||||
|
@Column({ type: "varchar", length: 200 })
|
||||||
|
lastName: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
birthDate: string;
|
||||||
|
|
||||||
|
@Column({ type: "varchar", length: 100 })
|
||||||
|
nationalCode: string;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { Injectable } from "@nestjs/common";
|
||||||
|
import { InjectRepository } from "@nestjs/typeorm";
|
||||||
|
import { Repository } from "typeorm";
|
||||||
|
|
||||||
|
import { User } from "../entities/user.entity";
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class UserRepository extends Repository<User> {
|
||||||
|
constructor(@InjectRepository(User) private userRepository: Repository<User>) {
|
||||||
|
super(userRepository.target, userRepository.manager, userRepository.queryRunner);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import { Injectable } from "@nestjs/common";
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class UsersService {}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import { Controller } from "@nestjs/common";
|
||||||
|
|
||||||
|
@Controller("users")
|
||||||
|
export class UsersController {}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import { Module } from "@nestjs/common";
|
||||||
|
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||||
|
|
||||||
|
import { User } from "./entities/user.entity";
|
||||||
|
import { UserRepository } from "./providers/users.repository";
|
||||||
|
import { UsersService } from "./providers/users.service";
|
||||||
|
import { UsersController } from "./users.controller";
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [TypeOrmModule.forFeature([User])],
|
||||||
|
providers: [UsersService, UserRepository],
|
||||||
|
controllers: [UsersController],
|
||||||
|
exports: [UsersService, TypeOrmModule],
|
||||||
|
})
|
||||||
|
export class UsersModule {}
|
||||||
Reference in New Issue
Block a user