Skip to content

Commit d60e36b

Browse files
committed
Changed queryNeptune and queryNeptuneSdk to take optional parameters and use parameterized query for getNodeProperties.
1 parent 12691dd commit d60e36b

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

src/NeptuneSchema.js

+33-23
Original file line numberDiff line numberDiff line change
@@ -57,41 +57,51 @@ function consoleOut(text) {
5757
loggerLog(text, VERBOSE);
5858
}
5959

60-
61-
async function queryNeptune(q) {
60+
/**
61+
* Executes a neptune query
62+
* @param query the query to execute
63+
* @param params optional query params
64+
* @returns {Promise<ExecuteOpenCypherQueryCommandOutput|any>}
65+
*/
66+
async function queryNeptune(query, params) {
6267
if (useSDK) {
63-
const response = await queryNeptuneSDK(q);
64-
return response;
68+
const response = await queryNeptuneSDK(query, params);
69+
return response;
6570
} else {
66-
try {
67-
const response = await axios.post(`https://${HOST}:${PORT}/${language}`, `query=${encodeURIComponent(q)}`);
68-
return response.data;
71+
try {
72+
let data = {
73+
...{query: query},
74+
...(params) && {parameters: params}
75+
};
76+
const response = await axios.post(`https://${HOST}:${PORT}/${language}`, data);
77+
return response.data;
6978
} catch (error) {
7079
msg = `Http query request failed: ${error.message}`;
7180
console.error(msg);
7281
loggerLog(msg + ': ' + JSON.stringify(error));
73-
82+
7483
if (NEPTUNE_TYPE == 'neptune-db') {
75-
consoleOut("Trying with the AWS SDK");
76-
const response = await queryNeptuneSDK(q);
84+
consoleOut("Trying with the AWS SDK");
85+
const response = await queryNeptuneSDK(query, params);
7786
useSDK = true;
78-
return response;
87+
return response;
7988
}
80-
89+
8190
throw new Error('AWS SDK for Neptune Analytics is not available, yet.');
8291
}
8392
}
8493
}
8594

8695

87-
async function queryNeptuneSDK(q) {
96+
async function queryNeptuneSDK(query, params) {
8897
try {
8998
const config = {
9099
endpoint: `https://${HOST}:${PORT}`
91100
};
92101
const client = new NeptunedataClient(config);
93102
const input = {
94-
openCypherQuery: q
103+
...{openCypherQuery: query},
104+
...(params) && {parameters: params}
95105
};
96106
const command = new ExecuteOpenCypherQueryCommand(input);
97107
const response = await client.send(command);
@@ -100,7 +110,7 @@ async function queryNeptuneSDK(q) {
100110
} catch (error) {
101111
msg = `SDK query request failed: ${error.message}`;
102112
console.error(msg);
103-
loggerLog(msg + ': ' + JSON.stringify(error));
113+
loggerLog(msg + ': ' + JSON.stringify(error));
104114
process.exit(1);
105115
}
106116
}
@@ -109,11 +119,11 @@ async function queryNeptuneSDK(q) {
109119
async function getNodesNames() {
110120
let query = `MATCH (a) RETURN labels(a), count(a)`;
111121
let response = await queryNeptune(query);
112-
loggerLog('Getting nodes names');
122+
loggerLog('Getting nodes names');
113123

114124
try {
115125
response.results.forEach(result => {
116-
schema.nodeStructures.push({ label: result['labels(a)'][0], properties: []});
126+
schema.nodeStructures.push({ label: result['labels(a)'][0], properties: []});
117127
consoleOut(' Found Node: ' + yellow(result['labels(a)'][0]));
118128
});
119129
}
@@ -212,7 +222,7 @@ function addUpdateEdgeProperty(edgeName, name, value) {
212222

213223
async function getEdgeProperties(edge) {
214224
let query = `MATCH ()-[n:${edge.label}]->() RETURN properties(n) as properties LIMIT ${SAMPLE}`;
215-
loggerLog(`Getting properties for edge: ${query}`);
225+
loggerLog(`Getting properties for edge: ${query}`);
216226
try {
217227
let response = await queryNeptune(query);
218228
let result = response.results;
@@ -238,10 +248,10 @@ async function getEdgesProperties() {
238248

239249

240250
async function getNodeProperties(node) {
241-
let query = `MATCH (n:${node.label}) RETURN properties(n) as properties LIMIT ${SAMPLE}`;
242-
loggerLog(`Getting properties for node: ${query}`);
251+
let query = `MATCH (n) WHERE $label IN LABELS(n) RETURN properties(n) as properties LIMIT $sample`;
252+
let parameters = `{"label": "${node.label}", "sample": ${SAMPLE}}`;
243253
try {
244-
let response = await queryNeptune(query);
254+
let response = await queryNeptune(query, parameters);
245255
let result = response.results;
246256
result.forEach(e => {
247257
Object.keys(e.properties).forEach(key => {
@@ -266,7 +276,7 @@ async function getNodesProperties() {
266276

267277
async function checkEdgeDirectionCardinality(d) {
268278
let queryFrom = `MATCH (from:${d.from})-[r:${d.edge.label}]->(to:${d.to}) WITH to, count(from) as rels WHERE rels > 1 RETURN rels LIMIT 1`;
269-
loggerLog(`Checking edge direction cardinality: ${queryFrom}`);
279+
loggerLog(`Checking edge direction cardinality: ${queryFrom}`);
270280
let responseFrom = await queryNeptune(queryFrom);
271281
let resultFrom = responseFrom.results[0];
272282
let queryTo = `MATCH (from:${d.from})-[r:${d.edge.label}]->(to:${d.to}) WITH from, count(to) as rels WHERE rels > 1 RETURN rels LIMIT 1`;
@@ -344,7 +354,7 @@ async function getNeptuneSchema(quiet) {
344354
try {
345355
await getAWSCredentials();
346356
} catch (error) {
347-
msg = "There are no AWS credentials configured. \nGetting the schema from an Amazon Neptune database with IAM authentication works only with AWS credentials.";
357+
msg = "There are no AWS credentials configured. \nGetting the schema from an Amazon Neptune database with IAM authentication works only with AWS credentials.";
348358
consoleOut(msg);
349359
loggerLog(msg + ': ' + JSON.stringify(error));
350360
}

0 commit comments

Comments
 (0)