TypeORM Entity_EntityTypeORM Entity @Entity() 依赖注入 / 注解 / 修饰器 PostgreSQL TypeScript



TypeORM Entity

Entity

Entity is a class that maps to a database table when using SQL database(or collection when using MongoDB).

​https://orkhan.gitbook.io/typeorm/docs/entities​

TypeORM Entity_Entity

@Entity()

依赖注入 / 注解 / 修饰器

You can create an entity by defining a ​​new class​​ and mark it with ​​@Entity()​​:

// step 1, 创建一个实体

import {
Entity,
PrimaryGeneratedColumn,
Column,
} from "typeorm";


@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column()
isActive: boolean;
}


TypeORM Entity_Entity_03

register entity

Each entity must be registered in your connection options:

// step 2, 注册一个实体
import {createConnection, Connection} from "typeorm";
import {User} from "./entity/User";

const connection: Connection = await createConnection({
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
entities: [User]
});


you can specify the whole directory with all entities inside - and all of them will be loaded:

// step 2, 注册一组实体
import {createConnection, Connection} from "typeorm";

const connection: Connection = await createConnection({
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
entities: ["entity/*.js"], // * 通配符
});


alias 别名

If you want to use an ​​alternative table name​​ for the ​​User entity​​ you can specify it in @Entity: ​​@Entity("my_users")​​.

If you want to set a base ​​prefix​​ for all database tables in your application you can specify ​​entityPrefix​​ in connection options.

connection options

​https://orkhan.gitbook.io/typeorm/docs/connection-options​

mysql / mariadb connection options

postgres / cockroachdb connection options

sqlite connection options

cordova connection options

react-native connection options

nativescript connection options

mssql connection options

mongodb connection options

sql.js connection options

expo connection options

Common connection options

type - Database type. (required)

"mysql", "postgres", "cockroachdb", "mariadb", "sqlite", "cordova", "nativescript", "oracle", "mssql", "mongodb", "sqljs", "react-native".

​https://orkhan.gitbook.io/typeorm/docs/connection-options#common-connection-options​

PostgreSQL

postgres

​https://orkhan.gitbook.io/typeorm/docs/connection-options#postgres-cockroachdb-connection-options​

TypeORM Entity_Entity_04

Decorators reference

​https://orkhan.gitbook.io/typeorm/docs/decorator-reference#entity​

create a database table named "users".

@Entity("users")
export class User {
//...
}


specify some additional entity options:

  • name - table name. If not specified, then table name is generated from entity class name.
  • database - database name in selected DB server.
  • schema - schema name.
  • engine - database engine to be set during table creation (works only in some databases).
  • synchronize - entities marked with false are skipped from schema updates.
  • orderBy - specifies default ordering for entities when using find operations and QueryBuilder.

@Entity({
name: "users",
engine: "MyISAM",
database: 'example_dev',
schema: 'schema_with_best_tables',
synchronize: false,
orderBy: {
name: "ASC",
id: "DESC"
}
})
export class User {
//...
}


refs

PostgreSQL

PostgreSQL is a powerful, open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.

TypeORM Entity_TypeORM_05