简体   繁体   English

node.js循环数据库插入

[英]node.js looping database inserts

I have an application, with an address object, and a list of people associated with the application. 我有一个带有地址对象的应用程序,以及与该应用程序关联的人员列表。

{
  field1: value,
  field2: value,
  address: {
    street: value,
    apt: value,
    city: value
  },
  owners: [
    {name: value, etc.},
    {name: value, etc.}
  ]
}

I am passing the owners off to a function that loops over the owners and passes the information for each query off to an async function: 我将所有者传递给一个遍历所有者的函数,并将每个查询的信息传递给异步函数:

async function insertAllOwners(appId, owners) {
  for (var i = 0, len = owners.length; i < len; i++) {
    console.log("insert owner", i);
    await insertOwner(appId, owners[i]);
  }
}

The function being called here turns the query into a promise: 在此调用的函数将查询变成一个承诺:

function insertOwner(appId, owner)  {
  let sqlOwner = 'insert into ...';
  return new Promise( ( resolve, reject ) => {
    mySqlClient.query(sqlOwner, [appId, owner.memberType ...], (err, rows) => {
      if ( err )
        return reject( err );
      console.log("rows:", rows);
      resolve( rows );
    } );
  } );
}

The address insert is nested inside the application insert, then the code that calls the owner insert loop is nested inside the address insert. 地址插入嵌套在应用程序插入中,然后将调用所有者插入循环的代码嵌套在地址插入中。 The looping owner insert function is called here: 循环所有者插入函数在这里被调用:

      if(app.owners) {
        /* insert owners, then commit.  else, just commit.*/
        insertAllOwners(appId, app.owners).then( result => {
          mySqlClient.commit(function(err, result) {
            if (err) { 
              console.error("ERROR DURING COMMIT:", err);
              mySqlClient.rollback(function() {
                throw err;
              });
            }
            else {
              console.log("commit:",result);
            }
          });                  
        });
      }

The output looks perfect, but it doesn't appear to actually commit anything (the new rows are not showing up). 输出看起来很完美,但实际上并没有提交任何内容(新行未显示)。 Any suggestions? 有什么建议么? Output of logging is below: 日志输出如下:

OkPacket {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 0,
  serverStatus: 1,
  warningCount: 1,
  message: '',
  protocol41: true,
  changedRows: 0
}

address result:
OkPacket {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 35,
  serverStatus: 1,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0
}

insert owner 0
rows:
OkPacket {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 70,
  serverStatus: 1,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0
}

...

insert owner 4
rows:
OkPacket {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 74,
  serverStatus: 1,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0
}

commit:
OkPacket {
  fieldCount: 0,
  affectedRows: 0,
  insertId: 0,
  serverStatus: 0,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0
}

The first thing is that , this is not a good practice to execute a query. 首先是,这不是执行查询的好习惯。

what you are doing to run a for loop and each loop execute each query. 您正在执行的运行for循环和每个循环执行每个查询的操作。 Bu this way you give load to mysql. 通过这种方式,您可以将负载分配给mysql。 And also it will take longer time execute your API. 而且执行您的API将花费更长的时间。

I suggest this way. 我建议这样。

In for loop : 在for循环中:

for (var i = 0, len = owners.length; i < len; i++) {
   inserData.push(owners[i]); // Add all data in to array ... 
}

After that execute query once with all data. 之后,对所有数据执行一次查询

connection.query("INSERT INTO TABLE (columns) VLAUES ? " , insertData)... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM