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 38be9061
authored
Oct 10, 2013
by
Thanasis Polychronakis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow functions as value for 'defaultValue' property
1 parent
f8b516da
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
37 additions
and
7 deletions
lib/dao-factory.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
lib/utils.js
lib/dao-factory.js
View file @
38be906
...
@@ -172,9 +172,8 @@ module.exports = (function() {
...
@@ -172,9 +172,8 @@ module.exports = (function() {
self
.
DAO
.
prototype
.
booleanValues
.
push
(
name
);
self
.
DAO
.
prototype
.
booleanValues
.
push
(
name
);
}
}
if
(
definition
.
hasOwnProperty
(
'defaultValue'
))
{
if
(
definition
.
hasOwnProperty
(
'defaultValue'
))
{
self
.
DAO
.
prototype
.
defaultValues
[
name
]
=
function
()
{
self
.
DAO
.
prototype
.
defaultValues
[
name
]
=
Utils
.
_
.
partial
(
return
Utils
.
toDefaultValue
(
definition
.
defaultValue
)
Utils
.
toDefaultValue
,
definition
.
defaultValue
)
}
}
}
if
(
definition
.
hasOwnProperty
(
'validate'
))
{
if
(
definition
.
hasOwnProperty
(
'validate'
))
{
...
...
lib/dialects/mysql/query-generator.js
View file @
38be906
...
@@ -529,7 +529,7 @@ module.exports = (function() {
...
@@ -529,7 +529,7 @@ module.exports = (function() {
}
}
// Blobs/texts cannot have a defaultValue
// Blobs/texts cannot have a defaultValue
if
(
dataType
.
type
!==
"TEXT"
&&
dataType
.
type
.
_binary
!==
true
&&
(
dataType
.
defaultValue
!==
undefined
)
&&
(
dataType
.
defaultValue
!=
DataTypes
.
NOW
))
{
if
(
dataType
.
type
!==
"TEXT"
&&
dataType
.
type
.
_binary
!==
true
&&
Utils
.
defaultValueSchemable
(
dataType
.
defaultValue
))
{
template
+=
" DEFAULT "
+
this
.
escape
(
dataType
.
defaultValue
)
template
+=
" DEFAULT "
+
this
.
escape
(
dataType
.
defaultValue
)
}
}
...
...
lib/dialects/postgres/query-generator.js
View file @
38be906
...
@@ -650,7 +650,10 @@ module.exports = (function() {
...
@@ -650,7 +650,10 @@ module.exports = (function() {
template
+=
" SERIAL"
template
+=
" SERIAL"
}
}
if
(
dataType
.
defaultValue
!==
undefined
)
{
if
(
Utils
.
defaultValueSchemable
(
dataType
.
defaultValue
))
{
// TODO thoroughly check that DataTypes.NOW will properly
// get populated on all databases as DEFAULT value
// i.e. mysql requires: DEFAULT CURRENT_TIMESTAMP
template
+=
" DEFAULT <%= defaultValue %>"
template
+=
" DEFAULT <%= defaultValue %>"
replacements
.
defaultValue
=
this
.
escape
(
dataType
.
defaultValue
)
replacements
.
defaultValue
=
this
.
escape
(
dataType
.
defaultValue
)
}
}
...
...
lib/dialects/sqlite/query-generator.js
View file @
38be906
...
@@ -313,7 +313,10 @@ module.exports = (function() {
...
@@ -313,7 +313,10 @@ module.exports = (function() {
template
+=
" NOT NULL"
template
+=
" NOT NULL"
}
}
if
(
dataType
.
defaultValue
!==
undefined
)
{
if
(
Utils
.
defaultValueSchemable
(
dataType
.
defaultValue
))
{
// TODO thoroughly check that DataTypes.NOW will properly
// get populated on all databases as DEFAULT value
// i.e. mysql requires: DEFAULT CURRENT_TIMESTAMP
template
+=
" DEFAULT <%= defaultValue %>"
template
+=
" DEFAULT <%= defaultValue %>"
replacements
.
defaultValue
=
this
.
escape
(
dataType
.
defaultValue
)
replacements
.
defaultValue
=
this
.
escape
(
dataType
.
defaultValue
)
}
}
...
...
lib/utils.js
View file @
38be906
...
@@ -373,7 +373,32 @@ var Utils = module.exports = {
...
@@ -373,7 +373,32 @@ var Utils = module.exports = {
},
},
toDefaultValue
:
function
(
value
)
{
toDefaultValue
:
function
(
value
)
{
return
(
value
===
DataTypes
.
NOW
)
?
Utils
.
now
()
:
value
if
(
lodash
.
isFunction
(
value
))
{
return
value
()
}
else
{
return
(
value
===
DataTypes
.
NOW
)
?
Utils
.
now
()
:
value
}
},
/**
* Determine if the default value provided exists and can be described
* in a db schema using the DEFAULT directive.
*
* @param {*} value Any default value.
* @return {boolean} yes / no.
*/
defaultValueSchemable
:
function
(
value
)
{
if
(
typeof
value
===
'undefined'
)
{
return
false
}
// TODO this will be schemable when all supported db
// have been normalized for this case
if
(
value
===
DataTypes
.
NOW
)
{
return
false
}
if
(
lodash
.
isFunction
(
value
))
{
return
false
}
return
true
},
},
setAttributes
:
function
(
hash
,
identifier
,
instance
,
prefix
)
{
setAttributes
:
function
(
hash
,
identifier
,
instance
,
prefix
)
{
...
...
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