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

@graphity/container

v0.9.0

Published

Super slim depdency injection container with Async/Promise for Javascript(& Typescript).

Downloads

4

Readme

Graphity - Container

Super slim DI(Depdency Injection) container with Async/Promise for Javascript(& Typescript).

Installation

npm install @graphity/container --save

Usage

import { Container } from '@graphity/container'

const container = new Container()

Bind simple value

container.instance('obj1', {message: 'this is obj1'})
container.instance('obj2', {message: 'this is obj2'})

await container.boot() // boot!

console.log(container.get('obj1')) // {message: 'this is obj1'}
console.log(container.get('obj2')) // {message: 'this is obj2'}

console.log(container.get('obj1') === container.get('obj1')) // true
console.log(container.get('obj2') === container.get('obj2')) // true

Bind promise value

function promise1() {
  return new Promise(resolve => resolve({message: 'this is promise1'}))
}
async function promise2() {
  sleep(500)
  return {message: 'this is promise2'}
}
container.instance('promise1', promise1())
container.instance('promise2', promise2())

await container.boot() // boot!

console.log(container.get('promise1')) // {message: 'this is promise1'}
console.log(container.get('promise2')) // {message: 'this is promise2'}

console.log(container.get('promise1') === container.get('promise1')) // true
console.log(container.get('promise2') === container.get('promise2')) // true

Bind resolver

container.resolver('resolver1', () => ({message: 'this is resolver'}))
container.resolver('resolver2', () => {
  return new Promise(resolve => {
    resolve({message: 'this is promise resolver'})
  })
})
container.resolver('resolver3', async () => {
  sleep(500)
  return {message: 'this is async resolver'}
})

await container.boot() // boot!

console.log(container.get('resolver1')) // {message: 'this is resolver'}
console.log(container.get('resolver2')) // {message: 'this is promise resolver'}
console.log(container.get('resolver3')) // {message: 'this is async resolver'}

Bind class

import { Inject } from '@graphity/container'

class Driver {
}

class Connection {
  public constructor(@Inject('driver') public driver: Driver) {
  }
}
container.bind('driver', Driver)
container.bind('connection', Connection)

await container.boot() // boot!

const connection = container.get<Connection>('connection')

console.log(connection) // Connection { driver: Driver {} }
console.log(connection.driver) // Driver {}

create

import { Inject } from '@graphity/container'

class Connection {
}

class Controller {
  public constructor(@Inject('connection') public connection: Connection) {
  }
}

container.bind('connection', Connection)

await container.boot()

const controller = await container.create(Controller)

console.log(controller) // Controller { connection: Connection {} }

invoke

import { Inject } from '@graphity/container'

class Connection {
}

class Controller {
  public retrieve(@Inject('connection') connection: Connection) {
    return connection
  }
}

container.bind('connection', Connection)

const controller = new Controller()

await container.boot()

console.log(await container.invoke(controller, 'retrieve')) // Connection { }

Service Provider

providers/typeorm.ts

import { Provider } from '@graphity/container'
import { Connection, createConnection } from 'typeorm'

export const typeorm: Provider = {
  register(app) {
    const DB_HOST = process.env.DB_HOST || 'localhost'
    const DB_DATABASE = process.env.DB_DATABASE || 'test'
    const DB_USERNAME = process.env.DB_USERNAME || 'root'
    const DB_PASSWORD = process.env.DB_PASSWORD || 'root'
    app.resolver(Connection, () => {
      return createConnection({
        type: 'mysql',
        host: DB_HOST,
        database: DB_DATABASE,
        username: DB_USERNAME,
        password: DB_PASSWORD,
        /* ... */
      })
    })
  },
  async close(app) {
    const connection = await app.get(Connection)
    await connection.close()
  },
}

controllers/user-controller.ts

import { Inject } from '@graphity/container'
import { Connection, Repository } from 'typeorm'
import { User } from '../entities/user.ts'

export class UserController {
  public constructor(
    @Inject(Connection) public connection: Connection,
    @Inject(Connection, conn => conn.getRepository(User)) public repoUsers: Repository<User>,
  ) {
  }

  public users() {
    return this.repoUsers.find()
  }

  /* ... */
}

entry.ts

import { Container } from '@graphity/container'
import { typeorm } from './providers/typeorm'
import { UserController } from './controllers/user-controller'

const app = new Container()
app.register(typeorm)

await app.boot()

const userController = await app.create(UserController)
await userController.users() // call controller!