Using a variable in n10s.rdf.import.fetch call

Is it possible to use a variable when making a call to
n10s.rdf.import.fetch(, 'Turtle')?

As can be seen from the code below, hard-coding the absolute path into the call
works perfectly. Problem is I want to load a number of rdf files into the same graph.

The call is the same except for the filename. Writing a new function for every different rdf file
is not really workable. Moving the project to another directory would require rewriting all
the hard coded calls, again not really workable.

The problem is in the line
"call n10s.rdf.import.fetch(${param}, 'Turtle');
${param} is not defined

everywhere else it is defined and evaluates to the expected string

// This is usingParam.js
// how to get n10s to work with a parameter

const loadFromFileMajorMap = ( session, filename ) =>
{
let directoryPath = path.join( __dirname, '../rdf_src');
directoryPath = directoryPath.replaceAll('\', '\\\\');
let param = "";

// first check that the file name ends in .ttl
if( filename.endsWith(".ttl"))
{
param = 'file:///' + directoryPath + '\\\\' + filename;
console.log('\nvalue of param is = ' + ${param});

    // If there is a hard coded path then the call works correctly
	// How to get it to work with a parameter?
   session
  .run
  (
    /* ***************************************************************
     *   This works!.                                                *
     *   "call n10s.rdf.import.fetch('file:///                       *
     * E:\\\\Graph_Databases\\\\Student_Admin_System\\\\rdf_src      *
     *    \\\\Software_Engineering_Major_Map.ttl', 'Turtle')"        *
     *                                                               *
     * ***************************************************************/
    "call n10s.rdf.import.fetch(`${param}`, 'Turtle')"
  )
  .then( ( ) =>
  {
      console.log('\nin .then() param is: ' + `${param}`);
  })
  .catch( (error) =>
  {
      console.log('error = ' + error);
  });

}
else
{
console.log('file name must end in .ttl');
}
}

module.exports = loadFromFileMajorMap

thanks

  Richard Tresider

Hey Richard,
Not sure I'm understanding it right. Are you asking what's the syntax in javascript to pass parameters to a cypher script? If that's the case then you can find an example here.

I'll adapt it to our case for convenience (note that I've not tested it :slight_smile: but I guess it gives you the idea) :

const neo4j = require('neo4j-driver')

const driver = neo4j.driver(uri, neo4j.auth.basic(user, password))
const session = driver.session()
const fileName = 'file:///path/to/file/myrdfdoc.ttl'

try {
  const result = await session.run(
    'call n10s.rdf.import.fetch($theFile, 'Turtle') ',
    { theFile: fileName }
  )

  const singleRecord = result.records[0]
  
... do what you want with the output ...

} finally {
  await session.close()
}

// on application exit:
await driver.close()

Let me know if that's what you were after or if i've missed the point.

Cheers,

JB.

Hi Jesus,
You've got it exactly right. I want one function that reads in rdf files. Since it's the
same n10s call the only thing that changes is the name of the file. Hence the
use of the parameter for the file name.

This is slightly embarrassing :blush:, that's the way I pass parameters into
cypher calls in Node.js for create, merge and match, don't know why I
thought n10s wouldn't work the same way.

Now it works perfectly

once again, thanks so much for your help

Richard Tresider

1 Like