Skcript's Logo
Skcript's Logo

Understanding Hyperledger Composer Query Language

Hyperledger Composer is a very popular Blockchain application development, the popularity is mainly because of it’s ease of use. Learn more.

Subscribe to our awesome Newsletter.

Understanding Hyperledger Composer Query Language

Currently reading:

Understanding Hyperledger Composer Query Language


Subscribe to our Newsletter:


Share article on:

One of the key component is the Query language which resembeles SQL language. In this article I’ll go through how we can build queries for your blockchain application.

In current version of Hyperledger Fabric, the LIMIT and SKIP is not supported it is so in Composer too.

The data model

For this example, I’ll consider the following data model as our application’s data model.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
enum UserRole {
  o ADMIN
  o MODERATOR
  o USER
}
participant User identified by id {
  o String id
  o String name
  o UserRole role
  o String[] hobbies
  --> Organization organization
}

participant Organization identified by id {
  o String id
  o String name
}

asset Product identified by id {
  o String id
  o String name
  o String description
  o Double quantity
  o DateTime createdAt
  --> Organization owner
}

Queries

Get the users based on role. In order to get the list of users based on their roles, we can use the where filter

1
2
3
4
5
6
query Q1 {
  description: "Select all users based on role"
  statement:
      SELECT org.acme.User
          WHERE (role == "ADMIN")
}

Also you can give the value from paramater as below

1
2
3
4
5
6
query Q1 {
    description: "Select all users based on role"
    statement:
        SELECT org.acme.User
            WHERE (role == _$role)
}

Get users based on organization To get the nodes based on their related or associated files you can use the where with following type

1
2
3
4
5
6
query Q2 {
  description: "Select all users of an organization"
  statement:
      SELECT org.acme.User
          WHERE (organization == "resource:org.acme.Organization#1")
}

Get Products with quantity more than minimal threshold We can use greater than or lesser than operators in integer or double values as follows.

1
2
3
4
5
6
query Q3 {
  description: "Select all products above the minimum quantity"
  statement:
      SELECT org.acme.Product
          WHERE (quantity > _$minimumThreshold)
}

Using AND operator

1
2
3
4
5
6
query Q4 {
  description: "Select all products above the minimum quantity of an organization"
  statement:
      SELECT org.acme.Product
          WHERE ((quantity > _$minimumThreshold) AND (owner == _$organization))
}

Using ORDER BY operator

1
2
3
4
5
6
7
query Q5 {
  description: "Select all products above the minimum quantity of an organization and order by quantity"
  statement:
      SELECT org.acme.Product
          WHERE ((quantity > _$minimumThreshold) AND (owner == _$organization))
              ORDER BY quantity
}

Using CONTAINS operator

1
2
3
4
5
6
query Q6 {
  description: "Select all users based on givien hobbies"
  statement:
      SELECT org.acme.User
          WHERE (hobbies CONTAINS ['driving', 'swimming']
}

Mostly you can do all the quering with the above mentioned operators, and also I think there will be even more complex queries in the future.


Read More:

Share article on

Comments and Discussions

Skcript


Stay Updated with Our Newsletter
SIGN UP