...by Daniel Szego
quote
"Simplicity is the ultimate sophistication."
Leonardo da Vinci

Saturday, July 7, 2018

Fabric composer tips and tricks - create a new concept


To create a concept in Fabric composer in the transaction is pretty similar to creating an asset, we consider the use case as you have to add a new concept as creating a new asset. you have to do the following steps:

0. Supposing you have a model file and an asset with a concept:

  concept ConceptName {
    o <type> Field1
    o <type> Field2
    ...
  }

  asset AsstetName identified by <primkey> {
    o <type> <primkey>
    o ConceptName ConceptField 
    ...
  }

1. get a factory object: it helps you to create other resources, events, assets, participants and so on.

  const factory = getFactory();

2. create a new concept with the help factory.newConcept after that  create a new asset with the help of the factory newResource call and with a new value for the primary key. After that update all the required fields both for your concept and for your asset and then associate the concept with the adequate field of your asset. 

  const newConcept = await factory.newConcept(namespace,
     'ConceptName');
  newConcept.Field1 = <value1>;
  newConcept.Field2 = <value2>;

  const newAsset = factory.newResource(namespace, 'AssetName',
     <primkey>);
  newAsset.ConceptField = newConcept ;
  newParticipant.RequiredField2 = value2;
    ...

3.  Get an asset registry to update your asset:

  const newAssetRegistry = await getAssetRegistry( 
      namespace + '.AssetName');

4.  add the new asset to the asset registry.

   await newAssetRegistry.add(newAsset);