What is @nestjs/typeorm?
@nestjs/typeorm is an integration package for NestJS that provides seamless integration with TypeORM, a popular ORM (Object-Relational Mapper) for TypeScript and JavaScript. It allows developers to easily manage database connections, entities, repositories, and perform database operations in a structured and efficient manner.
What are @nestjs/typeorm's main functionalities?
Database Connection
This feature allows you to establish a connection to a database using TypeORM's `createConnection` method. You can specify the database type, host, port, username, password, and other connection options.
const connection = await createConnection({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'test',
password: 'test',
database: 'test',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
});
Entity Definition
Entities in TypeORM are classes that map to database tables. This feature allows you to define entities using decorators like `@Entity`, `@Column`, and `@PrimaryGeneratedColumn`.
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
age: number;
}
Repository Pattern
The repository pattern in TypeORM allows you to interact with the database using repositories. This feature demonstrates how to use the `@InjectRepository` decorator to inject a repository and perform CRUD operations.
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private userRepository: Repository<User>,
) {}
findAll(): Promise<User[]> {
return this.userRepository.find();
}
findOne(id: string): Promise<User> {
return this.userRepository.findOne(id);
}
async remove(id: string): Promise<void> {
await this.userRepository.delete(id);
}
}
Other packages similar to @nestjs/typeorm
sequelize
Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication, and more. Compared to @nestjs/typeorm, Sequelize offers a different API and supports a wider range of SQL dialects.
mongoose
Mongoose is an ODM (Object Data Modeling) library for MongoDB and Node.js. It provides a straightforward, schema-based solution to model your application data. While @nestjs/typeorm is designed for SQL databases, Mongoose is specifically for MongoDB, making it a better choice for NoSQL databases.
prisma
Prisma is a next-generation ORM that can be used to build GraphQL servers, REST APIs, microservices, and more. It provides a type-safe database client and a powerful migration system. Prisma's approach to database management is more modern compared to TypeORM, offering better type safety and developer experience.
A progressive Node.js framework for building efficient and scalable server-side applications.
Description
TypeORM module for Nest.
Installation
$ npm i --save @nestjs/typeorm typeorm
Quick Start
Overview & Tutorial
Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Stay in touch
License
Nest is MIT licensed.