如何在 Hyperledger Composer 项目中加载示例数据

How to load sample data in Hyperledger Composer project

当我部署一个新的 Hyperledger Composer 项目时,它完全是空的。有没有办法加载某种 seed/fixtures 数据?

我过去为此所做的是编写一个名为 setup 的交易,只有管理员可以 运行。此函数使用大量可用于测试的模拟数据初始化您的链。例如:

型号:

transaction Setup {
}

交易脚本:

/**
 * 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]);
}

那么,你的.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
}