docs/docs/api/container.mdx
import Type from '@site/docs/api/components/type';
:::info
Current state: Stable
:::
The container module permits to generate containers.
new Container()const container = new Container();
container.add('config', {
get: (configName) => {},
set: (configName, value) => {},
});
const dbConfig = container.get('config').get('database');
container.add(name, resolver)name: <Type>String</Type> UID of the contentresolver: <Type>Function</Type> | <Type>Any</Type>
resolver(context, args)
context: <Type>Container</Type> The container instanceargs: <Type>Any</Type> Anything to be used by the resolver functionRegister a new content to be accessed inside the container. If the name is already used, it will throw an error.
const container = new Container();
container.add('config', (container, args) => {});
// or
container.add('services', {});
container.get(name, args)name: <Type>String</Type> UID of the contentargs: <Type>Any</Type> Value that will be passed to the resolver (if function)Get the value stored for a specific name.
const container = new Container();
container.add('config', { db: 'sqlite' });
const config = container.get('config');
// config.db === 'sqlite'
⚠️ If the resolver, used in the register function, is a function, the value will be the result of this resolver function with args as parameter on the first call to get.
Please pay attention that the resolver result value isn't awaited. So if resolver returns a promise, the value stored will be a promise.
const container = new Container();
container.add('boolean', (bool) => bool);
// First call - The value is resolved through the resolver above "(bool) => bool"
container.get('boolean', true);
// true
// Any further call will use the previously set value
container.get('boolean');
// true
// Even if we try to push a new value
container.get('boolean', false);
// true