Class: Factory

composer-runtime. Factory

A factory creates new instances of assets, participants, transactions,
and relationships.


new Factory()

Do not attempt to create an instance of this class.

You must use the getFactory
method instead.

Source:

Methods


newInstance(ns, type, id)

Create a new instance of an asset, participant, or transaction. The
properties of the new instance should be set as standard JavaScript
object properties. The new instance can then be stored in a registry
using the appropriate registry APIs, for example {@link
module:composer-runtime.AssetRegistry AssetRegistry}.

Parameters:
Name Type Description
ns string

The namespace of the resource to create.

type string

The type of the resource to create.

id string

The identifier of the new resource.

Source:
Throws:

If the specified type (specified by the namespace and
type) is not defined in the current version of the business network.

Type
Error
Returns:

The new instance of the resource.

Type
Resource
Example
// Get the factory.
var factory = getFactory();
// Create a new vehicle.
var vehicle = factory.newInstance('org.acme', 'Vehicle', 'VEHICLE_1');
// Set the properties of the new vehicle.
vehicle.colour = 'BLUE';
vehicle.manufacturer = 'Toyota';

newRelationship(ns, type, id)

Create a new relationship with a given namespace, type, and identifier.
A relationship is a typed pointer to an instance. For example, a new
relationship with namespace 'org.acme', type 'Vehicle' and identifier
'VEHICLE_1' creates` a pointer that points at an existing instance of
org.acme.Vehicle with the identifier 'VEHICLE_1'.

Parameters:
Name Type Description
ns string

The namespace of the resource referenced by the relationship.

type string

The type of the resource referenced by the relationship.

id string

The identifier of the resource referenced by the relationship.

Source:
Throws:

If the specified type (specified by the namespace and
type) is not defined in the current version of the business network.

Type
Error
Returns:

The new instance of the relationship.

Type
Relationship
Example
// The existing driver of the vehicle.
var driver;
// Get the factory.
var factory = getFactory();
// Create a new relationship to the vehicle.
var vehicle = factory.newRelationship('org.acme', 'Vehicle', 'VEHICLE_1');
// Set the relationship as the value of the vehicle property of the driver.
driver.vehicle = vehicle;