-
[Nest.js] 7- Entity & RepositoryNest.js 2021. 9. 9. 17:38
Contents
Entity?
Repository?
Entity
- Entity is a class that maps to a database table, so it is very closed to database model.
- @Entity() decorator in class-level
Example
- Entity
board/board.entity.ts - Table
- @Entity() : means that "CREATE TABLE board"
- @PrimaryGeneratedColumn() : it indicates this column is PK(Primary Key) in DB, and Auto_Increment constraint
- @Column : could specify column's properties, such as length( string default : varchar(255)), and type..
Repository
- Repository works/helps Entity CRUD tasks.
- each Entity has its own Repository which handles all operations with its entity.
- Repository Pattern : DB tasks are assigned to Repository
Example
1. Create a repository with Entity
boards/board.repository.ts - @EntityRepository(Board) : BoardRepository class is announced as Board's Custom Repository
- extends Repository(Board) : which entity would be controlled by this repository.
2. Register this repository as a feature in its Module
boards/boards.module.ts - imports: [TypeOrmModule.forFeature([BoardRepository])]
'Nest.js' 카테고리의 다른 글
[Nest.js] 9 - Authentification - module/components (0) 2021.09.13 [Nest.js] 8 - CRUD summary code(#1 ~ #7) (0) 2021.09.10 [Nest.js] 6 - DTO & PIPES (0) 2021.09.09 [Nest.js] 5 - TypeORM / MySql(MariaDB) (0) 2021.09.08 [Nest.js] 4 - Provider (0) 2021.09.08