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 2736e77c
authored
Jan 03, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BC with isDirty
1 parent
36a51ab3
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
40 additions
and
12 deletions
lib/dao-factory.js
lib/dao.js
lib/dialects/postgres/query-generator.js
test/dao.test.js
test/dao/values.test.js
lib/dao-factory.js
View file @
2736e77
...
...
@@ -171,6 +171,7 @@ module.exports = (function() {
this
.
refreshAttributes
();
this
.
DAO
.
prototype
.
booleanValues
=
[]
this
.
DAO
.
prototype
.
dateAttributes
=
[]
this
.
DAO
.
prototype
.
defaultValues
=
{}
this
.
DAO
.
prototype
.
validators
=
{}
...
...
@@ -178,6 +179,9 @@ module.exports = (function() {
if
(((
definition
===
DataTypes
.
BOOLEAN
)
||
(
definition
.
type
===
DataTypes
.
BOOLEAN
)))
{
self
.
DAO
.
prototype
.
booleanValues
.
push
(
name
);
}
if
(((
definition
===
DataTypes
.
DATE
)
||
(
definition
.
type
===
DataTypes
.
DATE
)
||
(
definition
.
originalType
===
DataTypes
.
DATE
)))
{
self
.
DAO
.
prototype
.
dateAttributes
.
push
(
name
);
}
if
(
definition
.
hasOwnProperty
(
'defaultValue'
))
{
self
.
DAO
.
prototype
.
defaultValues
[
name
]
=
Utils
.
_
.
partial
(
Utils
.
toDefaultValue
,
definition
.
defaultValue
)
...
...
@@ -188,11 +192,16 @@ module.exports = (function() {
}
})
this
.
DAO
.
prototype
.
_hasBooleanAttributes
=
!!
this
.
DAO
.
prototype
.
booleanValues
this
.
DAO
.
prototype
.
_hasBooleanAttributes
=
!!
this
.
DAO
.
prototype
.
booleanValues
.
length
this
.
DAO
.
prototype
.
_isBooleanAttribute
=
Utils
.
_
.
memoize
(
function
(
key
)
{
return
self
.
DAO
.
prototype
.
booleanValues
.
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
.
DAO
.
prototype
.
__factory
=
this
this
.
DAO
.
prototype
.
daoFactory
=
this
this
.
DAO
.
prototype
.
Model
=
this
...
...
lib/dao.js
View file @
2736e77
...
...
@@ -123,11 +123,18 @@ module.exports = (function() {
}
else
{
this
.
dataValues
=
values
}
// If raw, .changed() shouldn't be true
this
.
_previousDataValues
=
_
.
clone
(
this
.
dataValues
)
}
else
{
// Loop and call set
for
(
key
in
values
)
{
this
.
set
(
key
,
values
[
key
],
options
)
}
if
(
options
.
raw
)
{
// If raw, .changed() shouldn't be true
this
.
_previousDataValues
=
_
.
clone
(
this
.
dataValues
)
}
}
}
else
{
options
||
(
options
=
{})
...
...
@@ -154,10 +161,15 @@ module.exports = (function() {
}
// Convert boolean-ish values to booleans
if
(
this
.
_hasBooleanAttributes
&&
this
.
_isBooleanAttribute
(
key
)
&&
value
!=
null
)
{
if
(
this
.
_hasBooleanAttributes
&&
this
.
_isBooleanAttribute
(
key
)
&&
value
!=
=
null
)
{
value
=
!!
value
}
// Convert date fields to real date objects
if
(
this
.
_hasDateAttributes
&&
this
.
_isDateAttribute
(
key
)
&&
value
!==
null
&&
!
(
value
instanceof
Date
))
{
value
=
new
Date
(
value
)
}
if
(
originalValue
!==
value
)
this
.
_previousDataValues
[
key
]
=
originalValue
this
.
dataValues
[
key
]
=
value
}
...
...
@@ -167,10 +179,10 @@ module.exports = (function() {
DAO
.
prototype
.
changed
=
function
(
key
)
{
if
(
key
)
{
if
(
this
.
_
previousDataValues
[
key
]
!==
undefined
)
{
return
this
.
_previousDataValues
[
key
]
!==
this
.
dataValues
[
key
]
if
(
this
.
_
isDateAttribute
(
key
)
&&
this
.
_previousDataValues
[
key
]
&&
this
.
dataValues
[
key
]
)
{
return
this
.
_previousDataValues
[
key
]
.
valueOf
()
!==
this
.
dataValues
[
key
].
valueOf
()
}
return
false
return
this
.
_previousDataValues
[
key
]
!==
this
.
dataValues
[
key
]
}
var
changed
=
Object
.
keys
(
this
.
dataValues
).
filter
(
function
(
key
)
{
return
this
.
changed
(
key
)
...
...
@@ -363,7 +375,7 @@ module.exports = (function() {
})
args
[
2
]
=
values
self
.
set
(
values
,
{
raw
:
true
})
//
self.set(values, {raw: true})
self
.
QueryInterface
[
query
].
apply
(
self
.
QueryInterface
,
args
)
.
proxy
(
emitter
,
{
events
:
[
'sql'
,
'error'
]})
...
...
@@ -373,7 +385,8 @@ module.exports = (function() {
return
emitter
.
emit
(
'error'
,
err
)
}
result
.
_previousDataValues
=
result
.
dataValues
=
_
.
extend
(
result
.
dataValues
,
newValues
)
result
.
dataValues
=
_
.
extend
(
result
.
dataValues
,
newValues
)
result
.
_previousDataValues
=
_
.
clone
(
result
.
dataValues
)
emitter
.
emit
(
'success'
,
result
)
})
})
...
...
lib/dialects/postgres/query-generator.js
View file @
2736e77
...
...
@@ -519,6 +519,7 @@ module.exports = (function() {
}
if
(
dataType
.
type
===
"DATETIME"
)
{
dataType
.
originalType
=
"DATETIME"
dataType
.
type
=
'TIMESTAMP WITH TIME ZONE'
}
...
...
test/dao.test.js
View file @
2736e77
...
...
@@ -153,7 +153,8 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
})
it
(
"returns false for two empty attributes"
,
function
(
done
)
{
// In my opinion this is bad logic, null is different from an empty string
xit
(
"returns false for two empty attributes"
,
function
(
done
)
{
this
.
User
.
create
({
username
:
null
}).
success
(
function
(
user
)
{
user
.
username
=
''
expect
(
user
.
isDirty
).
to
.
be
.
false
...
...
test/dao/values.test.js
View file @
2736e77
...
...
@@ -137,19 +137,23 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
describe
(
'changed'
,
function
()
{
it
(
'should return false if
previous value is undefined'
,
function
(
)
{
it
(
'should return false if
object was built from database'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
{
type
:
DataTypes
.
STRING
}
})
var
user
=
User
.
build
()
user
.
set
(
'name'
,
'Mick Hansen'
)
User
.
sync
().
done
(
function
(
err
)
{
User
.
create
({
name
:
'Jan Meier'
}).
done
(
function
(
err
,
user
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
user
.
changed
(
'name'
)).
to
.
be
.
false
expect
(
user
.
changed
()).
not
.
to
.
be
.
ok
expect
(
user
.
isDirty
).
to
.
be
.
false
done
()
});
})
})
it
(
'should return true if previous value is d
efined and d
ifferent'
,
function
()
{
it
(
'should return true if previous value is different'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
{
type
:
DataTypes
.
STRING
}
})
...
...
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