I am using neo4j-driver in order to run my query:
import neo4j from "neo4j-driver";
const driver = neo4j.driver(
process.env.NEO4J_URI,
neo4j.auth.basic(process.env.NEO4J_USER, process.env.NEO4J_PASS)
);
export const session = driver.session();
When I retrieve the results, I now have to map
trough all of the records and retrieve all the values in order to format the data that is otherwise in the _fields
property. Is there a way to do this inside the DB query, or without using separate .get
calls. I am worried about performance, since the number of nodes will be be big, and mapping trough them could burn unnecessary resources.
export const getColorsList = async (): Promise<Color[]> => {
const readQuery = `
MATCH (c:Color)-[a:ASSOCIATED_WITH]->(t:Tag)
WITH COLLECT(t.name) AS tags, c
RETURN c.alternativeNames AS alternativeNames, c.hex AS hex, c.name AS name, c.temperature AS temperature, tags AS tags
`;
const readResult = await session.readTransaction((tx) => tx.run(readQuery));
const colors: Color[] = readResult.records.map((color) => {
const alternativeNames = color.get("alternativeNames");
const hex = color.get("hex");
const name = color.get("name");
const temperature = color.get("temperature");
const tags = color.get("tags");
return {
alternativeNames,
hex,
name,
temperature,
tags,
};
});
return colors;
};
You can try using a map projection in your query, something like this:
MATCH (c:Color)-[a:ASSOCIATED_WITH]->(t:Tag) WITH COLLECT(t.name) AS tags, c RETURN c{.alternativeNames, .hex, .name, .temperature, tags: tags}
Here's the link to the documentation.