Within my database, I have a table that includes the following data structure:
https://i.sstatic.net/ckJR9.png
The table contains a column named parentIdeaId
, which is used to reference the ID in the ideas
table itself.
Below is the unstructured data currently stored in the database, which needs to be queried under specific conditions:
[{
"id": "1",
"title": "Title 1",
"parentIdeaId": null
},{
"id": "2",
"title": "Title 2",
"parentIdeaId": 1
},{
"id": "3",
"title": "Title 3",
"parentIdeaId": 2
},{
"id": "4",
"title": "Title 4",
"parentIdeaId": null
}]
To retrieve and filter this data as needed, I have created two functions. The first function retrieves data based on the provided ID:
function find({ id }) {
return prisma.idea.findUnique({
where: {
id
}
});
}
The second function finds children of a parent idea based on the parentIdeaId:
function responseIdeas({ parentIdeaId }) {
return prisma.idea.findMany({
where: {
parentIdeaId
}
});
}
The desired outcome is for only linked data (based on their parentIdeaId
) to be retrieved. For example, querying the ID "1"
should yield the following result:
[{
"id": "1",
"title": "Title 1",
"parentIdeaId": null
},{
"id": "2",
"title": "Title 2",
"parentIdeaId": 1
}{
"id": "3",
"title": "Title 3",
"parentIdeaId": 2
}]
However, when running the code to achieve this, the output differs from expectations. It is structured in a way that was not intended. Here's the current implementation:
// JavaScript code snippet goes here
In the resulting JSON data, the structure is not as expected. This discrepancy leaves me wondering where exactly I went wrong in my implementation. Any insights would be greatly appreciated. Thank you.