Entities
What is Entity?
Entity is a class that maps to a database table (or collection when using MongoDB). You can create an entity by defining a new class and mark it with @Entity()
:
This will create following database table:
Basic entities consist of columns and relations. Each entity MUST have a primary column (or ObjectId column if are using MongoDB).
Each entity must be registered in your data source options:
Or you can specify the whole directory with all entities inside - and all of them will be loaded:
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 data source options.
When using an entity constructor its arguments must be optional. Since ORM creates instances of entity classes when loading from the database, therefore it is not aware of your constructor arguments.
Learn more about parameters @Entity
in Decorators reference.
Entity columns
Since database tables consist of columns your entities must consist of columns too. Each entity class property you marked with @Column
will be mapped to a database table column.
Primary columns
Each entity must have at least one primary column. There are several types of primary columns:
@PrimaryColumn()
creates a primary column which takes any value of any type. You can specify the column type. If you don't specify a column type it will be inferred from the property type. The example below will create id withint
as type which you must manually assign before save.
@PrimaryGeneratedColumn()
creates a primary column which value will be automatically generated with an auto-increment value. It will createint
column withauto-increment
/serial
/sequence
/identity
(depend on the database and configuration provided). You don't have to manually assign its value before save - value will be automatically generated.
@PrimaryGeneratedColumn("uuid")
creates a primary column which value will be automatically generated withuuid
. Uuid is a unique string id. You don't have to manually assign its value before save - value will be automatically generated.
You can have composite primary columns as well:
When you save entities using save
it always tries to find an entity in the database with the given entity id (or ids). If id/ids are found then it will update this row in the database. If there is no row with the id/ids, a new row will be inserted.
To find an entity by id you can use manager.findOneBy
or repository.findOneBy
. Example:
Special columns
There are several special column types with additional functionality available:
@CreateDateColumn
is a special column that is automatically set to the entity's insertion date. You don't need to set this column - it will be automatically set.@UpdateDateColumn
is a special column that is automatically set to the entity's update time each time you callsave
of entity manager or repository. You don't need to set this column - it will be automatically set.@DeleteDateColumn
is a special column that is automatically set to the entity's delete time each time you call soft-delete of entity manager or repository. You don't need to set this column - it will be automatically set. If the @DeleteDateColumn is set, the default scope will be "non-deleted".@VersionColumn
is a special column that is automatically set to the version of the entity (incremental number) each time you callsave
of entity manager or repository. You don't need to set this column - it will be automatically set.
Spatial columns
MS SQL, MySQL, MariaDB, PostgreSQL and CockroachDB all support spatial columns. TypeORM's support for each varies slightly between databases, particularly as the column names vary between databases.
MS SQL and MySQL / MariaDB's TypeORM support exposes (and expects) geometries to be provided as well-known text (WKT), so geometry columns should be tagged with the string
type.
TypeORM's PostgreSQL and CockroachDB support uses GeoJSON as an interchange format, so geometry columns should be tagged either as object
or Geometry
(or subclasses, e.g. Point
) after importing geojson
types or using TypeORM built in GeoJSON types.
TypeORM tries to do the right thing, but it's not always possible to determine when a value being inserted or the result of a PostGIS function should be treated as a geometry. As a result, you may find yourself writing code similar to the following, where values are converted into PostGIS geometry
s from GeoJSON and into GeoJSON as json
:
Column types
TypeORM supports all of the most commonly used database-supported column types. Column types are database-type specific - this provides more flexibility on how your database schema will look like.
You can specify column type as first parameter of @Column
or in the column options of @Column
, for example:
or
If you want to specify additional type parameters you can do it via column options. For example:
or
Note about
bigint
type:bigint
column type, used in SQL databases, doesn't fit into the regularnumber
type and maps property to astring
instead.
Column types for mysql
/ mariadb
mysql
/ mariadb
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
, set
, json
, binary
, varbinary
, geometry
, point
, linestring
, polygon
, multipoint
, multilinestring
, multipolygon
, geometrycollection
, uuid
, inet4
, inet6
Note: UUID, INET4, and INET6 are only available for mariadb and for respective versions that made them available.
Column types for postgres
postgres
int
, int2
, int4
, int8
, smallint
, integer
, bigint
, decimal
, numeric
, real
, float
, float4
, float8
, double precision
, money
, character varying
, varchar
, character
, char
, text
, citext
, hstore
, bytea
, bit
, varbit
, bit varying
, timetz
, timestamptz
, timestamp
, timestamp without time zone
, timestamp with time zone
, date
, time
, time without time zone
, time with time zone
, interval
, bool
, boolean
, enum
, point
, line
, lseg
, box
, path
, polygon
, circle
, cidr
, inet
, macaddr
, tsvector
, tsquery
, uuid
, xml
, json
, jsonb
, int4range
, int8range
, numrange
, tsrange
, tstzrange
, daterange
, int4multirange
, int8multirange
, nummultirange
, tsmultirange
, tstzmultirange
, multidaterange
, geometry
, geography
, cube
, ltree
Column types for cockroachdb
cockroachdb
array
, bool
, boolean
, bytes
, bytea
, blob
, date
, numeric
, decimal
, dec
, float
, float4
, float8
, double precision
, real
, inet
, int
, integer
, int2
, int8
, int64
, smallint
, bigint
, interval
, string
, character varying
, character
, char
, char varying
, varchar
, text
, time
, time without time zone
, timestamp
, timestamptz
, timestamp without time zone
, timestamp with time zone
, json
, jsonb
, uuid
Note: CockroachDB returns all numeric data types as
string
. However if you omit column type and define your property asnumber
ORM willparseInt
string into number.
Column types for sqlite
/ cordova
/ react-native
/ expo
sqlite
/ cordova
/ react-native
/ expo
int
, int2
, int8
, integer
, tinyint
, smallint
, mediumint
, bigint
, decimal
, numeric
, float
, double
, real
, double precision
, datetime
, varying character
, character
, native character
, varchar
, nchar
, nvarchar2
, unsigned big int
, boolean
, blob
, text
, clob
, date
Column types for mssql
mssql
int
, bigint
, bit
, decimal
, money
, numeric
, smallint
, smallmoney
, tinyint
, float
, real
, date
, datetime2
, datetime
, datetimeoffset
, smalldatetime
, time
, char
, varchar
, text
, nchar
, nvarchar
, ntext
, binary
, image
, varbinary
, hierarchyid
, sql_variant
, timestamp
, uniqueidentifier
, xml
, geometry
, geography
, rowversion
Column types for oracle
oracle
char
, nchar
, nvarchar2
, varchar2
, long
, raw
, long raw
, number
, numeric
, float
, dec
, decimal
, integer
, int
, smallint
, real
, double precision
, date
, timestamp
, timestamp with time zone
, timestamp with local time zone
, interval year to month
, interval day to second
, bfile
, blob
, clob
, nclob
, rowid
, urowid
Column types for spanner
spanner
bool
, int64
, float64
, numeric
, string
, json
, bytes
, date
, timestamp
, array
enum
column type
enum
column typeenum
column type is supported by postgres
and mysql
. There are various possible column definitions:
Using typescript enums:
Note: String, numeric and heterogeneous enums are supported.
Using array with enum values:
set
column type
set
column typeset
column type is supported by mariadb
and mysql
. There are various possible column definitions:
Using typescript enums:
Using array with set
values:
simple-array
column type
simple-array
column typeThere is a special column type called simple-array
which can store primitive array values in a single string column. All values are separated by a comma. For example:
Will be stored in a single database column as Alexander,Alex,Sasha,Shurik
value. When you'll load data from the database, the names will be returned as an array of names, just like you stored them.
Note you MUST NOT have any comma in values you write.
simple-json
column type
simple-json
column typeThere is a special column type called simple-json
which can store any values which can be stored in database via JSON.stringify. Very useful when you do not have json type in your database and you want to store and load object without any hassle. For example:
Will be stored in a single database column as {"name":"John","nickname":"Malkovich"}
value. When you'll load data from the database, you will have your object/array/primitive back via JSON.parse
Columns with generated values
You can create column with generated value using @Generated
decorator. For example:
uuid
value will be automatically generated and stored into the database.
Besides "uuid" there is also "increment", "identity" (Postgres 10+ only) and "rowid" (CockroachDB only) generated types, however there are some limitations on some database platforms with this type of generation (for example some databases can only have one increment column, or some of them require increment to be a primary key).
Column options
Column options defines additional options for your entity columns. You can specify column options on @Column
:
List of available options in ColumnOptions
:
type: ColumnType
- Column type. One of the type listed above.name: string
- Column name in the database table. By default the column name is generated from the name of the property. You can change it by specifying your own name.length: number
- Column type's length. For example if you want to createvarchar(150)
type you specify column type and length options.width: number
- column type's display width. Used only for MySQL integer typesonUpdate: string
-ON UPDATE
trigger. Used only in MySQL.nullable: boolean
- Makes columnNULL
orNOT NULL
in the database. By default column isnullable: false
.update: boolean
- Indicates if column value is updated by "save" operation. If false, you'll be able to write this value only when you first time insert the object. Default value istrue
.insert: boolean
- Indicates if column value is set the first time you insert the object. Default value istrue
.select: boolean
- Defines whether or not to hide this column by default when making queries. When set tofalse
, the column data will not show with a standard query. By default column isselect: true
default: string
- Adds database-level column'sDEFAULT
value.primary: boolean
- Marks column as primary. Same if you use@PrimaryColumn
.unique: boolean
- Marks column as unique column (creates unique constraint).comment: string
- Database's column comment. Not supported by all database types.precision: number
- The precision for a decimal (exact numeric) column (applies only for decimal column), which is the maximum number of digits that are stored for the values. Used in some column types.scale: number
- The scale for a decimal (exact numeric) column (applies only for decimal column), which represents the number of digits to the right of the decimal point and must not be greater than precision. Used in some column types.zerofill: boolean
- PutsZEROFILL
attribute on to a numeric column. Used only in MySQL. Iftrue
, MySQL automatically adds theUNSIGNED
attribute to this column.unsigned: boolean
- PutsUNSIGNED
attribute on to a numeric column. Used only in MySQL.charset: string
- Defines a column character set. Not supported by all database types.collation: string
- Defines a column collation.enum: string[]|AnyEnum
- Used inenum
column type to specify list of allowed enum values. You can specify array of values or specify a enum class.enumName: string
- Defines the name for the used enum.asExpression: string
- Generated column expression. Used only in MySQL.generatedType: "VIRTUAL"|"STORED"
- Generated column type. Used only in MySQL.hstoreType: "object"|"string"
- Return type ofHSTORE
column. Returns value as string or as object. Used only in Postgres.array: boolean
- Used for postgres and cockroachdb column types which can be array (for example int[])transformer: { from(value: DatabaseType): EntityType, to(value: EntityType): DatabaseType }
- Used to marshal properties of arbitrary typeEntityType
into a typeDatabaseType
supported by the database. Array of transformers are also supported and will be applied in natural order when writing, and in reverse order when reading. e.g.[lowercase, encrypt]
will first lowercase the string then encrypt it when writing, and will decrypt then do nothing when reading.
Note: most of those column options are RDBMS-specific and aren't available in MongoDB
.
Entity inheritance
You can reduce duplication in your code by using entity inheritance.
For example, you have Photo
, Question
, Post
entities:
As you can see all those entities have common columns: id
, title
, description
. To reduce duplication and produce a better abstraction we can create a base class called Content
for them:
All columns (relations, embeds, etc.) from parent entities (parent can extend other entity as well) will be inherited and created in final entities.
Tree entities
TypeORM supports the Adjacency list and Closure table patterns of storing tree structures.
Adjacency list
Adjacency list is a simple model with self-referencing. Benefit of this approach is simplicity, drawback is you can't load a big tree at once because of join limitations. Example:
Closure table
A closure table stores relations between parent and child in a separate table in a special way. Its efficient in both reads and writes. To learn more about closure table take a look at this awesome presentation by Bill Karwin. Example:
Last updated