я пытаюсь запросить некоторые данные с помощью knex. js используя это:
let query = knex
.select(
'project.custom_answer.id',
'project.custom_answer.content',
'project.custom_answer.is_private',
knex.raw(
'CASE ' +
'WHEN proposal.custom_question.id IS NULL THEN NULL ' +
'ELSE json_build_object(' +
"'id', proposal.custom_question.id," +
"'content', proposal.custom_question.content," +
"'is_mandatory', proposal.custom_question.is_mandatory," +
"'is_private', proposal.custom_question.is_private," +
"'has_media', proposal.custom_question.has_media," +
"'category', proposal.custom_question.category," +
"'type', proposal.custom_question.type," +
"'proposal'," +
'CASE ' +
'WHEN proposal.proposal.id IS NULL THEN NULL ' +
'ELSE json_build_object(' +
"'id', proposal.proposal.id," +
"'is_private', proposal.proposal.is_private," +
"'slug', proposal.proposal.slug," +
"'name', proposal.proposal.name" +
')' +
'END' +
') ' +
'END as custom_question'
)
)
.select([
knex.raw(
'CASE ' +
'WHEN proposal.custom_answer.id IS NULL THEN NULL ' +
'ELSE ARRAY_AGG(json_build_object(' +
"'id', proposal.custom_answer.id," +
"'content', proposal.custom_answer.content" +
'))' +
'END as custom_answer'
),
])
.modify(querybuildUtils.mediaBuilder, 'project.custom_answer.media_id')
.join(
'proposal.custom_question',
'proposal.custom_question.id',
'project.custom_answer.custom_question_id'
)
.leftJoin(
'proposal.custom_answer',
'proposal.custom_answer.custom_question_id',
'proposal.custom_question.id'
)
.join('proposal.proposal', 'proposal.proposal.id', 'proposal.custom_question.proposal_id')
// .modify(joinUserAsObject,"project.answer.member_id")
.from('project.custom_answer')
.limit(100)
.whereIn('project.custom_answer.id', arrayId)
// .where("project.custom_answer.is_private", "false")
.orderBy('project.custom_answer.created_at', 'desc')
.groupBy(
'project.custom_answer.id',
'proposal.custom_question.id',
'proposal.custom_answer.id',
'proposal.proposal.id'
);
Я хочу получить свой custom_question с первым knex.raw и создать массив для моего custom_answer со вторым knex .raw. Моя проблема здесь в том, что я создаю массив для каждого custom_question И custom_answer, поэтому теперь мои данные выглядят так:
custom_answers: [
{
content: "",
custom_answer: [{id: 59, content: "A1"}]
custom_question: {id: 236, content: "Q1" ....}
...
},
{
content: "",
custom_answer: [{id: 60, content: "A2"}]
custom_question: {id: 236, content: "Q1" ....}
...
},
]
И это то, что я хочу:
custom_answers: [
{
content: "",
custom_answer: [
{id: 59, content: "A1"},
{id: 60, content: "A2"},
]
custom_question: {id: 236, content: "Q1" ....}
...
}
]