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

Commit ceadd919 by Scott Tadman

Using pg-hstore library to properly encode and decode Postgres HSTORE data

1 parent 2a25f661
Showing with 6 additions and 12 deletions
......@@ -2,6 +2,7 @@ var Utils = require("./utils")
, Mixin = require("./associations/mixin")
, DaoValidator = require("./dao-validator")
, DataTypes = require("./data-types")
, PgHstore = require('pg-hstore')
module.exports = (function() {
var DAO = function(values, options, isNewRecord) {
......@@ -130,15 +131,7 @@ module.exports = (function() {
if (isHstore) {
if (typeof values[attrName] === "object") {
var text = []
Utils._.each(values[attrName], function(value, key){
if (typeof value !== "string" && typeof value !== "number") {
throw new Error('Value for HSTORE must be a string or number.')
}
text.push(this.QueryInterface.quoteIdentifier(key) + '=>' + (typeof value === "string" ? this.QueryInterface.quoteIdentifier(value) : value))
}.bind(this))
values[attrName] = text.join(',')
values[attrName] = PgHstore.stringify(values[attrName])
}
}
}
......@@ -363,7 +356,7 @@ module.exports = (function() {
for (key in values) {
if (values.hasOwnProperty(key)) {
if (typeof values[key] === "string" && !!this.__factory && !!this.__factory.rawAttributes[key] && !!this.__factory.rawAttributes[key].type && !!this.__factory.rawAttributes[key].type.type && this.__factory.rawAttributes[key].type.type === DataTypes.HSTORE.type) {
values[key] = this.QueryInterface.QueryGenerator.toHstore(values[key])
values[key] = PgHstore.stringify(values[key])
}
this.addAttribute(key, values[key])
......
var Utils = require("../../utils")
, AbstractQuery = require('../abstract/query')
, DataTypes = require('../../data-types')
, PgHstore = require('pg-hstore')
module.exports = (function() {
var Query = function(client, sequelize, callee, options) {
......@@ -132,7 +133,7 @@ module.exports = (function() {
if (rows[0].hasOwnProperty(key)) {
var record = rows[0][key]
if (!!this.callee.daoFactory && !!this.callee.daoFactory.rawAttributes && !!this.callee.daoFactory.rawAttributes[key] && !!this.callee.daoFactory.rawAttributes[key].type && !!this.callee.daoFactory.rawAttributes[key].type.type && this.callee.daoFactory.rawAttributes[key].type.type === DataTypes.HSTORE.type) {
record = this.callee.daoFactory.daoFactoryManager.sequelize.queryInterface.QueryGenerator.toHstore(record)
record = PgHstore.parse(record)
}
this.callee[key] = record
}
......@@ -146,7 +147,7 @@ module.exports = (function() {
if (rows[0].hasOwnProperty(key)) {
var record = rows[0][key]
if (!!this.callee.daoFactory && !!this.callee.daoFactory.rawAttributes && !!this.callee.daoFactory.rawAttributes[key] && !!this.callee.daoFactory.rawAttributes[key].type && !!this.callee.daoFactory.rawAttributes[key].type.type && this.callee.daoFactory.rawAttributes[key].type.type === DataTypes.HSTORE.type) {
record = this.callee.daoFactory.daoFactoryManager.sequelize.queryInterface.QueryGenerator.toHstore(record)
record = PgHstore.parse(record)
}
this.callee[key] = record
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!