什么是使用NodeJS在mysql中插入关系表的最佳方法

时间:2018-09-19 14:14:05

标签: javascript mysql node.js

首先,我必须说我完全是NodeJS技术的新手。 但是,我尝试做一些尝试来学习它。

这是问题所在: 我有3个表(PARTICIPANT,ADDRESS_PARTICIPANT和INSCRIPTION)。

ADDRESS_PARTICIPANT包含参与者ID(这是参与者的地址)。

INSCRIPTION包含参与者ID

因此,要存储题字行,首先需要保存PARTICIPANT和ADDRESS_PARTICIPANT。只有在此之后,我才能插入INSCRIPTION

我正在按照自己的学习方式来做,但是我认为有很多嵌套的ifs。

如何改进此代码?有人用Promise告诉我,我会很好..但我不知道。有人可以帮我吗?谢谢

这是代码:

this.save = function(data, retur) {
var con = db();
const SQL_INSERT_PARTICIPANT = 
  `INSERT INTO participant (nome_completo, tipo_funcionario, data_nascimento, sexo, unidade, cpf, email, telefone, telefone_emergencia) VALUES( ? )` ; 
const SQL_INSERT_ADDRESS_PARTICIPANT = 
  `INSERT INTO endereco_participante (participant_id, cep, estado, cidade, bairro, endereco, numero) values( ? )`;

const SQL_INSERT_INSCRIPTIONS = `......` 

var values = [
  data.nome_completo, data.tipo_funcionario, new Date(dateToEN(data.data_nascimento)), data.sexo, data.unidade, data.cpf_funcionario, data.email, data.telefone, data.telefone_emergencia
]

const insertParticipante = con.query(SQL_INSERT_PARTICIPANT , [values], function (err, result) {
  if (err) throw err;

  var values_end = [
    result.insertId, data.cep, data.estado, data.cidade, data.bairro, data.endereco, data.numero
  ]

  if (result.affectedRows > 0 ) {
    const insertEndPart = con.query(SQL_INSERT_ADDRESS_PARTICIPANT , [values_end], function(err, result2 ) {
      if (err) throw err;

      console.log('Number of records inserted in ADDRESS_PARTICIPANT table: ' + result2.affectedRows);
      console.log('insertId.: ' + result2.insertId)

      if (result.affectedRows > 0 ) {
        const insertInscricao = con.query(SQL_INSERT_INSCRIPTIONS, [values_ins], function(err, result3) {
          console.log(`Inscription recorded! id: `+resul3.insertId)
        })

      }
    })
  }          
})

}

1 个答案:

答案 0 :(得分:1)

您可以使用MySQL的LAST_INSERT_ID,我假设每个表都有一个带有auto_increment选项的主键列。

  

不带参数的LAST_INSERT_ID()返回BIGINT UNSIGNED(64位)   代表第一个自动生成的值的值   由于以下原因,已成功为AUTO_INCREMENT列插入   最近执行的INSERT语句。 LAST_INSERT_ID()的值   如果没有成功插入任何行,则保持不变。

https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id

然后您可以在NodeJS中使用这些INSERT

INSERT INTO participant (nome_completo, tipo_funcionario, data_nascimento, sexo, unidade, cpf, email, telefone, telefone_emergencia) VALUES( <other columns> )

下面的插入内容将使用LAST_INSERT_ID()来获取参与者。

INSERT INTO endereco_participante (participant_id, cep, estado, cidade, bairro, endereco, numero) values( LAST_INSERT_ID(), <other columns> )

有了三个表,问题变得更加复杂。
然后,您可以使用MySQL的用户变量。

INSERT INTO participant (nome_completo, tipo_funcionario, data_nascimento, sexo, unidade, cpf, email, telefone, telefone_emergencia) VALUES( <other columns> )

SET @participant_id = LAST_INSERT_ID();

INSERT INTO endereco_participante (participant_id, cep, estado, cidade, bairro, endereco, numero) values( @participant_id, <other columns> )

SET @endereco_participante_id = LAST_INSERT_ID();

然后,您可以在第三个插入查询中使用@participant_id和@endereco_participante_id。 (您在问题中未提供的接缝)。
请注意,SET查询是分开的查询,因此您还需要使用con.query('SET @participant_id = LAST_INSERT_ID();', ..)

执行它们
相关问题