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
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
129 additions
and
122 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
.
_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'
]
...
...
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
)
{
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
This diff is collapsed.
Click to expand it.
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