# EntityManager
API
dataSource
- The DataSource used byEntityManager
.
const dataSource = manager.dataSource
1
queryRunner
- The query runner used byEntityManager
. Used only in transactional instances of EntityManager.
const queryRunner = manager.queryRunner
1
transaction
- Provides a transaction where multiple database requests will be executed in a single database transaction. Learn more Transactions.
await manager.transaction(async (manager) => {
// NOTE: you must perform all database operations using the given manager instance
// it's a special instance of EntityManager working with this transaction
// and don't forget to await things here
})
1
2
3
4
5
2
3
4
5
query
- Executes a raw SQL query.
const rawData = await manager.query(`SELECT * FROM USERS`)
1
createQueryBuilder
- Creates a query builder use to build SQL queries. Learn more about QueryBuilder.
const users = await manager
.createQueryBuilder()
.select()
.from(User, "user")
.where("user.name = :name", { name: "John" })
.getMany()
1
2
3
4
5
6
2
3
4
5
6
hasId
- Checks if given entity has its primary column property defined.
if (manager.hasId(user)) {
// ... do something
}
1
2
3
2
3
getId
- Gets given entity's primary column property value. If the entity has composite primary keys then the returned value will be an object with names and values of primary columns.
const userId = manager.getId(user) // userId === 1
1
create
- Creates a new instance ofUser
. Optionally accepts an object literal with user properties which will be written into newly created user object.
const user = manager.create(User) // same as const user = new User();
const user = manager.create(User, {
id: 1,
firstName: "Timber",
lastName: "Saw",
}) // same as const user = new User(); user.firstName = "Timber"; user.lastName = "Saw";
1
2
3
4
5
6
2
3
4
5
6
merge
- Merges multiple entities into a single entity.
const user = new User()
manager.merge(User, user, { firstName: "Timber" }, { lastName: "Saw" }) // same as user.firstName = "Timber"; user.lastName = "Saw";
1
2
2
preload
- Creates a new entity from the given plain javascript object. If the entity already exist in the database, then it loads it (and everything related to it), replaces all values with the new ones from the given object, and returns the new entity. The new entity is actually loaded from the database entity with all properties replaced from the new object.
const partialUser = {
id: 1,
firstName: "Rizzrak",
profile: {
id: 1,
},
}
const user = await manager.preload(User, partialUser)
// user will contain all missing data from partialUser with partialUser property values:
// { id: 1, firstName: "Rizzrak", lastName: "Saw", profile: { id: 1, ... } }
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
save
- Saves a given entity or array of entities. If the entity already exists in the database, then it's updated. If the entity does not exist in the database yet, it's inserted. It saves all given entities in a single transaction (in the case of entity manager is not transactional). Also supports partial updating since all undefined properties are skipped. In order to make a valueNULL
, you must manually set the property to equalnull
.
await manager.save(user)
await manager.save([category1, category2, category3])
1
2
2
remove
- Removes a given entity or array of entities. It removes all given entities in a single transaction (in the case of entity, manager is not transactional).
await manager.remove(user)
await manager.remove([category1, category2, category3])
1
2
2
insert
- Inserts a new entity, or array of entities.
await manager.insert(User, {
firstName: "Timber",
lastName: "Timber",
})
await manager.insert(User, [
{
firstName: "Foo",
lastName: "Bar",
},
{
firstName: "Rizz",
lastName: "Rak",
},
])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
update
- Partially updates entity by a given update options or entity id.
await manager.update(User, { firstName: "Timber" }, { firstName: "Rizzrak" })
// executes UPDATE user SET firstName = Rizzrak WHERE firstName = Timber
await manager.update(User, 1, { firstName: "Rizzrak" })
// executes UPDATE user SET firstName = Rizzrak WHERE id = 1
1
2
3
4
5
2
3
4
5
upsert
- Inserts a new entity or array of entities unless they already exist in which case they are updated instead. Supported by AuroraDataApi, Cockroach, Mysql, Postgres, and Sqlite database drivers.
await manager.upsert(
User,
[
{ externalId: "abc123", firstName: "Rizzrak" },
{ externalId: "bca321", firstName: "Karzzir" },
],
["externalId"],
)
/** executes
* INSERT INTO user
* VALUES
* (externalId = abc123, firstName = Rizzrak),
* (externalId = cba321, firstName = Karzzir),
* ON CONFLICT (externalId) DO UPDATE firstName = EXCLUDED.firstName
**/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
delete
- Deletes entities by entity id, ids or given conditions:
await manager.delete(User, 1)
await manager.delete(User, [1, 2, 3])
await manager.delete(User, { firstName: "Timber" })
1
2
3
2
3
increment
- Increments some column by provided value of entities that match given options.
await manager.increment(User, { firstName: "Timber" }, "age", 3)
1
decrement
- Decrements some column by provided value that match given options.
await manager.decrement(User, { firstName: "Timber" }, "age", 3)
1
count
- Counts entities that matchFindOptions
. Useful for pagination.
const count = await manager.count(User, {
where: {
firstName: "Timber",
},
})
1
2
3
4
5
2
3
4
5
countBy
- Counts entities that matchFindOptionsWhere
. Useful for pagination.
const count = await manager.countBy(User, { firstName: "Timber" })
1
find
- Finds entities that match givenFindOptions
.
const timbers = await manager.find(User, {
where: {
firstName: "Timber",
},
})
1
2
3
4
5
2
3
4
5
findBy
- Finds entities that match givenFindWhereOptions
.
const timbers = await manager.findBy(User, {
firstName: "Timber",
})
1
2
3
2
3
findAndCount
- Finds entities that match givenFindOptions
. Also counts all entities that match given conditions, but ignores pagination settings (from and take options).
const [timbers, timbersCount] = await manager.findAndCount(User, {
where: {
firstName: "Timber",
},
})
1
2
3
4
5
2
3
4
5
findAndCountBy
- Finds entities that match givenFindOptionsWhere
. Also counts all entities that match given conditions, but ignores pagination settings (from and take options).
const [timbers, timbersCount] = await manager.findAndCount(User, {
firstName: "Timber",
})
1
2
3
2
3
findOne
- Finds the first entity that matches givenFindOptions
.
const timber = await manager.findOne(User, {
where: {
firstName: "Timber",
},
})
1
2
3
4
5
2
3
4
5
findOneBy
- Finds the first entity that matches givenFindOptionsWhere
.
const timber = await manager.findOne(User, { firstName: "Timber" })
1
findOneOrFail
- Finds the first entity that matches some id or find options. Rejects the returned promise if nothing matches.
const timber = await manager.findOneOrFail(User, {
where: {
firstName: "Timber",
},
})
1
2
3
4
5
2
3
4
5
findOneByOrFail
- Finds the first entity that matches givenFindOptions
. Rejects the returned promise if nothing matches.
const timber = await manager.findOneByOrFail(User, { firstName: "Timber" })
1
clear
- Clears all the data from the given table (truncates/drops it).
await manager.clear(User)
1
getRepository
- GetsRepository
to perform operations on a specific entity. Learn more about Repositories.
const userRepository = manager.getRepository(User)
1
getTreeRepository
- GetsTreeRepository
to perform operations on a specific entity. Learn more about Repositories.
const categoryRepository = manager.getTreeRepository(Category)
1
getMongoRepository
- GetsMongoRepository
to perform operations on a specific entity. Learn more about MongoDB.
const userRepository = manager.getMongoRepository(User)
1
withRepository
- Gets custom repository instance used in a transaction. Learn more about Custom repositories.
const myUserRepository = manager.withRepository(UserRepository)
1
release
- Releases query runner of an entity manager. Used only when query runner was created and managed manually.
await manager.release()
1