Introduction to TypeORM
Explore the foundational concepts of TypeORM, a TypeScript-based ORM for Node.js. Understand key decorators, entity relationships, and how to use the Repository API in a NestJS environment for efficient database management.
We'll cover the following...
Overview
TypeORM has first-class support for TypeScript ORM in Node.js since it’s built purely on TypeScript. With TypeORM, like with every other ORM, we can easily work with any SQL database of our choice. TypeORM provides simple JavaScript methods and functions that can be easily understood.
Design pattern
One key aspect of TypeORM is its design pattern. This is another property that makes TypeORM unique among ORMs. The design pattern for TypeORM is the decorative design pattern. In the decorative design pattern, we can add behaviors dynamically to an object or class.
As the name implies, decorators are like icing on a cake. The cake (class/object) retains its taste (behavior), but the icing (decorators) can make it more palatable and even improve it by adding extra features and functionalities.
TypeORM decorators
The code snippet shows how a schema is defined using TypeORM. The most essential decorators include:
@Entity: This declares the class as a TypeORM entity.@Column: This resolves each class property to an SQL column.@PrimaryGeneratedColumn: This is used to define an auto-generatedidcolumn. Another alternative to this decorator is the@PrimaryColumn, which works in a similar way.
An entity must have at least one primary column.
Relational decorators
To establish a relationship between tables or entities, the following decorators are provided by TypeORM:
ManyToManyManyToOneOneToManyOneToOne
We can specify the datatype of the table columns by simply passing an object with a property of type as shown above. The supported datatypes are:
Outside the type property, TypeORM also provides other configurations for our table columns.
name: The column name in the database.length: The column length in the database, which can be of typestringornumber.width: The column display width. In MySQL, this is used on some column types.nullable: In PostgreSQL, this enables us to save a column withnullvalues as default if a value is not provided during the create operation. This configuration is of typeboolean.unique: Specifies if the column value is unique. This configuration is of typeboolean.
Model layer
TypeORM provides the following APIs to interface with SQL databases:
EntityManagersRepository
These APIs offer methods that carry out queries against the SQL database without us having to write the SQL queries directly.
Next we’ll focus on the Repository API and how it’s integrated with the NestJS library.
The Repository method
The Repository API follows the open/close and single responsibility principles in the SOLID design pattern. This means we develop repositories per entity with the same methods that can be extended or tuned but not modified.
The NestJS library is OOP based. We use the @InjectRepository decorator to inject a repository property into a service class or the classes responsible for CRUD and database operations from the @nestjs/typeorm module.
TypeORM can be easily integrated with top Node.js libraries such as Express and NestJS.