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 44ac75c4
authored
Aug 04, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'durango-updateAttributes-spec'
2 parents
95a23b6d
86f258c6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
16 deletions
lib/dao.js
test/dao-factory.test.js
test/dao.test.js
lib/dao.js
View file @
44ac75c
...
...
@@ -108,7 +108,7 @@ module.exports = (function() {
fields
.
push
(
updatedAtAttr
)
}
if
(
fields
.
indexOf
(
createdAtAttr
)
===
-
1
)
{
if
(
fields
.
indexOf
(
createdAtAttr
)
===
-
1
&&
this
.
isNewRecord
===
true
)
{
fields
.
push
(
createdAtAttr
)
}
}
...
...
@@ -240,26 +240,38 @@ module.exports = (function() {
var
self
=
this
var
readOnlyAttributes
=
Object
.
keys
(
this
.
__factory
.
primaryKeys
)
readOnlyAttributes
.
push
(
'id'
)
readOnlyAttributes
.
push
(
'createdAt'
)
readOnlyAttributes
.
push
(
'updatedAt'
)
readOnlyAttributes
.
push
(
'deletedAt'
)
if
(
this
.
isNewRecord
!==
true
)
{
readOnlyAttributes
.
push
(
this
.
daoFactory
.
options
.
underscored
===
true
?
'created_at'
:
'createdAt'
)
}
// readOnlyAttributes.push(this.daoFactory.options.underscored === true ? 'updated_at' : 'updatedAt')
readOnlyAttributes
.
push
(
this
.
daoFactory
.
options
.
underscored
===
true
?
'deleted_at'
:
'deletedAt'
)
var
isDirty
=
this
.
isDirty
Utils
.
_
.
each
(
updates
,
function
(
value
,
attr
)
{
var
updateAllowed
=
(
(
readOnlyAttributes
.
indexOf
(
attr
)
==
-
1
)
&&
(
readOnlyAttributes
.
indexOf
(
Utils
.
_
.
underscored
(
attr
))
==
-
1
)
&&
(
self
.
attributes
.
indexOf
(
attr
)
>
-
1
)
)
if
(
updateAllowed
)
{
if
(
Utils
.
hasChanged
(
self
[
attr
],
value
))
{
isDirty
=
true
}
self
[
attr
]
=
value
}
})
// since we're updating the record, we should be updating the updatedAt column..
if
(
this
.
daoFactory
.
options
.
timestamps
===
true
)
{
isDirty
=
true
self
[
this
.
daoFactory
.
options
.
underscored
===
true
?
'updated_at'
:
'updatedAt'
]
=
new
Date
()
}
this
.
isDirty
=
isDirty
}
...
...
test/dao-factory.test.js
View file @
44ac75c
...
...
@@ -798,6 +798,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
describe
(
'update'
,
function
()
{
it
(
'updates the attributes that we select only without updating createdAt'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User1'
,
{
username
:
Sequelize
.
STRING
,
secretValue
:
Sequelize
.
STRING
},
{
paranoid
:
true
})
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'Peter'
,
secretValue
:
'42'
}).
success
(
function
(
user
)
{
user
.
updateAttributes
({
secretValue
:
'43'
},
[
'secretValue'
]).
on
(
'sql'
,
function
(
sql
)
{
expect
(
sql
).
to
.
match
(
/UPDATE
\s
+
[
`"
]
+User1s
[
`"
]
+
\s
+SET
\s
+
[
`"
]
+secretValue
[
`"
]
='43',
[
`"
]
+updatedAt
[
`"
]
+='
[^
`",
]
+'
\s
+WHERE
[
`"
]
+id
[
`"
]
+=1/
)
done
()
})
})
})
})
it
(
'allows sql logging of updated statements'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
name
:
Sequelize
.
STRING
,
...
...
test/dao.test.js
View file @
44ac75c
...
...
@@ -169,16 +169,38 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
})
it
(
"returns
false for bulk non-changed attribute
"
,
function
(
done
)
{
it
(
"returns
true for bulk non-changed attribute + model with timestamps
"
,
function
(
done
)
{
this
.
User
.
create
({
username
:
'user'
}).
success
(
function
(
user
)
{
user
.
setAttributes
({
username
:
'user'
})
expect
(
user
.
isDirty
).
to
.
be
.
false
expect
(
user
.
isDirty
).
to
.
be
.
rue
done
()
})
})
it
(
"returns false for bulk non-changed attribute + model without timestamps"
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
10000000
),
{
username
:
DataTypes
.
STRING
},
{
timestamps
:
false
})
User
.
sync
({
force
:
true
})
.
then
(
function
()
{
return
User
.
create
({
username
:
"user"
})
})
.
then
(
function
(
user
)
{
return
user
.
setAttributes
({
username
:
"user"
})
expect
(
user
.
isDirty
).
to
.
be
.
false
})
.
then
(
function
()
{
done
()
})
})
it
(
"returns true for changed and bulk non-changed attribute"
,
function
(
done
)
{
this
.
User
.
create
({
username
:
'user'
}).
success
(
function
(
user
)
{
user
.
aNumber
=
23
...
...
@@ -1112,17 +1134,21 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
identifier
:
'identifier'
}).
success
(
function
(
user
)
{
var
oldCreatedAt
=
user
.
createdAt
,
oldUpdatedAt
=
user
.
updatedAt
,
oldIdentifier
=
user
.
identifier
user
.
updateAttributes
({
name
:
'foobar'
,
createdAt
:
new
Date
(
2000
,
1
,
1
),
identifier
:
'another identifier'
}).
success
(
function
(
user
)
{
expect
((
new
Date
(
user
.
createdAt
)).
getTime
()).
to
.
equal
((
new
Date
(
oldCreatedAt
)).
getTime
())
expect
(
user
.
identifier
).
to
.
equal
(
oldIdentifier
)
done
()
})
setTimeout
(
function
()
{
user
.
updateAttributes
({
name
:
'foobar'
,
createdAt
:
new
Date
(
2000
,
1
,
1
),
identifier
:
'another identifier'
}).
success
(
function
(
user
)
{
expect
(
new
Date
(
user
.
createdAt
)).
to
.
equalDate
(
new
Date
(
oldCreatedAt
))
expect
(
new
Date
(
user
.
updatedAt
)).
to
.
not
.
equalTime
(
new
Date
(
oldUpdatedAt
))
expect
(
user
.
identifier
).
to
.
equal
(
oldIdentifier
)
done
()
})
},
1000
)
})
})
})
...
...
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