{"id":22408249,"url":"https://github.com/ribeiro-rodrigo/safira","last_synced_at":"2025-07-31T19:31:56.886Z","repository":{"id":20654155,"uuid":"90530569","full_name":"ribeiro-rodrigo/safira","owner":"ribeiro-rodrigo","description":"Container IOC for Node.js applications","archived":false,"fork":false,"pushed_at":"2023-01-11T15:03:52.000Z","size":153,"stargazers_count":7,"open_issues_count":7,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-08T20:56:11.834Z","etag":null,"topics":["dependency-injection","ecmascript6","nodejs","object-oriented-programming"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ribeiro-rodrigo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-07T12:21:28.000Z","updated_at":"2022-11-08T21:56:33.000Z","dependencies_parsed_at":"2023-01-13T21:04:45.215Z","dependency_job_id":null,"html_url":"https://github.com/ribeiro-rodrigo/safira","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ribeiro-rodrigo%2Fsafira","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ribeiro-rodrigo%2Fsafira/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ribeiro-rodrigo%2Fsafira/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ribeiro-rodrigo%2Fsafira/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ribeiro-rodrigo","download_url":"https://codeload.github.com/ribeiro-rodrigo/safira/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228283720,"owners_count":17896267,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection","ecmascript6","nodejs","object-oriented-programming"],"created_at":"2024-12-05T11:16:52.726Z","updated_at":"2024-12-05T11:16:53.509Z","avatar_url":"https://github.com/ribeiro-rodrigo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Safira\nA library for NodeJS projects that makes it possible to inject dependencies into Javascript classes written in ES6.\n\n[![NPM Version][npm-image]][npm-url]\n[![NPM Downloads][downloads-image]][downloads-url]\n[![Build Status][build-status]][build-url]\n[![Coverage][codecov-image]][codecov-url]\n\n# Installation\n```\nnpm install safira --save\n```\n# Defining classes \n```javascript \nconst safira = require('safira');\n\nclass Employee{\n  constructor(company){\n    this._company = company\n  }\n  get company(){\n    return this._company;\n  }\n}\n  \nsafira.define(Employee);\n\nclass Company{\n  constructor(){\n  }\n  get name(){\n    return 'Company Name'\n  }\n}\n  \nsafira.define(Company);\n```\n# Defining objects\n```javascript \nconst safira = require('safira');\n\nlet company = {name:\"My Company\"};\nsafira.defineObject(company,'company');\n```\n# Getting beans\nBy default the bean will be available through the class name with the first lowercase letter.\n```javascript \nconst safira = require('safira');\n\nlet employee = safira.bean('employee');\nconsole.log(employee.company.name); //Company Name\n```\nBy default all beans built by Safira are singleton, so to change that share, specify the singleton (false) option in the class definition.\n```javascript\nsafira.define(Employee)\n      .singleton(false);\n```\n```javascript\nconst safira = require('safira');\n\nconst employee1 = safira.bean('employee');\nconst employee2 = safira.bean('employee');\nconsole.log(employee1 === employee2); //false\n```\n# Getting class\nSafira allows you to get the class of a bean already declared\n```javascript\nclass Animal{\n  constructor(){}\n  get name(){\n    return \"Safira\";\n  }\n}\n        \nsafira.define(Animal);\n\nclass Cat extends safira.class('animal'){\n  constructor(){\n    super();\n  }\n}\n\nsafira.define(Cat);\n\nlet cat = safira.bean('cat');\n\nconsole.log(cat.name === 'Safira');\n```\n# Loading classes\nBy 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.\n```javascript\nconst safira = require('safira');\n\nclass Person{\n  constructor(){\n    this._name = 'my name';\n  }\n}\n\nsafira.define(Person)\n       .build()\n        .eager(); \n```\nIt 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.\n```javascript\nclass Person{\n  constructor(){\n    this._name = 'my name';\n  }\n  \n  created(){\n    console.log('bean created');\n  }\n}\n\nclass Animal{\n  constructor(){\n    this._name = 'animal name';\n  }\n  \n  callbackMethod(){\n    console.log('bean created');\n  }\n}\n\nsafira.define(Person)\n       .build()\n        .eager(); \n        \nsafira.define(Animal)\n      .postConstruct('callbackMethod')\n      .build()\n       .eager()\n```\n\n# Customizing Bean Name\n```javascript\nconst safira = require('safira');\n\nclass Company{\n  constructor(){\n  }\n  get name(){\n    return 'Company Name';\n  }\n}\n\nsafira.define(Company,'enterprise');\n\nlet company = safira.bean('enterprise');\nconsole.log(company.name) //Company Name\n```\n# Customizing Dependency Injection\nSafira 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.\n\n```javascript\n\nconst safira = require('safira');\n\nclass Company{\n  constructor(){\n  }\n  get name(){\n    return 'Company Name'\n  }\n}\n\nsafira.define(Company);\n\nclass Employee{\n  constructor(employeeCompany,age,firstName){\n    this._employeeCompany = employeeCompany\n    this._age = age;\n    this._firstName = firstName;\n  }\n  get employeeCompany(){\n    return this._employeeCompany;\n  }\n  get age(){\n    return this._age;\n  }\n  \n  get firstName(){\n    return this._firstName;\n  }\n}\n\nsafira.define(Employee)\n      .constructorArg({ref:\"company\",name:\"employeeCompany\"})\n      .constructorArg({value:28,name:\"age\"})\n      .constructorArg({value:\"Michael\",name:\"firstName\"})\n```\n\nIt is also possible to inject dependencies and values directly into the properties of the class.\n\n```javascript\nconst safira = require('safira');\n\nclass Company{\n  constructor(){\n  }\n  get name(){\n    return 'Company Name'\n  }\n}\n\nsafira.define(Company);\n\nclass Employee{\n  constructor(){}\n  get employeeCompany(){\n    return this._employeeCompany;\n  }\n  get age(){\n    return this._age;\n  }\n  \n  get firstName(){\n    return this._firstName;\n  }\n}\n\nsafira.define(Employee)\n      .inject({ref:\"company\",name:\"_employeeCompany\"})\n      .inject({value:28,name:\"_age\"})\n      .inject({value:\"Michael\",name:\"_firstName\"});\n\n```\n\nIf the ref value is equal to the class property name, the name property can be omitted. \n\n```javascript\nconst safira = require('safira');\n\nclass Company{\n  constructor(){\n  }\n  get name(){\n    return 'Company Name'\n  }\n}\n\nsafira.define(Company);\n\nclass Employee{\n  constructor(){}\n  get company(){\n    return this._company;\n  }\n  set company(company){\n    this._company = company;\n  }\n}\n\nsafira.define(Employee)\n      .inject({ref:\"company\"});\n```\n# Learn more\nSee a complete example project [here](https://github.com/ribeiro-rodrigo/planet-service)\n\n[npm-image]: https://img.shields.io/npm/v/safira.svg\n[npm-url]: https://npmjs.org/package/safira\n[downloads-image]: https://img.shields.io/npm/dm/safira.svg\n[downloads-url]: https://npmcharts.com/compare/safira?minimal=true\n[build-status]: https://travis-ci.org/ribeiro-rodrigo/safira.svg?branch=master\n[build-url]: https://travis-ci.org/ribeiro-rodrigo/safira \n[codecov-image]: https://codecov.io/gh/ribeiro-rodrigo/safira/branch/master/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/ribeiro-rodrigo/safira\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fribeiro-rodrigo%2Fsafira","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fribeiro-rodrigo%2Fsafira","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fribeiro-rodrigo%2Fsafira/lists"}