open-index/open-npm huggingface dataset:
npm package: @synanetics/settings-mixin
Description: Mixin for Moleculer JS service to apply JSON settings resolved by file pattern
Author: David Tolman
Latest version: 2.0.0

# `@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

```javascript
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

```javascript
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](https://www.npmjs.com/package/@synanetics/settings-resolver#loading-defaults-only).

### 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](https://www.npmjs.com/package/@synanetics/settings-resolver#applying-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](https://www.npmjs.com/package/@synanetics/settings-resolver#validating-the-resulting-object) but you would look to define a [fastest-validator](https://www.npmjs.com/package/fastest-validator) compliant schema.

some.service.ts

```javascript
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`.