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

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

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 发布文章使用:只允许注册用户才可以访问!


TypeORM Entity的更多相关文章

  1. 在 Nest.js 中使用 MongoDB 与 TypeORM

    在 Nest.js 中使用 MongoDB 与 TypeORM 首先要在 database 文件夹里建立DatabaseModule模块文件, // database/database.module. ...

  2. Nestjs 链接mysql

    文档 下插件 λ yarn add @nestjs/typeorm typeorm mysql 创建 cats模块, 控制器,service λ nest g mo cats λ nest g co ...

  3. [Nest] 05.nest之数据库

    数据库 Nest 与数据库无关,允许您轻松地与任何 SQL 或 NoSQL 数据库集成.根据您的偏好,您有许多可用的选项.一般来说,将 Nest 连接到数据库只需为数据库加载一个适当的 Node.js ...

  4. ORM All In One

    ORM All In One ORM Object Relational Mapping https://en.wikipedia.org/wiki/Object-relational_mapping ...

  5. 【译】Nodejs最好的ORM - TypeORM

    TypeORM github: https://github.com/typeorm/typeorm 这篇译文是从TypeORM github上的使用说明上翻译过来的,已经提交PR并merge到库中了 ...

  6. Nodejs最好的ORM - TypeORM

    TypeORM是一个采用TypeScript编写的用于Node.js的优秀ORM框架,支持使用TypeScript或Javascript(ES5, ES6, ES7)开发.目标是保持支持最新的Java ...

  7. 【nodejs】让nodejs像后端mvc框架(asp.net mvc)一orm篇【如EF般丝滑】typeorm介绍(8/8)

    文章目录 前情概要 在使用nodejs开发过程中,刚好碰到需要做一个小工具,需要用到数据库存储功能.而我又比较懒,一个小功能不想搞一个nodejs项目,又搞一个后端项目.不如直接在nodejs里面把对 ...

  8. typeORM 多对多关系不同情况的处理

    本文以RBAC权限管理中的用户和角色举例,两个实体存在多对多的关系,一个用户拥有多个角色,一个角色属于多个用户.typeorm的中文文档没有对自定义中间表的说明,发现英文有相关说明,但示例代码貌似有问 ...

  9. nodejs环境使用Typeorm连接查询Oracle

    首先是typeorm的官方地址, 国内有人翻了中文版,不保证时效性 ·通过npm安装下列包: typeorm //typeorm连接数据库 @types/node //类型系统 typescript ...

随机推荐

  1. (Sqlserver)sql求连续问题

    题目一:create table etltable( name varchar(20) , seq int, money int); create table etltarget ( name var ...

  2. websocket心跳重连 websocket-heartbeat-js

    初探和实现websocket心跳重连(npm: websocket-heartbeat-js) 心跳重连缘由 websocket是前后端交互的长连接,前后端也都可能因为一些情况导致连接失效并且相互之间 ...

  3. (转载)微软数据挖掘算法:Microsoft 决策树分析算法(1)

    微软数据挖掘算法:Microsoft 目录篇 介绍: Microsoft 决策树算法是分类和回归算法,用于对离散和连续属性进行预测性建模. 对于离散属性,该算法根据数据集中输入列之间的关系进行预测. ...

  4. JavaScript与多线程的不解之缘!

    前言 对于前端开发者来说,多线程是一个比较陌生的话题.因为JavaScript是单线程语言.也就是说,所有任务只能在一个线程上完成,一次只能做一件事.前面的任务没做完,后面的任务只能等着. UI渲染与 ...

  5. Spring Boot构建 RESTful 风格应用

    Spring Boot构建 RESTful 风格应用 1.Spring Boot构建 RESTful 风格应用 1.1 实战 1.1.1 创建工程 1.1.2 构建实体类 1.1.4 查询定制 1.1 ...

  6. centos 7_本地源制作

    1.安装工具 yum install yum-utils createrepo yum-plugin-priorities   2.自己创建一个阿里源 vim /etc/yum.repos.d/ope ...

  7. jQuery实战笔记

    文章目录 1.标签隐藏显示 2.时间戳转换 3.radio单选框获取选中 4.判断字符串是否为数字类型 5.tab标签页实现 6.标签点击事件 7.jquery跳转链接 8.jquery修改图片url ...

  8. 使用Docker Compose编排Spring Cloud微服务

    文章目录 微服务构建实例 简化Compose的编写 编排高可用的Eureka Server 编排高可用Spring Cloud微服务集群及动态伸缩 微服务项目名称 项目微服务中的角色 microser ...

  9. linux环境变量 shell变量 command not found解决方法(转)

    在Ubuntu.centos中有如下几个文件可以设置环境变量1./etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文 ...

  10. 20.LVM

    1.在硬盘分好区或者部署为RAID 磁盘阵列之后,再想修改硬盘分区大小就不容易了.换句话说,当用户想要随着实际需求的变化调整硬盘分区的大小时,会受到硬盘"灵活性"的限制. 这时就需 ...