New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

singular

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

singular

Application module system

latest
Source
npmnpm
Version
2.4.2
Version published
Maintainers
1
Created
Source

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:

npm i singular

Example

Singular is made for simple configuration and initialization:

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.

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.

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.

// 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
});

FAQs

Package last updated on 24 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts