EntityManager API

  • dataSource - The DataSource used by EntityManager.

const dataSource = manager.dataSource
  • queryRunner - The query runner used by EntityManager. Used only in transactional instances of EntityManager.

const queryRunner = manager.queryRunner
  • 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
})
  • query - Executes a raw SQL query.

const rawData = await manager.query(`SELECT * FROM USERS`)

// You can also use parameters to avoid SQL injection
// The syntax differs between the drivers

// aurora-mysql, better-sqlite3, capacitor, cordova,
// expo, mariadb, mysql, nativescript, react-native,
// sap, sqlite, sqljs
const rawData = await manager.query(
    "SELECT * FROM USERS WHERE name = ? and age = ?",
    ["John", 24],
)

// aurora-postgres, cockroachdb, postgres
const rawData = await manager.query(
    "SELECT * FROM USERS WHERE name = $1 and age = $2",
    ["John", 24],
)

// oracle
const rawData = await manager.query(
    "SELECT * FROM USERS WHERE name = :1 and age = :2",
    ["John", 24],
)

// spanner
const rawData = await manager.query(
    "SELECT * FROM USERS WHERE name = @param0 and age = @param1",
    ["John", 24],
)

// mssql
const rawData = await manager.query(
    "SELECT * FROM USERS WHERE name = @0 and age = @1",
    ["John", 24],
)
  • sql - Executes a raw SQL query using template literals.

Learn more about using the SQL Tag syntax.

  • createQueryBuilder - Creates a query builder use to build SQL queries. Learn more about QueryBuilder.

  • hasId - Checks if given entity has its primary column property defined.

  • 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.

  • create - Creates a new instance of User. Optionally accepts an object literal with user properties which will be written into newly created user object.

  • merge - Merges multiple entities into a single entity.

  • 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.

  • 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 value NULL, you must manually set the property to equal null.

  • 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).

  • insert - Inserts a new entity, or array of entities.

  • update - Updates entities by entity id, ids or given conditions. Sets fields from supplied partial entity.

  • updateAll - Updates all entities of target type (without WHERE clause). Sets fields from supplied partial entity.

  • 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.

When an upsert operation results in an update (due to a conflict), special columns like @UpdateDateColumn and @VersionColumn are automatically updated to their current values.

  • delete - Deletes entities by entity id, ids or given conditions.

  • deleteAll - Deletes all entities of target type (without WHERE clause).

Refer also to the clear method, which performs database TRUNCATE TABLE operation instead.

  • increment - Increments some column by provided value of entities that match given options.

  • decrement - Decrements some column by provided value that match given options.

  • exists - Check whether any entity exists that matches FindOptions.

  • existsBy - Checks whether any entity exists that matches FindOptionsWhere.

  • count - Counts entities that match FindOptions. Useful for pagination.

  • countBy - Counts entities that match FindOptionsWhere. Useful for pagination.

  • find - Finds entities that match given FindOptions.

  • findBy - Finds entities that match given FindWhereOptions.

  • findAndCount - Finds entities that match given FindOptions. Also counts all entities that match given conditions, but ignores pagination settings (from and take options).

  • findAndCountBy - Finds entities that match given FindOptionsWhere. Also counts all entities that match given conditions, but ignores pagination settings (from and take options).

  • findOne - Finds the first entity that matches given FindOptions.

  • findOneBy - Finds the first entity that matches given FindOptionsWhere.

  • findOneOrFail - Finds the first entity that matches some id or find options. Rejects the returned promise if nothing matches.

  • findOneByOrFail - Finds the first entity that matches given FindOptions. Rejects the returned promise if nothing matches.

  • clear - Clears all the data from the given table (truncates/drops it).

  • getRepository - Gets Repository to perform operations on a specific entity. Learn more about Repositories.

  • getTreeRepository - Gets TreeRepository to perform operations on a specific entity. Learn more about Repositories.

  • getMongoRepository - Gets MongoRepository to perform operations on a specific entity. Learn more about MongoDB.

  • withRepository - Gets custom repository instance used in a transaction. Learn more about Custom repositories.

  • release - Releases query runner of an entity manager. Used only when query runner was created and managed manually.

Last updated