# slim-di

> Minimal dependency injection framework using decorators

Latest version **0.0.8** (published 2023-03-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install slim-di
pnpm add slim-di
yarn add slim-di
bun add slim-di
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2023-03-18 |
| First published | 2023-03-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Max Netterberg |
| Maintainers | redsuperbat |
| Keywords | Dependency Injection, DI, Decorators |

## Links

- npm: https://www.npmjs.com/package/slim-di
- npm.io page: https://npm.io/package/slim-di

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.0.8 (latest) — 2023-03-18
- 0.0.6 — 2023-03-14
- 0.0.5 — 2023-03-14
- 0.0.4 — 2023-03-14
- 0.0.2 — 2023-03-14
- 0.0.1 — 2023-03-14

## README

# Slim DI

Minimalistic DI library weighing in at <1 kB. Built for usage with decorators and `reflect-metadata`.

## Installation

```
npm i slim-di
```

## Usage

`slim-di` uses typescript and classes to handle dependency injection. 
The 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.


```ts
import "reflect-metadata";

import { Injectable, createContainer } from 'slim-di';

@Injectable()
class FishingPole {}
@Injectable()
class FishingHook {}

@Injectable()
class FishingGear {
  constructor(
    private readonly pole: FishingPole,
    private readonly hook: FishingHook
  ) {}
}

@Injectable()
class FishingBoat {}

@Injectable()
class FisherManRoot {
  constructor(
    private readonly gear: FishingGear, 
    private readonly boat: FishingBoat
  ){}
}


async function main(){
  const container = createContainer(FisherManRoot);
  // Logs true
  console.log(container.get(FishingBoat) === container.get(FishingBoat))
}
main()
```

## Lifecycle hooks

`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.

```ts
import { PrismaClient } from "@prisma/client";
import { Injectable, createContainer } from 'slim-di';

@Injectable()
export class Prisma extends PrismaClient implements OnInit {
  async onInit() {
    await this.$connect();
  }
};


async function main() {
  const container = await createContainer(Prisma).init();
  const data = await container.get(Prisma).entity.findMany();
}

```


## Examples
Here is a small example using an express server and prisma.

```ts
import "reflect-metadata";

import { PrismaClient } from "@prisma/client";
import express from "express";
import { createContainer, Injectable } from "slim-di";
import { OnInit } from "slim-di";

@Injectable()
export class Prisma extends PrismaClient implements OnInit {
  async onInit() {
    console.log("Connecting to prisma...");
    await this.$connect();
    console.log("Connected!");
  }
}

@Injectable()
export class ExpressClient {
  public app = express();
}

@Injectable()
export class UserRouter {
  constructor(
    private readonly express: ExpressClient,
    private readonly prisma: Prisma
  ) {}

  register() {
    this.express.app.get("/users", async (_, res) => {
      const users = await this.prisma.user.findMany();
      res.json(users);
    });
  }
}

@Injectable()
export class MyApplication implements OnInit {
  constructor(
    private readonly express: ExpressClient,
    private readonly userRouter: UserRouter
  ) {}

  private port = 3000;

  onInit() {
    this.userRouter.register();
    this.express.app.listen(this.port, () => {
      console.log("Listening on port", this.port);
    });
  }
}

async function main() {
  await createContainer(MyApplication).init();
}

main();
```

---
_Source: https://npm.io/package/slim-di · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
