2017-01-16 74 views
0

我正在使用sequelezer将值插入到本地数据库中。这种安装脚本需要在服务器上首次运行。用于在SQLite中插入数据值的同步循环

由于的NodeJS“异步”函数调用,我的sqlite的分贝越来越锁,我收到以下错误 Unhandled rejection SequelizeTimeoutError: SQLITE_BUSY: database is locked

这里我的代码,

var country_json = { 
     "AF": "Afghanistan", 
     "AX": "\u00c5land Islands", 
     "AL": "Albania", 
     //........ 
     "ZW": "Zimbabwe" 
    }; 

for (key in country_json) { 

     console.log("ABUZAR"); 
     console.log(key.toString('utf8') + " " + country_json[key].toString('utf8')); 

     db.country_name.findOrCreate({ 

       where: { 
        country_iso: key.toString('utf8') 
       }, 
       defaults: { 
        country_name: country_json[key].toString('utf8') 
       } 
      }) 
      .spread(function(country, created) { 
       console.log(country.get({ 
       plain: true 
       })); 
       console.log(created); 
      }); 

    } 

我试过几个同步NPM模块,但他们每个人似乎都在锻炼。只是想知道如何在节点js中处理这些场景。

回答

1

可以用周期,逻辑,递归等使用SynJS混合异步功能 - 它将执行所有的步骤中的一个接一个在同步方式。下面是脚本来说明:

global.SynJS = global.SynJS || require('synjs'); 
var Sequelize = require('sequelize'); 

var db = new Sequelize('tracker', 'tracker', 'tracker123', { 
     host: 'localhost', 
     dialect: 'mysql', 
     pool: { 
     max: 5, 
     min: 0, 
     idle: 10000 
     }, 
    }); 

function insertWrapper(db,context,key,value) { // <- wrapper function that returns initially incomplete result 
    var res = {done: false}; 
    db.query("select CONCAT(?,'--->',?)", { replacements: [key,value], type: db.QueryTypes.SELECT }) 
    .spread(function(row) { 
     console.log('done: row=',row); 
     res.done = true; 
     SynJS.resume(context); // once result is ready, it notifies context of caller function to continue 
    }); 
    return res; 
} 

function myFunc(modules,db, country_json) { // <- function that is run via SynJS 
    for (var key in country_json) { 
     var res = modules.insertWrapper(db,_synjsContext,key,country_json[key]); 
     SynJS.wait(res.done); // <-- wait for the callback to finish 
    } 
}; 

var modules = { // <-- convenience object to pass whatever myFunc may need, as it cannot access closures 
     insertWrapper: insertWrapper, 
}; 

var country_json = { 
      "AF": "Afghanistan", 
      "AX": "\u00c5land Islands", 
      "AL": "Albania", 
      //........ 
      "ZW": "Zimbabwe" 
    }; 

// run myFunc via SynJS 
SynJS.run(myFunc,null,modules,db,country_json,function() { 
    db.close(); 
    console.log('done'); 
}); 

这将产生以下的输出:

Executing (default): select CONCAT('AF','--->','Afghanistan') 
done: row= { 'CONCAT(\'AF\',\'--->\',\'Afghanistan\')': 'AF--->Afghanistan' } 
Executing (default): select CONCAT('AX','--->','Ã…land Islands') 
done: row= { 'CONCAT(\'AX\',\'--->\',\'Ã…land Islands\')': 'AX--->Ã…land Islands' } 
Executing (default): select CONCAT('AL','--->','Albania') 
done: row= { 'CONCAT(\'AL\',\'--->\',\'Albania\')': 'AL--->Albania' } 
Executing (default): select CONCAT('ZW','--->','Zimbabwe') 
done: row= { 'CONCAT(\'ZW\',\'--->\',\'Zimbabwe\')': 'ZW--->Zimbabwe' } 
done