View on GitHub

coredi

Dependency Injection library for Node.js and Browser

Schemas

For container services describe, CoreDI use schema.

When you call:

const coredi = require('@empla/coredi');

coredi(schema, containerName).then(function (container) {
    //...
});

coredi accepts in first argument schema definition, and second argument is container name.

schemas look like this:

{
    containerName: {
        loaders: [
            {
                name: 'serviceName',
                config: {
                    defaultParameter: 'defaultValue',
                },
                async create() {
                    return myService;
                }
            },
            {
                name: 'serviceName2',
                config: {
                    defaultParameter: 'defaultValue2',
                },
                async create() {
                    return myService2;
                }
            },
            // ...
        ],
        config: {
            myService: {
                defaultParameter: 'override'
            },
            myService2: {
                defaultParameter: 'override2'
                someParameter: '...',
            },
        }   
    }
}

Schema properties

Config in schema overrides config in service loader. For override we use lodash.defaultsDeep() function