{"id":20790296,"url":"https://github.com/redsuperbat/slim-di","last_synced_at":"2026-04-18T10:04:41.583Z","repository":{"id":142640816,"uuid":"613781365","full_name":"redsuperbat/slim-di","owner":"redsuperbat","description":"Minimal dependency injection framework using decorators","archived":false,"fork":false,"pushed_at":"2023-03-20T11:01:13.000Z","size":178,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-25T22:32:58.810Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/redsuperbat.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-14T09:03:11.000Z","updated_at":"2023-03-14T12:46:24.000Z","dependencies_parsed_at":"2023-04-28T23:48:53.972Z","dependency_job_id":null,"html_url":"https://github.com/redsuperbat/slim-di","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/redsuperbat/slim-di","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redsuperbat%2Fslim-di","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redsuperbat%2Fslim-di/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redsuperbat%2Fslim-di/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redsuperbat%2Fslim-di/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redsuperbat","download_url":"https://codeload.github.com/redsuperbat/slim-di/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redsuperbat%2Fslim-di/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31964547,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T00:39:45.007Z","status":"online","status_checked_at":"2026-04-18T02:00:07.018Z","response_time":103,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-17T15:33:41.988Z","updated_at":"2026-04-18T10:04:36.564Z","avatar_url":"https://github.com/redsuperbat.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Slim DI\n\nMinimalistic DI library weighing in at \u003c1 kB. Built for usage with decorators and `reflect-metadata`.\n\n## Installation\n\n```\nnpm i slim-di\n```\n\n## Usage\n\n`slim-di` uses typescript and classes to handle dependency injection. \nThe first thing you need to do is declare your root class. By default `slim-di` creates a single instance of each class in the program.\n\n\n```ts\nimport \"reflect-metadata\";\n\nimport { Injectable, createContainer } from 'slim-di';\n\n@Injectable()\nclass FishingPole {}\n@Injectable()\nclass FishingHook {}\n\n@Injectable()\nclass FishingGear {\n  constructor(\n    private readonly pole: FishingPole,\n    private readonly hook: FishingHook\n  ) {}\n}\n\n@Injectable()\nclass FishingBoat {}\n\n@Injectable()\nclass FisherManRoot {\n  constructor(\n    private readonly gear: FishingGear, \n    private readonly boat: FishingBoat\n  ){}\n}\n\n\nasync function main(){\n  const container = createContainer(FisherManRoot);\n  // Logs true\n  console.log(container.get(FishingBoat) === container.get(FishingBoat))\n}\nmain()\n```\n\n## Lifecycle hooks\n\n`slim-di` exposes one lifecycle hook called `onInit` which triggers during instantiation and can be used to connect to databases or other init-work. To trigger the hook on your class instance you must call the `DIContainer.init` method.\n\n```ts\nimport { PrismaClient } from \"@prisma/client\";\nimport { Injectable, createContainer } from 'slim-di';\n\n@Injectable()\nexport class Prisma extends PrismaClient implements OnInit {\n  async onInit() {\n    await this.$connect();\n  }\n};\n\n\nasync function main() {\n  const container = await createContainer(Prisma).init();\n  const data = await container.get(Prisma).entity.findMany();\n}\n\n```\n\n\n## Examples\nHere is a small example using an express server and prisma.\n\n```ts\nimport \"reflect-metadata\";\n\nimport { PrismaClient } from \"@prisma/client\";\nimport express from \"express\";\nimport { createContainer, Injectable } from \"slim-di\";\nimport { OnInit } from \"slim-di\";\n\n@Injectable()\nexport class Prisma extends PrismaClient implements OnInit {\n  async onInit() {\n    console.log(\"Connecting to prisma...\");\n    await this.$connect();\n    console.log(\"Connected!\");\n  }\n}\n\n@Injectable()\nexport class ExpressClient {\n  public app = express();\n}\n\n@Injectable()\nexport class UserRouter {\n  constructor(\n    private readonly express: ExpressClient,\n    private readonly prisma: Prisma\n  ) {}\n\n  register() {\n    this.express.app.get(\"/users\", async (_, res) =\u003e {\n      const users = await this.prisma.user.findMany();\n      res.json(users);\n    });\n  }\n}\n\n@Injectable()\nexport class MyApplication implements OnInit {\n  constructor(\n    private readonly express: ExpressClient,\n    private readonly userRouter: UserRouter\n  ) {}\n\n  private port = 3000;\n\n  onInit() {\n    this.userRouter.register();\n    this.express.app.listen(this.port, () =\u003e {\n      console.log(\"Listening on port\", this.port);\n    });\n  }\n}\n\nasync function main() {\n  await createContainer(MyApplication).init();\n}\n\nmain();\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredsuperbat%2Fslim-di","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredsuperbat%2Fslim-di","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredsuperbat%2Fslim-di/lists"}