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 a54e1c78
authored
Feb 02, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into milestones/2.0.0
2 parents
3574404d
df42db65
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
166 additions
and
26 deletions
changelog.md
lib/associations/has-many.js
lib/dao-factory.js
lib/dialects/abstract/query-generator.js
lib/dialects/mysql/query-generator.js
lib/dialects/postgres/query-generator.js
lib/dialects/sqlite/query-generator.js
lib/query-interface.js
lib/utils.js
test/associations/has-many.test.js
test/dao-factory/create.test.js
test/dao-factory/findAll.test.js
test/mysql/query-generator.test.js
test/sqlite/query-generator.test.js
changelog.md
View file @
a54e1c7
...
@@ -5,6 +5,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
...
@@ -5,6 +5,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
-
fixes various issues with limit and includes
[
#1322
](
https://github.com/sequelize/sequelize/pull/1322
)
-
fixes various issues with limit and includes
[
#1322
](
https://github.com/sequelize/sequelize/pull/1322
)
-
fixes issues with migrations/queryInterface createTable and enums
-
fixes issues with migrations/queryInterface createTable and enums
-
migration/queryInterface.addIndex() no longer fals on reserved keywords like 'from'
-
migration/queryInterface.addIndex() no longer fals on reserved keywords like 'from'
-
bulkCreate now supports a
`ignoreDuplicates`
option for MySQL, SQLite and MariaDB that will use
`INSERT IGNORE`
#### Backwards compatability changes
#### Backwards compatability changes
-
find/findAll will now always return primary keys regardless of
`attributes`
settings. (Motivation was to fix various issues with eager loading)
-
find/findAll will now always return primary keys regardless of
`attributes`
settings. (Motivation was to fix various issues with eager loading)
...
...
lib/associations/has-many.js
View file @
a54e1c7
...
@@ -19,7 +19,7 @@ module.exports = (function() {
...
@@ -19,7 +19,7 @@ module.exports = (function() {
this
.
sequelize
=
source
.
daoFactoryManager
.
sequelize
this
.
sequelize
=
source
.
daoFactoryManager
.
sequelize
this
.
through
=
options
.
through
this
.
through
=
options
.
through
this
.
isMultiAssociation
=
true
this
.
isMultiAssociation
=
true
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
===
this
.
target
.
tableName
)
this
.
isSelfAssociation
=
this
.
source
===
this
.
target
this
.
doubleLinked
=
false
this
.
doubleLinked
=
false
this
.
combinedTableName
=
Utils
.
combineTableNames
(
this
.
combinedTableName
=
Utils
.
combineTableNames
(
this
.
source
.
tableName
,
this
.
source
.
tableName
,
...
@@ -136,7 +136,7 @@ module.exports = (function() {
...
@@ -136,7 +136,7 @@ module.exports = (function() {
// is there already a single sided association between the source and the target?
// is there already a single sided association between the source and the target?
// or is the association on the model itself?
// or is the association on the model itself?
if
((
this
.
isSelfAssociation
&&
this
.
through
)
||
doubleLinked
)
{
if
((
this
.
isSelfAssociation
&&
Object
(
this
.
through
)
===
this
.
through
)
||
doubleLinked
)
{
// remove the obsolete association identifier from the source
// remove the obsolete association identifier from the source
if
(
this
.
isSelfAssociation
)
{
if
(
this
.
isSelfAssociation
)
{
this
.
foreignIdentifier
=
Utils
.
_
.
underscoredIf
((
this
.
options
.
as
||
this
.
target
.
tableName
)
+
'Id'
,
this
.
options
.
underscored
)
this
.
foreignIdentifier
=
Utils
.
_
.
underscoredIf
((
this
.
options
.
as
||
this
.
target
.
tableName
)
+
'Id'
,
this
.
options
.
underscored
)
...
...
lib/dao-factory.js
View file @
a54e1c7
...
@@ -791,7 +791,8 @@ module.exports = (function() {
...
@@ -791,7 +791,8 @@ module.exports = (function() {
options
=
Utils
.
_
.
extend
({
options
=
Utils
.
_
.
extend
({
validate
:
false
,
validate
:
false
,
hooks
:
false
hooks
:
false
,
ignoreDuplicates
:
false
},
options
||
{})
},
options
||
{})
if
(
fieldsOrOptions
instanceof
Array
)
{
if
(
fieldsOrOptions
instanceof
Array
)
{
...
@@ -801,6 +802,12 @@ module.exports = (function() {
...
@@ -801,6 +802,12 @@ module.exports = (function() {
options
=
Utils
.
_
.
extend
(
options
,
fieldsOrOptions
)
options
=
Utils
.
_
.
extend
(
options
,
fieldsOrOptions
)
}
}
if
(
this
.
daoFactoryManager
.
sequelize
.
options
.
dialect
===
'postgres'
&&
options
.
ignoreDuplicates
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
emitter
.
emit
(
'error'
,
new
Error
(
'Postgres does not support the \'ignoreDuplicates\' option.'
))
}).
run
();
}
var
self
=
this
var
self
=
this
,
updatedAtAttr
=
Utils
.
_
.
underscoredIf
(
self
.
options
.
updatedAt
,
self
.
options
.
underscored
)
,
updatedAtAttr
=
Utils
.
_
.
underscoredIf
(
self
.
options
.
updatedAt
,
self
.
options
.
underscored
)
,
createdAtAttr
=
Utils
.
_
.
underscoredIf
(
self
.
options
.
createdAt
,
self
.
options
.
underscored
)
,
createdAtAttr
=
Utils
.
_
.
underscoredIf
(
self
.
options
.
createdAt
,
self
.
options
.
underscored
)
...
...
lib/dialects/abstract/query-generator.js
View file @
a54e1c7
var
Utils
=
require
(
"../../utils"
)
var
Utils
=
require
(
"../../utils"
)
,
SqlString
=
require
(
"../../sql-string"
)
,
SqlString
=
require
(
"../../sql-string"
)
,
daoFactory
=
require
(
"../../dao-factory"
)
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
QueryGenerator
=
{
var
QueryGenerator
=
{
...
@@ -331,7 +332,14 @@ module.exports = (function() {
...
@@ -331,7 +332,14 @@ module.exports = (function() {
/*
/*
Quote an object based on its type. This is a more general version of quoteIdentifiers
Quote an object based on its type. This is a more general version of quoteIdentifiers
Strings: should proxy to quoteIdentifiers
Strings: should proxy to quoteIdentifiers
Arrays: First argument should be qouted, second argument should be append without quoting
Arrays:
* Expects array in the form: [<model> (optional), <model> (optional),... String, String (optional)]
Each <model> can be a daoFactory or an object {model: DaoFactory, as: String}, matching include
* Zero or more models can be included in the array and are used to trace a path through the tree of
included nested associations. This produces the correct table name for the ORDER BY/GROUP BY SQL
and quotes it.
* If a single string is appended to end of array, it is quoted.
If two strings appended, the 1st string is quoted, the 2nd string unquoted.
Objects:
Objects:
* If raw is set, that value should be returned verbatim, without quoting
* If raw is set, that value should be returned verbatim, without quoting
* If fn is set, the string should start with the value of fn, starting paren, followed by
* If fn is set, the string should start with the value of fn, starting paren, followed by
...
@@ -339,14 +347,71 @@ module.exports = (function() {
...
@@ -339,14 +347,71 @@ module.exports = (function() {
unless they are themselves objects
unless they are themselves objects
* If direction is set, should be prepended
* If direction is set, should be prepended
Currently this function is only used for ordering / grouping columns, but it could
Currently this function is only used for ordering / grouping columns
and Sequelize.col()
, but it could
potentially also be used for other places where we want to be able to call SQL functions (e.g. as default values)
potentially also be used for other places where we want to be able to call SQL functions (e.g. as default values)
*/
*/
quote
:
function
(
obj
,
force
)
{
quote
:
function
(
obj
,
parent
,
force
)
{
if
(
Utils
.
_
.
isString
(
obj
))
{
if
(
Utils
.
_
.
isString
(
obj
))
{
return
this
.
quoteIdentifiers
(
obj
,
force
)
return
this
.
quoteIdentifiers
(
obj
,
force
)
}
else
if
(
Array
.
isArray
(
obj
))
{
}
else
if
(
Array
.
isArray
(
obj
))
{
return
this
.
quote
(
obj
[
0
],
force
)
+
' '
+
obj
[
1
]
// loop through array, adding table names of models to quoted
// (checking associations to see if names should be singularised or not)
var
quoted
=
[]
,
i
,
len
=
obj
.
length
for
(
i
=
0
;
i
<
len
-
1
;
i
++
)
{
var
item
=
obj
[
i
]
if
(
Utils
.
_
.
isString
(
item
)
||
item
instanceof
Utils
.
fn
||
item
instanceof
Utils
.
col
||
item
instanceof
Utils
.
literal
||
item
instanceof
Utils
.
cast
||
'raw'
in
item
)
{
break
}
if
(
item
instanceof
daoFactory
)
{
item
=
{
model
:
item
}
}
// find applicable association for linking parent to this model
var
model
=
item
.
model
,
as
,
associations
=
parent
.
associations
,
association
if
(
item
.
hasOwnProperty
(
'as'
))
{
as
=
item
.
as
association
=
Utils
.
_
.
find
(
associations
,
function
(
association
,
associationName
)
{
return
association
.
target
===
model
&&
associationName
===
as
})
}
else
{
association
=
Utils
.
_
.
find
(
associations
,
function
(
association
,
associationName
)
{
return
association
.
target
===
model
?
associationName
===
(
association
.
doubleLinked
?
association
.
combinedName
:
(
association
.
isSingleAssociation
?
Utils
.
singularize
(
model
.
tableName
,
model
.
options
.
language
)
:
parent
.
tableName
+
model
.
tableName
)
)
:
association
.
targetAssociation
&&
association
.
targetAssociation
.
through
===
model
})
// NB association.target !== model clause below is to singularize names of through tables in hasMany-hasMany joins
as
=
(
association
&&
(
association
.
isSingleAssociation
||
association
.
target
!==
model
))
?
Utils
.
singularize
(
model
.
tableName
,
model
.
options
.
language
)
:
model
.
tableName
}
quoted
[
i
]
=
as
if
(
!
association
)
{
throw
new
Error
(
'\''
+
quoted
.
join
(
'.'
)
+
'\' in order / group clause is not valid association'
)
}
parent
=
model
}
// add 1st string as quoted, 2nd as unquoted raw
var
sql
=
(
i
>
0
?
this
.
quoteIdentifier
(
quoted
.
join
(
'.'
))
+
'.'
:
''
)
+
this
.
quote
(
obj
[
i
],
parent
,
force
)
if
(
i
<
len
-
1
)
{
sql
+=
' '
+
obj
[
i
+
1
]
}
return
sql
}
else
if
(
obj
instanceof
Utils
.
fn
||
obj
instanceof
Utils
.
col
||
obj
instanceof
Utils
.
literal
||
obj
instanceof
Utils
.
cast
)
{
}
else
if
(
obj
instanceof
Utils
.
fn
||
obj
instanceof
Utils
.
col
||
obj
instanceof
Utils
.
literal
||
obj
instanceof
Utils
.
cast
)
{
return
obj
.
toString
(
this
)
return
obj
.
toString
(
this
)
}
else
if
(
Utils
.
_
.
isObject
(
obj
)
&&
'raw'
in
obj
)
{
}
else
if
(
Utils
.
_
.
isObject
(
obj
)
&&
'raw'
in
obj
)
{
...
@@ -497,12 +562,12 @@ module.exports = (function() {
...
@@ -497,12 +562,12 @@ module.exports = (function() {
}
}
if
(
attr
instanceof
Utils
.
fn
||
attr
instanceof
Utils
.
col
)
{
if
(
attr
instanceof
Utils
.
fn
||
attr
instanceof
Utils
.
col
)
{
return
self
.
quote
(
attr
)
return
attr
.
toString
(
self
)
}
}
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
==
2
)
{
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
==
2
)
{
if
(
attr
[
0
]
instanceof
Utils
.
fn
||
attr
[
0
]
instanceof
Utils
.
col
)
{
if
(
attr
[
0
]
instanceof
Utils
.
fn
||
attr
[
0
]
instanceof
Utils
.
col
)
{
attr
[
0
]
=
self
.
quote
(
attr
[
0
]
)
attr
[
0
]
=
attr
[
0
].
toString
(
self
)
addTable
=
false
addTable
=
false
}
}
attr
=
[
attr
[
0
],
this
.
quoteIdentifier
(
attr
[
1
])].
join
(
' as '
)
attr
=
[
attr
[
0
],
this
.
quoteIdentifier
(
attr
[
1
])].
join
(
' as '
)
...
@@ -703,7 +768,7 @@ module.exports = (function() {
...
@@ -703,7 +768,7 @@ module.exports = (function() {
// Add GROUP BY to sub or main query
// Add GROUP BY to sub or main query
if
(
options
.
group
)
{
if
(
options
.
group
)
{
options
.
group
=
Array
.
isArray
(
options
.
group
)
?
options
.
group
.
map
(
function
(
t
)
{
return
this
.
quote
(
t
)
}.
bind
(
this
)).
join
(
', '
)
:
options
.
group
options
.
group
=
Array
.
isArray
(
options
.
group
)
?
options
.
group
.
map
(
function
(
t
)
{
return
this
.
quote
(
t
,
factory
)
}.
bind
(
this
)).
join
(
', '
)
:
options
.
group
if
(
subQuery
)
{
if
(
subQuery
)
{
subQueryItems
.
push
(
" GROUP BY "
+
options
.
group
)
subQueryItems
.
push
(
" GROUP BY "
+
options
.
group
)
}
else
{
}
else
{
...
@@ -723,7 +788,7 @@ module.exports = (function() {
...
@@ -723,7 +788,7 @@ module.exports = (function() {
// Add ORDER to sub or main query
// Add ORDER to sub or main query
if
(
options
.
order
)
{
if
(
options
.
order
)
{
options
.
order
=
Array
.
isArray
(
options
.
order
)
?
options
.
order
.
map
(
function
(
t
)
{
return
this
.
quote
(
t
)
}.
bind
(
this
)).
join
(
', '
)
:
options
.
order
options
.
order
=
Array
.
isArray
(
options
.
order
)
?
options
.
order
.
map
(
function
(
t
)
{
return
this
.
quote
(
t
,
factory
)
}.
bind
(
this
)).
join
(
', '
)
:
options
.
order
if
(
subQuery
)
{
if
(
subQuery
)
{
subQueryItems
.
push
(
" ORDER BY "
+
options
.
order
)
subQueryItems
.
push
(
" ORDER BY "
+
options
.
order
)
...
@@ -950,9 +1015,9 @@ module.exports = (function() {
...
@@ -950,9 +1015,9 @@ module.exports = (function() {
})
})
if
(
options
.
include
)
{
if
(
options
.
include
)
{
return
this
.
quoteIdentifier
(
keyParts
.
join
(
'.'
))
+
'.'
+
this
.
quote
(
attributePart
);
return
this
.
quoteIdentifier
(
keyParts
.
join
(
'.'
))
+
'.'
+
this
.
quoteIdentifiers
(
attributePart
)
}
}
return
this
.
quoteIdentifiers
(
dao
.
tableName
+
'.'
+
attributePart
);
return
this
.
quoteIdentifiers
(
dao
.
tableName
+
'.'
+
attributePart
)
},
},
getConditionalJoins
:
function
(
options
,
originalDao
){
getConditionalJoins
:
function
(
options
,
originalDao
){
...
...
lib/dialects/mysql/query-generator.js
View file @
a54e1c7
...
@@ -176,8 +176,8 @@ module.exports = (function() {
...
@@ -176,8 +176,8 @@ module.exports = (function() {
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
.
join
(
', '
)
})
return
Utils
.
_
.
template
(
query
)({
tableName
:
tableName
,
attributes
:
attrString
.
join
(
', '
)
})
},
},
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
)
{
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
,
options
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
var
query
=
"INSERT
<%= ignoreDuplicates %>
INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
,
tuples
=
[]
,
tuples
=
[]
,
allAttributes
=
[]
,
allAttributes
=
[]
...
@@ -196,6 +196,7 @@ module.exports = (function() {
...
@@ -196,6 +196,7 @@ module.exports = (function() {
}.
bind
(
this
))
}.
bind
(
this
))
var
replacements
=
{
var
replacements
=
{
ignoreDuplicates
:
options
&&
options
.
ignoreDuplicates
?
' IGNORE'
:
''
,
table
:
this
.
quoteIdentifier
(
tableName
),
table
:
this
.
quoteIdentifier
(
tableName
),
attributes
:
allAttributes
.
map
(
function
(
attr
){
attributes
:
allAttributes
.
map
(
function
(
attr
){
return
this
.
quoteIdentifier
(
attr
)
return
this
.
quoteIdentifier
(
attr
)
...
...
lib/dialects/postgres/query-generator.js
View file @
a54e1c7
...
@@ -267,7 +267,7 @@ module.exports = (function() {
...
@@ -267,7 +267,7 @@ module.exports = (function() {
})
})
},
},
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
)
{
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
,
options
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;"
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;"
,
tuples
=
[]
,
tuples
=
[]
,
serials
=
[]
,
serials
=
[]
...
...
lib/dialects/sqlite/query-generator.js
View file @
a54e1c7
...
@@ -172,8 +172,8 @@ module.exports = (function() {
...
@@ -172,8 +172,8 @@ module.exports = (function() {
return
"SELECT name FROM sqlite_master WHERE type='table' and name!='sqlite_sequence';"
return
"SELECT name FROM sqlite_master WHERE type='table' and name!='sqlite_sequence';"
},
},
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
)
{
bulkInsertQuery
:
function
(
tableName
,
attrValueHashes
,
options
)
{
var
query
=
"INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
var
query
=
"INSERT
<%= ignoreDuplicates %>
INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
,
tuples
=
[]
,
tuples
=
[]
,
allAttributes
=
[]
,
allAttributes
=
[]
...
@@ -192,6 +192,7 @@ module.exports = (function() {
...
@@ -192,6 +192,7 @@ module.exports = (function() {
}.
bind
(
this
))
}.
bind
(
this
))
var
replacements
=
{
var
replacements
=
{
ignoreDuplicates
:
options
&&
options
.
ignoreDuplicates
?
' OR IGNORE'
:
''
,
table
:
this
.
quoteIdentifier
(
tableName
),
table
:
this
.
quoteIdentifier
(
tableName
),
attributes
:
allAttributes
.
map
(
function
(
attr
){
attributes
:
allAttributes
.
map
(
function
(
attr
){
return
this
.
quoteIdentifier
(
attr
)
return
this
.
quoteIdentifier
(
attr
)
...
...
lib/query-interface.js
View file @
a54e1c7
...
@@ -510,7 +510,7 @@ module.exports = (function() {
...
@@ -510,7 +510,7 @@ module.exports = (function() {
}
}
QueryInterface
.
prototype
.
bulkInsert
=
function
(
tableName
,
records
,
options
)
{
QueryInterface
.
prototype
.
bulkInsert
=
function
(
tableName
,
records
,
options
)
{
var
sql
=
this
.
QueryGenerator
.
bulkInsertQuery
(
tableName
,
records
)
var
sql
=
this
.
QueryGenerator
.
bulkInsertQuery
(
tableName
,
records
,
options
)
return
queryAndEmit
.
call
(
this
,
[
sql
,
null
,
options
],
'bulkInsert'
)
return
queryAndEmit
.
call
(
this
,
[
sql
,
null
,
options
],
'bulkInsert'
)
}
}
...
...
lib/utils.js
View file @
a54e1c7
...
@@ -534,6 +534,9 @@ var Utils = module.exports = {
...
@@ -534,6 +534,9 @@ var Utils = module.exports = {
},
},
col
:
function
(
col
)
{
col
:
function
(
col
)
{
if
(
arguments
.
length
>
1
)
{
col
=
Array
.
prototype
.
slice
.
call
(
arguments
);
}
this
.
col
=
col
this
.
col
=
col
},
},
...
@@ -588,21 +591,25 @@ Utils.cast.prototype.toString = function(queryGenerator) {
...
@@ -588,21 +591,25 @@ Utils.cast.prototype.toString = function(queryGenerator) {
return
'CAST('
+
this
.
val
+
' AS '
+
this
.
type
.
toUpperCase
()
+
')'
return
'CAST('
+
this
.
val
+
' AS '
+
this
.
type
.
toUpperCase
()
+
')'
}
}
Utils
.
fn
.
prototype
.
toString
=
function
(
queryGenerator
)
{
Utils
.
fn
.
prototype
.
toString
=
function
(
queryGenerator
,
parentModel
)
{
return
this
.
fn
+
'('
+
this
.
args
.
map
(
function
(
arg
)
{
return
this
.
fn
+
'('
+
this
.
args
.
map
(
function
(
arg
)
{
if
(
arg
instanceof
Utils
.
fn
||
arg
instanceof
Utils
.
col
)
{
if
(
arg
instanceof
Utils
.
fn
||
arg
instanceof
Utils
.
col
)
{
return
arg
.
toString
(
queryGenerator
)
return
arg
.
toString
(
queryGenerator
,
parentModel
)
}
else
{
}
else
{
return
queryGenerator
.
escape
(
arg
)
return
queryGenerator
.
escape
(
arg
)
}
}
}).
join
(
', '
)
+
')'
}).
join
(
', '
)
+
')'
}
}
Utils
.
col
.
prototype
.
toString
=
function
(
queryGenerator
)
{
Utils
.
col
.
prototype
.
toString
=
function
(
queryGenerator
,
parentModel
)
{
if
(
this
.
col
.
indexOf
(
'*'
)
===
0
)
{
if
(
Array
.
isArray
(
this
.
col
))
{
if
(
!
parent
)
{
throw
new
Error
(
'Cannot call Sequelize.col() with array outside of order / group clause'
)
}
}
else
if
(
this
.
col
.
indexOf
(
'*'
)
===
0
)
{
return
'*'
return
'*'
}
}
return
queryGenerator
.
quote
(
this
.
col
)
return
queryGenerator
.
quote
(
this
.
col
,
parentModel
)
}
}
Utils
.
CustomEventEmitter
=
require
(
__dirname
+
"/emitters/custom-event-emitter"
)
Utils
.
CustomEventEmitter
=
require
(
__dirname
+
"/emitters/custom-event-emitter"
)
...
...
test/associations/has-many.test.js
View file @
a54e1c7
...
@@ -518,7 +518,17 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -518,7 +518,17 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
// end optimization using bulk create, destroy and update
})
// end optimization using bulk create, destroy and update
describe
(
'selfAssociations'
,
function
()
{
describe
(
'selfAssociations'
,
function
()
{
it
(
'should work'
,
function
(
done
)
{
it
(
'should work with alias'
,
function
(
done
)
{
var
Person
=
this
.
sequelize
.
define
(
'Group'
,
{})
Person
.
hasMany
(
Person
,
{
as
:
'Children'
});
this
.
sequelize
.
sync
().
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
done
()
})
})
it
(
'should work with through'
,
function
(
done
)
{
var
Group
=
this
.
sequelize
.
define
(
'Group'
,
{})
var
Group
=
this
.
sequelize
.
define
(
'Group'
,
{})
Group
.
hasMany
(
Group
,
{
through
:
'groups_outsourcing_companies'
,
as
:
'OutsourcingCompanies'
});
Group
.
hasMany
(
Group
,
{
through
:
'groups_outsourcing_companies'
,
as
:
'OutsourcingCompanies'
});
...
...
test/dao-factory/create.test.js
View file @
a54e1c7
...
@@ -24,7 +24,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -24,7 +24,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
data
:
DataTypes
.
STRING
,
data
:
DataTypes
.
STRING
,
intVal
:
DataTypes
.
INTEGER
,
intVal
:
DataTypes
.
INTEGER
,
theDate
:
DataTypes
.
DATE
,
theDate
:
DataTypes
.
DATE
,
aBool
:
DataTypes
.
BOOLEAN
aBool
:
DataTypes
.
BOOLEAN
,
uniqueName
:
{
type
:
DataTypes
.
STRING
,
unique
:
true
}
})
})
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
sync
({
force
:
true
}).
success
(
function
()
{
...
@@ -1005,6 +1006,47 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -1005,6 +1006,47 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
})
if
(
Support
.
getTestDialect
()
!==
'postgres'
)
{
it
(
"should support the ignoreDuplicates option"
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
uniqueName
:
'Peter'
,
secretValue
:
'42'
},
{
uniqueName
:
'Paul'
,
secretValue
:
'23'
}]
this
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
]
}).
success
(
function
()
{
data
.
push
({
uniqueName
:
'Michael'
,
secretValue
:
'26'
});
self
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
],
ignoreDuplicates
:
true
}).
success
(
function
()
{
self
.
User
.
findAll
({
order
:
'id'
}).
success
(
function
(
users
)
{
expect
(
users
.
length
).
to
.
equal
(
3
)
expect
(
users
[
0
].
uniqueName
).
to
.
equal
(
"Peter"
)
expect
(
users
[
0
].
secretValue
).
to
.
equal
(
"42"
);
expect
(
users
[
1
].
uniqueName
).
to
.
equal
(
"Paul"
)
expect
(
users
[
1
].
secretValue
).
to
.
equal
(
"23"
);
expect
(
users
[
2
].
uniqueName
).
to
.
equal
(
"Michael"
)
expect
(
users
[
2
].
secretValue
).
to
.
equal
(
"26"
);
done
()
});
});
})
})
}
else
{
it
(
"should throw an error when the ignoreDuplicates option is passed"
,
function
(
done
)
{
var
self
=
this
,
data
=
[{
uniqueName
:
'Peter'
,
secretValue
:
'42'
},
{
uniqueName
:
'Paul'
,
secretValue
:
'23'
}]
this
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
]
}).
success
(
function
()
{
data
.
push
({
uniqueName
:
'Michael'
,
secretValue
:
'26'
});
self
.
User
.
bulkCreate
(
data
,
{
fields
:
[
'uniqueName'
,
'secretValue'
],
ignoreDuplicates
:
true
}).
error
(
function
(
err
)
{
expect
(
err
).
to
.
exist
expect
(
err
.
message
).
to
.
match
(
/Postgres does not support the
\'
ignoreDuplicates
\'
option./
)
done
();
})
})
})
}
describe
(
'enums'
,
function
()
{
describe
(
'enums'
,
function
()
{
it
(
'correctly restores enum values'
,
function
(
done
)
{
it
(
'correctly restores enum values'
,
function
(
done
)
{
var
self
=
this
var
self
=
this
...
...
test/dao-factory/findAll.test.js
View file @
a54e1c7
This diff is collapsed.
Click to expand it.
test/mysql/query-generator.test.js
View file @
a54e1c7
...
@@ -389,6 +389,9 @@ if (Support.dialectIsMySQL()) {
...
@@ -389,6 +389,9 @@ if (Support.dialectIsMySQL()) {
},
{
},
{
arguments
:
[
'myTable'
,
[{
name
:
"foo"
,
value
:
true
},
{
name
:
'bar'
,
value
:
false
}]],
arguments
:
[
'myTable'
,
[{
name
:
"foo"
,
value
:
true
},
{
name
:
'bar'
,
value
:
false
}]],
expectation
:
"INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',true),('bar',false);"
expectation
:
"INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',true),('bar',false);"
},
{
arguments
:
[
'myTable'
,
[{
name
:
'foo'
},
{
name
:
'bar'
}],
{
ignoreDuplicates
:
true
}],
expectation
:
"INSERT IGNORE INTO `myTable` (`name`) VALUES ('foo'),('bar');"
}
}
],
],
...
...
test/sqlite/query-generator.test.js
View file @
a54e1c7
...
@@ -385,6 +385,9 @@ if (dialect === 'sqlite') {
...
@@ -385,6 +385,9 @@ if (dialect === 'sqlite') {
arguments
:
[
'myTable'
,
[{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
},
{
name
:
'bar'
,
foo
:
2
,
nullValue
:
null
}]],
arguments
:
[
'myTable'
,
[{
name
:
'foo'
,
foo
:
1
,
nullValue
:
null
},
{
name
:
'bar'
,
foo
:
2
,
nullValue
:
null
}]],
expectation
:
"INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',2,NULL);"
,
expectation
:
"INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',2,NULL);"
,
context
:
{
options
:
{
omitNull
:
true
}}
// Note: As above
context
:
{
options
:
{
omitNull
:
true
}}
// Note: As above
},
{
arguments
:
[
'myTable'
,
[{
name
:
'foo'
},
{
name
:
'bar'
}],
{
ignoreDuplicates
:
true
}],
expectation
:
"INSERT OR IGNORE INTO `myTable` (`name`) VALUES ('foo'),('bar');"
}
}
],
],
...
...
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