npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

safira

v1.2.1

Published

Container IOC for Node.js applications

Downloads

13

Readme

Safira

A library for NodeJS projects that makes it possible to inject dependencies into Javascript classes written in ES6.

Installation

npm install safira --save

Defining classes

const safira = require('safira');

class Employee{
  constructor(company){
    this._company = company
  }
  get company(){
    return this._company;
  }
}
  
safira.define(Employee);

class Company{
  constructor(){
  }
  get name(){
    return 'Company Name'
  }
}
  
safira.define(Company);

Defining objects

const safira = require('safira');

let company = {name:"My Company"};
safira.defineObject(company,'company');

Getting beans

By default the bean will be available through the class name with the first lowercase letter.

const safira = require('safira');

let employee = safira.bean('employee');
console.log(employee.company.name); //Company Name

By default all beans built by Safira are singleton, so to change that share, specify the singleton (false) option in the class definition.

safira.define(Employee)
      .singleton(false);
const safira = require('safira');

const employee1 = safira.bean('employee');
const employee2 = safira.bean('employee');
console.log(employee1 === employee2); //false

Getting class

Safira allows you to get the class of a bean already declared

class Animal{
  constructor(){}
  get name(){
    return "Safira";
  }
}
        
safira.define(Animal);

class Cat extends safira.class('animal'){
  constructor(){
    super();
  }
}

safira.define(Cat);

let cat = safira.bean('cat');

console.log(cat.name === 'Safira');

Loading classes

By default, safira will create the beans only when needed, ie when a call to safira.bean('beanName') is performed. To change this behavior and create the bean at the time of its setting do as described below.

const safira = require('safira');

class Person{
  constructor(){
    this._name = 'my name';
  }
}

safira.define(Person)
       .build()
        .eager(); 

It is also possible to define a callback method that is invoked when the class is loaded by the safira container. By default, safira will invoke the created method if it is set, but if it is necessary to define another method as callback, the postConstruct option can be used.

class Person{
  constructor(){
    this._name = 'my name';
  }
  
  created(){
    console.log('bean created');
  }
}

class Animal{
  constructor(){
    this._name = 'animal name';
  }
  
  callbackMethod(){
    console.log('bean created');
  }
}

safira.define(Person)
       .build()
        .eager(); 
        
safira.define(Animal)
      .postConstruct('callbackMethod')
      .build()
       .eager()

Customizing Bean Name

const safira = require('safira');

class Company{
  constructor(){
  }
  get name(){
    return 'Company Name';
  }
}

safira.define(Company,'enterprise');

let company = safira.bean('enterprise');
console.log(company.name) //Company Name

Customizing Dependency Injection

Safira allows you to inject dependencies through the class constructor and properties. The beans injected by the constructor that have the same name as the class with the first lowercase letter are automatically injected, but the bean name can be customized and other values can also be injected.


const safira = require('safira');

class Company{
  constructor(){
  }
  get name(){
    return 'Company Name'
  }
}

safira.define(Company);

class Employee{
  constructor(employeeCompany,age,firstName){
    this._employeeCompany = employeeCompany
    this._age = age;
    this._firstName = firstName;
  }
  get employeeCompany(){
    return this._employeeCompany;
  }
  get age(){
    return this._age;
  }
  
  get firstName(){
    return this._firstName;
  }
}

safira.define(Employee)
      .constructorArg({ref:"company",name:"employeeCompany"})
      .constructorArg({value:28,name:"age"})
      .constructorArg({value:"Michael",name:"firstName"})

It is also possible to inject dependencies and values directly into the properties of the class.

const safira = require('safira');

class Company{
  constructor(){
  }
  get name(){
    return 'Company Name'
  }
}

safira.define(Company);

class Employee{
  constructor(){}
  get employeeCompany(){
    return this._employeeCompany;
  }
  get age(){
    return this._age;
  }
  
  get firstName(){
    return this._firstName;
  }
}

safira.define(Employee)
      .inject({ref:"company",name:"_employeeCompany"})
      .inject({value:28,name:"_age"})
      .inject({value:"Michael",name:"_firstName"});

If the ref value is equal to the class property name, the name property can be omitted.

const safira = require('safira');

class Company{
  constructor(){
  }
  get name(){
    return 'Company Name'
  }
}

safira.define(Company);

class Employee{
  constructor(){}
  get company(){
    return this._company;
  }
  set company(company){
    this._company = company;
  }
}

safira.define(Employee)
      .inject({ref:"company"});