Skip to main content

ObjectSource

The ObjectSource implementation of ConfigurationSource is quite straightforward; it simply uses a JavaScript Object as a source of configuration data. This is most useful for setting application defaults or runtime overrides.

The default Configurator source list uses two ObjectSource implementations:

  • A low-priority source reading from context.defaults (sequence 300) - for application defaults
  • A high-priority source reading from context.overrides (sequence 1000) - for runtime overrides

You can create additional ObjectSource instances with custom contextName values to read from different fields in the context.

import { Configurator, Schema } from '@versionzero/configurator';
import { ObjectSource } from '@versionzero/configurator/sources';

// Custom source reading from context.magic
const magicSource = new ObjectSource({ contextName: 'magic', sequence: 500 });

const schema = new Schema('object')
.property('server', new Schema('object')
.property('hostname', new Schema('string').default('127.0.0.1'))
.property('port', new Schema('number').default(8080))
)

const configurator = new Configurator({schema, sources: [...Configurator.getDefaultSources(), magicSource]})

const context = {
appName: 'neato',
env: {'NEATO_SERVER_PORT': '80'},
magic: {
server: {hostname: 'localhost'}
}
}

console.log( await configurator.configure(context) );
// -> { server: { hostname: 'localhost', port: 80 } }

caution

This library uses the word defaults in multiple contexts:

  • Schema defaults default values defined in schema's default options
  • Application defaults (loaded by ObjectSource from context.defaults) - application-level defaults

API

See the ObjectSource API for more info.