From 09faf1358508c8e2de7ea75269374d04e5e93b90 Mon Sep 17 00:00:00 2001 From: Yaapa Hage Date: Fri, 26 Jun 2020 12:51:46 +0530 Subject: [PATCH] feat: refactor Refactor Signed-off-by: Yaapa Hage --- .../typeorm.decorator.ts => decorators.ts} | 6 +-- packages/typeorm/src/decorators/index.ts | 6 --- packages/typeorm/src/index.ts | 4 +- packages/typeorm/src/keys.ts | 12 ------ .../src/{mixins/typeorm.mixin.ts => mixin.ts} | 31 +++++++++++++- packages/typeorm/src/mixins/index.ts | 6 --- packages/typeorm/src/services/connections.ts | 42 ------------------- packages/typeorm/src/services/index.ts | 6 --- packages/typeorm/src/typeorm.component.ts | 19 --------- tsconfig.json | 3 -- 10 files changed, 33 insertions(+), 102 deletions(-) rename packages/typeorm/src/{decorators/typeorm.decorator.ts => decorators.ts} (91%) delete mode 100644 packages/typeorm/src/decorators/index.ts rename packages/typeorm/src/{mixins/typeorm.mixin.ts => mixin.ts} (69%) delete mode 100644 packages/typeorm/src/mixins/index.ts delete mode 100644 packages/typeorm/src/services/connections.ts delete mode 100644 packages/typeorm/src/services/index.ts delete mode 100644 packages/typeorm/src/typeorm.component.ts diff --git a/packages/typeorm/src/decorators/typeorm.decorator.ts b/packages/typeorm/src/decorators.ts similarity index 91% rename from packages/typeorm/src/decorators/typeorm.decorator.ts rename to packages/typeorm/src/decorators.ts index 1e4755ef1d41..e6cba8642db6 100644 --- a/packages/typeorm/src/decorators/typeorm.decorator.ts +++ b/packages/typeorm/src/decorators.ts @@ -5,7 +5,7 @@ import {Context, inject, Injection, ResolutionSession} from '@loopback/core'; import {QueryRunner} from 'typeorm'; -import {TypeOrmBindings} from '../keys'; +import {TypeOrmBindings} from './keys'; export namespace typeorm { export function connection(connectionName?: string) { @@ -62,6 +62,6 @@ export namespace typeorm { * @param connectionName - Optional connection name */ async function getConnection(ctx: Context, connectionName?: string) { - const connections = await ctx.get(TypeOrmBindings.CONNECTIONS); - return connections.manager.get(connectionName); + const manager = await ctx.get(TypeOrmBindings.MANAGER); + return manager.get(connectionName); } diff --git a/packages/typeorm/src/decorators/index.ts b/packages/typeorm/src/decorators/index.ts deleted file mode 100644 index 07aabe8d4acf..000000000000 --- a/packages/typeorm/src/decorators/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright IBM Corp. 2020. All Rights Reserved. -// Node module: @loopback/typeorm -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -export * from './typeorm.decorator'; diff --git a/packages/typeorm/src/index.ts b/packages/typeorm/src/index.ts index ded74ac6d0f6..2e4a6c74207c 100644 --- a/packages/typeorm/src/index.ts +++ b/packages/typeorm/src/index.ts @@ -5,8 +5,6 @@ export * from './decorators'; export * from './keys'; -export * from './mixins'; -export * from './services'; -export * from './typeorm.component'; +export * from './mixin'; export * from './types'; export * from './utils'; diff --git a/packages/typeorm/src/keys.ts b/packages/typeorm/src/keys.ts index 448c8548080e..3261befb1412 100644 --- a/packages/typeorm/src/keys.ts +++ b/packages/typeorm/src/keys.ts @@ -5,24 +5,12 @@ import {BindingKey} from '@loopback/core'; import {ConnectionManager} from 'typeorm'; -import {TypeOrmConnections} from './services'; -import {TypeOrmComponent} from './typeorm.component'; /** * Binding keys used by this component. */ export namespace TypeOrmBindings { - export const COMPONENT = BindingKey.create( - 'components.TypeOrmComponent', - ); - - export const CONNECTIONS = BindingKey.create( - 'services.TypeOrmConnections', - ); - export const MANAGER = BindingKey.create( 'services.TypeOrmConnectionManager', ); } - -export const CONNECTION_OPTIONS_EXTENSION_POINT = 'typeorm.connectionOptions'; diff --git a/packages/typeorm/src/mixins/typeorm.mixin.ts b/packages/typeorm/src/mixin.ts similarity index 69% rename from packages/typeorm/src/mixins/typeorm.mixin.ts rename to packages/typeorm/src/mixin.ts index 25325b8d9dab..840cc6193672 100644 --- a/packages/typeorm/src/mixins/typeorm.mixin.ts +++ b/packages/typeorm/src/mixin.ts @@ -3,10 +3,17 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {Application, MixinTarget} from '@loopback/core'; +import { + Application, + BindingScope, + inject, + lifeCycleObserver, + LifeCycleObserver, + MixinTarget, +} from '@loopback/core'; import debugFactory from 'debug'; import {ConnectionManager, ConnectionOptions} from 'typeorm'; -import {TypeOrmBindings} from '../keys'; +import {TypeOrmBindings} from './keys'; const debug = debugFactory('loopback:typeorm:mixin'); @@ -19,6 +26,7 @@ export function TypeOrmMixin>( // eslint-disable-next-line @typescript-eslint/no-explicit-any constructor(...args: any[]) { super(...args); + this.lifeCycleObserver(StartStop); this.connectionManager = new ConnectionManager(); const binding = this.bind(TypeOrmBindings.MANAGER).to( this.connectionManager, @@ -37,6 +45,7 @@ export function TypeOrmMixin>( async migrateSchema(): Promise { // TODO: implement using TypeORM + throw new Error('TypeORM migration not implemented.'); } }; } @@ -46,3 +55,21 @@ export interface ApplicationUsingTypeOrm extends Application { connection(options: ConnectionOptions): void; migrateSchema(): Promise; } + +@lifeCycleObserver('datasource', { + scope: BindingScope.SINGLETON, +}) +export class StartStop implements LifeCycleObserver { + constructor( + @inject(TypeOrmBindings.MANAGER) + private manager: ConnectionManager, + ) {} + + async start() { + Promise.all(this.manager.connections.map(c => c.connect())); + } + + async stop() { + Promise.all(this.manager.connections.map(c => c.close())); + } +} diff --git a/packages/typeorm/src/mixins/index.ts b/packages/typeorm/src/mixins/index.ts deleted file mode 100644 index d3d73ec59f35..000000000000 --- a/packages/typeorm/src/mixins/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright IBM Corp. 2018,2020. All Rights Reserved. -// Node module: @loopback/typeorm -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -export * from './typeorm.mixin'; diff --git a/packages/typeorm/src/services/connections.ts b/packages/typeorm/src/services/connections.ts deleted file mode 100644 index 3225e7bf68b8..000000000000 --- a/packages/typeorm/src/services/connections.ts +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright IBM Corp. 2020. All Rights Reserved. -// Node module: @loopback/typeorm -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -import { - BindingScope, - ContextTags, - CoreBindings, - inject, - LifeCycleObserver, - lifeCycleObserver, -} from '@loopback/core'; -import {ConnectionManager} from 'typeorm'; -import {ApplicationUsingTypeOrm, TypeOrmBindings} from '../'; - -@lifeCycleObserver('datasource', { - tags: {[ContextTags.KEY]: TypeOrmBindings.CONNECTIONS}, - scope: BindingScope.SINGLETON, -}) -export class TypeOrmConnections implements LifeCycleObserver { - constructor( - @inject(CoreBindings.APPLICATION_INSTANCE) - private app: ApplicationUsingTypeOrm, - ) {} - - readonly manager = new ConnectionManager(); - - async start() { - const connectionOptions = await this.app.typeormConnectionOptions; - for (const option of connectionOptions) { - const connection = this.manager.create(option); - await connection.connect(); - } - } - - async stop() { - for (const connection of this.manager.connections) { - await connection.close(); - } - } -} diff --git a/packages/typeorm/src/services/index.ts b/packages/typeorm/src/services/index.ts deleted file mode 100644 index 50c7740c0338..000000000000 --- a/packages/typeorm/src/services/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright IBM Corp. 2020. All Rights Reserved. -// Node module: @loopback/typeorm -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -export * from './connections'; diff --git a/packages/typeorm/src/typeorm.component.ts b/packages/typeorm/src/typeorm.component.ts deleted file mode 100644 index 3aa1aeccb47a..000000000000 --- a/packages/typeorm/src/typeorm.component.ts +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright IBM Corp. 2020. All Rights Reserved. -// Node module: @loopback/typeorm -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -import { - bind, - Component, - ContextTags, - createBindingFromClass, -} from '@loopback/core'; -import {TypeOrmBindings} from './keys'; -import {TypeOrmConnections} from './services'; -@bind({ - tags: {[ContextTags.KEY]: TypeOrmBindings.COMPONENT}, -}) -export class TypeOrmComponent implements Component { - bindings = [createBindingFromClass(TypeOrmConnections)]; -} diff --git a/tsconfig.json b/tsconfig.json index 71857160a13e..f62d1044efca 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -180,9 +180,6 @@ }, { "path": "packages/typeorm/tsconfig.json" - }, - { - "path": "sandbox/appa/tsconfig.json" } ], "files": []