How to use the awilix.Lifetime.SINGLETON function in awilix

To help you get started, we’ve selected a few awilix examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dannielhugo / typescript-clean-architecture / src / deliveries / api / app.ts View on Github external
private injectStorage(): void {
    // transform something.repo.repository into repoRepository
    const formatter = (name, descriptor) => {
      const splat = name.split('.');
      splat.shift();

      return camelcase(splat.join('.'));
    };

    this.registerModule([
      [`${__dirname}/../../external/repositories/${config.storage}/**/*.js`, { lifetime: Lifetime.SINGLETON }]
    ], formatter);
  }
github mollyywang / koa2-mongodb-jwt-server / src / lib / container.js View on Github external
import { createContainer, Lifetime, InjectionMode, asValue } from 'awilix'
import { logger } from './logger'

/**
 * Configures a new container 配置容器 
 *
 * @return {Object} The container.
 */

const modulesToLoad = [
  ['services/*.js', Lifetime.SCOPED],
  ['stores/*.js', Lifetime.SINGLETON]
]

export function configureContainer() {
  const opts = {
    injectionMode: InjectionMode.CLASSIC
  }
  return createContainer(opts)
    // load modules and registers 加载,注册模块 e.g. import userService from `services/user-service.js` 
    .loadModules(modulesToLoad, {
      cwd: `${__dirname}/..`,
      formatName: 'camelCase'
    })
    .register({
      // construct logger 
      logger: asValue(logger)
    })
github asyrafduyshart / node-clean-architecture / src / container.js View on Github external
const MongoUsersRepository = require('./infra/repository/user/MongoUsersRepository');

const { User: UserModel } = require('./infra/database/models');

const logger = require('./infra/logging/logger');

const database = require('./infra/mongoose');

const container = createContainer();

container.register({
  // System
  app: asClass(Application, { lifetime: Lifetime.SINGLETON }),
  server: asClass(Server, { lifetime: Lifetime.SINGLETON }),
  
  router: asFunction(router, { lifetime: Lifetime.SINGLETON }),
  logger: asFunction(logger, { lifetime: Lifetime.SINGLETON }),

  config: asValue(config),

  //Middlewares
  loggerMiddleware: asFunction(loggerMiddleware, { lifetime: Lifetime.SINGLETON }),
  containerMiddleware: asValue(scopePerRequest(container)),
  errorHandler: asValue(config.production ? errorHandler : devErrorHandler),
  swaggerMiddleware: asValue(swaggerMiddleware),


  //Repositories
  usersRepository: asClass(MongoUsersRepository, { lifetime: Lifetime.SINGLETON }),

  //Database
  database: asFunction(database),
github inkubux / MediaSpeed / src / lib / container.js View on Github external
import ffmpegHlsStreamer from './streamer/ffmpeg-hls-streamer';
import ffmpeDashStreamer from './streamer/ffmpeg-dash-streamer';
import hlsPresetDecision from './streamer/hls/hls-preset-decision';
import M3u8Generator from './streamer/m3u8-generator';
import fsExtra from 'fs-extra';
import path from 'path';
import throttledQueue from 'throttled-queue';

/**
 * Using Awilix, the following files and folders (glob patterns)
 * will be loaded.
 */
const modulesToLoad = [
    ['services/*.js', Lifetime.SCOPED],
    ['lib/extended-info-providers/*-provider.js', Lifetime.SINGLETON],
    ['stores/*.js', Lifetime.SINGLETON]
];

/**
 * Configures a new container.
 *
 * @return {Object} The container.
 */
export async function configureContainer() {
    const opts = {
        resolutionMode: ResolutionMode.CLASSIC
    };

    // @todo create a bootstrap sequence
    const dataFolder = process.env.DATA_FOLDER || path.join(process.env.HOME, '.media_speed');
    const imagesFolder = path.join(dataFolder, 'cache', 'images');
    const transcodingTempFolder = path.join(dataFolder, 'cache', 'transcoding_temp');
github dannielhugo / typescript-clean-architecture / src / deliveries / api / app.ts View on Github external
private injectModules(): void {
    // Core injections
    this.registerModule([
      [`${__dirname}/../../application/business/**/*.js`, { lifetime: Lifetime.SCOPED }],
      [`${__dirname}/../../application/entities/services/**/*.js`, { lifetime: Lifetime.SINGLETON }]
    ]);
  }
github jeffijoe / koa-es7-boilerplate / src / lib / container.js View on Github external
import { createContainer, Lifetime, InjectionMode, asValue } from 'awilix'
import { logger } from './logger'

/**
 * Using Awilix, the following files and folders (glob patterns)
 * will be loaded.
 */
const modulesToLoad = [
  // Services should be scoped to the request.
  // This means that each request gets a separate instance
  // of a service.
  ['services/*.js', Lifetime.SCOPED],
  // Stores will be singleton (1 instance per process).
  // This is just for demo purposes, you can do whatever you want.
  ['stores/*.js', Lifetime.SINGLETON]
]

/**
 * Configures a new container.
 *
 * @return {Object} The container.
 */
export function configureContainer() {
  const opts = {
    // Classic means Awilix will look at function parameter
    // names rather than passing a Proxy.
    injectionMode: InjectionMode.CLASSIC
  }
  return createContainer(opts)
    .loadModules(modulesToLoad, {
      // `modulesToLoad` paths should be relative
github asyrafduyshart / node-clean-architecture / src / container.js View on Github external
const devErrorHandler = require('./interfaces/http/errors/devErrorHandler');
const swaggerMiddleware = require('./interfaces/http/swagger/swaggerMiddleware');

const MongoUsersRepository = require('./infra/repository/user/MongoUsersRepository');

const { User: UserModel } = require('./infra/database/models');

const logger = require('./infra/logging/logger');

const database = require('./infra/mongoose');

const container = createContainer();

container.register({
  // System
  app: asClass(Application, { lifetime: Lifetime.SINGLETON }),
  server: asClass(Server, { lifetime: Lifetime.SINGLETON }),
  
  router: asFunction(router, { lifetime: Lifetime.SINGLETON }),
  logger: asFunction(logger, { lifetime: Lifetime.SINGLETON }),

  config: asValue(config),

  //Middlewares
  loggerMiddleware: asFunction(loggerMiddleware, { lifetime: Lifetime.SINGLETON }),
  containerMiddleware: asValue(scopePerRequest(container)),
  errorHandler: asValue(config.production ? errorHandler : devErrorHandler),
  swaggerMiddleware: asValue(swaggerMiddleware),


  //Repositories
  usersRepository: asClass(MongoUsersRepository, { lifetime: Lifetime.SINGLETON }),
github dannielhugo / typescript-clean-architecture / src / injector.ts View on Github external
registerAll(): void {
    this.container.loadModules([
      [`${__dirname}/business/**/*.js`, Lifetime.TRANSIENT],
      [`${__dirname}/services/**/*.js`, Lifetime.SINGLETON],
      [`${__dirname}/adapters/**/*.js`, Lifetime.TRANSIENT]
    ], {
        formatName: 'camelCase',
        cwd: '.',
        registrationOptions: {
          resolutionMode: ResolutionMode.CLASSIC
        }
      });

    debug('app:dependency-resolver')(this.container.registrations);
  }
}
github lanvige / koa2-boilerplate / lib / serviceContainer.js View on Github external
if (target.init) {
        container.register({ [name]: asFunction(target.init.bind(target)).singleton() })
      } else {
        container.register({ name, target })
      }
    }
  )

  container.loadModules([
    '../app/services/*.js'
  ], {
    cwd: __dirname,
    formatName: 'camelCase',
    registrationOptions: {
      lifetime: Lifetime.SINGLETON
    }
  })

  return container
}