Reputation: 115
Are there any guidelines to design the data model for chaincode in Hyperledger Fabric? Any tips in designing 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?
Upvotes: 2
Views: 485
Reputation: 537
I would suggest two approaches.
FIRST:
If its a data data you want to make queries by key use:
key, err := stub.CreateCompositeKey(index, []string{key1, key2, key3})
// Skipped
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 querying its key in an efficient way.
SECOND:
If query by the attribute is not required, just put the attribute name or id in your model before serialize and put on state.
Upvotes: 2
Reputation: 1232
Using Hyperledger composer you can easily put a RelationShip between Assets or Participates. Using extends
you can take all properties and fields required by the super-type and add any additional properties or fields from its own definition. For more, you can follow this link for Model Language Concept.
For example
Consider your example, Create a 1 Asset
Library. Ex:
asset Library identified by LibraryId {
o String LibraryId
}
Then also create a concept Book
and also create a Participate Author
which is extended by Book concept. And also you can put your addition field in any Participate/assets/concepts.
Upvotes: 0