Skip to content
DMNO
🚧 DMNO is still in beta! Use with caution!
✨ If you've tried DMNO or looked through the docs, let us know what you think!

Helper methods

defineDmnoWorkspace

defineDmnoWorkspace({ opts })

See the root service config in the schema guide for more information.

This method is used to define the workspace configuration. It takes an object as an argument with the following properties:

type DmnoWorkspaceConfig = {
name?: string,
schema: Record<string, ConfigItemDefinitionOrShorthand>,
};

The ConfigItemDefinitonOrShorthand type is a union of the ConfigItemDefinition and TypeExtendsDefinition types.

ConfigItemDefinition

The ConfigItemDefinition type is an object with the following properties:

type ConfigItemDefinition = {
asyncValidate?: function(),
coerce?: function(),
description?: string,
dynamic?: boolean,
exampleValue?: any,
exportEnvKey?: string,
expose?: boolean,
extends?: DmnobaseTypes | string | function,
externalDocs?: { description, url },
importEnvKey?: string,
required?: boolean,
sensitive?: boolean,
summary?: string,
typeDescription?: string,
ui?: { color, icon },
useAt?: 'build' | 'boot' | 'run' | 'deploy' ,
validate?: function(),
value?: InlineValueResolverDef
}

Examples illustrating implicit and explicit type extensions:

import { defineDmnoWorkspace, DmnoBaseTypes } from 'dmno';
export default defineDmnoWorkspace({
schema: {
ITEM0: {}, // defaults to string
ITEM1: 'string',
ITEM2: DmnoBaseTypes.string,
ITEM3: DmnoBaseTypes.string(),
ITEM4: {
extends: 'string',
},
ITEM5: {
extends: DmnoBaseTypes.string,
},
ITEM6: {
extends: DmnoBaseTypes.string({}),
},
},
});

defineDmnoService

defineDmnoService({ opts })

See the service config in the schema guide for more information.

This method is used to define the configuration schema in each of your services (not the root). It takes an object as an argument with the following properties:

type DmnoServiceConfig = {
name?: string,
parent?: string,
tags?: string[],
settings?: {
dynamicConfig: DynamicConfigModes,
}
pick?: Array<PickConfigItemDefinition | string>,
schema: Record<string, ConfigItemDefinitionOrShorthand>,
};

This is very similar to defineDmnoWorkspace with a few additions:

  • parent is the name of the parent service.
  • tags is an array of tags for the service.
  • pick is an array of items to pick from the parent or other services. This can be a string (the key) or a PickConfigItemDefinition object.

The schema is the same as the schema property in the defineDmnoWorkspace method above.

PickItemDefinition

The PickItemDefinition type is an object with the following properties:

type PickConfigItemDefinition = {
/** which service to pick from, defaults to "root" */
source?: string;
/** key(s) to pick, or function that matches against all keys from source */
key: string | Array<string> | ((key: string) => boolean),
/** new key name or function to rename key(s) */
renameKey?: string | ((key: string) => string),
/** function to transform value(s) */
transformValue?: (value: any) => any,
};

Example:

import { defineDmnoService, DmnoBaseTypes } from 'dmno';
export default defineDmnoService({
name: 'MyConfig',
parent: 'root',
pick: [
{
key: 'ITEM0',
},
{
key: 'ITEM1',
renameKey: 'NEW_ITEM1',
},
{
key: 'ITEM2',
transformValue: (value) => `${value} transformed`,
},
],
schema: {
MYFIELD: DmnoBaseTypes.string({
required: true,
}),
MYFIELD2: DmnoBaseTypes.number({
required: true,
}),
MYFIELD3: DmnoBaseTypes.boolean({
required: true,
}),
},
});

DynamicConfigModes

The DynamicConfigModes type has the following values:

type DynamicConfigModes =
/* non-sensitive = static, sensitive = dynamic (this is the default) */
'public_static' |
/* everything static, dynamic not supported */
'only_static' |
/* everything dynamic, static not supported */
'only_dynamic' |
/* default is static */
'default_static' |
/* default_dynamic */
'default_dynamic';

createDmnoDataType

createDmnoDataType({ opts })

This method is used to create a new data type. It takes an object as an argument with the following properties:

type DataTypeOpts = {
name: string;
extends: string | DmnoDataType | (() => DmnoDataType);
settingsSchema?: Record<string, unknown>;
validate?: (ctx, settings) => boolean;
coerce?: (ctx, settings) => unknown;
};

Example:

const myType = createDmnoDataType({
name: 'MyType',
extends: DmnoBaseTypes.string({
// string specific settings object
}),
settingsSchema: {
// user type specific settings object
},
validate: (value) => {
// return true if value is valid
// has access to settingsSchema
},
coerce: (value) => {
// return coerced value
// has access to settingsSchema
},
});

You can then use it in your config schema like so:

const myType = DmnoBaseTypes.MyType({ settings });
export default defineDmnoService({
name: 'MyConfig',
parent: 'root',
schema: {
MYFIELD: {
extends: myType,
required: true,
},
MYFIELD2,
MYFIELD3,
},
});

switchBy

switchBy('SWITCH_BY_KEY': string, { opts })

This method is used to define different configurations for different values of a particular config item. Its arguments are a string (i.e., the key name) and an object with the following properties:

type opts = {
_default?: any;
[key: string]: any;
};

Note: _default is a reserved key that will be used if the current key is not defined in the object. All the other keys should match the possible values of the SWITCH_BY_KEY config item.

Example:

import { switchBy } from '@dmno/core';
export default defineDmnoService({
schema: {
SWITCH_BY_KEY: {
value: 'val1',
},
// MY_CONFIG_ITEM will be 'first value'
MY_CONFIG_ITEM: {
value: switchBy('SWITCH_BY_KEY', {
_default: 'default value',
val1: 'first value',
val2: 'second value',
val3: 'third value',
}),
},
},
});

switchByNodeEnv

switchByNodeEnv({ opts })

This method is used to define different configurations for different environments. It takes an object as an argument with the following properties:

type SwitchByNodeEnvOpts = {
_default?: any;
development?: any;
staging?: any;
production?: any;
[key: string]: any;
};

Note: _default is a reserved key that will be used if the current environment is not defined in the object. All the other keys should match the possible values of the NODE_ENV environment variable.

Example:

import { switchByNodeEnv } from '@dmno/core';
export default defineDmnoService({
schema: {
MY_CONFIG_ITEM: {
value: switchByNodeEnv({
_default: 'default value',
development: 'development value',
staging: 'staging value',
production: 'production value',
}),
},
},
});