When I deploy a new Hyperledger Composer project it is completely empty. Is there a way to load some kind of seed/fixtures data?
Asked
Active
Viewed 58 times
1 Answers
2
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
}

xandermonkey
- 4,054
- 2
- 31
- 53