@synanetics/settings-mixin
v2.0.0
Published
Mixin for Moleculer JS service to apply JSON settings resolved by file pattern
Downloads
14
Maintainers
Keywords
Readme
@synanetics/settings-mixin
Settings Mixin for Moleculer JS services. The mixin will attempt to resolve a local (default) settings file named after your service. Resolve environment configured overrides. Merge setting values into the service settings.
It will validate that default and override settings are structured correctly. It will also optionally look at the services existing settings.schema
property and apply this validation schema to the resolved values.
Usage
const { settingsMixinSchema } = require('@synanetics/settings-mixin');
// or
import { settingsMixinSchema } from '@synanetics/settings-mixin';
...
const someService: ServiceSchema {
name: 'some',
mixins: [settingsMixinSchema],
}
Defaults
For this mixin to work you must provide a default settings file co-located with your service. Assume you have a service like above:
some.service.ts
const someService: ServiceSchema {
name: 'some',
mixins: [settingsMixinSchema],
}
then a corresponding file named some.settings.json
must exist. For the structure of the contents please follow this guidance on settings-resolver defaults.
Overrides
The mixin will look for an environment variable following the name of the service. For example some.service.ts
with name: 'some'
would attempt to check process.env.SOME_SETTINGS
. Please follow the guidance on settings-resolver overrides to see what this value can be.
If the service specific environment variable cannot be found, the mixin will fallback to checking process.env.SETTINGS
. If both are undefined just the default file values will be set on the service settings
.
Value Validation
You can optionally supply additional validation schema to apply to the resolved settings to ensure that your values conform as expected. This is done by setting the service settings.schema
to the desired validation schema. There is additional guidance on settings-resolver additional validation but you would look to define a fastest-validator compliant schema.
some.service.ts
const someService: ServiceSchema {
name: 'some',
settings: {
schema: { ...your validation schema },
},
mixins: [settingsMixinSchema],
}
Errors
Any errors encountered are raised as Moleculer JS specific MoleculerError
or ValidationError
.