嵌套记录插入 knex.js
Nested records insert with knex.js
我们有如下数据库模式:
students
table
|编号 |姓名 |地址 |
一名学生有多种教育背景。
education_histories
table
|编号 |学位 |大学 | student_id | #student_id
是外键指向学生table
我们从客户端得到 JSON 结构如下:
{
"name" :"Ram Neupane",
"address": "Kathmandu, Bagmati",
"education_histories": [
{
"degree": "I.Sc.",
"university": "Tribhuvan University"
}, {
"degree": "BE",
"university": "Tribhuvan University"
}
]
}
我不熟悉 Node.js and Knex.js. I want to insert student
and nested education history
on database using bluebird 与 Knex 的承诺。我该如何解决这个问题。
我已经尝试过以下承诺:
function create (jsonParam) {
return new Promise(function (resolve, reject) {
knex('students')
.insert({name: jsonParam.name, address: jsonParam.address})
.returning('id')
.then(function (response) {
# Here, I want to insert record to education_histories table
})
.catch(function (err) {
reject(err);
});
})
}
Sorry to answer my own question But I want to explain only that how do i solve.
我们可以通过多种方式进行操作
参考Knex insert docs,我们可以直接将多条记录插入到table,只需将json对象放入数组即可。所以,我们有 jsonParam['education_histories']
这是一个数组,包含 education_history
json 对象。
var student_id = response[0];
var educationHistoryParams = jsonParam['education_histories'];
educationHistoryParams.forEach(function(educationHistory){
educationHistory.student_id = student_id;
});
knex('education_histories').insert(educationHistoryParams)
.then(resolve());
另一种方法是使用 Bluebird Promise.join API or Bluebird Promise.map API.
var student_id = response[0];
var educationHistoryParams = jsonParam['education_histories'];
return Promise.join(
educationHistoryParams.forEach(function (educationHistory) {
educationHistory.student_id = student_id;
new Promise(function (resolve, reject) {
knex('education_histories').insert(educationHistory)
.then(resolve())
.catch(function (err) {
reject(err);
})
});
});
);
我们有如下数据库模式:
students
table
|编号 |姓名 |地址 |
一名学生有多种教育背景。
education_histories
table
|编号 |学位 |大学 | student_id | #student_id
是外键指向学生table
我们从客户端得到 JSON 结构如下:
{
"name" :"Ram Neupane",
"address": "Kathmandu, Bagmati",
"education_histories": [
{
"degree": "I.Sc.",
"university": "Tribhuvan University"
}, {
"degree": "BE",
"university": "Tribhuvan University"
}
]
}
我不熟悉 Node.js and Knex.js. I want to insert student
and nested education history
on database using bluebird 与 Knex 的承诺。我该如何解决这个问题。
我已经尝试过以下承诺:
function create (jsonParam) {
return new Promise(function (resolve, reject) {
knex('students')
.insert({name: jsonParam.name, address: jsonParam.address})
.returning('id')
.then(function (response) {
# Here, I want to insert record to education_histories table
})
.catch(function (err) {
reject(err);
});
})
}
Sorry to answer my own question But I want to explain only that how do i solve.
我们可以通过多种方式进行操作
参考Knex insert docs,我们可以直接将多条记录插入到table,只需将json对象放入数组即可。所以,我们有
jsonParam['education_histories']
这是一个数组,包含education_history
json 对象。var student_id = response[0]; var educationHistoryParams = jsonParam['education_histories']; educationHistoryParams.forEach(function(educationHistory){ educationHistory.student_id = student_id; }); knex('education_histories').insert(educationHistoryParams) .then(resolve());
另一种方法是使用 Bluebird Promise.join API or Bluebird Promise.map API.
var student_id = response[0]; var educationHistoryParams = jsonParam['education_histories']; return Promise.join( educationHistoryParams.forEach(function (educationHistory) { educationHistory.student_id = student_id; new Promise(function (resolve, reject) { knex('education_histories').insert(educationHistory) .then(resolve()) .catch(function (err) { reject(err); }) }); }); );