Skip to content

Commit

Permalink
feat: refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
Yaapa Hage committed Jun 26, 2020
1 parent 5ed42e1 commit 8231130
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
6 changes: 0 additions & 6 deletions packages/typeorm/src/decorators/index.ts

This file was deleted.

4 changes: 1 addition & 3 deletions packages/typeorm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
12 changes: 0 additions & 12 deletions packages/typeorm/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeOrmComponent>(
'components.TypeOrmComponent',
);

export const CONNECTIONS = BindingKey.create<TypeOrmConnections>(
'services.TypeOrmConnections',
);

export const MANAGER = BindingKey.create<ConnectionManager>(
'services.TypeOrmConnectionManager',
);
}

export const CONNECTION_OPTIONS_EXTENSION_POINT = 'typeorm.connectionOptions';
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -19,6 +26,7 @@ export function TypeOrmMixin<T extends MixinTarget<Application>>(
// 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,
Expand All @@ -37,6 +45,7 @@ export function TypeOrmMixin<T extends MixinTarget<Application>>(

async migrateSchema(): Promise<void> {
// TODO: implement using TypeORM
throw new Error('TypeORM migration not implemented.');
}
};
}
Expand All @@ -46,3 +55,21 @@ export interface ApplicationUsingTypeOrm extends Application {
connection(options: ConnectionOptions): void;
migrateSchema(): Promise<void>;
}

@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()));
}
}
6 changes: 0 additions & 6 deletions packages/typeorm/src/mixins/index.ts

This file was deleted.

42 changes: 0 additions & 42 deletions packages/typeorm/src/services/connections.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/typeorm/src/services/index.ts

This file was deleted.

19 changes: 0 additions & 19 deletions packages/typeorm/src/typeorm.component.ts

This file was deleted.

0 comments on commit 8231130

Please sign in to comment.