Metadata
Metadata provides a way for actions (Action) to control about how a handler should perform the reading (Reader) and writing (Writer) operations. Therefore, making possible handlers to perform differently per action basis.
This is done by defining Reader & Writer options through the metadata. By doing that the options are passed from the action to the handler during the handler's execution (Handler.runAction).
You can define options by either using option variables or the full option location:
Option var (recommended): Eliminates the need of using convoluted long names to define the options by simply using a variable that represents a full option location:
Example:
class MyAction extends Mebo.Action{
constructor(){
super();
// defining a custom uploadDirectory by using the `$webUploadDirectory` variable,
// rather than the full option location (`handler.web.readOptions.uploadDirectory`)
this.setMeta('$webUploadDirectory', '/tmp/customUploadDir');
}
async _perform(data){
// ...
}
async _after(err, value){
// defining a custom header that only affects the web handler
// this call could be done inside of the _perform method. However, we
// are defining it inside of the _after to keep _perform as
// abstract as possible. Since, _after is always called (even during
// an error) after the execution of the action, it provides a way to
// hook and define custom metadata related with the result.
if (!err){
// defining a custom header by using the `$webHeaders` variable, rather
// than the full option location (`handler.web.writeOptions.headers`)
this.setMeta('$webHeaders', {
someOption: 'foo',
});
}
}
}
Full option location: Uses a convention interpreted by the Handler.metadata to describe where the option is localized:
handler.<HANDLER_NAME>.<OPERATION>Options.<OPTION_NAME>
Example:
class MyAction extends Mebo.Action{
constructor(){
super();
this.setMeta('handler.web.readOptions.uploadDirectory', '/tmp/customUploadDir');
}
async _perform(data){
// ...
}
async _after(err, value){
if (!err){
// location (not recommended, see option var)
this.setMeta('handler.web.writeOptions.headers', {
someOption: 'foo',
});
}
}
}
The complete list of the available option variables can be found bellow, it's separated by handler type. Also, new variables can be assigned through Metadata.registerOptionVar.
Web Variables
Variable name | Value | Value used by |
---|---|---|
$web | handler.web |
|
$webUploadDirectory | $web.readOptions.uploadDirectory |
WebRequest |
$webUploadPreserveName | $web.readOptions.uploadPreserveName |
WebRequest |
$webDefaultUploadMaxFileSize | $web.readOptions.uploadDefaultMaxFileSize |
WebRequest |
$webMaxFields | $web.readOptions.maxFields |
WebRequest |
$webMaxFieldsSize | $web.readOptions.maxFieldsSize |
WebRequest |
$webHeaders | $web.writeOptions.headers |
WebResponse |
$webHeadersOnly | $web.writeOptions.headersOnly |
WebResponse |
$webResult | $web.writeOptions.result |
WebResponse |
$webRoot | $web.writeOptions.root |
WebResponse |
$webStatus | $web.writeOptions.status |
WebResponse |
$webResultLabel | $web.writeOptions.resultLabel |
WebResponse |
Cli Variables
Variable name | Value | Value used by |
---|---|---|
$cli | handler.cli |
|
$cliResult | $cli.writeOptions.result |
CliOutput |
Static Method Summary
Static Public Methods | ||
public static |
hasOptionVar(name: string): boolean Returns a boolean telling if the variable name is defined |
|
public static |
Returns the value for an option variable |
|
public static |
registerOptionVar(name: string, value: string) register an option variable under metadata |
|
public static |
Returns a list of the registered variable names under the metadata |
Constructor Summary
Public Constructor | ||
public |
Creates a metadata |
Method Summary
Public Methods | ||
public |
Returns a list of the root levels under the metadata |
|
public |
Sets a value to the metadata. |
|
public |
Returns a value under the metadata. |
Static Public Methods
public static hasOptionVar(name: string): boolean source
Returns a boolean telling if the variable name is defined
Params:
Name | Type | Attribute | Description |
name | string | variable name |
public static optionVar(name: string, processValue: boolean): string source
Returns the value for an option variable
for instance:
const myVariableValue = Mebo.Metadata.optionVar('$myVariable');
console.log(myVariableValue)
Throw:
throws an error if the var name is undefined under the metadata. |
Public Constructors
Public Methods
public setValue(path: string, value: *, merge: boolean) source
Sets a value to the metadata.
Params:
Name | Type | Attribute | Description |
path | string | path about where the value should be stored under the metadata (the levels must be separated by '.') |
|
value | * | value that is going to be stored under the collection |
|
merge | boolean |
|
this option is used to decide in case of the last level is already existing under the collection, if the value should be either merged (default) or overridden. |
public value(path: string, defaultValue: *): * source
Returns a value under the metadata.
Params:
Name | Type | Attribute | Description |
path | string | path about where the value is localized (the levels must be separated by '.'). In case of empty string the entire metadata is returned. |
|
defaultValue | * |
|
default value returned in case a value was not found for the path |
Return:
* |