Skip to content

Overview

The @sodacore/registry package provides a simple way to manage a global registry of keys and their values. The registry is globalised meaning that all no matter what package includes it (which can cause multiple instances) it will always be the same instance.

The Registry is a core part of the Sodacore framework as this is where we store all configuration, class modules, plugins and more, this is essentially the root of the application in a lot of cases and is used by the DI system to resolve dependencies.

Usage

As noted above, the Registry is a simple key and value storage, and has a variety of methods, see below.

Setting a value

To set a value in the registry you can use the set method, this method takes two arguments and an optional third, the key, value and whether to override/replace if a value exists (by default it will error).

typescript
import { Registry } from '@sodacore/registry';

Registry.set('myKey', 'myValue');

Getting a value

To get a value from the registry you can use the get method, this method takes one argument, the key, and will return the value if it exists, this has a generic for the value type.

typescript
import { Registry } from '@sodacore/registry';

const value = Registry.get<string>('myKey');
// value = 'myValue'

Checking if a key exists

To check if a key exists in the registry you can use the has method, this method takes one argument, the key, and will return a boolean if the key exists.

typescript
import { Registry } from '@sodacore/registry';

const exists = Registry.has('myKey');
// exists = true

Deleting a key

To delete a key from the registry you can use the delete method, this method takes one argument, the key, and will remove the key from the registry.

typescript
import { Registry } from '@sodacore/registry';

Registry.remove('myKey');

Clearing the registry

To clear the registry you can use the clear method, this method takes no arguments and will remove all keys from the registry.

typescript
import { Registry } from '@sodacore/registry';

Registry.clear();

Getting all keys

To get all keys from the registry you can use the keys method, this method takes no arguments and will return an array of all keys in the registry.

typescript
import { Registry } from '@sodacore/registry';

const keys = Registry.keys();
// [
//   'myKey',
// ]

Searching for a key

You can search for a specific key structure, using the .includes() which takes a single argument, the key to search for, the argument is optional and will simply return all keys, like the above is left.

typescript
import { Registry } from '@sodacore/registry';

const resultKeys = Registry.search('@sodacore:controller:');
// [
//   '@sodacore:controller:example',
//   '@sodacore:controller:example2',
// ]

Getting all values

Due to the nature of the framework being metadata based, you can simply return all values from the Registry which will return the value with no keys, can be useful for checking metadata.

typescript
import { Registry } from '@sodacore/registry';

const values = Registry.values();
// [
//   'myValue',
// ]

Released under the Apache-2.0 License.