Joshua's Docs - TypeORM Cheatsheet (WIP)

===== WIP ======



What & Link Type Description
TypeORM Official Docs Docs Official Documentation
TypeORM Migrations Guide Docs Official guide to writing Migrations
Sequelize JS Docs Docs Competing NodeJS ORM. Syntax is much closer to Laravel Eloquent, but has less TS and non-node support.


Columns - Decorators

Decorator What Type Eloquent Equivalent
@column() A regular column any (specify by type of class member) $table->{{type}}
@PrimaryGeneratedColumn() [AUTO] Auto-incr. PK number $table->increments('id')*
@CreateDateColumn() [AUTO] Created Timestamp Date $table->timestamps()

* = BigInt is not yet completely supported in JS land and not in TypeOrm. So there is not really an equivalent to Eloquent's $table->bigIncrements() or $table->bigInteger(). In fact, this is something to be wary of if you are coming from Laravel, since default migrations now use BigInts over there.

PrimaryGeneratedColumn(), in MySQL, generates an INT() column, which should have a max size of 2147483647 (unsigned).

Columns - Data Types

Full list can be found here

Cheatsheet for MySQL:

___ ___ ___ ___
bit int integer tinyint
smallint mediumint bigint float
double double precision dec decimal
numeric fixed bool boolean
date datetime timestamp time
year char nchar national char
varchar nvarchar national varchar text
tinytext mediumtext blob longtext
tinyblob mediumblob longblob enum
json binary varbinary geometry
point linestring polygon multipoint
multilinestring multipolygon geometrycollection ---

Columns - "Gotchas"

  • When creating a foreign column, with something like @ManyToOne, the generator will automatically put the string 'id' at the end of whatever you put. So this:
    public userId: User;
    ... Would actually generate a column name of `"userIdId". Whoops!
Markdown Source Last Updated:
Thu Feb 27 2020 06:30:23 GMT+0000 (Coordinated Universal Time)
Markdown Source Created:
Mon Aug 19 2019 17:06:24 GMT+0000 (Coordinated Universal Time)
© 2024 Joshua Tzucker, Built with Gatsby