Replace unnecessary DB with IDatabase
This commit is contained in:
parent
291d05e2be
commit
67ae8dac57
@ -6,7 +6,6 @@ import * as user from '../user'
|
||||
import cookieParser from 'cookie-parser'
|
||||
import express from 'express'
|
||||
import {AsyncRouter, TransactionalRouter} from '../router'
|
||||
import {DB} from '../database/DB'
|
||||
import {IApplication} from './IApplication'
|
||||
import {IConfig} from './IConfig'
|
||||
import {IDatabase} from '../database/IDatabase'
|
||||
@ -19,7 +18,6 @@ import {ILoggerFactory} from '@rondo.dev/logger'
|
||||
import {json} from 'body-parser'
|
||||
|
||||
export class Application implements IApplication {
|
||||
readonly db: DB
|
||||
readonly transactionManager: ITransactionManager
|
||||
readonly server: express.Application
|
||||
|
||||
@ -30,7 +28,6 @@ export class Application implements IApplication {
|
||||
|
||||
constructor(readonly config: IConfig, readonly database: IDatabase) {
|
||||
this.transactionManager = database.transactionManager
|
||||
this.db = new DB(this.transactionManager)
|
||||
|
||||
this.services = this.configureServices()
|
||||
|
||||
@ -41,9 +38,9 @@ export class Application implements IApplication {
|
||||
|
||||
protected configureServices(): IServices {
|
||||
return {
|
||||
userService: new services.UserService(this.db),
|
||||
teamService: new team.TeamService(this.db),
|
||||
userPermissions: new user.UserPermissions(this.db),
|
||||
userService: new services.UserService(this.database),
|
||||
teamService: new team.TeamService(this.database),
|
||||
userPermissions: new user.UserPermissions(this.database),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,21 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,15 @@
|
||||
import {IDatabase} from './IDatabase'
|
||||
import {Namespace} from 'cls-hooked'
|
||||
import {TransactionManager} from './TransactionManager'
|
||||
import {createConnection, Connection, ConnectionOptions, Logger} from 'typeorm'
|
||||
import {
|
||||
createConnection,
|
||||
Connection,
|
||||
ConnectionOptions,
|
||||
Logger,
|
||||
EntitySchema,
|
||||
ObjectType,
|
||||
Repository,
|
||||
} from 'typeorm'
|
||||
|
||||
export class Database implements IDatabase {
|
||||
protected connection?: Connection
|
||||
@ -37,4 +45,14 @@ export class Database implements IDatabase {
|
||||
await this.getConnection().close()
|
||||
}
|
||||
|
||||
getEntityManager() {
|
||||
return this.transactionManager.getEntityManager()
|
||||
}
|
||||
|
||||
getRepository<Entity>(
|
||||
target: ObjectType<Entity> | EntitySchema<Entity> | string,
|
||||
): Repository<Entity> {
|
||||
return this.transactionManager.getRepository(target)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
import {Connection} from 'typeorm'
|
||||
import {
|
||||
Connection, EntityManager, ObjectType, EntitySchema, Repository
|
||||
} from 'typeorm'
|
||||
import {ITransactionManager} from './ITransactionManager'
|
||||
import {Namespace} from 'cls-hooked'
|
||||
|
||||
@ -7,5 +9,9 @@ export interface IDatabase {
|
||||
transactionManager: ITransactionManager
|
||||
connect(): Promise<Connection>
|
||||
getConnection(): Connection
|
||||
getEntityManager(): EntityManager
|
||||
getRepository<Entity>(
|
||||
target: ObjectType<Entity> | EntitySchema<Entity> | string,
|
||||
): Repository<Entity>
|
||||
close(): Promise<void>
|
||||
}
|
||||
|
||||
@ -2,4 +2,3 @@ export * from './ITransactionManager'
|
||||
export * from './TransactionManager'
|
||||
export * from './IDatabase'
|
||||
export * from './Database'
|
||||
export * from './DB'
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {DB} from '../database/DB'
|
||||
import {IDatabase} from '../database/IDatabase'
|
||||
import {Validator} from '../validator'
|
||||
import {Team} from '../entities/Team'
|
||||
import {UserTeam} from '../entities/UserTeam'
|
||||
@ -14,7 +14,7 @@ type IContext = t.IContext
|
||||
|
||||
export class TeamService2 implements Contextual<t.ITeamService, IContext> {
|
||||
constructor(
|
||||
protected readonly db: DB,
|
||||
protected readonly db: IDatabase,
|
||||
protected readonly permissions: IUserPermissions,
|
||||
) {}
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ describe('UserService', () => {
|
||||
const username = test.username
|
||||
const password = '1234567890'
|
||||
|
||||
const userService = new UserService(test.db)
|
||||
const userService = new UserService(test.bootstrap.database)
|
||||
|
||||
async function createUser(u = username, p = password) {
|
||||
return userService.createUser({
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import createError from 'http-errors'
|
||||
import {BaseService} from './BaseService'
|
||||
import {DB} from '../database/DB'
|
||||
import {IDatabase} from '../database/IDatabase'
|
||||
import {ICredentials, INewUser, IUser, trim} from '@rondo.dev/common'
|
||||
import {IUserService} from './IUserService'
|
||||
import {UserEmail} from '../entities/UserEmail'
|
||||
@ -13,7 +13,7 @@ const SALT_ROUNDS = 10
|
||||
const MIN_PASSWORD_LENGTH = 10
|
||||
|
||||
export class UserService implements IUserService {
|
||||
constructor(protected readonly db: DB) {}
|
||||
constructor(protected readonly db: IDatabase) {}
|
||||
|
||||
async createUser(payload: INewUser): Promise<IUser> {
|
||||
const newUser = {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {DB} from '../database/DB'
|
||||
import {IDatabase} from '../database/IDatabase'
|
||||
import {ITeamService} from './ITeamService'
|
||||
import {IUserInTeam, trim} from '@rondo.dev/common'
|
||||
import {IUserTeamParams} from './IUserTeamParams'
|
||||
@ -8,7 +8,7 @@ import {Validator} from '../validator'
|
||||
|
||||
export class TeamService implements ITeamService {
|
||||
|
||||
constructor(protected readonly db: DB) {}
|
||||
constructor(protected readonly db: IDatabase) {}
|
||||
|
||||
// TODO check team limit per user
|
||||
async create({name, userId}: {name: string, userId: number}) {
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import express from 'express'
|
||||
import supertest from 'supertest'
|
||||
import {Connection, QueryRunner} from 'typeorm'
|
||||
import {DB} from '../database/DB'
|
||||
import {
|
||||
ENTITY_MANAGER, ITransactionManager,
|
||||
} from '../database/ITransactionManager'
|
||||
@ -19,13 +18,11 @@ export class TestUtils<T extends IRoutes> {
|
||||
readonly app: express.Application
|
||||
readonly context: string
|
||||
readonly transactionManager: ITransactionManager
|
||||
readonly db: DB
|
||||
|
||||
constructor(readonly bootstrap: IBootstrap) {
|
||||
this.app = bootstrap.application.server
|
||||
this.context = this.bootstrap.getConfig().app.context
|
||||
this.transactionManager = this.bootstrap.database.transactionManager
|
||||
this.db = new DB(this.transactionManager)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import createError from 'http-errors'
|
||||
import {DB} from '../database/DB'
|
||||
import {IDatabase} from '../database/IDatabase'
|
||||
import {UserTeam} from '../entities/UserTeam'
|
||||
import {IUserPermissions} from './IUserPermissions'
|
||||
|
||||
export class UserPermissions implements IUserPermissions {
|
||||
constructor(protected readonly db: DB) {}
|
||||
constructor(protected readonly db: IDatabase) {}
|
||||
|
||||
async belongsToTeam(params: {userId: number, teamId: number}) {
|
||||
const {userId, teamId} = params
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user