# singular

> Application module system

Latest version **2.4.2** (published 2017-10-24) · 0 weekly downloads

## Install

```sh
npm install singular
pnpm add singular
yarn add singular
bun add singular
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.2 |
| Published | 2017-10-24 |
| First published | 2014-07-07 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | rumkin |

## Links

- npm: https://www.npmjs.com/package/singular
- Repository: https://github.com/rumkin/singular
- Homepage: https://github.com/rumkin/singular#readme
- Issues: https://github.com/rumkin/singular/issues
- npm.io page: https://npm.io/package/singular

## Dependencies (3)

- [bluebird](https://npm.io/package/bluebird.md) ^3.4.0
- [toposort](https://npm.io/package/toposort.md) ~0.2.10
- [function-arguments](https://npm.io/package/function-arguments.md) ^1.0.4

## Recent versions

- 2.4.2 (latest) — 2017-10-24
- 3.7.1-beta1 (next) — 2019-10-24
- 3.5.1-beta1 — 2019-10-24
- 4.3.0-b0 — 2019-09-15
- 4.2.1-beta1 — 2019-08-26
- 4.2.0-beta1 — 2019-08-26
- 4.1.2-beta1 — 2019-08-02
- 4.0.0-beta1 — 2019-08-01
- 3.7.0-beta1 — 2019-08-01
- 3.6.0-beta1 — 2019-07-30
- 3.5.0-beta1 — 2019-07-14
- 3.4.3-beta1 — 2019-07-04
- 3.4.2-beta1 — 2019-06-25
- 3.4.1-beta1 — 2019-06-25
- 3.4.0-beta1 — 2019-06-25
- … 31 more at https://npm.io/package/singular/versions

## README

Singular is modular applications boilerplate. It's using for dependency injection
in angular-like way with dependency resolving and asynchronous lazy initialization.

##Installation

Singular could be installed with NPM:

```shell
npm i singular
```

## Example

Singular is made for simple configuration and initialization:

```javascript
const Singular = require('singular');
const singular = new Singular({
  config: {    
    mongo: {
      host: 'localhost',
      port: 27017,
      base: 'testBase',
    },
    redis: {
      host: 'localhost',
      port: 6379,
    },
    users: {
      minAge: 18,
    },
  },
});

singular.module(require('./mongo.js'));
singular.module(require('./redis.js'));

async function addUser(name, age) {
  // Get config and initialized mongo and redis clients
  const [config, db, redis] = await singular.inject('config', 'mongo', 'redis');

  config.users.minAge; // => 18
  // ...
}

// ...
```

Usage and API
===

Instantiate and configure new singular instance.

```javascript
const Singular = require('singular');

const config = {
  debug: true,
  dir: __dirname,
};

const singular = new Singular({config});
```

Define factories and values using method `module`. You can do it with methods `value` or `factory` too.

```javascript
singular.module({
  // Simple value
  value: 1,
  // Define value factory
  oneFactory() {
    return 1;
  },
  // Define logger options
  loggerOptions(app) {
    return {
      ...app.config.logger,
      verbose: !! app.config.debug
    };
  },
  // Define logger function with loggerOptions
  logger(loggerOptions) {
    return function(message) {
      if (loggerOptions.verbose) console.log(message);
    };
  }
});
```

Inject dependencies with `inject` method. Inject has aliases `configure` and `run`.

```javascript
// Inject logger and log something
singular.inject((logger) => {
    logger('Hello world'); // -> 'Hello world'
});

// Regular promise
singular.inject(['mongo', 'redis'])
.then(([mongo, redis]) => {
  // do something with mongo and redis
});
```

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