इस लेख के बाद: Performance Boostpg-promise पुस्तकालय, और उसके द्वारा सुझाए गए दृष्टिकोण से:
// 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(',');
};
}
वास्तव में अपने मामले में के रूप में उपयोग, का एक उदाहरण:
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
});
और यह एक साथ काम करेंगे वस्तुओं की सरणी भी:
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
});
अद्यतन
एक INSERT
क्वेरी के माध्यम से उच्च प्रदर्शन दृष्टिकोण के लिए Multi-row insert with pg-promise देखें।
एक कारण तो आप बस दो बार सम्मिलित करें पर अमल नहीं किया जा सकता है? –
पीजी दस्तावेज़ीकरण के संबंध में यह दृष्टिकोण प्रदर्शन कम होने के कारण बहुत अवांछनीय है – stkvtflw
यदि 1 के बजाय 2 आवेषण निष्पादित करना आपके आवेदन के प्रदर्शन को खतरे में डाल देगा, तो 'नोड-पोस्टग्रेस', आपके लिए बिल्कुल नहीं है। लेकिन मेरा मानना है कि आप इसे गलत तरीके से देख रहे हैं, जहां आप नहीं करना चाहिए अनुकूलित करने की कोशिश कर रहे हैं। यह पुस्तकालय आसानी से 1 सेकंड से कम में 10,000 रिकॉर्ड डाल सकता है। –