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

Commit 88df17d9 by Nuno Sousa

Add multiple row support to hstore parsing

* Improved performance by checking if query contains an hstore field.
1 parent 6e959839
...@@ -194,6 +194,7 @@ module.exports = (function() { ...@@ -194,6 +194,7 @@ module.exports = (function() {
this._booleanAttributes = [] this._booleanAttributes = []
this._dateAttributes = [] this._dateAttributes = []
this._hstoreAttributes = []
this._defaultValues = {} this._defaultValues = {}
this.DAO.prototype.validators = {} this.DAO.prototype.validators = {}
...@@ -204,6 +205,9 @@ module.exports = (function() { ...@@ -204,6 +205,9 @@ module.exports = (function() {
if (((definition === DataTypes.DATE) || (definition.type === DataTypes.DATE) || (definition.originalType === DataTypes.DATE))) { if (((definition === DataTypes.DATE) || (definition.type === DataTypes.DATE) || (definition.originalType === DataTypes.DATE))) {
self._dateAttributes.push(name); self._dateAttributes.push(name);
} }
if (((definition === DataTypes.HSTORE.type) || (definition.type === DataTypes.HSTORE.type) || (definition.originalType === DataTypes.HSTORE.type))) {
self._hstoreAttributes.push(name);
}
if (definition.hasOwnProperty('defaultValue')) { if (definition.hasOwnProperty('defaultValue')) {
self._defaultValues[name] = Utils._.partial( self._defaultValues[name] = Utils._.partial(
Utils.toDefaultValue, definition.defaultValue) Utils.toDefaultValue, definition.defaultValue)
...@@ -224,6 +228,11 @@ module.exports = (function() { ...@@ -224,6 +228,11 @@ module.exports = (function() {
return self._dateAttributes.indexOf(key) !== -1 return self._dateAttributes.indexOf(key) !== -1
}) })
this._hasHstoreAttributes = !!this._hstoreAttributes.length
this._isHstoreAttribute = Utils._.memoize(function (key) {
return self._hstoreAttributes.indexOf(key) !== -1
})
this.DAO.prototype.Model = this this.DAO.prototype.Model = this
this._hasDefaultValues = !Utils._.isEmpty(this._defaultValues) this._hasDefaultValues = !Utils._.isEmpty(this._defaultValues)
......
...@@ -128,12 +128,16 @@ module.exports = (function() { ...@@ -128,12 +128,16 @@ module.exports = (function() {
}) })
} }
// Parse hstore fields. // Parse hstore fields if the model has any hstore fields.
// This cannot be done in the 'pg' lib because hstore is a UDT. // This cannot be done in the 'pg' lib because hstore is a UDT.
for (var key in rows[0]) { if (!!this.callee && !!this.callee._hasHstoreAttributes) {
if (!!this.callee && !!this.callee.rawAttributes && !!this.callee.rawAttributes[key] && !!this.callee.rawAttributes[key].type && this.callee.rawAttributes[key].type.toString() === DataTypes.HSTORE.toString()) { rows.forEach(function(row) {
rows[0][key] = hstore.parse(rows[0][key]) Utils._.keys(row).forEach(function(key) {
if (self.callee._isHstoreAttribute(key)) {
row[key] = hstore.parse(row[key])
} }
})
})
} }
this.emit('success', this.send('handleSelectQuery', rows)) this.emit('success', this.send('handleSelectQuery', rows))
......
...@@ -289,6 +289,27 @@ if (dialect.match(/^postgres/)) { ...@@ -289,6 +289,27 @@ if (dialect.match(/^postgres/)) {
}) })
.error(console.log) .error(console.log)
}) })
it("should read hstore correctly from multiple rows", function(done) {
var self = this
self.User
.create({ username: 'user1', email: ['foo@bar.com'], settings: { created: { test: '"value"' }}})
.then(function() {
return self.User.create({ username: 'user2', email: ['foo2@bar.com'], settings: { updated: { another: '"example"' }}})
})
.then(function() {
// Check that the hstore fields are the same when retrieving the user
return self.User.findAll({ order: 'username' })
})
.then(function(users) {
expect(users[0].settings).to.deep.equal({ created: { test: '"value"' }})
expect(users[1].settings).to.deep.equal({ updated: { another: '"example"' }})
done()
})
.error(console.log)
})
}) })
describe('[POSTGRES] Unquoted identifiers', function() { describe('[POSTGRES] Unquoted identifiers', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!