Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit ceadd919
authored
Jun 12, 2013
by
Scott Tadman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Using pg-hstore library to properly encode and decode Postgres HSTORE data
1 parent
2a25f661
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
12 deletions
lib/dao.js
lib/dialects/postgres/query.js
lib/dao.js
View file @
ceadd91
...
@@ -2,6 +2,7 @@ var Utils = require("./utils")
...
@@ -2,6 +2,7 @@ var Utils = require("./utils")
,
Mixin
=
require
(
"./associations/mixin"
)
,
Mixin
=
require
(
"./associations/mixin"
)
,
DaoValidator
=
require
(
"./dao-validator"
)
,
DaoValidator
=
require
(
"./dao-validator"
)
,
DataTypes
=
require
(
"./data-types"
)
,
DataTypes
=
require
(
"./data-types"
)
,
PgHstore
=
require
(
'pg-hstore'
)
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
DAO
=
function
(
values
,
options
,
isNewRecord
)
{
var
DAO
=
function
(
values
,
options
,
isNewRecord
)
{
...
@@ -130,15 +131,7 @@ module.exports = (function() {
...
@@ -130,15 +131,7 @@ module.exports = (function() {
if
(
isHstore
)
{
if
(
isHstore
)
{
if
(
typeof
values
[
attrName
]
===
"object"
)
{
if
(
typeof
values
[
attrName
]
===
"object"
)
{
var
text
=
[]
values
[
attrName
]
=
PgHstore
.
stringify
(
values
[
attrName
])
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
(
','
)
}
}
}
}
}
}
...
@@ -363,7 +356,7 @@ module.exports = (function() {
...
@@ -363,7 +356,7 @@ module.exports = (function() {
for
(
key
in
values
)
{
for
(
key
in
values
)
{
if
(
values
.
hasOwnProperty
(
key
))
{
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
)
{
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
])
this
.
addAttribute
(
key
,
values
[
key
])
...
...
lib/dialects/postgres/query.js
View file @
ceadd91
var
Utils
=
require
(
"../../utils"
)
var
Utils
=
require
(
"../../utils"
)
,
AbstractQuery
=
require
(
'../abstract/query'
)
,
AbstractQuery
=
require
(
'../abstract/query'
)
,
DataTypes
=
require
(
'../../data-types'
)
,
DataTypes
=
require
(
'../../data-types'
)
,
PgHstore
=
require
(
'pg-hstore'
)
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
Query
=
function
(
client
,
sequelize
,
callee
,
options
)
{
var
Query
=
function
(
client
,
sequelize
,
callee
,
options
)
{
...
@@ -132,7 +133,7 @@ module.exports = (function() {
...
@@ -132,7 +133,7 @@ module.exports = (function() {
if
(
rows
[
0
].
hasOwnProperty
(
key
))
{
if
(
rows
[
0
].
hasOwnProperty
(
key
))
{
var
record
=
rows
[
0
][
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
)
{
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
.
toHstor
e
(
record
)
record
=
PgHstore
.
pars
e
(
record
)
}
}
this
.
callee
[
key
]
=
record
this
.
callee
[
key
]
=
record
}
}
...
@@ -146,7 +147,7 @@ module.exports = (function() {
...
@@ -146,7 +147,7 @@ module.exports = (function() {
if
(
rows
[
0
].
hasOwnProperty
(
key
))
{
if
(
rows
[
0
].
hasOwnProperty
(
key
))
{
var
record
=
rows
[
0
][
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
)
{
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
.
toHstor
e
(
record
)
record
=
PgHstore
.
pars
e
(
record
)
}
}
this
.
callee
[
key
]
=
record
this
.
callee
[
key
]
=
record
}
}
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment