xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

TypeORM Entity All In One

TypeORM Entity All In One

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

@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;
}

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

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 {
  //...
}

TypeORM

TypeScript & Node.js

https://nestjs.com/

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.

https://www.postgresql.org/

https://www.postgresql.org/docs/12/index.html

PostgreSQL 12.2 手册

http://www.postgres.cn/v2/document

http://www.postgres.cn/docs/12/

fullstack (Angular 10.x + Nest.js + PostgreSQL + TypeORM + TypeScript)

https://www.sololearn.com/Course/fullstack/

https://github.com/SoloLearn-Courses/nest_init



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2020-09-13 18:51  xgqfrms  阅读(504)  评论(6编辑  收藏  举报