Binding the property with JSON parameter

136 Views Asked by At

I'm working on a system deals with dynamic properties.

If I want to create a vertex, I can do it like this before that:

select * from ag_catalog.cypher('people',$$ 
  create (nyk:person{name:'nyk'})
  return nyk 
$$) as (v ag_catalog.agtype);

But now, I don't know the property, the property is given by the user as a json object. The property may be {name:'asdf'} or {name:'asdf', age:25} or {name:'asdf', work:'programmer'} .... I don't know what property will get.

The problem is same when using the delete,update or get method. So how to deal with that? Can I bind the property with a json parameter?

5

There are 5 best solutions below

1
On

Yoou need to create a parameterized query with a placeholder for the JSON object and use the USING clause to send the JSON parameter when running the query to link a dynamic property with a JSON parameter in your Cypher query. This enables you to work with properties that change according to what the user enters.

SELECT *
FROM ag_catalog.cypher('people', $$
  CREATE (nyk:person {props}) RETURN nyk
$$) AS (v ag_catalog.agtype);

EXECUTE your_query USING json '{"props": {"name": "John", "age": 30, "job": "developer"}}';

0
On

You can later use the SET clause to insert the properties. Assuming that {name: 'John Doe'} is the JSON provided by the user, the following code demonstrates its usage:

SELECT * FROM cypher('test', $$
  CREATE (:Person)                   
$$) AS (res agtype);                      
 res 
-----
(0 rows)

SELECT * FROM cypher('test', $$
  MATCH (n:Person)
  SET n = {name: 'John Doe'} 
  RETURN n                
$$) AS (res agtype);
                                          res                                   
       
-----------------------------------------------------------------------------------------
 {"id": 1407374883553281, "label": "Person", "properties": {"name": "John Doe"}}::vertex
(1 row)

If you intend to incorporate user input, you'll need to use one of the drivers available for AGE, such as the Python driver.

0
On

If you would like to know the properties given by the user, you can run this query:

SELECT *
FROM cypher('people', $$
MATCH (p:people)
RETURN p
$$) as (people agtype);

This will return the nodes in the people label alongside the properties contained in the nodes.

0
On
SELECT * FROM ag_catalog.cypher('people', $$
  MATCH (p:person {name: 'nyk'}) SET p += $$ || user_properties || $$
  RETURN p
$$) AS (v ag_catalog.agtype);

Here result will be returned after comparing properties with the vertex for example {name: 'nyk'} it the property. You can use different properties at the place of user_properties. I hope it helps you Adil Bashir.

0
On

You can use any driver support to read the data from the user and then assign that read data according to the properties. You can view different driver support here at github. Once the data is read then it can be stored accordingly. like below

SELECT * FROM cypher('test', $$
  MATCH (s:Persons)
  SET s = {name: 'Talha'} 
  RETURN s                
$$) AS (res agtype);