Söz konusu haberin ardından: Performance Boostpg-promise kütüphane ve önerilen yaklaşımdan:
// Concatenates an array of objects or arrays of values, according to the template,
// to use with insert queries. Can be used either as a class type or as a function.
//
// template = formatting template string
// data = array of either objects or arrays of values
function Inserts(template, data) {
if (!(this instanceof Inserts)) {
return new Inserts(template, data);
}
this._rawDBType = true;
this.formatDBType = function() {
return data.map(d=>'(' + pgp.as.format(template, d) + ')').join(',');
};
}
tam olarak olduğu gibi, bunu kullanımına bir örnek:
var users = [['John', 23], ['Mike', 30], ['David', 18]];
db.none('INSERT INTO Users(name, age) VALUES $1', Inserts('$1, $2', users))
.then(data=> {
// OK, all records have been inserted
})
.catch(error=> {
// Error, no records inserted
});
Ve bir ile çalışacaktır nesneler dizisi de:
var users = [{name: 'John', age: 23}, {name: 'Mike', age: 30}, {name: 'David', age: 18}];
db.none('INSERT INTO Users(name, age) VALUES $1', Inserts('${name}, ${age}', users))
.then(data=> {
// OK, all records have been inserted
})
.catch(error=> {
// Error, no records inserted
});
UPDATE
Tek bir INSERT
sorgulamasıyla yüksek performanslı bir yaklaşım için bkz. Multi-row insert with pg-promise.
sadece iki kez INSERT yürütmek için bir sebep var mı? Pg belgelerine ilişkin –
, bu yaklaşım, performans düşüşü nedeniyle çok istenmeyen bir durumdur. – stkvtflw
1 yerine 2 insertin yürütülmesi, uygulamanızın performansını tehlikeye sokarsa, o zaman “node-postgres”, sizin için uygun değildir. Ama inanıyorum ki, yanlış yere bakıyorsunuz, yapmamanız gereken yerde iyileştirmeye çalışıyorsunuz. Bu kütüphane kolayca 1 saniyenin altında 10,000 kayıt ekleyebilirsiniz. Durumunuzda –