Skip to content

sdellysse/radford

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Radford: a service container and dependency injector for your application

Installation

npm install radford

Description

Radford is a lightweight service container for managing initialization and dependencies inside your app. Each service can request other services, and service can take optional arguments. This allows the components of your application to stay uncoupled from one another and Radford will build the service graph for you.

Usage

import Radford from "radford";
import { native: pgnative } from "pg";
import wrap from "pg.promised";

const cache = {};
const radford = new Radford({
    definitions: {
        db: {
            dependencies: [
                ["logger", {
                    name: "db",
                }],
            ],

            create: ({ logger }, {}) => {
                if (!cache.db) {
                    cache.db = wrap(pgnative);
                }

                return cache.db;
            },
        },

        logger: {
            create: ({}, { name }) {
                cache.logger = cache.logger || {};

                if (!cache.logger[name]) {
                    cache.logger[name] = function (...messages) {
                        console.log(`[${ name.toUpperCase() }]`, ...messages);
                    };
                }

                return cache.logger[name];
            },
        },
    },
});

const { db } = radford.invoke("db");

db.query() //...

About

An asynchronous service container for you application

Resources

Stars

Watchers

Forks

Packages

No packages published