steady_progress
steady_progress

Reputation: 3731

Accessing array property in "named query" in hyperledger-composer

In my hyperledger composer app I want to write a named query that returns all persons with a certain hobby.

The model for "Person" is the following:

participant Person identified by id {
  o String id
  o String firstName
  o String lastName
  o String email
  --> Hobby[] hobbies optional
}

The model for "Hobby" is the following:

asset Hobby identified by name {
  o String name
}

The named query has the following structure:

query selectPersonsByHobby {
  description: "Select all persons with a certain hobby."
  statement:
      SELECT org.comp.myapp.Person
          WHERE //don't know what to put here//
}

I don't know what to put after the "WHERE" operator in order to achieve what I want.

I want something like the following:

query selectPersonsByHobby {
      description: "Select all persons with a certain hobby."
      statement:
          SELECT org.comp.myapp.Person
              WHERE (hobbies.contains(_$hobby))
}

Is this even possible ?

Upvotes: 0

Views: 85

Answers (1)

R Thatcher
R Thatcher

Reputation: 5570

The short answer is that this query should work:

query selectConsultantsBySkill {
  description: "Select all persons with a certain hobby."
  statement:
      SELECT org.comp.myapp.Person
          WHERE (hobbies CONTAINS _$targetHobby)
}

But note that because your hobbies is an array of Relationships the targetHobby parameter will have to be something like resource:org.acme.Hobby#cycling . In a production scenario you would be 'calling' the query from a UI program so you could pre-pend the relationship syntax.

I guess this is just a test example, but I wonder if Hobby needs to be a relationship? It would be easier if not.

You could alternatively use a concept (and even an enumerated type within the concept!). Here is an example of a modified model and query with a concept:

participant Person identified by id {
  o String id
  o String firstName
  o String lastName
  o String email
  o Interest[] passTime
}

concept Interest {
  o String name
  o String description
}


query selectConsultantsBySkill {
  description: "Select all persons with a certain hobby."
  statement:
      SELECT org.comp.myapp.Person
          WHERE (passTime CONTAINS (name == _$targetHobby ))
}

Upvotes: 1

Related Questions