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 ecd3de33
authored
Mar 23, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enum can now be defined with Sequelize.ENUM('asd1', 'asd2', 'asd3')
1 parent
fa0167d9
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
59 additions
and
40 deletions
lib/dao.js
lib/data-types.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
spec/sequelize.spec.js
lib/dao.js
View file @
ecd3de3
...
@@ -121,7 +121,7 @@ module.exports = (function() {
...
@@ -121,7 +121,7 @@ module.exports = (function() {
for
(
var
attrName
in
this
.
daoFactory
.
rawAttributes
)
{
for
(
var
attrName
in
this
.
daoFactory
.
rawAttributes
)
{
if
(
this
.
daoFactory
.
rawAttributes
.
hasOwnProperty
(
attrName
))
{
if
(
this
.
daoFactory
.
rawAttributes
.
hasOwnProperty
(
attrName
))
{
var
definition
=
this
.
daoFactory
.
rawAttributes
[
attrName
]
var
definition
=
this
.
daoFactory
.
rawAttributes
[
attrName
]
,
isEnum
=
(
definition
.
type
===
'ENUM'
)
,
isEnum
=
(
definition
.
type
&&
(
definition
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
())
)
,
hasValue
=
(
typeof
values
[
attrName
]
!==
'undefined'
)
,
hasValue
=
(
typeof
values
[
attrName
]
!==
'undefined'
)
,
valueOutOfScope
=
((
definition
.
values
||
[]).
indexOf
(
values
[
attrName
])
===
-
1
)
,
valueOutOfScope
=
((
definition
.
values
||
[]).
indexOf
(
values
[
attrName
])
===
-
1
)
...
...
lib/data-types.js
View file @
ecd3de3
...
@@ -7,13 +7,30 @@ module.exports = {
...
@@ -7,13 +7,30 @@ module.exports = {
BOOLEAN
:
'TINYINT(1)'
,
BOOLEAN
:
'TINYINT(1)'
,
FLOAT
:
'FLOAT'
,
FLOAT
:
'FLOAT'
,
NOW
:
'NOW'
,
NOW
:
'NOW'
,
ENUM
:
'ENUM'
,
get
ENUM
()
{
var
result
=
function
()
{
return
{
type
:
'ENUM'
,
values
:
Array
.
prototype
.
slice
.
call
(
arguments
).
reduce
(
function
(
result
,
element
)
{
return
result
.
concat
(
Array
.
isArray
(
element
)
?
element
:
[
element
])
},
[])
}
}
result
.
toString
=
result
.
valueOf
=
function
()
{
return
'ENUM'
}
return
result
},
get
DECIMAL
()
{
get
DECIMAL
()
{
var
ret
=
function
(
precision
,
scale
)
{
var
result
=
function
(
precision
,
scale
)
{
return
'DECIMAL('
+
precision
+
','
+
scale
+
')'
;
return
'DECIMAL('
+
precision
+
','
+
scale
+
')'
};
}
ret
.
toString
=
ret
.
valueOf
=
function
()
{
return
'DECIMAL'
;
};
return
ret
;
result
.
toString
=
result
.
valueOf
=
function
()
{
return
'DECIMAL'
}
return
result
},
},
ARRAY
:
function
(
type
)
{
return
type
+
'[]'
}
ARRAY
:
function
(
type
)
{
return
type
+
'[]'
}
}
}
lib/dialects/mysql/query-generator.js
View file @
ecd3de3
...
@@ -378,7 +378,7 @@ module.exports = (function() {
...
@@ -378,7 +378,7 @@ module.exports = (function() {
var
template
=
"<%= type %>"
var
template
=
"<%= type %>"
,
replacements
=
{
type
:
dataType
.
type
}
,
replacements
=
{
type
:
dataType
.
type
}
if
(
dataType
.
type
===
DataTypes
.
ENUM
)
{
if
(
dataType
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
()
)
{
if
(
Array
.
isArray
(
dataType
.
values
)
&&
(
dataType
.
values
.
length
>
0
))
{
if
(
Array
.
isArray
(
dataType
.
values
)
&&
(
dataType
.
values
.
length
>
0
))
{
replacements
.
type
=
"ENUM("
+
Utils
.
_
.
map
(
dataType
.
values
,
function
(
value
)
{
replacements
.
type
=
"ENUM("
+
Utils
.
_
.
map
(
dataType
.
values
,
function
(
value
)
{
return
Utils
.
escape
(
value
)
return
Utils
.
escape
(
value
)
...
...
lib/dialects/postgres/query-generator.js
View file @
ecd3de3
...
@@ -518,7 +518,7 @@ module.exports = (function() {
...
@@ -518,7 +518,7 @@ module.exports = (function() {
var
template
=
"<%= type %>"
var
template
=
"<%= type %>"
,
replacements
=
{
type
:
dataType
.
type
}
,
replacements
=
{
type
:
dataType
.
type
}
if
(
dataType
.
type
===
DataTypes
.
ENUM
)
{
if
(
dataType
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
()
)
{
if
(
Array
.
isArray
(
dataType
.
values
)
&&
(
dataType
.
values
.
length
>
0
))
{
if
(
Array
.
isArray
(
dataType
.
values
)
&&
(
dataType
.
values
.
length
>
0
))
{
replacements
.
type
=
"ENUM("
+
Utils
.
_
.
map
(
dataType
.
values
,
function
(
value
)
{
replacements
.
type
=
"ENUM("
+
Utils
.
_
.
map
(
dataType
.
values
,
function
(
value
)
{
return
Utils
.
escape
(
value
)
return
Utils
.
escape
(
value
)
...
...
lib/dialects/sqlite/query-generator.js
View file @
ecd3de3
...
@@ -151,7 +151,7 @@ module.exports = (function() {
...
@@ -151,7 +151,7 @@ module.exports = (function() {
var
template
=
"<%= type %>"
var
template
=
"<%= type %>"
,
replacements
=
{
type
:
dataType
.
type
}
,
replacements
=
{
type
:
dataType
.
type
}
if
(
dataType
.
type
===
DataTypes
.
ENUM
)
{
if
(
dataType
.
type
.
toString
()
===
DataTypes
.
ENUM
.
toString
()
)
{
replacements
.
type
=
"TEXT"
replacements
.
type
=
"TEXT"
if
(
!
(
Array
.
isArray
(
dataType
.
values
)
&&
(
dataType
.
values
.
length
>
0
)))
{
if
(
!
(
Array
.
isArray
(
dataType
.
values
)
&&
(
dataType
.
values
.
length
>
0
)))
{
...
...
spec/sequelize.spec.js
View file @
ecd3de3
...
@@ -153,43 +153,45 @@ describe(Helpers.getTestDialectTeaser("Sequelize"), function() {
...
@@ -153,43 +153,45 @@ describe(Helpers.getTestDialectTeaser("Sequelize"), function() {
})
})
describe
(
'define'
,
function
()
{
describe
(
'define'
,
function
()
{
describe
(
'enum'
,
function
()
{
[
before
(
function
(
done
)
{
{
type
:
Helpers
.
Sequelize
.
ENUM
,
values
:
[
'scheduled'
,
'active'
,
'finished'
]},
this
.
Review
=
this
.
sequelize
.
define
(
'review'
,
{
Helpers
.
Sequelize
.
ENUM
(
'scheduled'
,
'active'
,
'finished'
)
status
:
{
type
:
Helpers
.
Sequelize
.
ENUM
,
values
:
[
'scheduled'
,
'active'
,
'finished'
]}
].
forEach
(
function
(
status
)
{
describe
(
'enum'
,
function
()
{
before
(
function
(
done
)
{
this
.
Review
=
this
.
sequelize
.
define
(
'review'
,
{
status
:
status
})
this
.
Review
.
sync
({
force
:
true
}).
success
(
done
)
})
})
this
.
Review
.
sync
({
force
:
true
}).
success
(
done
)
it
(
'raises an error if no values are defined'
,
function
()
{
})
Helpers
.
assertException
(
function
()
{
this
.
sequelize
.
define
(
'omnomnom'
,
{
it
(
'raises an error if no values are defined'
,
function
()
{
bla
:
{
type
:
Helpers
.
Sequelize
.
ENUM
}
Helpers
.
assertException
(
function
()
{
})
this
.
sequelize
.
define
(
'omnomnom'
,
{
}.
bind
(
this
),
'Values for ENUM haven\'t been defined.'
)
bla
:
{
type
:
Helpers
.
Sequelize
.
ENUM
}
})
}.
bind
(
this
),
'Values for ENUM haven\'t been defined.'
)
})
it
(
'correctly stores values'
,
function
(
done
)
{
this
.
Review
.
create
({
status
:
'active'
}).
success
(
function
(
review
)
{
expect
(
review
.
status
).
toEqual
(
'active'
)
done
()
})
})
})
it
(
'correctly loads values'
,
function
(
done
)
{
it
(
'correctly stores values'
,
function
(
done
)
{
this
.
Review
.
create
({
status
:
'active'
}).
success
(
function
()
{
this
.
Review
.
create
({
status
:
'active'
}).
success
(
function
(
review
)
{
this
.
Review
.
findAll
().
success
(
function
(
reviews
)
{
expect
(
review
.
status
).
toEqual
(
'active'
)
expect
(
reviews
[
0
].
status
).
toEqual
(
'active'
)
done
()
done
()
})
})
}.
bind
(
this
))
})
})
it
(
"doesn't save an instance if value is not in the range of enums"
,
function
()
{
it
(
'correctly loads values'
,
function
(
done
)
{
Helpers
.
assertException
(
function
()
{
this
.
Review
.
create
({
status
:
'active'
}).
success
(
function
()
{
this
.
Review
.
create
({
status
:
'fnord'
})
this
.
Review
.
findAll
().
success
(
function
(
reviews
)
{
}.
bind
(
this
),
'Value "fnord" for ENUM status is out of allowed scope. Allowed values: scheduled, active, finished'
)
expect
(
reviews
[
0
].
status
).
toEqual
(
'active'
)
done
()
})
}.
bind
(
this
))
})
it
(
"doesn't save an instance if value is not in the range of enums"
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
Review
.
create
({
status
:
'fnord'
})
}.
bind
(
this
),
'Value "fnord" for ENUM status is out of allowed scope. Allowed values: scheduled, active, finished'
)
})
})
})
})
})
})
})
...
...
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