TypeORM v0.3.0 Release Notes
Release Date: 2022-03-17 // about 2 years ago-
๐ Changes in the version includes changes from the
next
branch andtypeorm@next
version. They were pending their migration from 2018. Finally, they are in the master branch and master version.๐ Features
compilation
target
now ises2020
. This requires Node.JS version14+
TypeORM now properly works when installed within different node_modules contexts (often happen if TypeORM is a dependency of another library or TypeORM is heavily used in monorepo projects)
Connection
was renamed toDataSource
. OldConnection
is still there, but now it's deprecated. It will be completely removed in next version. New API:
export const dataSource = new DataSource({ // ... options ... }) // load entities, establish db connection, sync schema, etc. await dataSource.connect()
Previously, you could use
new Connection()
,createConnection()
,getConnectionManager().create()
, etc. ๐ They all deprecated in favour of new syntax you can see above.๐ New way gives you more flexibility and simplicity in usage.
- ๐ new custom repositories syntax:
export const UserRepository = myDataSource.getRepository(UserEntity).extend({ findUsersWithPhotos() { return this.find({ relations: { photos: true } }) } })
Old ways of custom repository creation were dropped.
โ added new option on relation load strategy called
relationLoadStrategy
. Relation load strategy is used on entity load and determines how relations must be loaded when you query entities and their relations from the database. Used onfind*
methods andQueryBuilder
. Value can be set tojoin
orquery
.join
- loads relations using SQLJOIN
expressionquery
- executes separate SQL queries for each relation
0๏ธโฃ Default is
join
, but default can be set inConnectionOptions
:createConnection({ /* ... */ relationLoadStrategy: "query" })
Also, it can be set per-query in
find*
methods:userRepository.find({ relations: { photos: true } })
And QueryBuilder:
userRepository .createQueryBuilder() .setRelationLoadStrategy("query")
For queries returning big amount of data, we recommend to use
query
strategy, because it can be a more performant approach to query relations.- โ added new
findOneBy
,findOneByOrFail
,findBy
,countBy
,findAndCountBy
methods toBaseEntity
,EntityManager
andRepository
:
const users = await userRepository.findBy({ name: "Michael" })
Overall
find*
andcount*
method signatures where changed, read the "breaking changes" section for more info.- ๐ new
select
type signature inFindOptions
(used infind*
methods):
userRepository.find({ select: { id: true, firstName: true, lastName: true, } })
Also, now it's possible to specify select columns of the loaded relations:
userRepository.find({ select: { id: true, firstName: true, lastName: true, photo: { id: true, filename: true, album: { id: true, name: true, } } } })
- ๐ new
relations
type signature inFindOptions
(used infind*
methods):
userRepository.find({ relations: { contacts: true, photos: true, } })
To load nested relations use a following signature:
userRepository.find({ relations: { contacts: true, photos: { album: true, }, } })
- ๐ new
order
type signature inFindOptions
(used infind*
methods):
userRepository.find({ order: { id: "ASC" } })
๐ Now supports nested order by-s:
userRepository.find({ order: { photos: { album: { name: "ASC" }, }, } })
- ๐ new
where
type signature inFindOptions
(used infind*
methods) now allows to build nested statements with conditional relations, for example:
userRepository.find({ where: { photos: { album: { name: "profile" } } } })
Gives you users who have photos in their "profile" album.
FindOperator
-s can be applied for relations inwhere
statement, for example:
userRepository.find({ where: { photos: MoreThan(10), } })
Gives you users with more than 10 photos.
boolean
can be applied for relations inwhere
statement, for example:
userRepository.find({ where: { photos: true } })
๐ฅ BREAKING CHANGES
minimal Node.JS version requirement now is
14+
โฌ๏ธ drop
ormconfig
support.ormconfig
still works if you use deprecated methods, ๐ however we do not recommend using it anymore, because it's support will be completely dropped in0.4.0
. If you want to have your connection options defined in a separate file, you can still do it like this:
import ormconfig from "./ormconfig.json" const MyDataSource = new DataSource(require("./ormconfig.json"))
Or even more type-safe approach with
resolveJsonModule
intsconfig.json
enabled:import ormconfig from "./ormconfig.json" const MyDataSource = new DataSource(ormconfig)
๐ But we do not recommend use this practice, because from
0.4.0
you'll only be able to specify entities / subscribers / migrations using direct references to entity classes / schemas (see "deprecations" section).๐ We won't be supporting all
ormconfig
extensions (e.g.json
,js
,ts
,yaml
,xml
,env
).๐ support for previously deprecated
migrations:*
commands was removed. Usemigration:*
commands instead.๐ all commands were re-worked. Please refer to new CLI documentation.
๐
cli
option fromBaseConnectionOptions
(nowBaseDataSourceOptions
options) was removed (since CLI commands were re-worked).๐ now migrations are running before schema synchronization if you have both pending migrations and schema synchronization pending (it works if you have both
migrationsRun
andsynchronize
enabled in connection options).aurora-data-api
driver now is calledaurora-mysql
aurora-data-api-pg
driver now is calledaurora-postgres
EntityManager.connection
is nowEntityManager.dataSource
Repository
now has a constructor (breaks classes extending Repository with custom constructor)๐
@TransactionRepository
,@TransactionManager
,@Transaction
decorators were completely removed. These decorators do the things out of the TypeORM scope.Only junction table names shortened.
MOTIVATION: We must shorten only table names generated by TypeORM. It's user responsibility to name tables short if their RDBMS limit table name length since it won't make sense to have table names as random hashes. ๐ It's really better if user specify custom table name into
@Entity
decorator. Also, for junction table it's possible to set a custom name using@JoinTable
decorator.findOne()
signature without parameters was dropped. If you need a single row from the db you can use a following syntax:
const [user] = await userRepository.find()
This change was made to prevent user confusion. ๐ See this issue for details.
findOne(id)
signature was dropped. Use following syntax instead:
const user = await userRepository.findOneBy({ id: id // where id is your column name })
This change was made to provide a more type-safe approach for data querying. ๐จ Due to this change you might need to refactor the way you load entities using MongoDB driver.
findOne
,findOneOrFail
,find
,count
,findAndCount
methods now only acceptFindOptions
as parameter, e.g.:
const users = await userRepository.find({ where: { /* conditions */ }, relations: { /* relations */ } })
To supply
where
conditions directly withoutFindOptions
new methods were added:findOneBy
,findOneByOrFail
,findBy
,countBy
,findAndCountBy
. Example:const users = await userRepository.findBy({ name: "Michael" })
This change was required to simply current
find*
andcount*
methods typings, ๐ improve type safety and prevent user confusion.- ๐
findByIds
was deprecated, usefindBy
method instead in conjunction withIn
operator, for example:
userRepository.findBy({ id: In([1, 2, 3]) })
This change was made to provide a more type-safe approach for data querying.
findOne
andQueryBuilder.getOne()
now returnnull
instead ofundefined
in the case if it didn't find anything in the database. Logically it makes more sense to returnnull
.findOne
now limits returning rows to 1 at database level.
NOTE:
FOR UPDATE
locking does not work withfindOne
in Oracle sinceFOR UPDATE
cannot be used withFETCH NEXT
in a single query.where
inFindOptions
(e.g.find({ where: { ... })
) is more sensitive to input criteria now.FindConditions
(where
inFindOptions
) was renamed toFindOptionsWhere
.๐
null
as value inwhere
used infind*
methods is not supported anymore. Now you must explicitly useIsNull()
operator.
Before:
userRepository.find({ where: { photo: null } })
After:
userRepository.find({ where: { photo: IsNull() } })
This change was made to make it more transparent on how to add "IS NULL" statement to final SQL, because before it bring too much confusion for ORM users.
- if you had entity properties of a non-primitive type (except Buffer) defined as columns,
then you won't be able to use it in
find*
'swhere
. Example:
Before for the
@Column(/*...*/) membership: MembershipKind
you could have a query like:userRepository.find({ membership: new MembershipKind("premium") })
now, you need to wrap this value into
Equal
operator:userRepository.find({ membership: Equal(new MembershipKind("premium")) })
This change is due to type-safety improvement new
where
signature brings.๐
order
inFindOptions
(used infind*
methods) doesn't support ordering by relations anymore. Define relation columns, and order by them instead.๐
where
inFindOptions
(used infind*
methods) previously supportedObjectLiteral
andstring
types. Now both signatures were removed. ObjectLiteral was removed because it seriously breaks the type safety, andstring
doesn't make sense in the context ofFindOptions
. UseQueryBuilder
instead.MongoRepository
andMongoEntityManager
now use new types calledMongoFindManyOptions
andMongoFindOneOptions
for theirfind*
methods.๐
primary relation
(e.g.@ManyToOne(() => User, { primary: true }) user: User
) support is removed. You still have an ability to use foreign keys as your primary keys, however now you must explicitly define a column marked as primary.
Example, before:
@ManyToOne(() => User, { primary: true }) user: User
Now:
@PrimaryColumn() userId: number @ManyToOne(() => User) user: User
Primary column name must match the relation name + join column name on related entity. If related entity has multiple primary keys, and you want to point to multiple primary keys, you can define multiple primary columns the same way:
@PrimaryColumn() userFirstName: string @PrimaryColumn() userLastName: string @ManyToOne(() => User) user: User
This change was required to simplify ORM internals and introduce new features.
prefix relation id columns contained in embedded entities (#7432)
find by Date object in sqlite driver (#7538)
๐ issue with non-reliable
new Date(ISOString)
parsing (#7796)
๐ DEPRECATIONS
๐ all CLI commands do not support
ormconfig
anymore. You must specify a file with data source instance instead.๐
entities
,migrations
,subscribers
options insideDataSourceOptions
acceptingstring
directories support is deprecated. You'll be only able to pass entity references in the future versions.0๏ธโฃ all container-related features (
UseContainerOptions
,ContainedType
,ContainerInterface
,defaultContainer
, ๐useContainer
,getFromContainer
) are deprecated.๐ EntityManager's
getCustomRepository
used within transactions is deprecated. UsewithRepository
method instead.๐
Connection.isConnected
is deprecated. Use.isInitialized
instead.๐
select
inFindOptions
(used infind*
methods) used as an array of property names is deprecated. Now you should use a new object-literal notation. Example:
๐ Deprecated way of loading entity relations:
userRepository.find({ select: ["id", "firstName", "lastName"] })
๐ New way of loading entity relations:
userRepository.find({ select: { id: true, firstName: true, lastName: true, } })
This change is due to type-safety improvement new
select
signature brings.- ๐
relations
inFindOptions
(used infind*
methods) used as an array of relation names is deprecated. Now you should use a new object-literal notation. Example:
๐ Deprecated way of loading entity relations:
userRepository.find({ relations: ["contacts", "photos", "photos.album"] })
๐ New way of loading entity relations:
userRepository.find({ relations: { contacts: true, photos: { album: true } } })
This change is due to type-safety improvement new
relations
signature brings.๐
join
inFindOptions
(used infind*
methods) is deprecated. UseQueryBuilder
to build queries containing manual joins.๐
Connection
,ConnectionOptions
are deprecated, new names to use are:DataSource
andDataSourceOptions
. To create the same connection you had before use a new syntax:new DataSource({ /*...*/ })
.๐
createConnection()
,createConnections()
are deprecated, sinceConnection
is calledDataSource
now, to create a connection and connect to the database simply do:
const myDataSource = new DataSource({ /*...*/ }) await myDataSource.connect()
- ๐
getConnection()
is deprecated. To have a globally accessible connection, simply export your data source and use it in places you need it:
export const myDataSource = new DataSource({ /*...*/ }) // now you can use myDataSource anywhere in your application
getManager()
,getMongoManager()
,getSqljsManager()
,getRepository()
,getTreeRepository()
,getMongoRepository()
,createQueryBuilder()
are all deprecated now. Use globally accessible data source instead:
export const myDataSource = new DataSource({ /*...*/ }) export const Manager = myDataSource.manager export const UserRepository = myDataSource.getRepository(UserEntity) export const PhotoRepository = myDataSource.getRepository(PhotoEntity) // ...
- ๐
getConnectionManager()
andConnectionManager
itself are deprecated - nowConnection
is calledDataSource
, and each data source can be defined in exported variable. If you want to have a collection of data sources, just define them in a variable, simply as:
const dataSource1 = new DataSource({ /*...*/ }) const dataSource2 = new DataSource({ /*...*/ }) const dataSource3 = new DataSource({ /*...*/ }) export const MyDataSources = { dataSource1, dataSource2, dataSource3, }
๐
getConnectionOptions()
is deprecated - in next version we are going to implement different mechanism of connection options loading๐
AbstractRepository
is deprecated. Use new way of custom repositories creation.๐
Connection.name
andBaseConnectionOptions.name
are deprecated. Connections don't need names anymore since we are going to drop all related methods relying on this property.๐ all deprecated signatures will be removed in
0.4.0
EXPERIMENTAL FEATURES NOT PORTED FROM NEXT BRANCH
observers
- we will consider returning them back with new API in future versionsalternative find operators
- using$any
,$in
,$like
and other operators inwhere
condition.