如何保存test_id
并用它在一个SQL事务中将tests_questions
表中的行插入(下面的代码很快)
const [{ test_id }] = await db.any(`
INSERT INTO tests (name, descr, show)
VALUES ('${name}', '${descr}', ${show})
RETURNING test_id;
`)
if (questions && questions.length) {
questions.map(async (question) => {
const [{ tests_question_id }] = await db.any(`
INSERT INTO tests_questions (test_id, name, code)
VALUES (${test_id}, '${question.name}', '${question.code}')
RETURNING tests_question_id;
`)
if (question.answers && question.answers.length) {
question.answers.map(async (answer) => {
await db.any(`
INSERT INTO tests_questions_answers (tests_question_id, name, is_true)
VALUES (${tests_question_id}, '${answer.name}', ${answer.isTrue});
`)
})
}
})
}