Add packages/server/src/database/DB.ts
This commit is contained in:
parent
d2a9d6faf6
commit
a6649def72
@ -12,10 +12,12 @@ import {IDatabase} from '../database/IDatabase'
|
|||||||
import {ILogger} from '../logger/ILogger'
|
import {ILogger} from '../logger/ILogger'
|
||||||
import {IRoutes} from '@rondo/common'
|
import {IRoutes} from '@rondo/common'
|
||||||
import {ITransactionManager} from '../database/ITransactionManager'
|
import {ITransactionManager} from '../database/ITransactionManager'
|
||||||
|
import {DB} from '../database/DB'
|
||||||
import {loggerFactory, LoggerFactory} from '../logger/LoggerFactory'
|
import {loggerFactory, LoggerFactory} from '../logger/LoggerFactory'
|
||||||
import {json} from 'body-parser'
|
import {json} from 'body-parser'
|
||||||
|
|
||||||
export class Application implements IApplication {
|
export class Application implements IApplication {
|
||||||
|
readonly db: DB
|
||||||
readonly transactionManager: ITransactionManager
|
readonly transactionManager: ITransactionManager
|
||||||
readonly server: express.Application
|
readonly server: express.Application
|
||||||
|
|
||||||
@ -29,7 +31,8 @@ export class Application implements IApplication {
|
|||||||
|
|
||||||
constructor(readonly config: IConfig, readonly database: IDatabase) {
|
constructor(readonly config: IConfig, readonly database: IDatabase) {
|
||||||
this.transactionManager = database.transactionManager
|
this.transactionManager = database.transactionManager
|
||||||
this.userService = new services.UserService(this.transactionManager)
|
this.db = new DB(this.transactionManager)
|
||||||
|
this.userService = new services.UserService(this.db)
|
||||||
|
|
||||||
this.teamService = new team.TeamService(this.transactionManager)
|
this.teamService = new team.TeamService(this.transactionManager)
|
||||||
this.userPermissions = new user.UserPermissions(this.transactionManager)
|
this.userPermissions = new user.UserPermissions(this.transactionManager)
|
||||||
|
|||||||
21
packages/server/src/database/DB.ts
Normal file
21
packages/server/src/database/DB.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import {
|
||||||
|
EntityManager,
|
||||||
|
EntitySchema,
|
||||||
|
ObjectType,
|
||||||
|
Repository,
|
||||||
|
} from 'typeorm'
|
||||||
|
import {ITransactionManager} from '../database/ITransactionManager'
|
||||||
|
|
||||||
|
export class DB {
|
||||||
|
constructor(protected readonly transactionManager: ITransactionManager) {}
|
||||||
|
|
||||||
|
getEntityManager(): EntityManager {
|
||||||
|
return this.transactionManager.getEntityManager()
|
||||||
|
}
|
||||||
|
|
||||||
|
getRepository<Entity>(
|
||||||
|
target: ObjectType<Entity> | EntitySchema<Entity> | string,
|
||||||
|
): Repository<Entity> {
|
||||||
|
return this.transactionManager.getRepository(target)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,3 +2,4 @@ export * from './ITransactionManager'
|
|||||||
export * from './TransactionManager'
|
export * from './TransactionManager'
|
||||||
export * from './IDatabase'
|
export * from './IDatabase'
|
||||||
export * from './Database'
|
export * from './Database'
|
||||||
|
export * from './DB'
|
||||||
|
|||||||
@ -8,7 +8,7 @@ describe('UserService', () => {
|
|||||||
const username = 'test@user.com'
|
const username = 'test@user.com'
|
||||||
const password = '1234567890'
|
const password = '1234567890'
|
||||||
|
|
||||||
const userService = new UserService(test.transactionManager)
|
const userService = new UserService(test.db)
|
||||||
|
|
||||||
async function createUser(u = username, p = password) {
|
async function createUser(u = username, p = password) {
|
||||||
return userService.createUser({
|
return userService.createUser({
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import createError from 'http-errors'
|
import createError from 'http-errors'
|
||||||
import {BaseService} from './BaseService'
|
import {BaseService} from './BaseService'
|
||||||
|
import {DB} from '../database/DB'
|
||||||
import {ICredentials, INewUser, IUser, trim} from '@rondo/common'
|
import {ICredentials, INewUser, IUser, trim} from '@rondo/common'
|
||||||
import {IUserService} from './IUserService'
|
import {IUserService} from './IUserService'
|
||||||
import {UserEmail} from '../entities/UserEmail'
|
import {UserEmail} from '../entities/UserEmail'
|
||||||
@ -11,7 +12,9 @@ import {Validator} from '../validator'
|
|||||||
const SALT_ROUNDS = 10
|
const SALT_ROUNDS = 10
|
||||||
const MIN_PASSWORD_LENGTH = 10
|
const MIN_PASSWORD_LENGTH = 10
|
||||||
|
|
||||||
export class UserService extends BaseService implements IUserService {
|
export class UserService implements IUserService {
|
||||||
|
constructor(protected readonly db: DB) {}
|
||||||
|
|
||||||
async createUser(payload: INewUser): Promise<IUser> {
|
async createUser(payload: INewUser): Promise<IUser> {
|
||||||
const newUser = {
|
const newUser = {
|
||||||
username: trim(payload.username),
|
username: trim(payload.username),
|
||||||
@ -34,11 +37,11 @@ export class UserService extends BaseService implements IUserService {
|
|||||||
.throw()
|
.throw()
|
||||||
|
|
||||||
const password = await this.hash(payload.password)
|
const password = await this.hash(payload.password)
|
||||||
const user = await this.getRepository(User).save({
|
const user = await this.db.getRepository(User).save({
|
||||||
...newUser,
|
...newUser,
|
||||||
password,
|
password,
|
||||||
})
|
})
|
||||||
await this.getRepository(UserEmail).save({
|
await this.db.getRepository(UserEmail).save({
|
||||||
email: newUser.username,
|
email: newUser.username,
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
})
|
})
|
||||||
@ -49,7 +52,7 @@ export class UserService extends BaseService implements IUserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findOne(id: number) {
|
async findOne(id: number) {
|
||||||
const user = await this.getRepository(User).findOne(id, {
|
const user = await this.db.getRepository(User).findOne(id, {
|
||||||
relations: ['emails'],
|
relations: ['emails'],
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -66,7 +69,7 @@ export class UserService extends BaseService implements IUserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findUserByEmail(email: string) {
|
async findUserByEmail(email: string) {
|
||||||
const userEmail = await this.getRepository(UserEmail)
|
const userEmail = await this.db.getRepository(UserEmail)
|
||||||
.findOne({ email }, {
|
.findOne({ email }, {
|
||||||
relations: ['user'],
|
relations: ['user'],
|
||||||
})
|
})
|
||||||
@ -91,7 +94,7 @@ export class UserService extends BaseService implements IUserService {
|
|||||||
newPassword: string,
|
newPassword: string,
|
||||||
}) {
|
}) {
|
||||||
const {userId, oldPassword, newPassword} = params
|
const {userId, oldPassword, newPassword} = params
|
||||||
const userRepository = this.getRepository(User)
|
const userRepository = this.db.getRepository(User)
|
||||||
const user = await userRepository
|
const user = await userRepository
|
||||||
.createQueryBuilder('user')
|
.createQueryBuilder('user')
|
||||||
.select('user')
|
.select('user')
|
||||||
@ -109,7 +112,7 @@ export class UserService extends BaseService implements IUserService {
|
|||||||
|
|
||||||
async validateCredentials(credentials: ICredentials) {
|
async validateCredentials(credentials: ICredentials) {
|
||||||
const {username, password} = credentials
|
const {username, password} = credentials
|
||||||
const user = await this.getRepository(User)
|
const user = await this.db.getRepository(User)
|
||||||
.createQueryBuilder('user')
|
.createQueryBuilder('user')
|
||||||
.select('user')
|
.select('user')
|
||||||
.addSelect('user.password')
|
.addSelect('user.password')
|
||||||
@ -131,7 +134,7 @@ export class UserService extends BaseService implements IUserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findUserEmails(userId: number) {
|
async findUserEmails(userId: number) {
|
||||||
return this.getRepository(UserEmail).find({ userId })
|
return this.db.getRepository(UserEmail).find({ userId })
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async hash(password: string): Promise<string> {
|
protected async hash(password: string): Promise<string> {
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import supertest from 'supertest'
|
import supertest from 'supertest'
|
||||||
import {Connection, QueryRunner} from 'typeorm'
|
import {Connection, QueryRunner} from 'typeorm'
|
||||||
|
import {DB} from '../database/DB'
|
||||||
import {
|
import {
|
||||||
ENTITY_MANAGER, ITransactionManager,
|
ENTITY_MANAGER, ITransactionManager,
|
||||||
} from '../database/ITransactionManager'
|
} from '../database/ITransactionManager'
|
||||||
@ -16,11 +17,13 @@ export class TestUtils<T extends IRoutes> {
|
|||||||
readonly app: express.Application
|
readonly app: express.Application
|
||||||
readonly context: string
|
readonly context: string
|
||||||
readonly transactionManager: ITransactionManager
|
readonly transactionManager: ITransactionManager
|
||||||
|
readonly db: DB
|
||||||
|
|
||||||
constructor(readonly bootstrap: IBootstrap) {
|
constructor(readonly bootstrap: IBootstrap) {
|
||||||
this.app = bootstrap.application.server
|
this.app = bootstrap.application.server
|
||||||
this.context = this.bootstrap.getConfig().app.context
|
this.context = this.bootstrap.getConfig().app.context
|
||||||
this.transactionManager = this.bootstrap.database.transactionManager
|
this.transactionManager = this.bootstrap.database.transactionManager
|
||||||
|
this.db = new DB(this.transactionManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user