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 743b9973
authored
Feb 04, 2016
by
Rafis Ganeyev
Committed by
Rafis Ganeyev
Feb 06, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Set updatedAt and createdAt values before validation
1 parent
888a75a8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
12 deletions
lib/instance.js
test/integration/instance.test.js
lib/instance.js
View file @
743b997
...
@@ -525,9 +525,10 @@ Instance.prototype.save = function(options) {
...
@@ -525,9 +525,10 @@ Instance.prototype.save = function(options) {
,
updatedAtAttr
=
this
.
Model
.
_timestampAttributes
.
updatedAt
,
updatedAtAttr
=
this
.
Model
.
_timestampAttributes
.
updatedAt
,
createdAtAttr
=
this
.
Model
.
_timestampAttributes
.
createdAt
,
createdAtAttr
=
this
.
Model
.
_timestampAttributes
.
createdAt
,
hook
=
self
.
isNewRecord
?
'Create'
:
'Update'
,
hook
=
self
.
isNewRecord
?
'Create'
:
'Update'
,
wasNewRecord
=
this
.
isNewRecord
;
,
wasNewRecord
=
this
.
isNewRecord
,
now
=
Utils
.
now
(
this
.
sequelize
.
options
.
dialect
);
if
(
updatedAtAttr
&&
options
.
fields
.
indexOf
(
updatedAtAttr
)
===
-
1
)
{
if
(
updatedAtAttr
&&
options
.
fields
.
length
>=
1
&&
options
.
fields
.
indexOf
(
updatedAtAttr
)
===
-
1
)
{
options
.
fields
.
push
(
updatedAtAttr
);
options
.
fields
.
push
(
updatedAtAttr
);
}
}
...
@@ -555,6 +556,14 @@ Instance.prototype.save = function(options) {
...
@@ -555,6 +556,14 @@ Instance.prototype.save = function(options) {
}
}
}
}
if
(
updatedAtAttr
&&
!
options
.
silent
&&
options
.
fields
.
indexOf
(
updatedAtAttr
)
!==
-
1
)
{
this
.
dataValues
[
updatedAtAttr
]
=
this
.
Model
.
$getDefaultTimestamp
(
updatedAtAttr
)
||
now
;
}
if
(
this
.
isNewRecord
&&
createdAtAttr
&&
!
this
.
dataValues
[
createdAtAttr
])
{
this
.
dataValues
[
createdAtAttr
]
=
this
.
Model
.
$getDefaultTimestamp
(
createdAtAttr
)
||
now
;
}
return
Promise
.
bind
(
this
).
then
(
function
()
{
return
Promise
.
bind
(
this
).
then
(
function
()
{
// Validate
// Validate
if
(
options
.
validate
)
{
if
(
options
.
validate
)
{
...
@@ -639,16 +648,7 @@ Instance.prototype.save = function(options) {
...
@@ -639,16 +648,7 @@ Instance.prototype.save = function(options) {
var
values
=
Utils
.
mapValueFieldNames
(
this
.
dataValues
,
options
.
fields
,
this
.
Model
)
var
values
=
Utils
.
mapValueFieldNames
(
this
.
dataValues
,
options
.
fields
,
this
.
Model
)
,
query
=
null
,
query
=
null
,
args
=
[]
,
args
=
[];
,
now
=
Utils
.
now
(
this
.
sequelize
.
options
.
dialect
);
if
(
updatedAtAttr
&&
!
options
.
silent
)
{
self
.
dataValues
[
updatedAtAttr
]
=
values
[
self
.
Model
.
rawAttributes
[
updatedAtAttr
].
field
||
updatedAtAttr
]
=
self
.
Model
.
$getDefaultTimestamp
(
updatedAtAttr
)
||
now
;
}
if
(
self
.
isNewRecord
&&
createdAtAttr
&&
!
values
[
createdAtAttr
])
{
self
.
dataValues
[
createdAtAttr
]
=
values
[
self
.
Model
.
rawAttributes
[
createdAtAttr
].
field
||
createdAtAttr
]
=
self
.
Model
.
$getDefaultTimestamp
(
createdAtAttr
)
||
now
;
}
if
(
self
.
isNewRecord
)
{
if
(
self
.
isNewRecord
)
{
query
=
'insert'
;
query
=
'insert'
;
...
...
test/integration/instance.test.js
View file @
743b997
...
@@ -1172,6 +1172,9 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
...
@@ -1172,6 +1172,9 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
self
.
clock
.
tick
(
2000
);
self
.
clock
.
tick
(
2000
);
return
user
.
save
().
then
(
function
(
newlySavedUser
)
{
return
user
.
save
().
then
(
function
(
newlySavedUser
)
{
expect
(
newlySavedUser
.
updatedAt
).
to
.
equalTime
(
updatedAt
);
expect
(
newlySavedUser
.
updatedAt
).
to
.
equalTime
(
updatedAt
);
return
self
.
User
.
findOne
({
username
:
'John'
}).
then
(
function
(
newlySavedUser
)
{
expect
(
newlySavedUser
.
updatedAt
).
to
.
equalTime
(
updatedAt
);
});
});
});
});
});
});
});
...
@@ -1242,6 +1245,28 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
...
@@ -1242,6 +1245,28 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
});
});
});
});
});
});
it
(
'works with `allowNull: false` on createdAt and updatedAt columns'
,
function
()
{
var
User2
=
this
.
sequelize
.
define
(
'User2'
,
{
username
:
DataTypes
.
STRING
,
createdAt
:
{
type
:
DataTypes
.
DATE
,
allowNull
:
false
},
updatedAt
:
{
type
:
DataTypes
.
DATE
,
allowNull
:
false
}
},
{
timestamps
:
true
});
return
User2
.
sync
().
then
(
function
()
{
return
User2
.
create
({
username
:
'john doe'
}).
then
(
function
(
johnDoe
)
{
expect
(
johnDoe
.
createdAt
).
to
.
be
.
an
.
instanceof
(
Date
);
expect
(
!
isNaN
(
johnDoe
.
createdAt
.
valueOf
())
).
to
.
be
.
ok
;
expect
(
johnDoe
.
createdAt
).
to
.
equalTime
(
johnDoe
.
updatedAt
);
});
});
});
});
});
it
(
'should fail a validation upon creating'
,
function
()
{
it
(
'should fail a validation upon creating'
,
function
()
{
...
...
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