Skip to content

Commit

Permalink
add typeorm & postgres & user-model
Browse files Browse the repository at this point in the history
  • Loading branch information
AziAlex committed Feb 10, 2024
1 parent 07f420a commit ce241d0
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 4 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"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",
"docker:up": "docker-compose up -d",
"docker:down": "docker-compose down"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.3.0",
"@nestjs/typeorm": "^10.0.2",
"pg": "^8.11.3",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
"rxjs": "^7.8.1",
"typeorm": "^0.3.20"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
24 changes: 22 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { TypeOrmModule } from '@nestjs/typeorm'
import { UserModule } from './user/user.module'

@Module({
imports: [],
imports: [ConfigModule.forRoot({ isGlobal: true }),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
host: configService.get('DB_HOST'),
port: configService.get('DB_PORT'),
username: configService.get('DB_USERNAME'),
password: configService.get('DB_PASSWORD'),
database: configService.get('DB_NAME'),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
autoLoadEntities: true,
}),
}),
UserModule],
controllers: [],
providers: [],
})
export class AppModule {}
export class AppModule {
}
47 changes: 47 additions & 0 deletions src/user/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
Column,
CreateDateColumn,
Entity,
JoinColumn, ManyToOne, OneToMany,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm'

@Entity()
export class User {
@PrimaryGeneratedColumn('uuid')
id: string

@Column({ unique: true })
email: string

@CreateDateColumn({name: 'created_at'})
createdAt: Date

@UpdateDateColumn({name: 'updated_at'})
updatedAt: Date

@OneToMany(() => Token, (token) => token.user, { onDelete: 'CASCADE' })
tokens: Token[]
}


@Entity()
export class Token {
@PrimaryGeneratedColumn()
token: string

@Column({ name: 'expires_at' })
expiresAt: Date

@Column({ name: 'user_agent' })
userAgent: string

@Column({ name: 'user_id', type: 'uuid' })
@JoinColumn({ name: 'user_id' })
userId: string

@ManyToOne(() => User, (user) => user.tokens)
@JoinColumn({ name: 'user_id' })
user: User
}
7 changes: 7 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Controller } from '@nestjs/common';
import { UserService } from './user.service';

@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
}
9 changes: 9 additions & 0 deletions src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';

@Module({
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
4 changes: 4 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class UserService {}

0 comments on commit ce241d0

Please sign in to comment.