0.0.6 • Published 3 months ago

decorator-injector v0.0.6

Weekly downloads
-
License
-
Repository
-
Last release
3 months ago

Decorator-injector

Decorator-injector is a very lightweight Dependency Injection ts library\ Features:

  • is very small and simple usage
  • built on TypeScript 5.*
  • latest decorator proposal (stage-3) https://github.com/tc39/proposal-decorators
  • class fields can be decorated
  • no need to use reflect-metadata or other extra packages

Examples of usage:

Register class as DI with decorator:

@Injectable("AnotherService")
export class AnotherService {
    helloWorld() {
        console.log("Hello world! Injected AnotherService");
    }
}

Inject as class field

export class AnotherServiceTwo {
    @Inject("AnotherService") anotherService: AnotherService;

    async run() {
        this.anotherService.helloWorld();
    }
}

Register dependency directly (free to customize)

register("Knex", {
    create(args: any[]) {
        // some logic you might need
        return new MyClass(args)
    }
})

Register Knex dependency

register("Knex", {
    create(options: { connection?: any }) {
        if (!options.connection) {
            throw new Error("Database connection config is not defined");
        }
        return knex({
            client: "pg",
            connection: options.connection,
        });
    },
});

export class AnotherServiceTwo {
    @Inject("Knex", {
        connection: {
            user: "user",
            database: "admin",
            password: "1234****",
            host: "hattie.db.com",
            ssl: true,
        },
    })
    knex: Knex;

    async run() {
        const result = await this.knex.raw("SELECT 1");
    }
}

const anotherService = new AnotherServiceTwo();

await anotherService.run();
0.0.5

3 months ago

0.0.4

3 months ago

0.0.6

3 months ago

0.0.3

4 months ago

0.0.2

4 months ago

0.0.1

4 months ago