我想在一个事务中将Object保存到数据库中。 Object有2个子对象和一个数组列表对象,数组列表中的每个对象都有自己的数组列表:
{
"person": {
"id": null,
"personal_no": "555555555555555555"
},
"reservation": {
"id": null,
"personal_no": null,
"status_id": null,
"reservationDetail": [
{
"id": null,
"status_id": null,
"room_id": 44,
"start_date": "2017-08-09T18:04:26.938Z",
"end_date": "2017-08-09T18:04:26.938Z",
"category_id": 64,
"reservationPerson": [
{
"reservation_id": "123",
"person_id": "5555555555555555551"
},
{
"reservation_id": "123",
"person_id": "5555555555555555552"
}
],
"reservationService": [
{
"reservation_id": "123",
"person_id": "5555555555555555553"
},
{
"reservation_id": "123",
"person_id": "5555555555555555554"
}
]
},
{
"id": null,
"status_id": null,
"room_id": 44,
"start_date": "2017-08-09T18:04:26.938Z",
"end_date": "2017-08-09T18:04:26.938Z",
"category_id": 64,
"reservationPerson": [
{
"reservation_id": "123",
"person_id": "5555555555555555556"
},
{
"reservation_id": "123",
"person_id": "5555555555555555557"
}
],
"reservationService": [
{
"reservation_id": "123",
"person_id": "5555555555555555558"
},
{
"reservation_id": "123",
"person_id": "5555555555555555559"
}
]
}
]
}
}
现在这是我的代码,我被卡住了,我不知道如何继续。
pool.registerReservation = function (reservation) {
return new Promise(function (resolve, reject) {
pool.getConnection(function (err, connection) {
var person = reservation.person;
connection.beginTransaction(function (err) {
if (err) {
reject(err);
}
connection.query('insert into person (first_name,last_name,personal_no,email,gender,address,birthdate,phone)values (?,?,?,?,?,?,?,?)',
[person.first_name, person.last_name, person.personal_no, person.email, person.gender, person.address, person.birthdate, person.phone],
function (error, results, fields) {
if (error) {
if (error.code != 'ER_DUP_ENTRY') {
return connection.rollback(function () {
console.log("person", error.code);
reject(error);
});
}
}
connection.query('insert into reservation (create_date,person_no,status_id)values(current_timestamp,?,1)', [person.personal_no], function (error, results, fields) {
if (error) {
return connection.rollback(function () {
console.log("reservation", error);
reject(error);
});
}
var reservID = results.insertId;
var reservDetails = reservation.reservation.reservationDetail;
reservDetails.map(data => {
connection.query('insert into reservation_detail (reservation_id,create_date,room_id,status_id,start_date,end_date,payment_type,adult,child,additional_bed,payment_status)values(?,current_timestamp,?,?,?,?,?,?,?,?,?)',
[reservID, data.room_id, data.status_id, data.start_date, data.end_date,
data.payment_type, data.adult, data.child, data.additional_bed, data.payment_status],
function (error, results, fields) {
if (error) {
return connection.rollback(function () {
console.log("reservation_detail", error);
reject(error);
});
}
data.id = results.insertId;
var id = results.insertId;
data.reservationPerson.map(person => {
connection.query('insert into reservation_person(reservation_id,person_id)values(?,?)',
[id, person.person_id],
function (error, results, fields) {
if (error) {
return connection.rollback(function () {
console.log("person", error.code);
reject(error);
});
}
});
});
data.reservationService.map(service => {
connection.query('insert into reservation_service (reservation_id, service_id,frequency, additional_comment)values (?,?,?,?)',
[id, service.service_id, service.frequency, service.additional_comment],
function (error, results, fields) {
if (error) {
return connection.rollback(function () {
console.log("service", error);
reject(error);
});
}
});
});
});
});
connection.commit(function (err) {
console.log("commit");
if (err) {
return connection.rollback(function () {
console.log(err);
reject(err);
});
}
resolve("OK");
});
});
});
});
});
});
}