将多个查询 returns 格式化为 JSON 对象
Formatting multiple query returns as a JSON object
我正在使用 JavaScript 和 neo4j-driver
将节点和一些关系保存到我的图表中。一切都按预期工作,但我想读取我的写入结果并在之后以特定格式输出,我遇到了麻烦。现在我只设法 return 我查询中的所有变量,但我希望输出采用这种特定格式:
{
"name": p.name
"colors: [
{
hex: c.hex
}
{
hex: c.hex
}
]
}
到目前为止,这是我的代码,如有任何帮助,我们将不胜感激:
export const savePallete = async (palette: Palette) => {
if (!!palette.name === false) {
throw Error("Palette name required!");
}
const writeQuery = `MERGE (p:Palette {name: $name})
ON CREATE SET p.name = $name
FOREACH (color IN $colors |
MERGE (c:Color {hex: color.hex})
ON CREATE SET c.hex = color.hex
CREATE (p)-[i:INCLUDES]->(c))
`;
await session.writeTransaction((tx) =>
tx.run(writeQuery, { name: palette.name, colors: palette.colors })
);
const readQuery = `
MATCH (p:Palette)
WHERE p.name = $name
MATCH (p)-[:INCLUDES]->(c:Color)
RETURN *
`;
const readResult = await session.readTransaction((tx) =>
tx.run(readQuery, { name: palette.name })
);
return readResult;
};
例如,如果我用这个对象作为参数调用 savePallete
,我想得到相同的结果:
{
"name": "First test palette",
"colors": [
{
"hex": "#123123"
},
{
"hex": "#ffffff"
},
{
"hex": "#000000"
}
]
}
一种方法是将 readQuery
替换为(最后两行不同):
MATCH (p:Palette)
WHERE p.name = $name
MATCH (p)-[:INCLUDES]->(c:Color)
WITH COLLECT({hex: c.hex}) AS colors, p
RETURN {name: p.name, colors: colors}
这会将颜色的十六进制收集为 JSON 并将 return 为 JSON 格式。
使用示例数据时:
MERGE (a:Palette {name: "First test palette"})
MERGE (b:Color {hex: "#123123"})
MERGE (c:Color {hex: "#ffffff"})
MERGE (d:Color {hex: "#000000"})
MERGE (a)-[:INCLUDES]-(b)
MERGE (a)-[:INCLUDES]-(c)
MERGE (a)-[:INCLUDES]-(d)
它return是您的预期结果
我正在使用 JavaScript 和 neo4j-driver
将节点和一些关系保存到我的图表中。一切都按预期工作,但我想读取我的写入结果并在之后以特定格式输出,我遇到了麻烦。现在我只设法 return 我查询中的所有变量,但我希望输出采用这种特定格式:
{
"name": p.name
"colors: [
{
hex: c.hex
}
{
hex: c.hex
}
]
}
到目前为止,这是我的代码,如有任何帮助,我们将不胜感激:
export const savePallete = async (palette: Palette) => {
if (!!palette.name === false) {
throw Error("Palette name required!");
}
const writeQuery = `MERGE (p:Palette {name: $name})
ON CREATE SET p.name = $name
FOREACH (color IN $colors |
MERGE (c:Color {hex: color.hex})
ON CREATE SET c.hex = color.hex
CREATE (p)-[i:INCLUDES]->(c))
`;
await session.writeTransaction((tx) =>
tx.run(writeQuery, { name: palette.name, colors: palette.colors })
);
const readQuery = `
MATCH (p:Palette)
WHERE p.name = $name
MATCH (p)-[:INCLUDES]->(c:Color)
RETURN *
`;
const readResult = await session.readTransaction((tx) =>
tx.run(readQuery, { name: palette.name })
);
return readResult;
};
例如,如果我用这个对象作为参数调用 savePallete
,我想得到相同的结果:
{
"name": "First test palette",
"colors": [
{
"hex": "#123123"
},
{
"hex": "#ffffff"
},
{
"hex": "#000000"
}
]
}
一种方法是将 readQuery
替换为(最后两行不同):
MATCH (p:Palette)
WHERE p.name = $name
MATCH (p)-[:INCLUDES]->(c:Color)
WITH COLLECT({hex: c.hex}) AS colors, p
RETURN {name: p.name, colors: colors}
这会将颜色的十六进制收集为 JSON 并将 return 为 JSON 格式。
使用示例数据时:
MERGE (a:Palette {name: "First test palette"})
MERGE (b:Color {hex: "#123123"})
MERGE (c:Color {hex: "#ffffff"})
MERGE (d:Color {hex: "#000000"})
MERGE (a)-[:INCLUDES]-(b)
MERGE (a)-[:INCLUDES]-(c)
MERGE (a)-[:INCLUDES]-(d)
它return是您的预期结果