backend-app-api: enable service overrides over feature loaders

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-12-07 20:12:23 +01:00
parent c0f0e263b0
commit ebf083d902
5 changed files with 185 additions and 2 deletions
@@ -73,6 +73,29 @@ export default createBackendFeatureLoader({
});
```
### Overriding service factories
Service factories registered by feature loaders have lower priority by ones added directly via `backend.add`. This allows you to use a feature loader for a larger number of service implementations, but still override individual services.
The ordering in which different feature loaders or service factories are added does not matter. There is also no priority between feature loaders, if two different feature loaders add a factory for the same service, the backend will fail to start.
```ts
const backend = createBackend();
backend.add(
createBackendFeatureLoader({
async *loader() {
yield import('./commonDiscoveryService'); // discovery service
yield import('./commonRootLoggerService'); // root logger service
},
}),
);
backend.add(import('./myDiscoveryService')); // discovery service
```
The result of the above example is that the backend starts up with `./myDiscoveryService` as the discovery service implementation, while `./commonDiscoveryService` is ignored. The `./commonRootLoggerService` will still be used.
### Dynamic logic
A feature loader can also be asynchronous, and for example fetch data from an external source to determine which features to load: