- Picobuf
Represents the main class for the Picobuf library.
Picobuf can be instantiated in two ways: using the
new
keyword or callingPicobuf
as a function. Both methods are equivalent. The constructor can receive an optionaldefinitions
parameter, which is an object that can include model, enum, and service definitions. Alternatively, these elements can be defined after instantiation, using the relevant methods.A Picobuf instance is a Proxy object, so it allows destructuring of models, enums, and services directly from the instance.
- Config :
Config
Represents the Config class that handles configuration for the application.
- Domain :
Domain
Represents a Domain.
- BaseEncoder
Represents a base encoder.
- NoneEncoder ⇐
BaseEncoder
Represents a NoneEncoder.
- MsgPackEncoder ⇐
BaseEncoder
Represents a MsgPackEncoder.
- Enum
Represents an enumeration.
- Field
Represents a Field.
- BaseField
Represents a base field. This is an abstract base class and cannot be instantiated directly.
- StringField ⇐
BaseField
Represents a string field.
- BooleanField ⇐
BaseField
Represents a boolean field.
- NumberField ⇐
BaseField
Represents a numeric field.
- IntegerField ⇐
NumberField
Represents an integer field.
- FloatField ⇐
NumberField
Represents a floating-point number field.
- EnumField ⇐
BaseField
Represents an enumeration field.
- JsonField ⇐
BaseField
Represents a JSON field.
- BufferField ⇐
BaseField
Represents a buffer field.
- ForeignField ⇐
BaseField
Represents a foreign field.
- ModelBuilder
Represents a Model Builder.
- Model
Represents a model.
- PicobufNode ⇐
Picobuf
Represents the PicobufNode class, which extends Picobuf.
- Method
Represents a Method.
- Service
Represents a Service.
- DEFAULT_CONFIG :
Object
Default configuration values.
- GlobalConfig :
Config
The global configuration instance.
- DEFAULT_BUFFERS :
Array.<string>
Default buffers.
- DEFAULT_FIELD_TYPE :
string
Default field type.
- DEFAULT_OBJECT_MODE :
boolean
Default object mode.
- DEFAULT_ENCODER :
string
Default encoder.
- SINGLE_PROP :
string
Single property.
- DEFAULT_STRICT :
boolean
Default strict setting.
- MODEL_TYPES :
Object
Model types.
- GlobalDomain :
Domain
The global domain instance.
- _
Picobuf utilities. This module provides a custom utility library, combining individual lodash function modules into a single object. It's designed to provide the most frequently used lodash functions, while minimizing the amount of code that's needed. Each function is imported from its own lodash module, which allows for efficient tree-shaking by the bundler. Additionally, the module includes some custom utilities such as
isBrowser
for checking if the code is running in a browser environment.
Represents the main class for the Picobuf library.
Picobuf can be instantiated in two ways: using the new
keyword or calling Picobuf
as a function. Both methods are equivalent.
The constructor can receive an optional definitions
parameter, which is an object that can include model, enum, and service definitions.
Alternatively, these elements can be defined after instantiation, using the relevant methods.
A Picobuf instance is a Proxy object, so it allows destructuring of models, enums, and services directly from the instance.
- Picobuf
- ~Picobuf
- new Picobuf([options], config, domain)
- instance
- .load(options)
- .setDomain(domain)
- .createModel(name, fields, config, [modelClass]) ⇒
Model
- .createModels(models)
- .getModel(name) ⇒
Model
|undefined
- .createEnum(name, values, config, [enumClass]) ⇒
Enum
- .createEnums(enums)
- .getEnum(name) ⇒
Enum
|undefined
- .createService(name, definition) ⇒
Service
- .createServices(services)
- .getService(name) ⇒
Service
|undefined
- static
- ~Picobuf
Kind: inner class of Picobuf
- ~Picobuf
- new Picobuf([options], config, domain)
- instance
- .load(options)
- .setDomain(domain)
- .createModel(name, fields, config, [modelClass]) ⇒
Model
- .createModels(models)
- .getModel(name) ⇒
Model
|undefined
- .createEnum(name, values, config, [enumClass]) ⇒
Enum
- .createEnums(enums)
- .getEnum(name) ⇒
Enum
|undefined
- .createService(name, definition) ⇒
Service
- .createServices(services)
- .getService(name) ⇒
Service
|undefined
- static
Creates a new instance of Picobuf.
Throws:
Error
If the options are not valid or if the loading from file is attempted outside Node.js environment.
Param | Type | Description |
---|---|---|
[options] | object |
Optional options to configure the Picobuf instance. |
[options.models] | object |
An object where keys are model names and values are model definitions. |
[options.enums] | object |
An object where keys are enum names and values are enum definitions. |
[options.services] | object |
An object where keys are service names and values are service definitions. |
config | Config |
The configuration options for the Picobuf. |
domain | Domain |
The domain for the Picobuf. |
Loads the provided options into the Picobuf.
Kind: instance method of Picobuf
Param | Type | Description |
---|---|---|
options | Object |
The options to load. |
Sets the domain for the Picobuf.
Kind: instance method of Picobuf
Param | Type | Description |
---|---|---|
domain | Domain |
The domain to set. |
picobuf.createModel(name, fields, config, [modelClass]) ⇒ Model
Creates a new model in the Picobuf domain.
Kind: instance method of Picobuf
Returns: Model
- The created model.
Param | Type | Default | Description |
---|---|---|---|
name | string |
The name of the model. | |
fields | Object |
The fields for the model. | |
config | ConfigOptions |
The configuration options for the model. | |
[modelClass] | Model |
Model |
The class for the model. |
Creates multiple models in the Picobuf domain.
Kind: instance method of Picobuf
Param | Type | Description |
---|---|---|
models | Object |
An object mapping names to fields for each model. |
picobuf.getModel(name) ⇒ Model
| undefined
Retrieves a model from the Picobuf domain by its name.
Kind: instance method of Picobuf
Returns: Model
| undefined
- The retrieved model or undefined if the model does not exist.
Param | Type | Description |
---|---|---|
name | string |
The name of the model. |
picobuf.createEnum(name, values, config, [enumClass]) ⇒ Enum
Creates a new enum in the Picobuf domain.
Kind: instance method of Picobuf
Returns: Enum
- The created enum.
Param | Type | Default | Description |
---|---|---|---|
name | string |
The name of the enum. | |
values | Array.<string> |
The values for the enum. | |
config | ConfigOptions |
The configuration options for the enum. | |
[enumClass] | Enum |
Enum |
The class for the enum. |
Creates multiple enums in the Picobuf domain.
Kind: instance method of Picobuf
Param | Type | Description |
---|---|---|
enums | Object |
An object mapping names to values for each enum. |
picobuf.getEnum(name) ⇒ Enum
| undefined
Retrieves an enum from the Picobuf domain by its name.
Kind: instance method of Picobuf
Returns: Enum
| undefined
- The retrieved enum or undefined if the enum does not exist.
Param | Type | Description |
---|---|---|
name | string |
The name of the enum. |
picobuf.createService(name, definition) ⇒ Service
Creates a new service in the Picobuf domain.
Kind: instance method of Picobuf
Returns: Service
- The created service.
Param | Type | Description |
---|---|---|
name | string |
The name of the service. |
definition | Object |
The definition for the service. |
Creates multiple services in the Picobuf domain.
Kind: instance method of Picobuf
Param | Type | Description |
---|---|---|
services | Object |
An object mapping names to definitions for each service. |
picobuf.getService(name) ⇒ Service
| undefined
Retrieves a service from the Picobuf domain by its name.
Kind: instance method of Picobuf
Returns: Service
| undefined
- The retrieved service or undefined if the service does not exist.
Param | Type | Description |
---|---|---|
name | string |
The name of the service. |
Sets the Config for the Picobuf class.
Kind: static method of Picobuf
Param | Type | Description |
---|---|---|
newConfig | ConfigOptions |
The new configuration options for the Picobuf class. |
Config : Config
Represents the Config class that handles configuration for the application.
Kind: global class
- Config :
Config
- new Config(initialConfig)
- instance
- .defaults :
Object
- .setConfig(newConfig)
- .setDefault(defaultConfig)
- .get(key, [defaultValue]) ⇒
any
- .getConfig() ⇒
Object
- .defaults :
- static
Creates a new Config instance.
Param | Type | Description |
---|---|---|
initialConfig | Object |
The initial configuration. |
The default configuration.
Kind: instance property of Config
Updates the current configuration.
Kind: instance method of Config
Param | Type | Description |
---|---|---|
newConfig | Object |
The new configuration. |
Merges the default configuration into the current configuration.
Kind: instance method of Config
Param | Type | Description |
---|---|---|
defaultConfig | Object |
The default configuration. |
Gets the value of a key from the configuration.
Kind: instance method of Config
Returns: any
- The value.
Param | Type | Default | Description |
---|---|---|---|
key | string |
The key. | |
[defaultValue] | any |
|
The default value if key is not found. |
Gets the current configuration.
Kind: instance method of Config
Returns: Object
- The current configuration.
Config.get(config) ⇒ Config
Returns an instance of Config based on the passed config parameter.
Kind: static method of Config
Returns: Config
- The instance of Config.
Param | Type | Description |
---|---|---|
config | Config | Object |
The config object. |
Config.set(newConfig) ⇒ Config
Updates the global configuration.
Kind: static method of Config
Returns: Config
- The updated global configuration.
Param | Type | Description |
---|---|---|
newConfig | Object |
The new configuration. |
Returns the configuration object of a Config instance or default config.
Kind: static method of Config
Returns: Object
- The configuration object.
Param | Type | Description |
---|---|---|
config | Config | Object |
The config object. |
Merges new configuration with default configuration.
Kind: static method of Config
Param | Type | Description |
---|---|---|
newConfig | Object |
The new configuration. |
Domain : Domain
Represents a Domain.
Kind: global class
- Domain :
Domain
- new Domain(config)
- .createModel(name, fields, config, modelClass) ⇒
object
- .setModel(name, model)
- .getModel(name) ⇒
object
|null
- .createEnum(name, values, config, enumClass) ⇒
object
- .getEnum(name) ⇒
object
|null
- .setEnum(name, enumInstance)
Creates a new instance of Domain. This class provides methods to manage models and enums.
Param | Type | Description |
---|---|---|
config | Config |
A Config instance. |
Creates a new Model instance.
Kind: instance method of Domain
Returns: object
- The newly created model instance.
Param | Type | Description |
---|---|---|
name | string |
The name of the model. |
fields | object |
The fields for the model. |
config | Config |
A Config instance for the model. |
modelClass | function |
The constructor of the model class. |
Sets a Model in the Domain.
Kind: instance method of Domain
Param | Type | Description |
---|---|---|
name | string |
The name of the model. |
model | object |
The model instance to be set. |
Gets a Model from the Domain.
Kind: instance method of Domain
Returns: object
| null
- The requested model, if found. Otherwise, returns null.
Param | Type | Description |
---|---|---|
name | string |
The name of the model. |
Creates a new Enum instance.
Kind: instance method of Domain
Returns: object
- The newly created enum instance.
Param | Type | Description |
---|---|---|
name | string |
The name of the enum. |
values | Array.<string> |
The values for the enum. |
config | Config |
A Config instance for the enum. |
enumClass | function |
The constructor of the enum class. |
Gets an Enum from the Domain.
Kind: instance method of Domain
Returns: object
| null
- The requested enum, if found. Otherwise, returns null.
Param | Type | Description |
---|---|---|
name | string |
The name of the enum. |
Sets an Enum in the Domain.
Kind: instance method of Domain
Param | Type | Description |
---|---|---|
name | string |
The name of the enum. |
enumInstance | object |
The enum instance to be set. |
Represents a base encoder.
Kind: global class
Creates a new instance of BaseEncoder.
Throws:
TypeError
- If a new instance of BaseEncoder is attempted to be created directly.
Param | Type | Description |
---|---|---|
model | Model |
The model that the encoder will operate on. |
Method stub for encoding data.
Kind: instance method of BaseEncoder
Throws:
Error
- If the method is not implemented.
Param | Type | Description |
---|---|---|
data | * |
The data to encode. |
Method stub for decoding data.
Kind: instance method of BaseEncoder
Throws:
Error
- If the method is not implemented.
Param | Type | Description |
---|---|---|
data | * |
The data to decode. |
NoneEncoder ⇐ BaseEncoder
Represents a NoneEncoder.
Kind: global class
Extends: BaseEncoder
- NoneEncoder ⇐
BaseEncoder
- .encode(data) ⇒
*
- .decode(data) ⇒
*
- .encode(data) ⇒
Encodes the data by returning it unchanged.
Kind: instance method of NoneEncoder
Overrides: encode
Returns: *
- - The unchanged data.
Param | Type | Description |
---|---|---|
data | * |
The data to encode. |
Decodes the data by returning it unchanged.
Kind: instance method of NoneEncoder
Overrides: decode
Returns: *
- - The unchanged data.
Param | Type | Description |
---|---|---|
data | * |
The data to decode. |
MsgPackEncoder ⇐ BaseEncoder
Represents a MsgPackEncoder.
Kind: global class
Extends: BaseEncoder
- MsgPackEncoder ⇐
BaseEncoder
- .encode(data) ⇒
*
- .decode(data) ⇒
*
- .encode(data) ⇒
Encodes the data using msgpack.
Kind: instance method of MsgPackEncoder
Overrides: encode
Returns: *
- - The encoded data.
Param | Type | Description |
---|---|---|
data | * |
The data to encode. |
Decodes the data using msgpack.
Kind: instance method of MsgPackEncoder
Overrides: decode
Returns: *
- - The decoded data.
Param | Type | Description |
---|---|---|
data | * |
The data to decode. |
Represents an enumeration.
Kind: global class
- Enum
- new Enum(name, values, config, [domain])
- .hasValue(value) ⇒
boolean
- .getIndex(value) ⇒
number
- .getValue(index) ⇒
*
- .addValue(value) ⇒
void
Creates a new instance of Enum.
Param | Type | Default | Description |
---|---|---|---|
name | string |
The name of the enum. | |
values | Array.<string> |
The values of the enum. | |
config | ConfigOptions |
The configuration options for the enum. | |
[domain] | Domain |
GlobalDomain |
The domain for the enum, defaults to GlobalDomain if none provided. |
Checks if a value exists in the enumeration.
Kind: instance method of Enum
Returns: boolean
- True if the value exists, false otherwise.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Retrieves the index of a value in the enumeration.
Kind: instance method of Enum
Returns: number
- The index of the value, or undefined if the value does not exist.
Param | Type | Description |
---|---|---|
value | * |
The value to get the index for. |
Retrieves the value at a specific index in the enumeration.
Kind: instance method of Enum
Returns: *
- The value at the specified index, or undefined if no value exists at that index.
Param | Type | Description |
---|---|---|
index | number |
The index to get the value for. |
Adds a new value to the enum.
Kind: instance method of Enum
Throws:
Error
If the value already exists in the enum.
Param | Type | Description |
---|---|---|
value | * |
The value to be added to the enum. |
Represents a Field.
Creates a new instance of Field.
Param | Type | Description |
---|---|---|
name | string |
The name of the field. |
field | FieldOption |
The field option for the field. |
config | ConfigOptions |
The configuration options for the field. |
domain | Domain |
The domain for the field. |
Represents a base field. This is an abstract base class and cannot be instantiated directly.
Kind: global class
Creates a new instance of BaseField.
Throws:
TypeError
If attempted to be instantiated directly.
Param | Type | Description |
---|---|---|
name | string |
The name of the field. |
field | Object |
An object representing the field. This object should include 'type', 'required', 'default', and 'list' properties. |
config | ConfigOptions |
The configuration options for the field. |
domain | Domain |
The domain for the field. |
Validates the provided value against the field.
Kind: instance method of BaseField
Throws:
Error
If the value is of an incorrect type for the field.
Param | Type | Description |
---|---|---|
value | * |
The value to validate. |
Serializes the provided data.
Kind: instance method of BaseField
Returns: *
- The serialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to serialize. |
Deserializes the provided data.
Kind: instance method of BaseField
Returns: *
- The deserialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to deserialize. |
StringField ⇐ BaseField
Represents a string field.
Kind: global class
Extends: BaseField
Validates the provided value against the field.
Kind: instance method of StringField
Throws:
Error
If the value is of an incorrect type for the field.
Param | Type | Description |
---|---|---|
value | * |
The value to validate. |
Serializes the provided data.
Kind: instance method of StringField
Returns: *
- The serialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to serialize. |
Deserializes the provided data.
Kind: instance method of StringField
Returns: *
- The deserialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to deserialize. |
BooleanField ⇐ BaseField
Represents a boolean field.
Kind: global class
Extends: BaseField
- BooleanField ⇐
BaseField
- .validate(value)
- .serialize(value) ⇒
number
- .deserialize(value) ⇒
boolean
Validates the provided boolean value.
Kind: instance method of BooleanField
Overrides: validate
Throws:
Error
If the value is not a boolean.
Param | Type | Description |
---|---|---|
value | * |
The value to validate. |
Serializes the provided boolean value to 1 (for true) or 0 (for false).
Kind: instance method of BooleanField
Overrides: serialize
Returns: number
- The serialized value.
Param | Type | Description |
---|---|---|
value | * |
The boolean value to serialize. |
Deserializes the provided value to a boolean. 1 is deserialized to true, and anything else to false.
Kind: instance method of BooleanField
Overrides: deserialize
Returns: boolean
- The deserialized value.
Param | Type | Description |
---|---|---|
value | * |
The value to deserialize. |
NumberField ⇐ BaseField
Represents a numeric field.
Kind: global class
Extends: BaseField
Validates the provided numeric value.
Kind: instance method of NumberField
Overrides: validate
Throws:
Error
If the value is not a number or is NaN.
Param | Type | Description |
---|---|---|
value | * |
The value to validate. |
Serializes the provided data.
Kind: instance method of NumberField
Returns: *
- The serialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to serialize. |
Deserializes the provided data.
Kind: instance method of NumberField
Returns: *
- The deserialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to deserialize. |
IntegerField ⇐ NumberField
Represents an integer field.
Kind: global class
Extends: NumberField
Validates the provided integer value.
Kind: instance method of IntegerField
Overrides: validate
Throws:
Error
If the value is not an integer.
Param | Type | Description |
---|---|---|
value | * |
The value to validate. |
Serializes the provided data.
Kind: instance method of IntegerField
Returns: *
- The serialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to serialize. |
Deserializes the provided data.
Kind: instance method of IntegerField
Returns: *
- The deserialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to deserialize. |
FloatField ⇐ NumberField
Represents a floating-point number field.
Kind: global class
Extends: NumberField
- FloatField ⇐
NumberField
- .serialize(data) ⇒
string
- .deserialize(data) ⇒
number
- .validate(value)
- .serialize(data) ⇒
Serializes the provided floating-point number.
Kind: instance method of FloatField
Overrides: serialize
Returns: string
- The serialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to serialize. |
Deserializes the provided string to a floating-point number.
Kind: instance method of FloatField
Overrides: deserialize
Returns: number
- The deserialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to deserialize. |
Validates the provided numeric value.
Kind: instance method of FloatField
Throws:
Error
If the value is not a number or is NaN.
Param | Type | Description |
---|---|---|
value | * |
The value to validate. |
EnumField ⇐ BaseField
Represents an enumeration field.
Kind: global class
Extends: BaseField
Creates a new instance of EnumField.
Param | Type | Description |
---|---|---|
name | string |
The name of the enum field. |
field | object |
The field object. |
config | ConfigOptions |
The configuration options for the enum field. |
Validates the provided enum value.
Kind: instance method of EnumField
Overrides: validate
Throws:
Error
If the value is not a string or if it's not a valid enum value.
Param | Type | Description |
---|---|---|
value | * |
The value to validate. |
Serializes the provided enum value.
Kind: instance method of EnumField
Overrides: serialize
Returns: number
- The serialized value.
Param | Type | Description |
---|---|---|
value | * |
The enum value to serialize. |
Deserializes the provided value to an enum value.
Kind: instance method of EnumField
Overrides: deserialize
Returns: string
- The deserialized value.
Param | Type | Description |
---|---|---|
value | * |
The value to deserialize. |
JsonField ⇐ BaseField
Represents a JSON field.
Kind: global class
Extends: BaseField
Validates the provided JSON value.
Kind: instance method of JsonField
Overrides: validate
Throws:
Error
If the value is not an object.
Param | Type | Description |
---|---|---|
value | * |
The value to validate. |
Serializes the provided data.
Kind: instance method of JsonField
Returns: *
- The serialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to serialize. |
Deserializes the provided data.
Kind: instance method of JsonField
Returns: *
- The deserialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to deserialize. |
BufferField ⇐ BaseField
Represents a buffer field.
Kind: global class
Extends: BaseField
Validates the provided buffer value.
Kind: instance method of BufferField
Overrides: validate
Throws:
Error
If the value is not a buffer.
Param | Type | Description |
---|---|---|
value | * |
The value to validate. |
Serializes the provided data.
Kind: instance method of BufferField
Returns: *
- The serialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to serialize. |
Deserializes the provided data.
Kind: instance method of BufferField
Returns: *
- The deserialized data.
Param | Type | Description |
---|---|---|
data | * |
The data to deserialize. |
ForeignField ⇐ BaseField
Represents a foreign field.
Kind: global class
Extends: BaseField
Creates a new instance of ForeignField.
Throws:
Error
If the model for the foreign field could not be found.
Param | Type | Description |
---|---|---|
name | string |
The name of the foreign field. |
field | object |
The field object. |
config | ConfigOptions |
The configuration options for the foreign field. |
domain | Domain |
The domain for the foreign field. |
Validates the provided value using the model of the foreign field.
Kind: instance method of ForeignField
Overrides: validate
Param | Type | Description |
---|---|---|
value | * |
The value to validate. |
Serializes the provided value using the model of the foreign field.
Kind: instance method of ForeignField
Overrides: serialize
Returns: *
- The serialized value.
Param | Type | Description |
---|---|---|
value | * |
The value to serialize. |
Deserializes the provided value to a model instance of the foreign field.
Kind: instance method of ForeignField
Overrides: deserialize
Returns: *
- The deserialized value.
Param | Type | Description |
---|---|---|
value | * |
The value to deserialize. |
Represents a Model Builder.
Kind: global class
- ModelBuilder
- new ModelBuilder(model, fields, config, domain)
- .getFields() ⇒
Array
- .getInitialType() ⇒
string
- .getFieldsArray(arr) ⇒
Array
- .getFieldsSingle(input) ⇒
object
- .getFieldsObject(input) ⇒
Array.<Field>
- .getEncoder() ⇒
Encoder
- .getEncoderClass(encoder, encoders) ⇒
function
|undefined
- .build() ⇒
Model
Creates a new instance of ModelBuilder.
Param | Type | Description |
---|---|---|
model | Model |
The model to be built. |
fields | object |
The fields of the model. |
config | ConfigOptions |
The configuration options for the model. |
domain | Domain |
The domain for the model. |
Fetches the fields for the model.
Kind: instance method of ModelBuilder
Returns: Array
- An array of fields for the model.
Determines the initial type of the model.
Kind: instance method of ModelBuilder
Returns: string
- The initial type of the model.
Converts the model fields into an array.
Kind: instance method of ModelBuilder
Returns: Array
- The converted model fields.
Param | Type | Description |
---|---|---|
arr | Array |
The model fields to convert. |
Fetches a single field from the model.
Kind: instance method of ModelBuilder
Returns: object
- The fetched field.
Param | Type | Description |
---|---|---|
input | string |
The name of the field to fetch. |
modelBuilder.getFieldsObject(input) ⇒ Array.<Field>
Transforms an object of fields into an array of Field instances.
This method takes into account several field configurations such as type
, required
, default
,
repeated
, enum
and creates a Field instance for each field definition.
Kind: instance method of ModelBuilder
Returns: Array.<Field>
- An array of Field instances, each representing a field in the input.
Here's what happens in detail:
- If the field definition is a string, it's assumed to be the
type
of the field. An object is then created with thetype
set to the string. - If the
type
of the field isn't explicitly set, it's defaulted to thedefaultFieldType
from the configuration. - If the
required
property isn't explicitly set to a boolean, it's set based on thestrict
mode from the configuration. If strict mode is enabled, the field is consideredrequired
. - If a
default
value is provided and strict mode is enabled, the field is marked as notrequired
. - If both a
default
value is provided and the field is marked asrequired
, an error is thrown because this is an inconsistent state. - If the field is marked as
repeated
, thelist
property is set totrue
andrepeated
is deleted. - If the
type
of the field is an array, the actualtype
is set to the first element of the array andlist
is set totrue
. - If the
type
starts with '', therequired
property is set totrue
and the '' is removed from thetype
. - If
enum
orvalues
properties are provided, thetype
is set to 'enum'. - If the
type
is 'enum', depending on theenum
property value:- If it's a string, it's assumed to be the name of an Enum instance which is fetched from the domain. The values from the Enum instance are also fetched.
- If it's an Enum instance, its values are fetched.
- If it's an array, it's used as the values for the Enum.
- Finally, an Enum instance is created if
enum
is not already an Enum instance. - For each field, a Field instance is created with the name and the final configuration, and pushed to the array to be returned.
Param | Type | Description |
---|---|---|
input | Object |
An object whose properties represent field definitions. Each property value could be a string (representing a type), or an object with detailed configuration. |
Fetches the encoder for the model.
Kind: instance method of ModelBuilder
Returns: Encoder
- The encoder for the model.
Fetches the encoder class based on a string.
Kind: instance method of ModelBuilder
Returns: function
| undefined
- The fetched encoder class.
Param | Type | Description |
---|---|---|
encoder | string |
The name of the encoder to fetch. |
encoders | object |
The object containing the encoder classes keyed by class name. |
modelBuilder.build() ⇒ Model
Builds the model.
Kind: instance method of ModelBuilder
Returns: Model
- The built model.
Represents a model.
Kind: global class
- Model
- new Model(name, fields, config, [domain])
- .create(data, ...args) ⇒
*
- .validate(data) ⇒
boolean
- .encode(data) ⇒
string
- .decode(input) ⇒
Object
Creates a new instance of Model.
Param | Type | Default | Description |
---|---|---|---|
name | string |
The name of the model. | |
fields | Array.<Field> |
The fields of the model. | |
config | ConfigOptions |
The configuration options for the model. | |
[domain] | Domain |
GlobalDomain |
The domain for the model, defaults to GlobalDomain if none provided. |
Creates a new instance of the model with the provided data.
Kind: instance method of Model
Returns: *
- The created model instance.
Param | Type | Description |
---|---|---|
data | Object |
The data for the new model instance. |
...args | * |
Additional arguments. |
Validates the provided data against the model.
Kind: instance method of Model
Returns: boolean
- True if the data is valid, false otherwise.
Param | Type | Description |
---|---|---|
data | Object |
The data to validate. |
Encodes the provided data.
Kind: instance method of Model
Returns: string
- The encoded data.
Param | Type | Description |
---|---|---|
data | Object |
The data to encode. |
Decodes the provided input.
Kind: instance method of Model
Returns: Object
- The decoded data.
Param | Type | Description |
---|---|---|
input | string |
The input to decode. |
Represents the PicobufNode class, which extends Picobuf.
Kind: global class
Extends: Picobuf
- PicobufNode ⇐
Picobuf
Creates a new instance of PicobufNode.
Param | Type | Description |
---|---|---|
options | object |
The options for the PicobufNode. |
config | ConfigOptions |
The configuration options for the PicobufNode. |
domain | Domain |
The domain for the PicobufNode. |
Provides a Proxy for instantiating new PicobufNode objects.
Kind: instance property of PicobufNode
picobufNode.load(options, [loader]) ⇒ PicobufNode
Loads configuration data for the PicobufNode.
Kind: instance method of PicobufNode
Returns: PicobufNode
- The PicobufNode instance.
Throws:
Error
Throws an error if an invalid loader is provided or if the options cannot be parsed.
Param | Type | Description |
---|---|---|
options | object | string |
The options to load. If a string is passed, it is treated as a file path to load configuration from. |
[loader] | function | string |
The loader to use for loading the configuration. If a string is passed, it is used to find a default loader function. If not provided, the loader will be determined based on the file extension. |
Represents a Method.
Creates a new instance of Method.
Param | Type | Description |
---|---|---|
name | string |
The name of the method. |
requestModel | Model |
The request model for the method. |
responseModel | Model |
The response model for the method. |
Represents a Service.
Kind: global class
Creates a new instance of Service.
Param | Type | Default | Description |
---|---|---|---|
name | string |
The name of the service. | |
definition | Object |
The definition of the service. | |
[domain] | Domain |
GlobalDomain |
The domain of the service. |
service.createMethod(name, requestModel, responseModel, [domain]) ⇒ Method
Creates a new method within the Service.
Kind: instance method of Service
Returns: Method
- The created method.
Param | Type | Default | Description |
---|---|---|---|
name | string |
The name of the method. | |
requestModel | Model | string | object |
The request model for the method. Can be a Model instance, model name string or model definition object. | |
responseModel | Model | string | object |
The response model for the method. Can be a Model instance, model name string or model definition object. | |
[domain] | Domain |
this.domain |
The domain for the method. Defaults to the Service's domain. |
service.getMethod(name) ⇒ Method
| null
Retrieves a method from the Service by its name.
Kind: instance method of Service
Returns: Method
| null
- The retrieved method or null if not found.
Param | Type | Description |
---|---|---|
name | string |
The name of the method. |
Default configuration values.
Kind: global constant
Properties
Name | Type | Description |
---|---|---|
objectMode | boolean |
Default object mode. Defaults to CONSTANTS.DEFAULT_OBJECT_MODE . |
strict | boolean |
Whether to use strict mode. Defaults to CONSTANTS.DEFAULT_STRICT . |
defaultFieldType | string |
The default field type. Defaults to CONSTANTS.DEFAULT_FIELD_TYPE . |
defaultEncoder | string |
The default encoder. Defaults to CONSTANTS.DEFAULT_ENCODER . |
singleProp | boolean |
Whether to use single property mode. Defaults to CONSTANTS.SINGLE_PROP . |
fieldClasses | Object |
The field classes. |
encoderClasses | Object |
The encoder classes. |
GlobalConfig : Config
The global configuration instance.
Default buffers.
Default field type.
Default object mode.
Default encoder.
Single property.
Default strict setting.
Model types.
GlobalDomain : Domain
The global domain instance.
Picobuf utilities.
This module provides a custom utility library, combining individual lodash function modules into a single object.
It's designed to provide the most frequently used lodash functions, while minimizing the amount of code that's needed.
Each function is imported from its own lodash module, which allows for efficient tree-shaking by the bundler.
Additionally, the module includes some custom utilities such as isBrowser
for checking if the code is running in a browser environment.
Kind: global constant
See: Lodash Documentation
- _
- .isBrowser :
boolean
- .log(...args)
- .print(...args)
- .objProp(obj, key, value, opts)
- .proxyGet(obj, callback) ⇒
Proxy
- .proxyNew(obj) ⇒
Proxy
- .isBinary(value) ⇒
boolean
- .toBuffer(data, [encoding]) ⇒
Buffer
|Uint8Array
- .isBrowser :
Checks if the current execution environment is a web browser.
It returns true if the global window
object is defined, false otherwise.
Kind: static property of _
Log a message to the console.
Kind: static method of _
Param | Type | Description |
---|---|---|
...args | * |
The message or messages to log. |
Alias for _.log.
Kind: static method of _
Param | Type | Description |
---|---|---|
...args | * |
The message or messages to log. |
Define a property on an object with given options.
Kind: static method of _
Param | Type | Description |
---|---|---|
obj | Object |
The object to define a property on. |
key | string |
The name of the property. |
value | * |
The value of the property. |
opts | Object |
The property descriptor. |
Create a Proxy with a custom getter.
Kind: static method of _
Returns: Proxy
- The Proxy object.
Param | Type | Description |
---|---|---|
obj | Object |
The object to wrap. |
callback | function |
The callback to run when getting properties. |
Create a Proxy that can be used to instantiate the target as if it were a class.
Kind: static method of _
Returns: Proxy
- The Proxy object.
Param | Type | Description |
---|---|---|
obj | function |
The function to wrap. |
Check if a value is binary.
Kind: static method of _
Returns: boolean
- Whether the value is binary or not.
Param | Type | Description |
---|---|---|
value | * |
The value to check. |
Converts data to a buffer (or Uint8Array in a browser context).
Kind: static method of _
Returns: Buffer
| Uint8Array
- The data as a Buffer or Uint8Array.
Throws:
Error
If the data type is unsupported.
Param | Type | Description |
---|---|---|
data | * |
The data to convert. |
[encoding] | string |
The encoding to use. |