Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/neverstew/cypher-template-strings
ES6 tagged template strings for prepared statements with neo4j
https://github.com/neverstew/cypher-template-strings
Last synced: about 1 month ago
JSON representation
ES6 tagged template strings for prepared statements with neo4j
- Host: GitHub
- URL: https://github.com/neverstew/cypher-template-strings
- Owner: neverstew
- Created: 2022-07-03T08:24:55.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-17T21:33:40.000Z (over 1 year ago)
- Last Synced: 2024-11-10T13:43:21.526Z (about 2 months ago)
- Language: TypeScript
- Size: 532 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Cypher Template Strings
An easy way to use tagged template strings to construct cypher queries for the official [neo4j-javascript-driver](https://github.com/neo4j/neo4j-javascript-driver).
## Importing
```js
// with ES6
import cypher from 'cypher-template-strings'
import { cypher } from 'cypher-template-strings'// with CommonJS
const { cypher } = require('cypher-template-strings');
const cypher = require('cypher-template-strings').default;
```## Examples
```js
// simple substitution
let id = 1234;
driver.run(cypher`MATCH (person:Person { id: ${id} }) RETURN person`)// skips undefined or null expressions
// the following expression is equivalent to the first
let u = undefined;
let n = null;
driver.run(cypher`
MATCH (person:Person { id: ${id} })
${u && 'WHERE person.firstname = "Barney"'}
${n && 'ORDER BY person.id'}
RETURN person
`)// supports infinitely nestable templates
let brother = 2345
driver.run(cypher`
MATCH (person:Person)
WHERE person.id = ${id}
${brother && cypher`
AND EXISTS {
MATCH (person)-[:BROTHER]->(:Person { id: ${brother} })
}
`}
RETURN person
`)
```## How it works
The `query` and `params` objects passed to the driver are constructed by converting values in the tagged template into parameter keys e.g. `p_1`, `p_2` and adding those to the `params` object.
Using the example from earlier, we can see the corresponding output:
```js
let id = 1234
let brother = 2345
cypher`
MATCH (person:Person)
WHERE person.id = ${id}
${brother && cypher`
AND EXISTS {
MATCH (person)-[:BROTHER]->(:Person { id: ${brother} })
}
`}
RETURN person
`{
query: `
MATCH (person:Person)
WHERE person.id = $p_0
AND EXISTS {
MATCH (person)-[:BROTHER]->(:Person { id: $p_1 })
}
RETURN person
`,
params: { p_0: 1234, p_1: 2345 }
}
```## Contributing
Feel free! This is just getting started.
## Improvements
- [ ] Convert some js types into standard serializable types e.g. Date
- [ ] Strongly type params for better inspection