0.0.5 • Published 10 months ago

@rym-lib/inversify-bundler v0.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

@rym-lib/inversify-bundler

Bundle ContainerModule for inversify. Co-located module bundling solution.

installation

npm i -D @rym-lib/inversify-bundler

Usage

Module creation

interface ExamplePort {}

@injectable()
class Example implements ExamplePort {
  constructor(
    @inject(DBIdentifier)
    private db: DBPort,
  ) {}
}

const { identifier: ExampleIdentifier, bundler: ExampleModule } =
  createModule<ExamplePort>('Example', Example, [DBModule])

export { ExampleIdentifier, ExampleModule }
export type { ExamplePort }

Bundling modules

import { ExampleModule, ExamplePort, ExampleIdentifier } from './example'

const container = new Container()
container.load(...ExampleModule.resolve())

const example = container.get<ExamplePort>(ExampleIdentifier)

createModule<Interface>(name: string, Module: Newable<Interface>, bindings: ModuleInput[])

const { identifier, bundler } = createModule<InteractionPort>(
  'UserRegister',
  UserRegisterInteraction,
  [PrismaModule, LoggerModule],
)

new ModuleBundler(...modules: ModuleInput[])

import { ContainerModule as InversifyContainerModule } from 'inversify'

import { ContainerModule, ModuleBundler } from '@rym-lib/inversify-bundler'

import { LoggerModule } from '~/logger'
import { prisma } from '~/my-prisma'

const name = 'Prisma'
const prismaIdentifier = Symbol.for(name)

const redisIdentifier = Symbol.for('Redis')

const bundler = new Bundler(
  LoggerModule,
  new ContainerModule(
    (bind) => {
      bind<PrismaPort>(prismaIdentifier).toDynamicValue(() => prisma)
    },
    { name },
  ),
  new InversifyContainerModule((bind) => {
    bind<RedisPort>(redisIdentifier).toDynamicValue(() => redis)
  }),
)

resolve module

resolve() exclude duplicate module and return flat ContainerModule array. But, can not resolve in already registered container's modules. Use child container.

import { Container } from 'inversify'

import { bundler } from '~/my-module'

const container = new Container()

container.load(...bundler.resolve())
0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

1 year ago