-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #449 from jiho-kr/feature/embedded-entities
- Loading branch information
Showing
18 changed files
with
251 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { HttpStatus, INestApplication } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import request from 'supertest'; | ||
|
||
import { EmbeddedEntitiesModule } from './embedded-entities.module'; | ||
import { EmployeeEntity } from './employee.entity'; | ||
import { UserEntity } from './user.entity'; | ||
import { TestHelper } from '../test.helper'; | ||
|
||
describe('Embedded-entities', () => { | ||
let app: INestApplication; | ||
|
||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [EmbeddedEntitiesModule, TestHelper.getTypeOrmPgsqlModule([UserEntity, EmployeeEntity])], | ||
}).compile(); | ||
app = moduleFixture.createNestApplication(); | ||
await app.init(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app?.close(); | ||
}); | ||
|
||
it('should be provided url', async () => { | ||
const routerPathList = TestHelper.getRoutePath(app.getHttpServer()); | ||
expect(routerPathList.get).toEqual(expect.arrayContaining(['/user/:id', '/user', '/employee/:id', '/employee'])); | ||
}); | ||
|
||
it('should be used embedded entity', async () => { | ||
const { body: created } = await request(app.getHttpServer()) | ||
.post('/user') | ||
.send({ isActive: true, name: { first: 'firstUserName', last: 'lastUserName' } }) | ||
.expect(HttpStatus.CREATED); | ||
|
||
const { body: readOne } = await request(app.getHttpServer()).get(`/user/${created.id}`).expect(HttpStatus.OK); | ||
expect(readOne).toEqual({ | ||
id: created.id, | ||
isActive: true, | ||
name: { first: 'firstUserName', last: 'lastUserName' }, | ||
}); | ||
|
||
const { body: readMany } = await request(app.getHttpServer()).get('/user').expect(HttpStatus.OK); | ||
expect(readMany.data[0].name).toBeDefined(); | ||
|
||
const { body: updated } = await request(app.getHttpServer()) | ||
.patch(`/user/${created.id}`) | ||
.send({ isActive: false, name: { first: 'updatedFirstUserName', last: 'updatedLastUserName' } }) | ||
.expect(HttpStatus.OK); | ||
expect(updated).toEqual({ | ||
id: created.id, | ||
isActive: false, | ||
name: { first: 'updatedFirstUserName', last: 'updatedLastUserName' }, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
||
import { EmployeeController } from './employee.controller'; | ||
import { EmployeeEntity } from './employee.entity'; | ||
import { EmployeeService } from './employee.service'; | ||
import { UserController } from './user.controller'; | ||
import { UserEntity } from './user.entity'; | ||
import { UserService } from './user.service'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([UserEntity, EmployeeEntity])], | ||
controllers: [UserController, EmployeeController], | ||
providers: [UserService, EmployeeService], | ||
}) | ||
export class EmbeddedEntitiesModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Controller } from '@nestjs/common'; | ||
|
||
import { EmployeeEntity } from './employee.entity'; | ||
import { EmployeeService } from './employee.service'; | ||
import { Crud } from '../../src/lib/crud.decorator'; | ||
import { CrudController } from '../../src/lib/interface'; | ||
|
||
@Crud({ | ||
entity: EmployeeEntity, | ||
}) | ||
@Controller('employee') | ||
export class EmployeeController implements CrudController<EmployeeEntity> { | ||
constructor(public readonly crudService: EmployeeService) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Type } from 'class-transformer'; | ||
import { IsNumber, IsOptional } from 'class-validator'; | ||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; | ||
|
||
import { Name } from './name'; | ||
import { GROUP } from '../../src/lib/interface'; | ||
|
||
@Entity() | ||
export class EmployeeEntity { | ||
@PrimaryGeneratedColumn() | ||
@Type(() => Number) | ||
@IsNumber({ allowNaN: false, allowInfinity: false }, { groups: [GROUP.PARAMS] }) | ||
id: string; | ||
|
||
@Column(() => Name) | ||
@Type(() => Name) | ||
name: Name; | ||
|
||
@Column() | ||
@IsNumber({ allowNaN: false, allowInfinity: false }, { always: true }) | ||
@IsOptional({ always: true }) | ||
salary: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
|
||
import { EmployeeEntity } from './employee.entity'; | ||
import { CrudService } from '../../src/lib/crud.service'; | ||
|
||
@Injectable() | ||
export class EmployeeService extends CrudService<EmployeeEntity> { | ||
constructor(@InjectRepository(EmployeeEntity) repository: Repository<EmployeeEntity>) { | ||
super(repository); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { IsOptional, IsString } from 'class-validator'; | ||
import { Column } from 'typeorm'; | ||
|
||
export class Name { | ||
@Column({ nullable: true, default: 'first' }) | ||
@IsString({ always: true }) | ||
@IsOptional({ always: true }) | ||
first: string; | ||
|
||
@Column({ nullable: true, default: 'last' }) | ||
@IsString({ always: true }) | ||
@IsOptional({ always: true }) | ||
last: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Controller } from '@nestjs/common'; | ||
|
||
import { UserEntity } from './user.entity'; | ||
import { UserService } from './user.service'; | ||
import { Crud } from '../../src/lib/crud.decorator'; | ||
import { CrudController } from '../../src/lib/interface'; | ||
|
||
@Crud({ | ||
entity: UserEntity, | ||
}) | ||
@Controller('user') | ||
export class UserController implements CrudController<UserEntity> { | ||
constructor(public readonly crudService: UserService) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Type } from 'class-transformer'; | ||
import { IsBoolean, IsNumber, IsObject, IsOptional, ValidateNested } from 'class-validator'; | ||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; | ||
|
||
import { Name } from './name'; | ||
import { GROUP } from '../../src/lib/interface'; | ||
|
||
@Entity() | ||
export class UserEntity { | ||
@PrimaryGeneratedColumn() | ||
@Type(() => Number) | ||
@IsNumber({ allowNaN: false, allowInfinity: false }, { groups: [GROUP.PARAMS] }) | ||
id: string; | ||
|
||
@Column(() => Name) | ||
@Type(() => Name) | ||
@IsOptional({ always: true }) | ||
@IsObject({ always: true }) | ||
@ValidateNested({ always: true }) | ||
name: Name; | ||
|
||
@Column() | ||
@Type(() => Boolean) | ||
@IsBoolean({ always: true }) | ||
@IsOptional({ always: true }) | ||
isActive: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
|
||
import { UserEntity } from './user.entity'; | ||
import { CrudService } from '../../src/lib/crud.service'; | ||
|
||
@Injectable() | ||
export class UserService extends CrudService<UserEntity> { | ||
constructor(@InjectRepository(UserEntity) repository: Repository<UserEntity>) { | ||
super(repository); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.