npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

moject

v1.0.0

Published

Moject is a dependency injection and app factory package built around the modules idea of Angular and NestJs.

Downloads

11

Readme

Moject

codecov npm version npm downloads/month npm downloads license

Moject is an IoC container and an app factory package built around the modules idea of Angular and NestJs.

Usage

  npm install moject 

Use @Module decorator to define modules, @Injectable and @Inject decorators to define and inject dependencies.

Examples

See examples.

Modules

Each module is an isolated DI container. Modules can be used to group related dependencies. Moject doesn't dictate how to group dependencies, you can use module-per-feature, module-per-layer or any other approach.

@Module decorator is used to define a module:


@Module({
  imports: [/* modules to import */],
  providers: [/* providers */],
  exports: [/* providers and modules to export */]
})
export class AppModule {
  static async beforeStart() {
    // invoked before application starts
  }

  static async beforeInit() {
    // invoked before modules initialisation
  }

  async afterInit() {
    // invoked after modules initialisation
  }

  async beforeStop() {
    // invoked before application stops
  }

  async afterStop() {
    // invoked after application stops
  }
}

Providers

There are 3 types of providers: class, factory and value:

@Module({
  providers: [
    // Class provider
    {
      identifier: 'APP_CLASS',
      useClass: AppClass,
    },
    // Shortened syntax for class provider
    AppClass,
    // Factory provider
    {
      identifier: 'FACTORY',
      useFactory: () => {
        return new AppClass();
      },
    },
    // Factory provider
    {
      identifier: 'FACTORY',
      useFactory: () => {
        return new AppClass();
      },
    },
    // Value provider
    {
      identifier: 'VALUE',
      useValue: 'value to inject',
    },
  ],
})
class AppModule {}

Each class provider must be marked with @Injectable decorator as shown below:

@Injectable()
class AppClass {}

Use @Inject decorator to inject a dependency by its identifier:

@Injectable()
class SomeOtherClass {
  constructor(@Inject(AppClass) appClass: AppClass) {
    // ...
  }
}

Injection scopes

  • SINGLETONE A single instance of the provider is shared across the entire application. This is the default scope.

  • TRANSIENT Each consumer that injects a transient provider will receive a new, dedicated instance.

@Module({
  providers: [
    // Singletone scope
    {
      identifier: SomeClass,
      useClass: SomeClass,
      scope: 'SINGLETONE'
    },
    // Transient scope
    {
      identifier: SomeOtherClass,
      useClass: SomeClassClass,
      scope: 'TRANSIENT'
    },
  ],
})
class AppModule {}

Import/Exoprt

Each module can import other modules and export its providers or imported modules.

// Databse module

@Module({
  providers: [DB],
  exports: [DB],
})
class DatabaseModule {}

// App module

@Module({
  imports: [DatabaseModule],
})
class AppModule {
  // Inject a provider imported from DatabaseModule
  constructor(@Inject(DB) private gateway: DB) {}

  async afterInit() {
    await this.gateway.migrate();
  }
}

App Factory

Use App class to initialise modules and start an app as shown below:

import { AppModule } from './app.module';

// Build app
const app = App.create(AppModule);

// Start app
app.start().catch(console.error);

Once app.start is called, all your modules get initialised and dependencies resolved.

Logger

Moject provides you with a built-in logger, that can be used across the entire app:

import { AppLogger, IDENTIFIERS } from 'moject';

@Module()
class AppModule {
  constructor(@Inject(IDENTIFIERS.LOGGER) private readonly logger: AppLogger) {
    this.logger.log('Hello, world!');
  }
}

There is no need to import or provide the logger. Just inject it in your code as shown above.

If you use debug method of the logger, don't forget to set NODE_DEBUG env variable in order to see debug messages.

License

Moject is MIT licensed.