dataSource - The DataSource used by EntityManager.
constdataSource=manager.dataSource
queryRunner - The query runner used by EntityManager. Used only in transactional instances of EntityManager.
constqueryRunner=manager.queryRunner
transaction - Provides a transaction where multiple database requests will be executed in a single database transaction. Learn more Transactions.
awaitmanager.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.
constrawData=awaitmanager.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, sqljsconstrawData=awaitmanager.query('SELECT * FROM USERS WHERE name = ? and age = ?', [ 'John',24 ])// aurora-postgres, cockroachdb, postgresconstrawData=awaitmanager.query('SELECT * FROM USERS WHERE name = $1 and age = $2', ['John',24])// oracleconstrawData=awaitmanager.query('SELECT * FROM USERS WHERE name = :1 and age = :2', ['John',24])// spannerconstrawData=awaitmanager.query('SELECT * FROM USERS WHERE name = @param0 and age = @param1', [ 'John',24 ])// mssqlconstrawData=awaitmanager.query('SELECT * FROM USERS WHERE name = @0 and age = @1', [ 'John',24 ])
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.
if (manager.hasId(user)) {// ... do something}
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.
constuserId=manager.getId(user) // userId === 1
create - Creates a new instance of User. Optionally accepts an object literal with user properties which will be written into newly created user object.
constuser=manager.create(User) // same as const user = new User();constuser=manager.create(User, { id:1, firstName:"Timber", lastName:"Saw",}) // same as const user = new User(); user.firstName = "Timber"; user.lastName = "Saw";
merge - Merges multiple entities into a single entity.
constuser=newUser()manager.merge(User, user, { firstName:"Timber" }, { lastName:"Saw" }) // same as user.firstName = "Timber"; user.lastName = "Saw";
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.
constpartialUser= { id:1, firstName:"Rizzrak", profile: { id:1, },}constuser=awaitmanager.preload(User, partialUser)// user will contain all missing data from partialUser with partialUser property values:// { id: 1, firstName: "Rizzrak", lastName: "Saw", profile: { id: 1, ... } }
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).
update - Partially updates entity by a given update options or entity id.
awaitmanager.update(User, { age:18 }, { category:"ADULT" })// executes UPDATE user SET category = ADULT WHERE age = 18awaitmanager.update(User,1, { firstName:"Rizzrak" })// executes UPDATE user SET firstName = Rizzrak WHERE id = 1
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.
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).