Created
August 11, 2020 18:36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type IsolationOptions = { | |
entityManager?: EntityManager<IDatabaseDriver<Connection>>; | |
flush?: boolean; | |
}; | |
@Injectable() | |
export class UsersService { | |
constructor( | |
private readonly _entityManager: EntityManager<IDatabaseDriver<Connection>>, | |
) {} | |
public async create( | |
options: UserEntityOptions, | |
{ | |
entityManager = this._entityManager, | |
flush = true, | |
}: IsolationOptions = {}, | |
): Promise<UserEntity> { | |
const repo = this._getUserEntityRepository(entityManager); | |
const password = await bcrypt.hash(options.password, BCRYPT_ROUNDS); | |
const user = new UserEntity({ ...options, password }); | |
repo.persist(user); | |
if (flush) { | |
await entityManager.flush(); | |
} | |
return user; | |
} | |
private _getUserEntityRepository( | |
entityManager: EntityManager<IDatabaseDriver<Connection>> = this | |
._entityManager, | |
) { | |
return entityManager.getRepository(UserEntity); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment