不要怂,就是干,撸起袖子干!

Commit c92353a6 by Mick Hansen

bulkCreate no longer requires a .sync()

1 parent fdba0dd4
...@@ -919,7 +919,7 @@ module.exports = (function() { ...@@ -919,7 +919,7 @@ module.exports = (function() {
records.push(values) records.push(values)
}) })
self.QueryInterface.bulkInsert(self.tableName, records, options) self.QueryInterface.bulkInsert(self.tableName, records, options, self)
.on('sql', function(sql) { .on('sql', function(sql) {
emitter.emit('sql', sql) emitter.emit('sql', sql)
}) })
......
...@@ -267,25 +267,25 @@ module.exports = (function() { ...@@ -267,25 +267,25 @@ module.exports = (function() {
}) })
}, },
bulkInsertQuery: function(tableName, attrValueHashes, options) { bulkInsertQuery: function(tableName, attrValueHashes, options, modelAttributes) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;" var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;"
, tuples = [] , tuples = []
, serials = [] , serials = []
, allAttributes = [] , allAttributes = []
Utils._.forEach(attrValueHashes, function(attrValueHash, i) { Utils._.forEach(attrValueHashes, function(attrValueHash) {
Utils._.forOwn(attrValueHash, function(value, key, hash) { Utils._.forOwn(attrValueHash, function(value, key) {
if (allAttributes.indexOf(key) === -1) allAttributes.push(key) if (allAttributes.indexOf(key) === -1) {
allAttributes.push(key)
}
if (tables[tableName] && tables[tableName][key]) { if (modelAttributes && modelAttributes[key] && modelAttributes[key].autoIncrement === true) {
if (['bigserial', 'serial'].indexOf(tables[tableName][key]) !== -1 && serials.indexOf(key) === -1) { serials.push(key)
serials.push(key)
}
} }
}) })
}) })
Utils._.forEach(attrValueHashes, function(attrValueHash, i) { Utils._.forEach(attrValueHashes, function(attrValueHash) {
tuples.push("(" + tuples.push("(" +
allAttributes.map(function (key) { allAttributes.map(function (key) {
if (serials.indexOf(key) !== -1) { if (serials.indexOf(key) !== -1) {
......
...@@ -510,8 +510,8 @@ module.exports = (function() { ...@@ -510,8 +510,8 @@ module.exports = (function() {
}) })
} }
QueryInterface.prototype.bulkInsert = function(tableName, records, options) { QueryInterface.prototype.bulkInsert = function(tableName, records, options, Model) {
var sql = this.QueryGenerator.bulkInsertQuery(tableName, records, options) var sql = this.QueryGenerator.bulkInsertQuery(tableName, records, options, Model.rawAttributes)
return queryAndEmit.call(this, [sql, null, options], 'bulkInsert') return queryAndEmit.call(this, [sql, null, options], 'bulkInsert')
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!