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 45503e7d
authored
Feb 12, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Plain Diff
[merge]
2 parents
9ee88b92
2525b46e
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
176 additions
and
177 deletions
lib/associations/belongs-to.js
lib/associations/has-many-double-linked.js
lib/associations/has-many-single-linked.js
lib/associations/has-many.js
lib/associations/has-one.js
lib/dao-factory.js
lib/dao-validator.js
lib/dao.js
lib/dialects/abstract/query-generator.js
lib/dialects/abstract/query.js
lib/dialects/postgres/query.js
lib/query-interface.js
test/associations/belongs-to.test.js
test/associations/has-many.test.js
test/associations/has-one.test.js
test/dao-factory/find.test.js
test/sequelize.test.js
lib/associations/belongs-to.js
View file @
45503e7
...
...
@@ -85,7 +85,7 @@ module.exports = (function() {
var
self
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
associatedObject
,
options
)
{
var
primaryKeys
=
!!
associatedObject
&&
!!
associatedObject
.
daoFactory
?
Object
.
keys
(
associatedObject
.
daoFactory
.
primaryKeys
)
:
[]
var
primaryKeys
=
!!
associatedObject
&&
!!
associatedObject
.
Model
?
Object
.
keys
(
associatedObject
.
Model
.
primaryKeys
)
:
[]
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
this
[
self
.
identifier
]
=
associatedObject
?
associatedObject
[
primaryKey
]
:
null
...
...
lib/associations/has-many-double-linked.js
View file @
45503e7
...
...
@@ -23,7 +23,7 @@ module.exports = (function() {
,
targetAssociation
=
self
.
association
.
targetAssociation
//fully qualify
var
instancePrimaryKeys
=
Object
.
keys
(
self
.
instance
.
daoFactory
.
primaryKeys
)
var
instancePrimaryKeys
=
Object
.
keys
(
self
.
instance
.
Model
.
primaryKeys
)
,
instancePrimaryKey
=
instancePrimaryKeys
.
length
>
0
?
instancePrimaryKeys
[
0
]
:
'id'
where
[
through
.
tableName
+
"."
+
self
.
association
.
identifier
]
=
self
.
instance
[
instancePrimaryKey
]
...
...
lib/associations/has-many-single-linked.js
View file @
45503e7
...
...
@@ -32,7 +32,7 @@ module.exports = (function() {
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
,
defaultAttributes
)
{
var
self
=
this
,
associationKeys
=
Object
.
keys
((
oldAssociations
[
0
]
||
newAssociations
[
0
]
||
{
daoFactory
:
{
primaryKeys
:
{}}}).
daoFactory
.
primaryKeys
||
{})
,
associationKeys
=
Object
.
keys
((
oldAssociations
[
0
]
||
newAssociations
[
0
]
||
{
Model
:
{
primaryKeys
:
{}}}).
Model
.
primaryKeys
||
{})
,
associationKey
=
(
associationKeys
.
length
===
1
)
?
associationKeys
[
0
]
:
'id'
,
chainer
=
new
Utils
.
QueryChainer
()
,
options
=
{}
...
...
@@ -77,7 +77,7 @@ module.exports = (function() {
if
(
unassociatedObjects
.
length
>
0
)
{
// For the self.instance
var
pkeys
=
Object
.
keys
(
self
.
instance
.
daoFactory
.
primaryKeys
)
var
pkeys
=
Object
.
keys
(
self
.
instance
.
Model
.
primaryKeys
)
,
pkey
=
pkeys
.
length
===
1
?
pkeys
[
0
]
:
'id'
// For chainer
,
primaryKeys
=
Object
.
keys
(
this
.
__factory
.
target
.
primaryKeys
)
...
...
@@ -109,7 +109,7 @@ module.exports = (function() {
}
HasManySingleLinked
.
prototype
.
injectAdder
=
function
(
emitterProxy
,
newAssociation
,
additionalAttributes
)
{
var
primaryKeys
=
Object
.
keys
(
this
.
instance
.
daoFactory
.
primaryKeys
)
var
primaryKeys
=
Object
.
keys
(
this
.
instance
.
Model
.
primaryKeys
)
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
options
=
{}
...
...
lib/associations/has-many.js
View file @
45503e7
...
...
@@ -273,12 +273,12 @@ module.exports = (function() {
obj
[
this
.
accessors
.
add
]
=
function
(
newAssociatedObject
,
additionalAttributes
)
{
var
instance
=
this
,
primaryKeys
=
Object
.
keys
(
newAssociatedObject
.
daoFactory
.
primaryKeys
||
{})
,
primaryKeys
=
Object
.
keys
(
newAssociatedObject
.
Model
.
primaryKeys
||
{})
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
where
=
{}
where
[
newAssociatedObject
.
daoFactory
.
tableName
+
'.'
+
primaryKey
]
=
newAssociatedObject
[
primaryKey
]
where
[
newAssociatedObject
.
Model
.
tableName
+
'.'
+
primaryKey
]
=
newAssociatedObject
[
primaryKey
]
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
]({
where
:
where
})
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]})
...
...
lib/associations/has-one.js
View file @
45503e7
...
...
@@ -57,7 +57,7 @@ module.exports = (function() {
,
smart
obj
[
this
.
accessors
.
get
]
=
function
(
params
)
{
var
primaryKeys
=
Object
.
keys
(
this
.
daoFactory
.
primaryKeys
)
var
primaryKeys
=
Object
.
keys
(
this
.
Model
.
primaryKeys
)
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
where
=
{}
,
id
=
this
[
primaryKey
]
||
this
.
id
...
...
@@ -94,7 +94,7 @@ module.exports = (function() {
obj
[
this
.
accessors
.
set
]
=
function
(
associatedObject
,
options
)
{
var
instance
=
this
,
instanceKeys
=
Object
.
keys
(
instance
.
daoFactory
.
primaryKeys
)
,
instanceKeys
=
Object
.
keys
(
instance
.
Model
.
primaryKeys
)
,
instanceKey
=
instanceKeys
.
length
===
1
?
instanceKeys
[
0
]
:
'id'
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
...
...
lib/dao-factory.js
View file @
45503e7
...
...
@@ -120,72 +120,73 @@ module.exports = (function() {
this
.
primaryKeys
=
{}
self
.
options
.
uniqueKeys
=
{}
Utils
.
_
.
each
(
this
.
rawAttributes
,
function
(
columnValues
,
columnName
)
{
if
(
columnValues
.
hasOwnProperty
(
'unique'
)
&&
columnValues
.
unique
!==
true
&&
columnValues
.
unique
!==
false
)
{
var
idxName
=
columnValues
.
unique
if
(
typeof
columnValues
.
unique
===
"object"
)
{
idxName
=
columnValues
.
unique
.
name
// Setup names of timestamp attributes
this
.
_timestampAttributes
=
{}
if
(
this
.
options
.
timestamps
)
{
if
(
this
.
options
.
createdAt
)
{
this
.
_timestampAttributes
.
createdAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
createdAt
,
this
.
options
.
underscored
)
}
if
(
this
.
options
.
updatedAt
)
{
this
.
_timestampAttributes
.
updatedAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
updatedAt
,
this
.
options
.
underscored
)
}
if
(
this
.
options
.
paranoid
&&
this
.
options
.
deletedAt
)
{
this
.
_timestampAttributes
.
deletedAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
deletedAt
,
this
.
options
.
underscored
)
}
}
// Identify primary and unique attributes
Utils
.
_
.
each
(
this
.
rawAttributes
,
function
(
options
,
attribute
)
{
if
(
options
.
hasOwnProperty
(
'unique'
)
&&
options
.
unique
!==
true
&&
options
.
unique
!==
false
)
{
var
idxName
=
options
.
unique
if
(
typeof
options
.
unique
===
"object"
)
{
idxName
=
options
.
unique
.
name
}
self
.
options
.
uniqueKeys
[
idxName
]
=
self
.
options
.
uniqueKeys
[
idxName
]
||
{
fields
:
[],
msg
:
null
}
self
.
options
.
uniqueKeys
[
idxName
].
fields
.
push
(
columnNam
e
)
self
.
options
.
uniqueKeys
[
idxName
].
msg
=
self
.
options
.
uniqueKeys
[
idxName
].
msg
||
columnValue
s
.
unique
.
msg
||
null
self
.
options
.
uniqueKeys
[
idxName
].
fields
.
push
(
attribut
e
)
self
.
options
.
uniqueKeys
[
idxName
].
msg
=
self
.
options
.
uniqueKeys
[
idxName
].
msg
||
option
s
.
unique
.
msg
||
null
}
})
Utils
.
_
.
each
(
this
.
attributes
,
function
(
dataTypeString
,
attributeName
)
{
if
(
dataTypeString
.
indexOf
(
'PRIMARY KEY'
)
!==
-
1
)
{
self
.
primaryKeys
[
attributeName
]
=
dataTypeString
if
(
options
.
primaryKey
===
true
)
{
self
.
primaryKeys
[
attribute
]
=
self
.
attributes
[
attribute
]
}
})
// Add head and tail default attributes (id, timestamps)
addDefaultAttributes
.
call
(
this
)
addOptionalClassMethods
.
call
(
this
)
findAutoIncrementField
.
call
(
this
)
// Primary key convenience variables
this
.
primaryKeyAttributes
=
Object
.
keys
(
this
.
primaryKeys
)
this
.
primaryKeyAttribute
=
this
.
primaryKeyAttributes
[
0
]
this
.
primaryKeyCount
=
this
.
primaryKeyAttributes
.
length
this
.
options
.
hasPrimaryKeys
=
this
.
hasPrimaryKeys
=
this
.
primaryKeyCount
>
0
this
.
_hasPrimaryKeys
=
this
.
options
.
hasPrimaryKeys
=
this
.
hasPrimaryKeys
=
this
.
primaryKeyCount
>
0
this
.
_isPrimaryKey
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
primaryKeyAttributes
.
indexOf
(
key
)
!==
-
1
})
if
(
typeof
this
.
options
.
defaultScope
===
"object"
)
{
Utils
.
injectScope
.
call
(
this
,
this
.
options
.
defaultScope
)
}
// DAO prototype
// WTF ... ?
this
.
DAO
=
function
()
{
DAO
.
apply
(
this
,
arguments
);
}
Util
.
inherits
(
this
.
DAO
,
DAO
);
this
.
_timestampAttributes
=
{}
if
(
this
.
options
.
timestamps
)
{
if
(
this
.
options
.
createdAt
)
{
this
.
_timestampAttributes
.
createdAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
createdAt
,
this
.
options
.
underscored
)
}
if
(
this
.
options
.
updatedAt
)
{
this
.
_timestampAttributes
.
updatedAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
updatedAt
,
this
.
options
.
underscored
)
}
if
(
this
.
options
.
paranoid
&&
this
.
options
.
deletedAt
)
{
this
.
_timestampAttributes
.
deletedAt
=
Utils
.
_
.
underscoredIf
(
this
.
options
.
deletedAt
,
this
.
options
.
underscored
)
}
this
.
DAO
.
prototype
.
_readOnlyAttributes
=
Object
.
keys
(
this
.
_timestampAttributes
)
}
this
.
DAO
.
prototype
.
_hasReadOnlyAttributes
=
this
.
DAO
.
prototype
.
_readOnlyAttributes
&&
this
.
DAO
.
prototype
.
_readOnlyAttributes
.
length
this
.
DAO
.
prototype
.
_isReadOnlyAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
DAO
.
prototype
.
_hasReadOnlyAttributes
&&
self
.
DAO
.
prototype
.
_readOnlyAttributes
.
indexOf
(
key
)
!==
-
1
this
.
_readOnlyAttributes
=
Object
.
keys
(
this
.
_timestampAttributes
)
this
.
_hasReadOnlyAttributes
=
this
.
_readOnlyAttributes
&&
this
.
_readOnlyAttributes
.
length
this
.
_isReadOnlyAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
_hasReadOnlyAttributes
&&
self
.
_readOnlyAttributes
.
indexOf
(
key
)
!==
-
1
})
addDefaultAttributes
.
call
(
this
)
addOptionalClassMethods
.
call
(
this
)
findAutoIncrementField
.
call
(
this
)
this
.
DAO
.
prototype
.
rawAttributes
=
this
.
rawAttributes
;
this
.
DAO
.
prototype
.
_hasPrimaryKeys
=
this
.
options
.
hasPrimaryKeys
this
.
DAO
.
prototype
.
_isPrimaryKey
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
primaryKeyAttributes
.
indexOf
(
key
)
!==
-
1
&&
key
!==
'id'
})
if
(
this
.
options
.
instanceMethods
)
{
Utils
.
_
.
each
(
this
.
options
.
instanceMethods
,
function
(
fct
,
name
)
{
self
.
DAO
.
prototype
[
name
]
=
fct
...
...
@@ -194,20 +195,20 @@ module.exports = (function() {
this
.
refreshAttributes
();
this
.
DAO
.
prototype
.
booleanValu
es
=
[]
this
.
DAO
.
prototype
.
dateAttributes
=
[]
this
.
DAO
.
prototype
.
defaultValues
=
{}
this
.
_booleanAttribut
es
=
[]
this
.
_
dateAttributes
=
[]
this
.
_
defaultValues
=
{}
this
.
DAO
.
prototype
.
validators
=
{}
Utils
.
_
.
each
(
this
.
rawAttributes
,
function
(
definition
,
name
)
{
if
(((
definition
===
DataTypes
.
BOOLEAN
)
||
(
definition
.
type
===
DataTypes
.
BOOLEAN
)))
{
self
.
DAO
.
prototype
.
booleanValu
es
.
push
(
name
);
self
.
_booleanAttribut
es
.
push
(
name
);
}
if
(((
definition
===
DataTypes
.
DATE
)
||
(
definition
.
type
===
DataTypes
.
DATE
)
||
(
definition
.
originalType
===
DataTypes
.
DATE
)))
{
self
.
DAO
.
prototype
.
dateAttributes
.
push
(
name
);
self
.
_
dateAttributes
.
push
(
name
);
}
if
(
definition
.
hasOwnProperty
(
'defaultValue'
))
{
self
.
DAO
.
prototype
.
defaultValues
[
name
]
=
Utils
.
_
.
partial
(
self
.
_
defaultValues
[
name
]
=
Utils
.
_
.
partial
(
Utils
.
toDefaultValue
,
definition
.
defaultValue
)
}
...
...
@@ -216,21 +217,19 @@ module.exports = (function() {
}
})
this
.
DAO
.
prototype
.
_hasBooleanAttributes
=
!!
this
.
DAO
.
prototype
.
booleanValu
es
.
length
this
.
DAO
.
prototype
.
_isBooleanAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
DAO
.
prototype
.
booleanValu
es
.
indexOf
(
key
)
!==
-
1
this
.
_hasBooleanAttributes
=
!!
this
.
_booleanAttribut
es
.
length
this
.
_isBooleanAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
_booleanAttribut
es
.
indexOf
(
key
)
!==
-
1
})
this
.
DAO
.
prototype
.
_hasDateAttributes
=
!!
this
.
DAO
.
prototype
.
dateAttributes
.
length
this
.
DAO
.
prototype
.
_isDateAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
DAO
.
prototype
.
dateAttributes
.
indexOf
(
key
)
!==
-
1
this
.
_hasDateAttributes
=
!!
this
.
_
dateAttributes
.
length
this
.
_isDateAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
_
dateAttributes
.
indexOf
(
key
)
!==
-
1
})
this
.
DAO
.
prototype
.
__factory
=
this
this
.
DAO
.
prototype
.
daoFactory
=
this
this
.
DAO
.
prototype
.
Model
=
this
this
.
DAO
.
prototype
.
hasDefaultValues
=
!
Utils
.
_
.
isEmpty
(
this
.
DAO
.
prototype
.
defaultValues
)
this
.
DAO
.
prototype
.
daoFactoryName
=
this
.
name
this
.
DAO
.
prototype
.
Model
=
this
this
.
_hasDefaultValues
=
!
Utils
.
_
.
isEmpty
(
this
.
_defaultValues
)
return
this
}
...
...
@@ -1276,7 +1275,11 @@ module.exports = (function() {
var
addDefaultAttributes
=
function
()
{
var
self
=
this
,
tail
=
{}
,
head
=
{
,
head
=
{}
// Add id if no primary key was manually added to definition
if
(
!
Object
.
keys
(
this
.
primaryKeys
).
length
)
{
head
=
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
...
...
@@ -1285,9 +1288,6 @@ module.exports = (function() {
_autoGenerated
:
true
}
}
if
(
this
.
hasPrimaryKeys
)
{
head
=
{}
}
if
(
this
.
_timestampAttributes
.
createdAt
)
{
...
...
@@ -1316,6 +1316,10 @@ module.exports = (function() {
self
.
rawAttributes
[
attr
]
=
value
}
})
if
(
!
Object
.
keys
(
this
.
primaryKeys
).
length
)
{
self
.
primaryKeys
[
'id'
]
=
self
.
attributes
[
'id'
]
}
}
var
findAutoIncrementField
=
function
()
{
...
...
@@ -1344,8 +1348,12 @@ module.exports = (function() {
options
.
includeNames
.
push
(
include
.
as
)
options
.
includeNames
.
push
(
include
.
as
.
substr
(
0
,
1
).
toLowerCase
()
+
include
.
as
.
substr
(
1
))
if
(
include
.
association
.
isMultiAssociation
||
include
.
hasMultiAssociation
)
options
.
hasMultiAssociation
=
true
if
(
include
.
association
.
isSingleAssociation
||
include
.
hasSingleAssociation
)
options
.
hasSingleAssociation
=
true
if
(
include
.
association
.
isMultiAssociation
||
include
.
hasMultiAssociation
)
{
options
.
hasMultiAssociation
=
true
}
if
(
include
.
association
.
isSingleAssociation
||
include
.
hasSingleAssociation
)
{
options
.
hasSingleAssociation
=
true
}
options
.
hasIncludeWhere
=
options
.
hasIncludeWhere
||
include
.
hasIncludeWhere
||
!!
include
.
where
options
.
hasIncludeRequired
=
options
.
hasIncludeRequired
||
include
.
hasIncludeRequired
||
!!
include
.
required
...
...
@@ -1378,8 +1386,8 @@ module.exports = (function() {
var
primaryKeys
;
if
(
include
.
daoFactory
.
hasPrimaryKeys
)
{
primaryKeys
=
[]
for
(
var
field
_name
in
include
.
daoFactory
.
primaryKeys
)
{
primaryKeys
.
push
(
field
_name
)
for
(
var
field
in
include
.
daoFactory
.
primaryKeys
)
{
primaryKeys
.
push
(
field
)
}
}
else
{
primaryKeys
=
[
'id'
]
...
...
@@ -1393,7 +1401,7 @@ module.exports = (function() {
if
(
include
.
_pseudo
)
return
include
// check if the current daoFactory is actually associated with the passed daoFactory - or it's a pseudo include
var
association
=
parent
.
getAssociation
(
include
.
daoFactory
,
include
.
as
)
var
association
=
parent
.
getAssociation
(
include
.
daoFactory
,
include
.
as
)
if
(
association
)
{
include
.
association
=
association
include
.
as
=
association
.
as
...
...
lib/dao-validator.js
View file @
45503e7
...
...
@@ -2,11 +2,11 @@ var Validator = require("validator")
,
Utils
=
require
(
"./utils"
)
,
sequelizeError
=
require
(
"./errors"
)
var
DaoValidator
=
module
.
exports
=
function
(
model
,
options
)
{
var
DaoValidator
=
module
.
exports
=
function
(
model
Instance
,
options
)
{
options
=
options
||
{}
options
.
skip
=
options
.
skip
||
[]
this
.
model
=
model
this
.
model
Instance
=
modelInstance
this
.
chainer
=
new
Utils
.
QueryChainer
()
this
.
options
=
options
}
...
...
@@ -44,10 +44,9 @@ DaoValidator.prototype.validate = function() {
DaoValidator
.
prototype
.
hookValidate
=
function
()
{
var
self
=
this
,
errors
=
{}
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
model
.
daoFactory
.
runHooks
(
'beforeValidate'
,
self
.
model
,
function
(
err
)
{
self
.
model
Instance
.
Model
.
runHooks
(
'beforeValidate'
,
self
.
modelInstance
,
function
(
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
...
...
@@ -57,12 +56,12 @@ DaoValidator.prototype.hookValidate = function() {
return
emitter
.
emit
(
'error'
,
error
)
}
self
.
model
.
daoFactory
.
runHooks
(
'afterValidate'
,
self
.
model
,
function
(
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
self
.
model
Instance
.
Model
.
runHooks
(
'afterValidate'
,
self
.
modelInstance
,
function
(
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
emitter
.
emit
(
'success'
,
self
.
model
)
emitter
.
emit
(
'success'
,
self
.
modelInstance
)
})
})
})
...
...
@@ -71,7 +70,7 @@ DaoValidator.prototype.hookValidate = function() {
// private
var
validateModel
=
function
()
{
Utils
.
_
.
each
(
this
.
model
.
__options
.
validate
,
function
(
_validator
,
validatorType
)
{
Utils
.
_
.
each
(
this
.
model
Instance
.
__options
.
validate
,
function
(
_validator
,
validatorType
)
{
var
validator
=
prepareValidationOfAttribute
.
call
(
this
,
undefined
,
_validator
,
validatorType
,
{
omitValue
:
true
})
this
.
chainer
.
add
(
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
...
...
@@ -99,12 +98,12 @@ var validateAttributes = function() {
var
self
=
this
,
errors
=
{}
Utils
.
_
.
each
(
this
.
model
.
rawAttributes
,
function
(
rawAttribute
,
field
)
{
var
value
=
self
.
model
.
dataValues
[
field
]
Utils
.
_
.
each
(
this
.
model
Instance
.
rawAttributes
,
function
(
rawAttribute
,
field
)
{
var
value
=
self
.
model
Instance
.
dataValues
[
field
]
,
hasAllowedNull
=
((
rawAttribute
===
undefined
||
rawAttribute
.
allowNull
===
true
)
&&
((
value
===
null
)
||
(
value
===
undefined
)))
,
isSkipped
=
self
.
options
.
skip
.
length
>
0
&&
self
.
options
.
skip
.
indexOf
(
field
)
!==
-
1
if
(
self
.
model
.
validators
.
hasOwnProperty
(
field
)
&&
!
hasAllowedNull
&&
!
isSkipped
)
{
if
(
self
.
model
Instance
.
validators
.
hasOwnProperty
(
field
)
&&
!
hasAllowedNull
&&
!
isSkipped
)
{
errors
=
Utils
.
_
.
merge
(
errors
,
validateAttribute
.
call
(
self
,
value
,
field
))
}
})
...
...
@@ -114,7 +113,7 @@ var validateAttributes = function() {
var
validateAttribute
=
function
(
value
,
field
)
{
// for each validator
Utils
.
_
.
each
(
this
.
model
.
validators
[
field
],
function
(
details
,
validatorType
)
{
Utils
.
_
.
each
(
this
.
model
Instance
.
validators
[
field
],
function
(
details
,
validatorType
)
{
var
validator
=
prepareValidationOfAttribute
.
call
(
this
,
value
,
details
,
validatorType
)
this
.
chainer
.
add
(
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
...
...
@@ -162,7 +161,7 @@ var prepareValidationOfAttribute = function(value, details, validatorType, optio
}
try
{
details
.
apply
(
this
.
model
,
callArgs
)
details
.
apply
(
this
.
model
Instance
,
callArgs
)
}
catch
(
ex
)
{
return
next
(
ex
)
}
...
...
lib/dao.js
View file @
45503e7
...
...
@@ -9,9 +9,9 @@ module.exports = (function() {
var
DAO
=
function
(
values
,
options
)
{
this
.
dataValues
=
{}
this
.
_previousDataValues
=
{}
this
.
__options
=
this
.
__factory
.
options
this
.
__options
=
this
.
Model
.
options
this
.
options
=
options
this
.
hasPrimaryKeys
=
this
.
__factory
.
options
.
hasPrimaryKeys
this
.
hasPrimaryKeys
=
this
.
Model
.
options
.
hasPrimaryKeys
// What is selected values even used for?
this
.
selectedValues
=
options
.
include
?
_
.
omit
(
values
,
options
.
includeNames
)
:
values
this
.
__eagerlyLoadedAssociations
=
[]
...
...
@@ -23,7 +23,7 @@ module.exports = (function() {
Utils
.
_
.
extend
(
DAO
.
prototype
,
Mixin
.
prototype
)
Object
.
defineProperty
(
DAO
.
prototype
,
'sequelize'
,
{
get
:
function
(){
return
this
.
__factory
.
daoFactoryManager
.
sequelize
}
get
:
function
(){
return
this
.
Model
.
daoFactoryManager
.
sequelize
}
})
Object
.
defineProperty
(
DAO
.
prototype
,
'QueryInterface'
,
{
...
...
@@ -53,7 +53,7 @@ module.exports = (function() {
var
result
=
{}
,
self
=
this
Utils
.
_
.
each
(
this
.
__factory
.
primaryKeys
,
function
(
_
,
attr
)
{
Utils
.
_
.
each
(
this
.
Model
.
primaryKeys
,
function
(
_
,
attr
)
{
result
[
attr
]
=
self
.
dataValues
[
attr
]
})
...
...
@@ -63,14 +63,9 @@ module.exports = (function() {
Object
.
defineProperty
(
DAO
.
prototype
,
"identifiers"
,
{
get
:
function
()
{
var
primaryKeys
=
Object
.
keys
(
this
.
__factory
.
primaryKeys
)
var
primaryKeys
=
Object
.
keys
(
this
.
Model
.
primaryKeys
)
,
result
=
{}
,
self
=
this
if
(
!
this
.
__factory
.
hasPrimaryKeys
)
{
primaryKeys
=
[
'id'
]
}
primaryKeys
.
forEach
(
function
(
identifier
)
{
result
[
identifier
]
=
self
.
dataValues
[
identifier
]
})
...
...
@@ -128,7 +123,7 @@ module.exports = (function() {
}
// If raw, and we're not dealing with includes, just set it straight on the dataValues object
if
(
options
.
raw
&&
!
(
this
.
options
&&
this
.
options
.
include
)
&&
!
this
.
_hasBooleanAttributes
)
{
if
(
options
.
raw
&&
!
(
this
.
options
&&
this
.
options
.
include
)
&&
!
this
.
Model
.
_hasBooleanAttributes
)
{
if
(
Object
.
keys
(
this
.
dataValues
).
length
)
{
this
.
dataValues
=
_
.
extend
(
this
.
dataValues
,
values
)
}
else
{
...
...
@@ -163,28 +158,22 @@ module.exports = (function() {
return
}
else
{
// If attempting to set primary key and primary key is already defined, return
if
(
this
.
_hasPrimaryKeys
&&
originalValue
&&
this
.
_isPrimaryKey
(
key
))
{
return
}
// If attempting to set generated id and id is already defined, return
// This is hack since generated id is not in primaryKeys, although it should be
if
(
originalValue
&&
key
===
"id"
)
{
if
(
this
.
Model
.
_hasPrimaryKeys
&&
originalValue
&&
this
.
Model
.
_isPrimaryKey
(
key
))
{
return
}
// If attempting to set read only attributes, return
if
(
!
options
.
raw
&&
this
.
_hasReadOnlyAttributes
&&
this
.
_isReadOnlyAttribute
(
key
))
{
if
(
!
options
.
raw
&&
this
.
Model
.
_hasReadOnlyAttributes
&&
this
.
Model
.
_isReadOnlyAttribute
(
key
))
{
return
}
// Convert boolean-ish values to booleans
if
(
this
.
_hasBooleanAttributes
&&
this
.
_isBooleanAttribute
(
key
)
&&
value
!==
null
&&
value
!==
undefined
)
{
if
(
this
.
Model
.
_hasBooleanAttributes
&&
this
.
Model
.
_isBooleanAttribute
(
key
)
&&
value
!==
null
&&
value
!==
undefined
)
{
value
=
!!
value
}
// Convert date fields to real date objects
if
(
this
.
_hasDateAttributes
&&
this
.
_isDateAttribute
(
key
)
&&
value
!==
null
&&
!
(
value
instanceof
Date
))
{
if
(
this
.
Model
.
_hasDateAttributes
&&
this
.
Model
.
_isDateAttribute
(
key
)
&&
value
!==
null
&&
!
(
value
instanceof
Date
))
{
value
=
new
Date
(
value
)
}
...
...
@@ -199,7 +188,7 @@ module.exports = (function() {
DAO
.
prototype
.
changed
=
function
(
key
)
{
if
(
key
)
{
if
(
this
.
_isDateAttribute
(
key
)
&&
this
.
_previousDataValues
[
key
]
&&
this
.
dataValues
[
key
])
{
if
(
this
.
Model
.
_isDateAttribute
(
key
)
&&
this
.
_previousDataValues
[
key
]
&&
this
.
dataValues
[
key
])
{
return
this
.
_previousDataValues
[
key
].
valueOf
()
!==
this
.
dataValues
[
key
].
valueOf
()
}
return
this
.
_previousDataValues
[
key
]
!==
this
.
dataValues
[
key
]
...
...
@@ -309,13 +298,13 @@ module.exports = (function() {
}
})
for
(
var
attrName
in
self
.
daoFactory
.
rawAttributes
)
{
if
(
self
.
daoFactory
.
rawAttributes
.
hasOwnProperty
(
attrName
))
{
var
definition
=
self
.
daoFactory
.
rawAttributes
[
attrName
]
for
(
var
attrName
in
self
.
Model
.
rawAttributes
)
{
if
(
self
.
Model
.
rawAttributes
.
hasOwnProperty
(
attrName
))
{
var
definition
=
self
.
Model
.
rawAttributes
[
attrName
]
,
isHstore
=
!!
definition
.
type
&&
!!
definition
.
type
.
type
&&
definition
.
type
.
type
===
DataTypes
.
HSTORE
.
type
,
isEnum
=
definition
.
type
&&
(
definition
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
,
isMySQL
=
[
'mysql'
,
'mariadb'
].
indexOf
(
self
.
daoFactory
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
!==
-
1
,
ciCollation
=
!!
self
.
daoFactory
.
options
.
collate
&&
self
.
daoFactory
.
options
.
collate
.
match
(
/_ci$/i
)
,
isMySQL
=
[
'mysql'
,
'mariadb'
].
indexOf
(
self
.
Model
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
!==
-
1
,
ciCollation
=
!!
self
.
Model
.
options
.
collate
&&
self
.
Model
.
options
.
collate
.
match
(
/_ci$/i
)
,
valueOutOfScope
// Unfortunately for MySQL CI collation we need to map/lowercase values again
...
...
@@ -341,20 +330,20 @@ module.exports = (function() {
values
[
updatedAtAttr
]
=
(
(
self
.
isNewRecord
&&
!!
self
.
daoFactory
.
rawAttributes
[
updatedAtAttr
]
&&
!!
self
.
daoFactory
.
rawAttributes
[
updatedAtAttr
].
defaultValue
&&
!!
self
.
Model
.
rawAttributes
[
updatedAtAttr
]
&&
!!
self
.
Model
.
rawAttributes
[
updatedAtAttr
].
defaultValue
)
?
self
.
daoFactory
.
rawAttributes
[
updatedAtAttr
].
defaultValue
?
self
.
Model
.
rawAttributes
[
updatedAtAttr
].
defaultValue
:
Utils
.
now
(
self
.
sequelize
.
options
.
dialect
))
}
if
(
self
.
isNewRecord
&&
createdAtAttr
&&
!
values
[
createdAtAttr
])
{
values
[
createdAtAttr
]
=
(
(
!!
self
.
daoFactory
.
rawAttributes
[
createdAtAttr
]
&&
!!
self
.
daoFactory
.
rawAttributes
[
createdAtAttr
].
defaultValue
!!
self
.
Model
.
rawAttributes
[
createdAtAttr
]
&&
!!
self
.
Model
.
rawAttributes
[
createdAtAttr
].
defaultValue
)
?
self
.
daoFactory
.
rawAttributes
[
createdAtAttr
].
defaultValue
?
self
.
Model
.
rawAttributes
[
createdAtAttr
].
defaultValue
:
values
[
updatedAtAttr
])
}
...
...
@@ -364,17 +353,17 @@ module.exports = (function() {
if
(
self
.
isNewRecord
)
{
query
=
'insert'
args
=
[
self
,
self
.
QueryInterface
.
QueryGenerator
.
addSchema
(
self
.
__factory
),
values
,
options
]
args
=
[
self
,
self
.
QueryInterface
.
QueryGenerator
.
addSchema
(
self
.
Model
),
values
,
options
]
hook
=
'Create'
}
else
{
var
identifier
=
self
.
__options
.
hasPrimaryKeys
?
self
.
primaryKeyValues
:
{
id
:
self
.
id
}
var
identifier
=
self
.
primaryKeyValues
if
(
identifier
===
null
&&
self
.
__options
.
whereCollection
!==
null
)
{
identifier
=
self
.
__options
.
whereCollection
;
}
query
=
'update'
args
=
[
self
,
self
.
QueryInterface
.
QueryGenerator
.
addSchema
(
self
.
__factory
),
values
,
identifier
,
options
]
args
=
[
self
,
self
.
QueryInterface
.
QueryGenerator
.
addSchema
(
self
.
Model
),
values
,
identifier
,
options
]
hook
=
'Update'
}
...
...
@@ -382,7 +371,7 @@ module.exports = (function() {
self
.
dataValues
=
_
.
extend
(
self
.
dataValues
,
values
)
// Run the beforeCreate / beforeUpdate hook
self
.
__factory
.
runHooks
(
'before'
+
hook
,
self
,
function
(
err
)
{
self
.
Model
.
runHooks
(
'before'
+
hook
,
self
,
function
(
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
...
...
@@ -405,7 +394,7 @@ module.exports = (function() {
var
fields
=
self
.
QueryInterface
.
QueryGenerator
.
uniqueConstraintMapping
.
map
(
err
.
toString
())
if
(
fields
!==
false
)
{
fields
=
fields
.
filter
(
function
(
f
)
{
return
f
!==
self
.
daoFactory
.
tableName
;
})
fields
=
fields
.
filter
(
function
(
f
)
{
return
f
!==
self
.
Model
.
tableName
;
})
Utils
.
_
.
each
(
self
.
__options
.
uniqueKeys
,
function
(
value
,
key
)
{
if
(
Utils
.
_
.
isEqual
(
value
.
fields
,
fields
)
&&
!!
value
.
msg
)
{
err
=
value
.
msg
...
...
@@ -424,7 +413,7 @@ module.exports = (function() {
result
.
dataValues
=
_
.
extend
(
result
.
dataValues
,
values
)
result
.
_previousDataValues
=
_
.
clone
(
result
.
dataValues
)
self
.
__factory
.
runHooks
(
'after'
+
hook
,
result
,
function
(
err
)
{
self
.
Model
.
runHooks
(
'after'
+
hook
,
result
,
function
(
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
...
...
@@ -445,12 +434,12 @@ module.exports = (function() {
*/
DAO
.
prototype
.
reload
=
function
(
options
)
{
var
where
=
[
this
.
QueryInterface
.
quoteIdentifier
(
this
.
__factory
.
tableName
)
+
'.'
+
this
.
QueryInterface
.
quoteIdentifier
(
'id'
)
+
'=?'
,
this
.
QueryInterface
.
quoteIdentifier
(
this
.
Model
.
tableName
)
+
'.'
+
this
.
QueryInterface
.
quoteIdentifier
(
'id'
)
+
'=?'
,
this
.
id
]
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
this
.
__factory
.
find
({
this
.
Model
.
find
({
where
:
where
,
limit
:
1
,
include
:
this
.
options
.
include
||
null
...
...
@@ -506,7 +495,7 @@ module.exports = (function() {
,
query
=
null
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
daoFactory
.
runHooks
(
self
.
daoFactory
.
options
.
hooks
.
beforeDestroy
,
self
,
function
(
err
)
{
self
.
Model
.
runHooks
(
self
.
Model
.
options
.
hooks
.
beforeDestroy
,
self
,
function
(
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
...
...
@@ -516,7 +505,7 @@ module.exports = (function() {
query
=
self
.
save
(
options
)
}
else
{
var
identifier
=
self
.
__options
.
hasPrimaryKeys
?
self
.
primaryKeyValues
:
{
id
:
self
.
id
};
query
=
self
.
QueryInterface
.
delete
(
self
,
self
.
QueryInterface
.
QueryGenerator
.
addSchema
(
self
.
__factory
.
tableName
,
self
.
__factory
.
options
.
schema
),
identifier
,
options
)
query
=
self
.
QueryInterface
.
delete
(
self
,
self
.
QueryInterface
.
QueryGenerator
.
addSchema
(
self
.
Model
.
tableName
,
self
.
Model
.
options
.
schema
),
identifier
,
options
)
}
query
.
on
(
'sql'
,
function
(
sql
)
{
...
...
@@ -526,7 +515,7 @@ module.exports = (function() {
emitter
.
emit
(
'error'
,
err
)
})
.
success
(
function
(
results
)
{
self
.
daoFactory
.
runHooks
(
self
.
daoFactory
.
options
.
hooks
.
afterDestroy
,
self
,
function
(
err
)
{
self
.
Model
.
runHooks
(
self
.
Model
.
options
.
hooks
.
afterDestroy
,
self
,
function
(
err
)
{
if
(
!!
err
)
{
return
emitter
.
emit
(
'error'
,
err
)
}
...
...
@@ -571,10 +560,10 @@ module.exports = (function() {
}
if
(
updatedAtAttr
&&
!
values
[
updatedAtAttr
])
{
countOrOptions
.
attributes
[
updatedAtAttr
]
=
Utils
.
now
(
this
.
daoFactory
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
countOrOptions
.
attributes
[
updatedAtAttr
]
=
Utils
.
now
(
this
.
Model
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
}
return
this
.
QueryInterface
.
increment
(
this
,
this
.
QueryInterface
.
QueryGenerator
.
addSchema
(
this
.
__factory
.
tableName
,
this
.
__factory
.
options
.
schema
),
values
,
identifier
,
countOrOptions
)
return
this
.
QueryInterface
.
increment
(
this
,
this
.
QueryInterface
.
QueryGenerator
.
addSchema
(
this
.
Model
.
tableName
,
this
.
Model
.
options
.
schema
),
values
,
identifier
,
countOrOptions
)
}
DAO
.
prototype
.
decrement
=
function
(
fields
,
countOrOptions
)
{
...
...
@@ -638,15 +627,18 @@ module.exports = (function() {
// private
var
initValues
=
function
(
values
,
options
)
{
// set id to null if not passed as value, a newly created dao has no id
var
defaults
=
this
.
hasPrimaryKeys
?
{}
:
{
id
:
null
},
var
defaults
=
{},
key
;
// set id to null if not passed as value, a newly created dao has no id
// removing this breaks bulkCreate
defaults
[
this
.
Model
.
primaryKeyAttribute
]
=
null
;
values
=
values
&&
_
.
clone
(
values
)
||
{}
if
(
options
.
isNewRecord
)
{
if
(
this
.
hasDefaultValues
)
{
Utils
.
_
.
each
(
this
.
defaultValues
,
function
(
valueFn
,
key
)
{
if
(
this
.
Model
.
_
hasDefaultValues
)
{
Utils
.
_
.
each
(
this
.
Model
.
_
defaultValues
,
function
(
valueFn
,
key
)
{
if
(
!
defaults
.
hasOwnProperty
(
key
))
{
defaults
[
key
]
=
valueFn
()
}
...
...
lib/dialects/abstract/query-generator.js
View file @
45503e7
...
...
@@ -512,7 +512,7 @@ module.exports = (function() {
- offset -> An offset value to start from. Only useable with limit!
*/
selectQuery
:
function
(
tableName
,
options
,
factory
)
{
selectQuery
:
function
(
tableName
,
options
,
Model
)
{
// Enter and change at your own peril -- Mick Hansen
options
=
options
||
{}
...
...
@@ -536,7 +536,7 @@ module.exports = (function() {
}.
bind
(
this
)).
join
(
", "
)
if
(
subQuery
&&
mainAttributes
)
{
mainAttributes
=
mainAttributes
.
concat
(
factory
.
hasPrimaryKeys
?
factory
.
primaryKeyAttributes
:
[
'id'
]
)
mainAttributes
=
mainAttributes
.
concat
(
Model
.
primaryKeyAttributes
)
}
// Escape attributes
...
...
@@ -616,16 +616,16 @@ module.exports = (function() {
,
throughAttributes
=
through
.
attributes
.
map
(
function
(
attr
)
{
return
self
.
quoteIdentifier
(
throughAs
)
+
"."
+
self
.
quoteIdentifier
(
attr
)
+
" AS "
+
self
.
quoteIdentifier
(
throughAs
+
"."
+
attr
)
})
,
primaryKeysSource
=
Object
.
keys
(
association
.
source
.
primaryKeys
)
,
primaryKeysSource
=
association
.
source
.
primaryKeyAttributes
,
tableSource
=
parentTable
,
identSource
=
association
.
identifier
,
attrSource
=
((
!
association
.
source
.
hasPrimaryKeys
||
primaryKeysSource
.
length
!==
1
)
?
'id'
:
primaryKeysSource
[
0
])
,
attrSource
=
primaryKeysSource
[
0
]
,
where
,
primaryKeysTarget
=
Object
.
keys
(
association
.
target
.
primaryKeys
)
,
primaryKeysTarget
=
association
.
target
.
primaryKeyAttributes
,
tableTarget
=
as
,
identTarget
=
association
.
foreignIdentifier
,
attrTarget
=
((
!
include
.
association
.
target
.
hasPrimaryKeys
||
primaryKeysTarget
.
length
!==
1
)
?
'id'
:
primaryKeysTarget
[
0
])
,
attrTarget
=
primaryKeysTarget
[
0
]
,
sourceJoinOn
,
targetJoinOn
...
...
@@ -673,10 +673,10 @@ module.exports = (function() {
}
}
}
else
{
var
primaryKeysLeft
=
((
association
.
associationType
===
'BelongsTo'
)
?
Object
.
keys
(
association
.
target
.
primaryKeys
)
:
Object
.
keys
(
include
.
association
.
source
.
primaryKeys
))
,
tableLeft
=
((
association
.
associationType
===
'BelongsTo'
)
?
as
:
parentTable
)
,
attrLeft
=
((
primaryKeysLeft
.
length
!==
1
)
?
'id'
:
primaryKeysLeft
[
0
])
,
tableRight
=
((
association
.
associationType
===
'BelongsTo'
)
?
parentTable
:
as
)
var
primaryKeysLeft
=
association
.
associationType
===
'BelongsTo'
?
association
.
target
.
primaryKeyAttributes
:
include
.
association
.
source
.
primaryKeyAttributes
,
tableLeft
=
association
.
associationType
===
'BelongsTo'
?
as
:
parentTable
,
attrLeft
=
primaryKeysLeft
[
0
]
,
tableRight
=
association
.
associationType
===
'BelongsTo'
?
parentTable
:
as
,
attrRight
=
association
.
identifier
,
where
...
...
@@ -737,7 +737,7 @@ module.exports = (function() {
// Add WHERE to sub or main query
if
(
options
.
hasOwnProperty
(
'where'
))
{
options
.
where
=
this
.
getWhereConditions
(
options
.
where
,
tableName
,
factory
,
options
)
options
.
where
=
this
.
getWhereConditions
(
options
.
where
,
tableName
,
Model
,
options
)
if
(
subQuery
)
{
subQueryItems
.
push
(
" WHERE "
+
options
.
where
)
}
else
{
...
...
@@ -747,7 +747,7 @@ module.exports = (function() {
// Add GROUP BY to sub or main query
if
(
options
.
group
)
{
options
.
group
=
Array
.
isArray
(
options
.
group
)
?
options
.
group
.
map
(
function
(
t
)
{
return
this
.
quote
(
t
,
factory
)
}.
bind
(
this
)).
join
(
', '
)
:
options
.
group
options
.
group
=
Array
.
isArray
(
options
.
group
)
?
options
.
group
.
map
(
function
(
t
)
{
return
this
.
quote
(
t
,
Model
)
}.
bind
(
this
)).
join
(
', '
)
:
options
.
group
if
(
subQuery
)
{
subQueryItems
.
push
(
" GROUP BY "
+
options
.
group
)
}
else
{
...
...
@@ -757,7 +757,7 @@ module.exports = (function() {
// Add HAVING to sub or main query
if
(
options
.
hasOwnProperty
(
'having'
))
{
options
.
having
=
this
.
getWhereConditions
(
options
.
having
,
tableName
,
factory
,
options
,
false
)
options
.
having
=
this
.
getWhereConditions
(
options
.
having
,
tableName
,
Model
,
options
,
false
)
if
(
subQuery
)
{
subQueryItems
.
push
(
" HAVING "
+
options
.
having
)
}
else
{
...
...
@@ -767,7 +767,7 @@ module.exports = (function() {
// Add ORDER to sub or main query
if
(
options
.
order
)
{
options
.
order
=
Array
.
isArray
(
options
.
order
)
?
options
.
order
.
map
(
function
(
t
)
{
return
this
.
quote
(
t
,
factory
)
}.
bind
(
this
)).
join
(
', '
)
:
options
.
order
options
.
order
=
Array
.
isArray
(
options
.
order
)
?
options
.
order
.
map
(
function
(
t
)
{
return
this
.
quote
(
t
,
Model
)
}.
bind
(
this
)).
join
(
', '
)
:
options
.
order
if
(
subQuery
)
{
subQueryItems
.
push
(
" ORDER BY "
+
options
.
order
)
...
...
lib/dialects/abstract/query.js
View file @
45503e7
...
...
@@ -173,7 +173,7 @@ module.exports = (function() {
var
handleInsertQuery
=
function
(
results
,
metaData
)
{
if
(
this
.
callee
)
{
// add the inserted row id to the instance
var
autoIncrementField
=
this
.
callee
.
__factory
.
autoIncrementField
var
autoIncrementField
=
this
.
callee
.
Model
.
autoIncrementField
,
id
=
null
id
=
id
||
(
results
&&
results
[
this
.
getInsertIdField
()])
...
...
lib/dialects/postgres/query.js
View file @
45503e7
...
...
@@ -137,7 +137,7 @@ module.exports = (function() {
for
(
var
key
in
rows
[
0
])
{
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
)
{
if
(
!!
this
.
callee
.
Model
&&
!!
this
.
callee
.
Model
.
rawAttributes
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
]
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
.
type
&&
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
.
type
===
DataTypes
.
HSTORE
.
type
)
{
record
=
hstore
.
parse
(
record
)
}
this
.
callee
.
dataValues
[
key
]
=
record
...
...
@@ -152,7 +152,7 @@ module.exports = (function() {
for
(
var
key
in
rows
[
0
])
{
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
)
{
if
(
!!
this
.
callee
.
Model
&&
!!
this
.
callee
.
Model
.
rawAttributes
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
]
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
&&
!!
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
.
type
&&
this
.
callee
.
Model
.
rawAttributes
[
key
].
type
.
type
===
DataTypes
.
HSTORE
.
type
)
{
record
=
hstore
.
parse
(
record
)
}
this
.
callee
.
dataValues
[
key
]
=
record
...
...
lib/query-interface.js
View file @
45503e7
...
...
@@ -504,7 +504,7 @@ module.exports = (function() {
}
QueryInterface
.
prototype
.
insert
=
function
(
dao
,
tableName
,
values
,
options
)
{
var
sql
=
this
.
QueryGenerator
.
insertQuery
(
tableName
,
values
,
dao
.
daoFactory
.
rawAttributes
)
var
sql
=
this
.
QueryGenerator
.
insertQuery
(
tableName
,
values
,
dao
.
Model
.
rawAttributes
)
return
queryAndEmit
.
call
(
this
,
[
sql
,
dao
,
options
],
'insert'
,
{
success
:
function
(
obj
)
{
obj
.
isNewRecord
=
false
}
})
...
...
@@ -518,15 +518,15 @@ module.exports = (function() {
QueryInterface
.
prototype
.
update
=
function
(
dao
,
tableName
,
values
,
identifier
,
options
)
{
var
self
=
this
,
restrict
=
false
,
sql
=
self
.
QueryGenerator
.
updateQuery
(
tableName
,
values
,
identifier
,
options
,
dao
.
daoFactory
.
rawAttributes
)
,
sql
=
self
.
QueryGenerator
.
updateQuery
(
tableName
,
values
,
identifier
,
options
,
dao
.
Model
.
rawAttributes
)
// Check for a restrict field
if
(
!!
dao
.
daoFactory
&&
!!
dao
.
daoFactory
.
associations
)
{
var
keys
=
Object
.
keys
(
dao
.
daoFactory
.
associations
)
if
(
!!
dao
.
Model
&&
!!
dao
.
Model
.
associations
)
{
var
keys
=
Object
.
keys
(
dao
.
Model
.
associations
)
,
length
=
keys
.
length
for
(
var
i
=
0
;
i
<
length
;
i
++
)
{
if
(
dao
.
daoFactory
.
associations
[
keys
[
i
]].
options
&&
dao
.
daoFactory
.
associations
[
keys
[
i
]].
options
.
onUpdate
&&
dao
.
daoFactory
.
associations
[
keys
[
i
]].
options
.
onUpdate
===
"restrict"
)
{
if
(
dao
.
Model
.
associations
[
keys
[
i
]].
options
&&
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
onUpdate
&&
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
onUpdate
===
"restrict"
)
{
restrict
=
true
}
}
...
...
@@ -581,20 +581,20 @@ module.exports = (function() {
var
self
=
this
,
restrict
=
false
,
cascades
=
[]
,
sql
=
self
.
QueryGenerator
.
deleteQuery
(
tableName
,
identifier
,
null
,
dao
.
daoFactory
)
,
sql
=
self
.
QueryGenerator
.
deleteQuery
(
tableName
,
identifier
,
null
,
dao
.
Model
)
// Check for a restrict field
if
(
!!
dao
.
daoFactory
&&
!!
dao
.
daoFactory
.
associations
)
{
var
keys
=
Object
.
keys
(
dao
.
daoFactory
.
associations
)
if
(
!!
dao
.
Model
&&
!!
dao
.
Model
.
associations
)
{
var
keys
=
Object
.
keys
(
dao
.
Model
.
associations
)
,
length
=
keys
.
length
for
(
var
i
=
0
;
i
<
length
;
i
++
)
{
if
(
dao
.
daoFactory
.
associations
[
keys
[
i
]].
options
&&
dao
.
daoFactory
.
associations
[
keys
[
i
]].
options
.
onDelete
)
{
if
(
dao
.
daoFactory
.
associations
[
keys
[
i
]].
options
.
onDelete
===
"restrict"
)
{
if
(
dao
.
Model
.
associations
[
keys
[
i
]].
options
&&
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
onDelete
)
{
if
(
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
onDelete
===
"restrict"
)
{
restrict
=
true
}
else
if
(
dao
.
daoFactory
.
associations
[
keys
[
i
]].
options
.
onDelete
===
"cascade"
&&
dao
.
daoFactory
.
associations
[
keys
[
i
]].
options
.
useHooks
===
true
)
{
cascades
[
cascades
.
length
]
=
dao
.
daoFactory
.
associations
[
keys
[
i
]].
accessors
.
get
else
if
(
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
onDelete
===
"cascade"
&&
dao
.
Model
.
associations
[
keys
[
i
]].
options
.
useHooks
===
true
)
{
cascades
[
cascades
.
length
]
=
dao
.
Model
.
associations
[
keys
[
i
]].
accessors
.
get
}
}
}
...
...
test/associations/belongs-to.test.js
View file @
45503e7
...
...
@@ -297,7 +297,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
// the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
__factory
)
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
Model
)
user
.
QueryInterface
.
update
(
user
,
tableName
,
{
id
:
999
},
user
.
id
)
.
success
(
function
()
{
Task
.
findAll
().
success
(
function
(
tasks
)
{
...
...
@@ -327,7 +327,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
// the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
__factory
)
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
Model
)
user
.
QueryInterface
.
update
(
user
,
tableName
,
{
id
:
999
},
user
.
id
)
.
error
(
function
()
{
// Should fail due to FK restriction
...
...
test/associations/has-many.test.js
View file @
45503e7
...
...
@@ -1389,7 +1389,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
// the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
__factory
)
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
Model
)
user
.
QueryInterface
.
update
(
user
,
tableName
,
{
id
:
999
},
user
.
id
)
.
success
(
function
()
{
Task
.
findAll
().
success
(
function
(
tasks
)
{
...
...
@@ -1421,7 +1421,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
// the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
__factory
)
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
Model
)
user
.
QueryInterface
.
update
(
user
,
tableName
,
{
id
:
999
},
user
.
id
)
.
error
(
function
()
{
// Should fail due to FK restriction
...
...
test/associations/has-one.test.js
View file @
45503e7
...
...
@@ -301,7 +301,7 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
// the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
__factory
)
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
Model
)
user
.
QueryInterface
.
update
(
user
,
tableName
,
{
id
:
999
},
user
.
id
)
.
success
(
function
()
{
Task
.
findAll
().
success
(
function
(
tasks
)
{
...
...
@@ -333,7 +333,7 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
// the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
__factory
)
var
tableName
=
user
.
QueryInterface
.
QueryGenerator
.
addSchema
(
user
.
Model
)
user
.
QueryInterface
.
update
(
user
,
tableName
,
{
id
:
999
},
user
.
id
)
.
error
(
function
()
{
// Should fail due to FK restriction
...
...
test/dao-factory/find.test.js
View file @
45503e7
...
...
@@ -101,7 +101,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it
(
'doesn\'t throw an error when entering in a non integer value'
,
function
(
done
)
{
x
it
(
'doesn\'t throw an error when entering in a non integer value'
,
function
(
done
)
{
this
.
User
.
find
(
'a string value'
).
success
(
function
(
user
)
{
expect
(
user
).
to
.
be
.
null
done
()
...
...
test/sequelize.test.js
View file @
45503e7
...
...
@@ -272,7 +272,7 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
var
self
=
this
self
.
sequelize
.
query
(
this
.
insertQuery
).
success
(
function
()
{
self
.
sequelize
.
query
(
"SELECT * FROM "
+
qq
(
self
.
User
.
tableName
)
+
";"
,
self
.
User
).
success
(
function
(
users
)
{
expect
(
users
[
0
].
__factory
).
to
.
equal
(
self
.
User
)
expect
(
users
[
0
].
Model
).
to
.
equal
(
self
.
User
)
done
()
})
})
...
...
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