Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// @flow
import { AsyncContainerModule, interfaces } from 'inversify'
import pg from 'pg'
import type { PgPoolConfig, Pool } from 'pg'
import config from 'config'
import { DBConnection } from '../interfaces'
import SERVICE_IDENTIFIER from '../constants/identifiers'
const createDb = async (dbSettings: PgPoolConfig): Pool => (new pg.Pool(dbSettings))
const dbModule = new AsyncContainerModule(async (bind: interfaces.Bind) => {
const dbConn = await createDb(config.get('db'))
bind(SERVICE_IDENTIFIER.DB_CONNECTION).toConstantValue(dbConn)
})
export default dbModule
register = async (Provider) => {
log('register', {Provider});
this.events.emit('app:provider:register', Provider);
let provider = Provider;
if ( Provider instanceof ServiceProvider === false ) {
provider = await this.loadProvider(Provider);
}
if ( 'register' in provider && Reflect.getMetadata('register', provider) !== true ) {
Reflect.defineMetadata('register', true, provider);
await this.loadAsync(new AsyncContainerModule(() => provider.register()));
}
this.providers.push(provider);
this.events.emit('app:provider:registered', provider);
return this;
};
async function loadModule(context: interfaces.Context, microModule: MicroModule): Promise {
if (microModule.submodules) {
const submodulesPromises = microModule.submodules.map(submoduleConstructor => {
const submodule = context.container.resolve(submoduleConstructor);
return loadModule(context, submodule);
});
await Promise.all(submodulesPromises);
}
if (microModule.onLoad) {
const containerModule = new AsyncContainerModule((...args) =>
(microModule as {
onLoad: (
context: interfaces.Context,
bind: interfaces.Bind,
unbind: interfaces.Unbind,
isBound: interfaces.IsBound,
rebind: interfaces.Rebind
) => Promise;
}).onLoad(context, ...args)
);
await context.container.loadAsync(containerModule);
}
}