Difference between revisions of "TypeOrm"
(Created page with " Insert using Query Builder You can create INSERT queries using QueryBuilder. Examples: <syntaxhighlight lang="javascript"> import {getConnection} from "typeorm"; await g...") |
|||
Line 2: | Line 2: | ||
− | + | =Code Samples= | |
− | Insert using Query Builder | + | ==Insert using Query Builder== |
You can create INSERT queries using QueryBuilder. Examples: | You can create INSERT queries using QueryBuilder. Examples: | ||
Line 22: | Line 22: | ||
This is the most efficient way in terms of performance to insert rows into your database. You can also perform bulk insertions this way. | This is the most efficient way in terms of performance to insert rows into your database. You can also perform bulk insertions this way. | ||
+ | |||
+ | ==Transaction Management== | ||
+ | |||
+ | '''Using QueryRunner to create and control state of single database connection''' | ||
+ | |||
+ | QueryRunner provides a single database connection. Transactions are organized using query runners. Single transactions can only be established on a single query runner. You can manually create a query runner instance and use it to manually control transaction state. Example: | ||
+ | |||
+ | <syntaxhighlight lang="javascript"> | ||
+ | |||
+ | import {getConnection} from "typeorm"; | ||
+ | |||
+ | // get a connection and create a new query runner | ||
+ | const connection = getConnection(); | ||
+ | const queryRunner = connection.createQueryRunner(); | ||
+ | |||
+ | // establish real database connection using our new query runner | ||
+ | await queryRunner.connect(); | ||
+ | |||
+ | // now we can execute any queries on a query runner, for example: | ||
+ | await queryRunner.query("SELECT * FROM users"); | ||
+ | |||
+ | // we can also access entity manager that works with connection created by a query runner: | ||
+ | const users = await queryRunner.manager.find(User); | ||
+ | |||
+ | // lets now open a new transaction: | ||
+ | await queryRunner.startTransaction(); | ||
+ | |||
+ | try { | ||
+ | |||
+ | // execute some operations on this transaction: | ||
+ | await queryRunner.manager.save(user1); | ||
+ | await queryRunner.manager.save(user2); | ||
+ | await queryRunner.manager.save(photos); | ||
+ | |||
+ | // commit transaction now: | ||
+ | await queryRunner.commitTransaction(); | ||
+ | |||
+ | } catch (err) { | ||
+ | |||
+ | // since we have errors lets rollback changes we made | ||
+ | await queryRunner.rollbackTransaction(); | ||
+ | |||
+ | } finally { | ||
+ | |||
+ | // you need to release query runner which is manually created: | ||
+ | await queryRunner.release(); | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | There are 3 methods to control transactions in QueryRunner: | ||
+ | |||
+ | *'''startTransaction''' - starts a new transaction inside the query runner instance. | ||
+ | *'''commitTransaction''' - commits all changes made using the query runner instance. | ||
+ | *'''rollbackTransaction''' - rolls all changes made using the query runner instance back. |
Latest revision as of 10:14, 6 February 2019
Code Samples
Insert using Query Builder
You can create INSERT queries using QueryBuilder. Examples:
import {getConnection} from "typeorm";
await getConnection()
.createQueryBuilder()
.insert()
.into(User)
.values([
{ firstName: "Timber", lastName: "Saw" },
{ firstName: "Phantom", lastName: "Lancer" }
])
.execute();
This is the most efficient way in terms of performance to insert rows into your database. You can also perform bulk insertions this way.
Transaction Management
Using QueryRunner to create and control state of single database connection
QueryRunner provides a single database connection. Transactions are organized using query runners. Single transactions can only be established on a single query runner. You can manually create a query runner instance and use it to manually control transaction state. Example:
import {getConnection} from "typeorm";
// get a connection and create a new query runner
const connection = getConnection();
const queryRunner = connection.createQueryRunner();
// establish real database connection using our new query runner
await queryRunner.connect();
// now we can execute any queries on a query runner, for example:
await queryRunner.query("SELECT * FROM users");
// we can also access entity manager that works with connection created by a query runner:
const users = await queryRunner.manager.find(User);
// lets now open a new transaction:
await queryRunner.startTransaction();
try {
// execute some operations on this transaction:
await queryRunner.manager.save(user1);
await queryRunner.manager.save(user2);
await queryRunner.manager.save(photos);
// commit transaction now:
await queryRunner.commitTransaction();
} catch (err) {
// since we have errors lets rollback changes we made
await queryRunner.rollbackTransaction();
} finally {
// you need to release query runner which is manually created:
await queryRunner.release();
}
There are 3 methods to control transactions in QueryRunner:
- startTransaction - starts a new transaction inside the query runner instance.
- commitTransaction - commits all changes made using the query runner instance.
- rollbackTransaction - rolls all changes made using the query runner instance back.