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 780abde3
authored
Sep 23, 2013
by
Daniel Durante
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
This commit allows you to define a defaultValue for timestamp fields.
1 parent
abd0f717
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
4 deletions
lib/dao-factory.js
lib/dao.js
test/dao-factory.test.js
lib/dao-factory.js
View file @
780abde
...
@@ -599,8 +599,13 @@ module.exports = (function() {
...
@@ -599,8 +599,13 @@ module.exports = (function() {
})
})
if
(
self
.
options
.
timestamps
)
{
if
(
self
.
options
.
timestamps
)
{
values
[
createdAtAttr
]
=
Utils
.
now
()
if
(
!
values
[
createdAtAttr
])
{
values
[
updatedAtAttr
]
=
Utils
.
now
()
values
[
createdAtAttr
]
=
Utils
.
now
(
self
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
}
if
(
!
values
[
updatedAtAttr
])
{
values
[
updatedAtAttr
]
=
Utils
.
now
(
self
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
}
}
}
records
.
push
(
values
)
records
.
push
(
values
)
...
...
lib/dao.js
View file @
780abde
...
@@ -165,7 +165,7 @@ module.exports = (function() {
...
@@ -165,7 +165,7 @@ module.exports = (function() {
}
}
if
(
this
.
__options
.
timestamps
&&
this
.
dataValues
.
hasOwnProperty
(
updatedAtAttr
))
{
if
(
this
.
__options
.
timestamps
&&
this
.
dataValues
.
hasOwnProperty
(
updatedAtAttr
))
{
this
.
dataValues
[
updatedAtAttr
]
=
values
[
updatedAtAttr
]
=
Utils
.
now
(
this
.
sequelize
.
options
.
dialect
)
this
.
dataValues
[
updatedAtAttr
]
=
values
[
updatedAtAttr
]
=
(
this
.
isNewRecord
&&
!!
this
.
daoFactory
.
rawAttributes
[
updatedAtAttr
]
&&
!!
this
.
daoFactory
.
rawAttributes
[
updatedAtAttr
].
defaultValue
?
this
.
daoFactory
.
rawAttributes
[
updatedAtAttr
].
defaultValue
:
Utils
.
now
(
this
.
sequelize
.
options
.
dialect
)
)
}
}
if
(
this
.
isNewRecord
)
{
if
(
this
.
isNewRecord
)
{
...
@@ -419,10 +419,15 @@ module.exports = (function() {
...
@@ -419,10 +419,15 @@ module.exports = (function() {
}
}
if
(
this
.
__options
.
timestamps
)
{
if
(
this
.
__options
.
timestamps
)
{
if
(
!
this
.
defaultValues
[
Utils
.
_
.
underscoredIf
(
this
.
__options
.
createdAt
,
this
.
__options
.
underscored
)])
{
defaults
[
Utils
.
_
.
underscoredIf
(
this
.
__options
.
createdAt
,
this
.
__options
.
underscored
)]
=
Utils
.
now
(
this
.
sequelize
.
options
.
dialect
)
defaults
[
Utils
.
_
.
underscoredIf
(
this
.
__options
.
createdAt
,
this
.
__options
.
underscored
)]
=
Utils
.
now
(
this
.
sequelize
.
options
.
dialect
)
}
if
(
!
this
.
defaultValues
[
Utils
.
_
.
underscoredIf
(
this
.
__options
.
updatedAt
,
this
.
__options
.
underscored
)])
{
defaults
[
Utils
.
_
.
underscoredIf
(
this
.
__options
.
updatedAt
,
this
.
__options
.
underscored
)]
=
Utils
.
now
(
this
.
sequelize
.
options
.
dialect
)
defaults
[
Utils
.
_
.
underscoredIf
(
this
.
__options
.
updatedAt
,
this
.
__options
.
underscored
)]
=
Utils
.
now
(
this
.
sequelize
.
options
.
dialect
)
}
if
(
this
.
__options
.
paranoid
)
{
if
(
this
.
__options
.
paranoid
&&
!
this
.
defaultValues
[
Utils
.
_
.
underscoredIf
(
this
.
__options
.
deletedAt
,
this
.
__options
.
underscored
)]
)
{
defaults
[
Utils
.
_
.
underscoredIf
(
this
.
__options
.
deletedAt
,
this
.
__options
.
underscored
)]
=
null
defaults
[
Utils
.
_
.
underscoredIf
(
this
.
__options
.
deletedAt
,
this
.
__options
.
underscored
)]
=
null
}
}
}
}
...
...
test/dao-factory.test.js
View file @
780abde
...
@@ -108,6 +108,39 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -108,6 +108,39 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
done
()
done
()
})
})
it
(
'should allow me to set a default value for createdAt and updatedAt'
,
function
(
done
)
{
var
UserTable
=
this
.
sequelize
.
define
(
'UserCol'
,
{
aNumber
:
Sequelize
.
INTEGER
,
createdAt
:
{
type
:
Sequelize
.
DATE
,
defaultValue
:
moment
(
'2012-01-01'
).
toDate
()
},
updatedAt
:
{
type
:
Sequelize
.
DATE
,
defaultValue
:
moment
(
'2012-01-02'
).
toDate
()
}
},
{
timestamps
:
true
})
UserTable
.
sync
({
force
:
true
}).
success
(
function
()
{
UserTable
.
create
({
aNumber
:
5
}).
success
(
function
(
user
)
{
UserTable
.
bulkCreate
([
{
aNumber
:
10
},
{
aNumber
:
12
}
]).
success
(
function
()
{
UserTable
.
all
({
where
:
{
aNumber
:
{
gte
:
10
}}}).
success
(
function
(
users
)
{
expect
(
moment
(
user
.
createdAt
).
format
(
'YYYY-MM-DD'
)).
to
.
equal
(
'2012-01-01'
)
expect
(
moment
(
user
.
updatedAt
).
format
(
'YYYY-MM-DD'
)).
to
.
equal
(
'2012-01-02'
)
users
.
forEach
(
function
(
u
)
{
expect
(
moment
(
u
.
createdAt
).
format
(
'YYYY-MM-DD'
)).
to
.
equal
(
'2012-01-01'
)
expect
(
moment
(
u
.
updatedAt
).
format
(
'YYYY-MM-DD'
)).
to
.
equal
(
'2012-01-02'
)
})
done
()
})
})
})
})
})
it
(
'should allow me to override updatedAt, createdAt, and deletedAt fields'
,
function
(
done
)
{
it
(
'should allow me to override updatedAt, createdAt, and deletedAt fields'
,
function
(
done
)
{
var
UserTable
=
this
.
sequelize
.
define
(
'UserCol'
,
{
var
UserTable
=
this
.
sequelize
.
define
(
'UserCol'
,
{
aNumber
:
Sequelize
.
INTEGER
aNumber
:
Sequelize
.
INTEGER
...
...
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