Home Intro Source Mebo GitHub
import Metadata from 'mebo/src/Metadata.js'
public class | source

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

Returns a boolean telling if the variable name is defined

public static

optionVar(name: string, processValue: boolean): string

Returns the value for an option variable

public static

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

setValue(path: string, value: *, merge: boolean)

Sets a value to the metadata.

public

value(path: string, defaultValue: *): *

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:

NameTypeAttributeDescription
name string

variable name

Return:

boolean

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)

Params:

NameTypeAttributeDescription
name string

name of the variable

processValue boolean
  • optional
  • default: true

process any variables that may be defined as part of the value

Return:

string

Throw:

Error

throws an error if the var name is undefined under the metadata.

public static registerOptionVar(name: string, value: string) source

register an option variable under metadata

The value of the variable can contain other pre-defined option variables, for instance:

Mebo.Metadata.registerOptionVar('$myVar', '$otherVar.data')

Params:

NameTypeAttributeDescription
name string

name of the variable

value string

value for the variable

public static registeredOptionVars(): Array<string> source

Returns a list of the registered variable names under the metadata

Return:

Array<string>

Public Constructors

public constructor() source

Creates a metadata

Public Methods

public root(): Array<string> source

Returns a list of the root levels under the metadata

Return:

Array<string>

public setValue(path: string, value: *, merge: boolean) source

Sets a value to the metadata.

Params:

NameTypeAttributeDescription
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
  • optional
  • default: true

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:

NameTypeAttributeDescription
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 *
  • optional

default value returned in case a value was not found for the path

Return:

*