malagu init demo database-app
@malagu/typeorm
组件。yarn add @malagu/typeorm# 或者执行 npm i @malagu/typeorm
# malagu.ymlbackend:malagu:typeorm:ormConfig:- type: mysqlhost: localhostport: 3306synchronize: trueusername: rootpassword: rootdatabase: test
# 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
,用于框架在执行方法前后如何开启、传播、提交和回滚事务。同时框架会将托管的 Entitymanager 对象放到数据库上下文中,方便在业务代码中使用。另外您也可以手动管理数据库事务和创建 EntityManager 对象。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
,以声明的方式定义事务的行为,Malagu 框架根据装饰器声明决定事务的开启、传播、提交和回滚行为。@Transactional
装饰器可以加在类和方法上,如果类和方法同时加上,最终的配置是使用方法的配置去合并类,且方法的配置优先级高于类上。装饰器配置选项如下:export interface TransactionalOption {name?: string; // 多数据源链接情况下,指定数据源链接名称,默认为 defaultisolation?: IsolationLevel; // 数据库隔离级别propagation?: Propagation; // 事务的传播行为,支持 Required 和 RequiresNew,默认为 RequiredreadOnly?: boolean; // 只读,不开启事务,默认为开启事务}
@Put()@Transactional()async modify(@Body() user: User): Promise<void> {const repo = OrmContext.getRepository(User);await repo.update(user.id, user);}
@Transactional
装饰器的方法,由事务传播行为的配置决定是复用上层方法的事务,还是重新开启新的事务。@Transactional
装饰器,并将 readonly 配置为 true,让框架创建一个不开启事务的 EntityManager,保持代码风格统一。示例如下:@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}.......@Transactional()async bar(): Promise<void> {...}
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();
工具 | 描述 |
DEFAULT_CONNECTION_NAME | 默认数据库连接名称 default。 |
autoBindEntities | 绑定实体类。 |
本页内容是否解决了您的问题?