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 = ; { this_company = company } { return this_company; } safira; { } { return 'Company Name' } safira;
Defining objects
const safira = ; let company = name:"My Company";safira;
Getting beans
By default the bean will be available through the class name with the first lowercase letter.
const safira = ; let employee = safira;console; //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 ;
const safira = ; const employee1 = safira;const employee2 = safira;console; //false
Getting class
Safira allows you to get the class of a bean already declared
{} { return "Safira"; } safira; { super; } safira; let cat = safira; console;
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 = ; { this_name = 'my name'; } safira ;
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.
{ this_name = 'my name'; } { console; } { this_name = 'animal name'; } { console; } safira ; safira
Customizing Bean Name
const safira = ; { } { return 'Company Name'; } safira; let company = safira;console //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 = ; { } { return 'Company Name' } safira; { this_employeeCompany = employeeCompany this_age = age; this_firstName = firstName; } { return this_employeeCompany; } { return this_age; } { return this_firstName; } safira
It is also possible to inject dependencies and values directly into the properties of the class.
const safira = ; { } { return 'Company Name' } safira; {} { return this_employeeCompany; } { return this_age; } { return this_firstName; } safira ;
If the ref value is equal to the class property name, the name property can be omitted.
const safira = ; { } { return 'Company Name' } safira; {} { return this_company; } { this_company = company; } safira ;