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.
const { settingsMixinSchema } = require('@synanetics/settings-mixin');
// or
import { settingsMixinSchema } from '@synanetics/settings-mixin';
...
const someService: ServiceSchema {
name: 'some',
mixins: [settingsMixinSchema],
}
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.
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
.
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],
}
Any errors encountered are raised as Moleculer JS specific MoleculerError
or ValidationError
.