# @inward/context

> LoopBack's container for Inversion of Control

Latest version **3.9.0** (published 2020-05-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install @inward/context
pnpm add @inward/context
yarn add @inward/context
bun add @inward/context
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.9.0 |
| Published | 2020-05-31 |
| First published | 2020-05-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 6 |
| Unpacked size | 637.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Inwio |
| Maintainers | towyuan |
| Keywords | LoopBack, IoC, Inversion, Control, Container, Decorators, Context |

## Links

- npm: https://www.npmjs.com/package/@inward/context
- Repository: https://github.com/inwio/inward
- Homepage: https://github.com/inwio/inward#readme
- Issues: https://github.com/inwio/inward/issues
- npm.io page: https://npm.io/package/@inward/context

## Dependencies (6)

- [uuid](https://npm.io/package/uuid.md) ^8.1.0
- [debug](https://npm.io/package/debug.md) ^4.1.1
- [tslib](https://npm.io/package/tslib.md) ^2.0.0
- [p-event](https://npm.io/package/p-event.md) ^4.1.0
- [@types/debug](https://npm.io/package/@types/debug.md) ^4.1.5
- [@inward/metadata](https://npm.io/package/@inward/metadata.md) ^2.2.0

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 3.9.0 (latest) — 2020-05-31

## README

# @inward/context

This module provides facilities to manage artifacts and their dependencies using
`Context` in your Node.js applications. It can be used independent of the
LoopBack framework.

## Overview

The `@inward/context` package exposes TypeScript/JavaScript APIs and decorators
to register artifacts, declare dependencies, and resolve artifacts by keys. The
`Context` also serves as an
[IoC container](https://en.wikipedia.org/wiki/Inversion_of_control) to support
[dependency injection](https://en.wikipedia.org/wiki/Dependency_injection).

`Context` and `Binding` are the two core concepts. A context is a registry of
bindings and each binding represents a resolvable artifact by the key.

- Bindings can be fulfilled by a constant, a factory function, a class, or a
  provider.
- Bindings can be grouped by tags and searched by tags.
- Binding scopes can be used to control how a resolved binding value is shared.
- Bindings can be resolved synchronously or asynchronously.
- Provide `@inject` and other variants of decorators to express dependencies.
- Support Constructor, property, and method injections.
- Allow contexts to form a hierarchy to share or override bindings.
- Track bindings and injections during resolution to detect circular
  dependencies.

## Installation

```sh
npm install --save @inward/context
```

## Basic use

### Locate an artifact by key

```js
const Context = require('@inward/context').Context;
const ctx = new Context();
ctx.bind('hello').to('world'); // BindingKey='hello', BindingValue='world'
const helloVal = ctx.getSync('hello');
console.log(helloVal); // => 'world'
```

The binding can also be located asynchronously:

```ts
const helloVal = await ctx.get('hello');
console.log(helloVal); // => 'world'
```

### Dependency injection using context

```ts
import {Context, inject} from '@inward/context';
const ctx = new Context();

// bind 'greeting' to 'Hello' as the constant value
ctx.bind('greeting').to('Hello');

class HelloController {
  constructor(
    // injecting the value bound to `greeting` using context
    @inject('greeting') private greeting: string,
  ) {}

  greet(name) {
    return `${this.greeting}, ${name}`;
  }
}

// Bind 'HelloController' to class HelloController
ctx.bind('HelloController').toClass(HelloController);

async function hello() {
  // Get an instance of HelloController
  const helloController = await ctx.get<HelloController>('HelloController');
  // helloController now has the `greeting` property injected with `Hello`
  console.log(helloController.greet('John')); // => Hello, John
}

hello();
```

For additional information, please refer to the
[Context page](http://inward.io/doc/en/lb4/Context.html).

## Examples

To learn more about advanced features, check out standalone examples at
[`@inward/example-context`](https://github.com/inwio/inward/tree/master/examples/context).

Use the following command to download the example project to try out:

```sh
lb4 example context
```

## Contributions

- [Guidelines](https://github.com/inwio/inward/blob/master/docs/CONTRIBUTING.md)
- [Join the team](https://github.com/inwio/inward/issues/110)

## Tests

Run `npm test` from the root folder.

## Contributors

See [all contributors](https://github.com/inwio/inward/graphs/contributors).

## License

MIT

---
_Source: https://npm.io/package/@inward/context · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
