database-app
. You can run the following command to quickly initialize a template application related to database operations:malagu init demo database-app
@malagu/typeorm
component directly in the project by running the following command:yarn add @malagu/typeorm# Or, run `npm i @malagu/typeorm`
default
by default.# malagu.ymlbackend:malagu:typeorm:ormConfig:- type: mysqlhost: localhostport: 3306synchronize: trueusername: rootpassword: rootdatabase: test
default
by default. When OrmContext APIs are used, the data source connection names will be used.# malagu.ymlbackend:malagu:typeorm:ormConfig:- type: mysqlhost: localhostport: 3306synchronize: trueusername: rootpassword: rootdatabase: test- type: mysqlname: 'datasource2'host: xxxxport: 3306synchronize: trueusername: rootpassword: rootdatabase: test
import { Controller, Get, Param, Delete, Put, Post, Body } from '@malagu/mvc/lib/node';import { Transactional, OrmContext } from '@malagu/typeorm/lib/node';import { User } from './entity';@Controller('users')export class UserController {@Get()@Transactional({ readOnly: true })list(): Promise<User[]> {const repo = OrmContext.getRepository(User);return repo.find();}@Get(':id')@Transactional({ readOnly: true })get(@Param('id') id: number): Promise<User | undefined> {const repo = OrmContext.getRepository(User);return repo.findOne(id);}@Delete(':id')@Transactional()async reomve(@Param('id') id: number): Promise<void> {const repo = OrmContext.getRepository(User);await repo.delete(id);}@Put()@Transactional()async modify(@Body() user: User): Promise<void> {const repo = OrmContext.getRepository(User);await repo.update(user.id, user);}@Post()@Transactional()create(@Body() user: User): Promise<User> {const repo = OrmContext.getRepository(User);return repo.save(user);}}
@Transactional
decorator for how the framework initiates, propagates, commits, and rolls back transactions before and after execution methods. Plus, the framework puts the managed EntityManager objects in the database context for easy use by the business code. In addition, you can also manually manage database transactions and create EntityManager objects.export namespace OrmContext {export function getEntityManager(name = DEFAULT_CONNECTION_NAME): EntityManager {...}export function getRepository<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, name?: string): Repository<Entity> {...}export function getTreeRepository<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, name?: string): TreeRepository<Entity> {...}export function getMongoRepository<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string, name?: string): MongoRepository<Entity> {...}export function getCustomRepository<T>(customRepository: ObjectType<T>, name?: string): T {...}export function pushEntityManager(name: string, entityManager: EntityManager): void {...}export function popEntityManager(name: string): EntityManager | undefined {...}}
@Transactional
decorator to define the behaviors of transactions in a declarative manner. It decides the opening, propagation, commit, and rollback behaviors of transactions according to the decorator's declaration.@Transactional
decorator can be added to classes and methods. If it is added to a class and a method at the same time, the final configuration will be to use the configuration of the method to merge the class, which has a higher priority than the class. The decorator configuration options are as follows:export interface TransactionalOption {name?: string; // In case of multiple data source connections, specify the data source connection name, which is `default` by defaultisolation?: IsolationLevel; // Database isolation levelpropagation?: Propagation; // Transaction propagation behavior. Valid values: Required, RequiresNew. Default value: RequiredreadOnly?: boolean; // Read-only mode, i.e., not to start transaction. Transaction is started by default}
@Put()@Transactional()async modify(@Body() user: User): Promise<void> {const repo = OrmContext.getRepository(User);await repo.update(user.id, user);}
default
.@Transactional
decorator, the configuration of transaction propagation behavior determines whether to reuse the transaction of the upper-layer method or start a new one.@Transactional
decorator to the method and configure readonly
to true
, so that the framework can create an EntityManager that does not start transactions and maintain a uniform code style. Below is a sample:@Get()@Transactional({ readOnly: true })list(): Promise<User[]> {const repo = OrmContext.getRepository(User);return repo.find();}
export enum Propagation {Required, RequiresNew}
...@Transactional()async foo(): Promise<void> {...await bar(); // `await` must be added}.......@Transactional()async bar(): Promise<void> {...}
autoBindEntities
method for binding entity classes, which is generally invoked in the module entry file and contains the following two parameters:default
by default.export function autoBindEntities(entities: any, name = DEFAULT_CONNECTION_NAME) {}
import { autoBindEntities } from '@malagu/typeorm';import * as entities from './entity';import { autoBind } from '@malagu/core';autoBindEntities(entities);export default autoBind();
Tool | Description |
DEFAULT_CONNECTION_NAME | The default database connection name is default . |
autoBindEntities | Binds entity class. |
Was this page helpful?