There are still some points to note when using TypeORM in Nestjs.
Notes on entities path configuration
To use TypeORM in nestjs, you need to configure the database connection (take MySQL as an example). Special attention should be paid to the entities field in the configuration parameters:
{ "type": "mysql", "host": "localhost", "port": 3306, "username": "root", "password": "root", "database": "zen-im", "entities": ["**/*.entity.{ts,js}"], "synchronize": true }
The role of the entities field is to find the entity file under the corresponding path at runtime according to the provided path string.
First of all, I suggest that it is better to use TypeORM.forRoot directly to import configuration, like this:
// app.module.ts const entitiesPaths = [join(__dirname, '..', '**', '*.entity.{ts,js}')] @Module({ imports: [ TypeOrmModule.forRoot({ "type": "mysql", "host": "localhost", "port": 3306, "username": "root", "password": "root", "database": "zen-im", "entities": entitiesPaths, "synchronize": true } )], controllers: [AppController], providers: [AppService], }) export class AppModule { }
The reason for this is to be able to control the search path for entities. In the above example, the path I control is any (**) subdirectory in the upper layer (..) directory of the current running js path (__dirname), and search for all files ending with .entity.js or .entity.ts Files as suffixes are scanned as entity files.
The reason why I used the upper layer (..) is because in my project, the above app.module.ts is placed in the src/module directory, and all my entity.ts are placed in src/entity Under contents:
src - module - app.module.ts - entity - user - user.entity.ts
The final generated js code will be placed in the project root directory/dist directory:
dist - module - app.module.js - entity - user - user.entity.js
So in actual operation, when configuring the entities field in app.module.js, you need to return to the previous layer (..) to find it. If in your project, app.module.ts is in the src directory, and the entity storage path is in the subdirectory where app.module.ts is located, you can directly configure it as:
join(__dirname, '**', "*.entity.{js,ts}")
If the path configuration is inconsistent, the following error will occur when running:
- EntityMetadataNotFoundError: No metadata for "your Entity" was found.
Notes on Entity column configuration
This place is more detailed. When I write the code, I use it according to the MyBatis-Plus annotations of Java, and when adding column definitions to the fields. Accidentally passing the name string directly as an argument:
import {Column, Entity, PrimaryColumn} from "typeorm"; @Entity('user') export class UserPo { /** * globally unique ID */ @PrimaryColumn('uid') uid: string; /** * username */ @Column('name') name: string; }
When running, it appears:
- DataTypeNotSupportedError: Data type "uid" in "UserPo.uid" is not supported by "mysql" database.
The reason is that if the parameter of the decorator @PrimaryColumn or @Column is a string, it is regarded as a database type! To pass an object, this object has a name field to represent the column name:
import {Column, Entity, PrimaryColumn} from "typeorm"; @Entity('user') export class UserPo { /** * A globally unique ID with no business logic meaning */ - @PrimaryColumn('uid') + @PrimaryColumn({ + name: 'uid' + }) uid: string; /** * username */ - @Column('name') + @Column({ + name: 'name' + }) name: string; }