npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cypher-tagged-templates

v4.0.0

Published

A tiny helper for writing and running Cypher queries using Javascript tagged templates

Downloads

21

Readme

cypher-tagged-templates

A tiny helper for writing and running Cypher queries using Javascript tagged templates.

Basic example

It supports variables interpolation, automatically using the Neo4j driver to escape values.

The return value of the query is an array of records, after calling the toObject method on them.

const neo4j = require('neo4j-driver').v1
const Cypher = require('cypher-tagged-templates').default


const driver = neo4j.driver('bolt://...', neo4j.auth.basic('neo4j', 'pass'))
const cypher = new Cypher({driver}).query

const email = '[email protected]'
const query = cypher`
	MATCH (user:User {email: ${email}}) RETURN user
`

const result = query.run().then(result => {
	console.log(result[0].user)
})

// at some point
// driver.close()

Enable automatic integers parsing

You can configure the helper to automatically convert Neo4j integers to native Javascript, avoiding having to deal with that yourself.

// ...

const driver = neo4j.driver('bolt://...', neo4j.auth.basic('neo4j', 'pass'))

const cypher = new Cypher({
	driver,
	parseIntegers: true
}).query

// ...

Override configuration options when running a query

// ...
const cypher = new Cypher({driver}).query
const query = cypher`
	MATCH (user:User {status: "active"}) RETURN user
`

const result = await query.run({parseIntegers: true})
// ...

Nested queries

You can also nest subqueries as variables.

// ...setup

const email = '[email protected]'
const selectDb = cypher`MATCH (neo:Database {name: "Neo4j"})`
const selectPerson = cypher`MATCH (anna:Person {email: ${email}})`
const createFriend = cypher`
	CREATE (anna)
		-[:FRIEND]->(:Person:Expert {name:"Amanda"})
		-[:WORKED_WITH]->(neo)
`

const mainQuery = cypher`
	${selectDb}
	${selectPerson}
	${createFriend}
`

const result = mainQuery.run().then(result => {
	console.log(result.records)
})

Manual queries

Instead of directly runing the queries, you can export them as a string and a parameters object so you can execute them yourself (E.g. execute multiple queries as part of a transaction).

// ...setup

const email = '[email protected]'
const status = 'active'
const findUser = cypher`
	MATCH (user:User {email: ${email}})
	WHERE status = ${status}
	RETURN user
`

const [query, params] = findUser.export()

/*
query = 'MATCH (user:User {email: {p_0}}) WHERE status = {p_1} RETURN user'
params = {
	p_0: '[email protected]',
	p_1: 'active'
}
*/

Using with Typescript

An example of using Typescript's generic types

// ...
const cypher = new Cypher({driver}).query
const query = cypher`
	MATCH (user:User {status: "active"}) RETURN user
`

interface IUser {
	name: string
	status: 'active' | 'disabled'
}

const result = await query.run<{user: IUser}>({parseIntegers: true})
// result is an array of {user: IUser}
// ...

API

interface IHelperConfig {
	driver?: neo4j.Driver
	parseIntegers?: boolean
}

class CypherHelper {
	constructor(config: IHelperConfig = {})
	query = (strings: TemplateStringsArray, ...params: any[]): CypherQuery
}

class CypherQuery {
	export(prefix: string = 'p'): [string, any]
	async run<T extends Object>(config: IHelperConfig = {}): Promise<T[]>
}