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 349a1f78
authored
Sep 22, 2013
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use function as default value
1 parent
c503b5cf
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
0 deletions
test/dao-factory.test.js
test/dao-factory.test.js
View file @
349a1f7
...
...
@@ -344,6 +344,55 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
it
(
'is possible to use functions as default values'
,
function
(
done
)
{
var
self
=
this
,
userWithDefaults
if
(
dialect
.
indexOf
(
'postgres'
)
===
0
)
{
userWithDefaults
=
self
.
sequelize
.
define
(
'userWithDefaults'
,
{
uuid
:
{
type
:
'UUID'
,
defaultValue
:
self
.
sequelize
.
fn
(
'uuid_generate_v4'
)
}
})
userWithDefaults
.
sync
({
force
:
true
}).
success
(
function
()
{
userWithDefaults
.
create
({}).
success
(
function
(
user
)
{
// uuid validation regex taken from http://stackoverflow.com/a/13653180/800016
expect
(
user
.
uuid
).
to
.
match
(
/^
[
0-9a-f
]{8}
-
[
0-9a-f
]{4}
-
[
1-5
][
0-9a-f
]{3}
-
[
89ab
][
0-9a-f
]{3}
-
[
0-9a-f
]{12}
$/i
)
done
()
})
})
}
else
if
(
dialect
===
'sqlite'
)
{
// The definition here is a bit hacky. sqlite expects () around the expression for default values, so we call a function without a name
// to enclose the date function in (). http://www.sqlite.org/syntaxdiagrams.html#column-constraint
userWithDefaults
=
self
.
sequelize
.
define
(
'userWithDefaults'
,
{
year
:
{
type
:
Sequelize
.
STRING
,
defaultValue
:
self
.
sequelize
.
fn
(
''
,
self
.
sequelize
.
fn
(
'date'
,
'now'
))
}
})
userWithDefaults
.
sync
({
force
:
true
}).
success
(
function
()
{
userWithDefaults
.
create
({}).
success
(
function
(
user
)
{
userWithDefaults
.
find
(
user
.
id
).
success
(
function
(
user
)
{
var
now
=
new
Date
()
,
pad
=
function
(
number
)
{
if
(
number
>
9
)
{
return
number
}
return
'0'
+
number
}
expect
(
user
.
year
).
to
.
equal
(
now
.
getFullYear
()
+
'-'
+
pad
(
now
.
getMonth
()
+
1
)
+
'-'
+
pad
(
now
.
getDate
()))
done
()
})
})
})
}
else
{
// functions as default values are not supported in mysql, see http://stackoverflow.com/a/270338/800016
done
()
}
})
it
(
"casts empty arrays correctly for postgresql insert"
,
function
(
done
)
{
if
(
dialect
!==
"postgres"
&&
dialect
!==
"postgresql-native"
)
{
expect
(
''
).
to
.
equal
(
''
)
...
...
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