, SQLite. node-sqlite3.
IMHO , . SQLite, "", PostgreSQL, , , . , insertData. :
function insertData(callback, cmds) {
db.serialize(function() {
db.run("BEGIN;");
cmds.forEach(function(item) {
db.run("INSERT INTO data (t) VALUES (?)", item, function(e) {
if (e) {
console.log('error');
} else {
}
});
});
db.run("ROLLBACK;", function(e) {
return callback();
});
});
}
:
init(function() {
function doTransactionInsert(e) {
if (e) return console.log(e);
setTimeout(insertData, 10, doTransactionInsert, ['all', 'your', 'base', 'are', 'belong', 'to', 'us']);
}
doTransactionInsert();
var i=0;
function doIntegerInsert() {
insertData(function(e) {
if (e) return console.log(e);
setTimeout(doIntegerInsert, 9);
}, [i++]);
}
...
:
- cmds, , (cmds - , SQL)
- db.exec db.run( )
- db.serialize
- ommited callback BEGIN
slide underscore
.