Piotr Brudny
Piotr Brudny

Reputation: 656

How to load sample data in Hyperledger Composer project

When I deploy a new Hyperledger Composer project it is completely empty. Is there a way to load some kind of seed/fixtures data?

Upvotes: 2

Views: 58

Answers (1)

xandermonkey
xandermonkey

Reputation: 4422

What I have done in the past for this is write a transaction called setup that only the admin is allowed to run. This function initializes your chain with a decent amount of mock data that you can use for testing. For example:

Model:

transaction Setup {
}

Transaction script:

/**
 * Seed chain with mock data for testing
 * @param {com.your.namespace} tx - set up request
 * @transaction
 */
async function setup(tx) {
  const factory = getFactory();

  const exampleRegistry = await getParticipantRegistry(`${namespace}.example`);

  const exampleResource= factory.newResource(namespace, "Example", "ExampleResourceName");
  example.exampleProperty = 2000;

  await exampleRegistry.add(exampleResource);

  const otherExample = factory.newResource(namespace, "OtherExample", "OtherExampleName");
  otherExample.exampleProperty = 0;
  const otherExampleRef = factory.newRelationship(namespace, "OtherExample", "OtherExampleName");

  await otherExampleRegistry.addAll([otherExample]);

  const thirdExample = factory.newResource(namespace, "ThirdExample", "ThirdExampleName");
  thirdExample.exampleRelationshipProperty = otherExampleRef
  thirdExample.exampleProperty = 0;

  await thirdExampleRegistry.addAll([thirdExample]);
}

Then, your .ACL:

rule SetupTransaction {
    description: "Deny anyone but admin access to call Setup transaction"
    participant: "com.your.namespace.**"
    operation: ALL
    resource: "com.your.namespace.Setup"
    action: DENY
}

Upvotes: 2

Related Questions