Blobafet
Blobafet

Reputation: 519

How can I import a Cypher query to my Node.js logic?

I'm not a very experienced developer but I am looking too structure my project so it is easier to work on.

Lets say I have a function like this:

const x = async (tx, hobby) => {
  const result = await tx.run(
    "MATCH (a:Person) - [r] -> (b:$hobby) " +
    "RETURN properties(a)",
    { hobby }
  )
  return result
}

Can I put my cypher query scripts in seperate files, and reference it? I have seen a similar pattern for SQL scripts.

This is what I'm thinking:

const CYPHER_SCRIPT = require('./folder/myCypherScript.cyp')

const x = async (tx, hobby) => {
  const result = await tx.run( 
    CYPHER_SCRIPT,
    { hobby }
  )
  return result
}

..or will i need to stringify the contents of the .cyp file? Thanks

Upvotes: 0

Views: 333

Answers (2)

fridaystreet
fridaystreet

Reputation: 166

For anyone landing here late, If you're using type script you can add it to a globa type definition

//global.d.ts
declare module '*.cypher' {
  const content: string;
  export default content;
}

then you can just do

import cypher from './mycypher.cypher'

Upvotes: 0

cybersam
cybersam

Reputation: 67019

You can use the @cybersam/require-cypher package (which I just created).

For example, if folder/myCypherScript.cyp contains this:

MATCH (a:Person)-->(:$hobby)
RETURN PROPERTIES(a)

then after the package is installed (npm i @cybersam/require-cypher), this code will output the contents of that file:

// Just require the package. You don't usually need to use the returned module directly.
// Handlers for files with extensions .cyp, .cql, and .cypher will be registered.
require('@cybersam/require-cypher');

// Now require() will return the string content of Cypher files
const CYPHER_SCRIPT = require('./folder/myCypherScript.cyp')

console.log(CYPHER_SCRIPT);

Upvotes: 0

Related Questions