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 40d67cae
authored
Apr 11, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update updated_at on save
1 parent
2e768918
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
3 deletions
lib/sequelize/model.js
test/Model/save.js
test/Model/values.js
lib/sequelize/model.js
View file @
40d67ca
...
@@ -14,8 +14,21 @@ var Model = module.exports = function(values, options) {
...
@@ -14,8 +14,21 @@ var Model = module.exports = function(values, options) {
// set id to null if not passed as value
// set id to null if not passed as value
// a newly created model has no id
// a newly created model has no id
if
(
!
this
.
hasOwnProperty
(
'id'
))
this
.
addAttribute
(
'id'
,
null
)
var
defaults
=
{
id
:
null
}
if
(
this
.
options
.
timestamps
)
{
defaults
[
this
.
options
.
camelcase
?
'created_at'
:
'createdAt'
]
=
new
Date
()
defaults
[
this
.
options
.
camelcase
?
'updated_at'
:
'updatedAt'
]
=
new
Date
()
if
(
this
.
options
.
safeDelete
)
defaults
[
this
.
options
.
camelcase
?
'deleted_at'
:
'deletedAt'
]
=
null
}
Utils
.
_
.
map
(
defaults
,
function
(
value
,
attr
)
{
if
(
!
self
.
hasOwnProperty
(
attr
))
self
.
addAttribute
(
attr
,
value
)
})
}
}
Utils
.
addEventEmitter
(
Model
)
Utils
.
addEventEmitter
(
Model
)
Utils
.
_
.
map
(
Mixin
.
classMethods
,
function
(
fct
,
name
)
{
Model
[
name
]
=
fct
})
Utils
.
_
.
map
(
Mixin
.
classMethods
,
function
(
fct
,
name
)
{
Model
[
name
]
=
fct
})
...
@@ -40,6 +53,11 @@ Model.prototype.query = function() {
...
@@ -40,6 +53,11 @@ Model.prototype.query = function() {
}
}
Model
.
prototype
.
save
=
function
()
{
Model
.
prototype
.
save
=
function
()
{
var
attr
=
this
.
options
.
camelcase
?
'updated_at'
:
'updatedAt'
if
(
this
.
hasOwnProperty
(
attr
))
this
[
attr
]
=
new
Date
()
if
(
this
.
isNewRecord
)
if
(
this
.
isNewRecord
)
return
this
.
query
(
QueryGenerator
.
insertQuery
(
this
.
definition
.
tableName
,
this
.
values
))
return
this
.
query
(
QueryGenerator
.
insertQuery
(
this
.
definition
.
tableName
,
this
.
values
))
else
else
...
...
test/Model/save.js
View file @
40d67ca
...
@@ -19,5 +19,26 @@ module.exports = {
...
@@ -19,5 +19,26 @@ module.exports = {
})
})
})
})
})
})
},
'save should update the timestamp updated_at'
:
function
(
exit
)
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
var
now
=
Date
.
now
()
// timeout is needed, in order to check the update of the timestamp
setTimeout
(
function
()
{
var
u
=
User
.
build
({
name
:
'foo'
,
bio
:
'bar'
})
,
uNow
=
u
.
updatedAt
assert
.
eql
(
true
,
uNow
.
getTime
()
>
now
)
setTimeout
(
function
()
{
u
.
save
().
on
(
'success'
,
function
()
{
assert
.
eql
(
true
,
uNow
.
getTime
()
<
u
.
updatedAt
)
exit
()
})
},
100
)
},
100
)
})
}
}
}
}
\ No newline at end of file
test/Model/values.js
View file @
40d67ca
...
@@ -4,7 +4,7 @@ var assert = require("assert")
...
@@ -4,7 +4,7 @@ var assert = require("assert")
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
,
sequelize
=
new
Sequelize
(
config
.
database
,
config
.
username
,
config
.
password
,
{
logging
:
false
})
var
initUsers
=
function
(
num
,
callback
)
{
var
initUsers
=
function
(
num
,
callback
)
{
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
})
var
User
=
sequelize
.
define
(
'User'
+
parseInt
(
Math
.
random
()
*
99999999
),
{
name
:
Sequelize
.
STRING
,
bio
:
Sequelize
.
TEXT
}
,
{
timestamps
:
false
}
)
,
users
=
[]
,
users
=
[]
User
.
sync
({
force
:
true
}).
on
(
'success'
,
function
()
{
User
.
sync
({
force
:
true
}).
on
(
'success'
,
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