How to use the @loopback/context.createBindingFromClass function in @loopback/context

To help you get started, we’ve selected a few @loopback/context 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 strongloop / loopback-next / examples / context / src / find-bindings.ts View on Github external
export async function main() {
  const ctx = new Context('request');

  // Add EnglishGreeter for now
  ctx.add(createBindingFromClass(EnglishGreeter, {namespace: 'greeters'}));

  // Add ChineseGreeter
  ctx
    .bind('greeters.ChineseGreeter')
    .toClass(ChineseGreeter)
    .tag('greeter');

  const enlishGreeterBinding = ctx.getBinding('greeters.EnglishGreeter');
  console.log(enlishGreeterBinding.key);

  let possibleEnglishGreeters = ctx.find('*.EnglishGreeter');
  console.log(possibleEnglishGreeters.map(b => b.key));

  possibleEnglishGreeters = ctx.find(/\w+\.EnglishGreeter$/);
  console.log(possibleEnglishGreeters.map(b => b.key));
github strongloop / loopback-next / examples / greeter-extension / src / component.ts View on Github external
// License text available at https://opensource.org/licenses/MIT

import {createBindingFromClass} from '@loopback/context';
import {Component} from '@loopback/core';
import {ChineseGreeter} from './greeters/greeter-cn';
import {EnglishGreeter} from './greeters/greeter-en';
import {GreetingService} from './greeting-service';
import {GREETING_SERVICE} from './keys';

/**
 * Define a component to register the greeter extension point and built-in
 * extensions
 */
export class GreetingComponent implements Component {
  bindings = [
    createBindingFromClass(GreetingService, {
      key: GREETING_SERVICE,
    }),
    createBindingFromClass(EnglishGreeter),
    createBindingFromClass(ChineseGreeter),
  ];
}
github strongloop / loopback-next / packages / repository / src / mixins / repository.mixin.ts View on Github external
dataSource(
      dataSource: Class | D,
      name?: string,
    ): Binding {
      // We have an instance of
      if (dataSource instanceof juggler.DataSource) {
        const key = `datasources.${name || dataSource.name}`;
        return this.bind(key)
          .to(dataSource)
          .tag('datasource');
      } else if (typeof dataSource === 'function') {
        const binding = createBindingFromClass(dataSource, {
          name: name || dataSource.dataSourceName,
          namespace: 'datasources',
          type: 'datasource',
          defaultScope: BindingScope.SINGLETON,
        });
        this.add(binding);
        return binding;
      } else {
        throw new Error('not a valid DataSource.');
      }
    }
github strongloop / loopback-next / packages / core / src / __tests__ / unit / application-lifecycle.unit.ts View on Github external
it('honors @lifeCycleObserver', async () => {
      const app = new Application();
      const binding = createBindingFromClass(MyObserverWithDecorator);
      app.add(binding);
      expect(binding.tagMap[CoreTags.LIFE_CYCLE_OBSERVER_GROUP]).to.eql(
        'my-group',
      );
      expect(binding.scope).to.eql(BindingScope.SINGLETON);

      const observer = await app.get(binding.key);
      expect(observer.status).to.equal('not-initialized');
      await app.start();
      expect(observer.status).to.equal('started');
      await app.stop();
      expect(observer.status).to.equal('stopped');
    });
github strongloop / loopback-next / packages / core / src / __tests__ / unit / lifecycle-registry.unit.ts View on Github external
function givenAsyncObserver(name: string, group = '', delayInMs = 0) {
    @bind({tags: {[CoreTags.LIFE_CYCLE_OBSERVER_GROUP]: group}})
    class MyAsyncObserver implements LifeCycleObserver {
      async start() {
        await sleep(delayInMs);
        events.push(`${name}-start`);
      }
      async stop() {
        await sleep(delayInMs);
        events.push(`${name}-stop`);
      }
    }
    const binding = createBindingFromClass(MyAsyncObserver, {
      key: `observers.observer-${name}`,
    }).apply(asLifeCycleObserver);
    context.add(binding);

    return MyAsyncObserver;
  }
});
github strongloop / loopback-next / examples / context / src / parameterized-decoration.ts View on Github external
export async function main() {
  const ctx = new Context();

  ctx.bind('name1').to('John');
  ctx.bind('name2').to('Jane');

  const class1 = createClassWithDecoration('name1', {tags: {prefix: '1'}});
  const binding1 = createBindingFromClass(class1, {key: 'greeter1'});
  ctx.add(binding1);
  console.log('1:', binding1.tagMap);

  const class2 = createClassWithDecoration('name2', {tags: {prefix: '2'}});
  const binding2 = createBindingFromClass(class2, {key: 'greeter2'});
  ctx.add(binding2);
  console.log('2:', binding2.tagMap);

  const greeting1 = await ctx.get('greeter1');
  console.log('1: %s', greeting1.hello());

  const greeting2 = await ctx.get('greeter2');
  console.log('2: %s', greeting2.hello());
}
github strongloop / loopback-next / examples / context / src / parameterized-decoration.ts View on Github external
export async function main() {
  const ctx = new Context();

  ctx.bind('name1').to('John');
  ctx.bind('name2').to('Jane');

  const class1 = createClassWithDecoration('name1', {tags: {prefix: '1'}});
  const binding1 = createBindingFromClass(class1, {key: 'greeter1'});
  ctx.add(binding1);
  console.log('1:', binding1.tagMap);

  const class2 = createClassWithDecoration('name2', {tags: {prefix: '2'}});
  const binding2 = createBindingFromClass(class2, {key: 'greeter2'});
  ctx.add(binding2);
  console.log('2:', binding2.tagMap);

  const greeting1 = await ctx.get('greeter1');
  console.log('1: %s', greeting1.hello());

  const greeting2 = await ctx.get('greeter2');
  console.log('2: %s', greeting2.hello());
}
github strongloop / loopback-next / examples / greeter-extension / src / component.ts View on Github external
import {Component} from '@loopback/core';
import {ChineseGreeter} from './greeters/greeter-cn';
import {EnglishGreeter} from './greeters/greeter-en';
import {GreetingService} from './greeting-service';
import {GREETING_SERVICE} from './keys';

/**
 * Define a component to register the greeter extension point and built-in
 * extensions
 */
export class GreetingComponent implements Component {
  bindings = [
    createBindingFromClass(GreetingService, {
      key: GREETING_SERVICE,
    }),
    createBindingFromClass(EnglishGreeter),
    createBindingFromClass(ChineseGreeter),
  ];
}
github strongloop / loopback-next / packages / boot / src / booters / interceptor.booter.ts View on Github external
async load() {
    await super.load();

    this.interceptors = this.classes as InterceptorProviderClass[];
    for (const interceptor of this.interceptors) {
      debug('Bind interceptor: %s', interceptor.name);
      const binding = createBindingFromClass(interceptor, {
        defaultScope: BindingScope.TRANSIENT,
      });
      this.app.add(binding);
      debug('Binding created for interceptor: %j', binding);
    }
  }
}
github strongloop / loopback-next / packages / repository / src / mixins / repository.mixin.ts View on Github external
repository>(
      repoClass: Class,
      name?: string,
    ): Binding {
      const binding = createBindingFromClass(repoClass, {
        name,
        namespace: 'repositories',
        type: 'repository',
        defaultScope: BindingScope.TRANSIENT,
      });
      this.add(binding);
      return binding;
    }