Explore Courses Blog Tutorials Interview Questions
0 votes
in Blockchain by (12.7k points)

Are there any guidelines to design the data model for chaincode in Hyperledger Fabric? Any tips in designing a data model if we have complex relationships?

For example, if I want to design the Library data model. Every book can have multiple authors and each author can have multiple books. Each book can be tagged to different categories. What is the best way to write chaincode for this example so that it is easy to query by tags or authors?

1 Answer

0 votes
by (29.5k points)

If its a data data you want to make queries by key use:

key, err := stub.CreateCompositeKey(index, []string{key1, key2, key3})

// Skiped

stub.PutState(key, value)

Put the key of the other attribute you want to query by on the composite key and then the use:

getStateByPartialCompositeKey(objectType, attributes)

That way you can get the Books of an Author or Category by quering its key in a performatic way.

Browse Categories