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 dd8bb8b4
authored
Oct 14, 2012
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
eager loading for postgresql
1 parent
aa72fcfa
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
356 additions
and
331 deletions
lib/dialects/postgres/query-generator.js
lib/dialects/postgres/query.js
spec/dao-factory.spec.js
lib/dialects/postgres/query-generator.js
View file @
dd8bb8b
...
@@ -91,7 +91,7 @@ module.exports = (function() {
...
@@ -91,7 +91,7 @@ module.exports = (function() {
dropTableQuery
:
function
(
tableName
,
options
)
{
dropTableQuery
:
function
(
tableName
,
options
)
{
options
=
options
||
{}
options
=
options
||
{}
var
query
=
"DROP TABLE IF EXISTS <%= table %>;"
var
query
=
"DROP TABLE IF EXISTS <%= table %>;"
return
Utils
.
_
.
template
(
query
)({
table
:
addQuotes
(
tableName
)
})
return
Utils
.
_
.
template
(
query
)({
table
:
addQuotes
(
tableName
)
})
},
},
renameTableQuery
:
function
(
before
,
after
)
{
renameTableQuery
:
function
(
before
,
after
)
{
...
@@ -175,7 +175,9 @@ module.exports = (function() {
...
@@ -175,7 +175,9 @@ module.exports = (function() {
},
},
selectQuery
:
function
(
tableName
,
options
)
{
selectQuery
:
function
(
tableName
,
options
)
{
options
=
options
||
{}
var
query
=
"SELECT <%= attributes %> FROM <%= table %>"
options
=
options
||
{}
options
.
table
=
Array
.
isArray
(
tableName
)
?
tableName
.
map
(
function
(
t
){
return
addQuotes
(
t
);}).
join
(
", "
)
:
addQuotes
(
tableName
)
options
.
table
=
Array
.
isArray
(
tableName
)
?
tableName
.
map
(
function
(
t
){
return
addQuotes
(
t
);}).
join
(
", "
)
:
addQuotes
(
tableName
)
options
.
attributes
=
options
.
attributes
&&
options
.
attributes
.
map
(
function
(
attr
){
options
.
attributes
=
options
.
attributes
&&
options
.
attributes
.
map
(
function
(
attr
){
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
==
2
)
{
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
==
2
)
{
...
@@ -186,27 +188,58 @@ module.exports = (function() {
...
@@ -186,27 +188,58 @@ module.exports = (function() {
return
addQuotes
(
attr
)
return
addQuotes
(
attr
)
}
}
}).
join
(
", "
)
}).
join
(
", "
)
options
.
attributes
=
options
.
attributes
||
'*'
options
.
attributes
=
options
.
attributes
||
'*'
var
query
=
"SELECT <%= attributes %> FROM <%= table %>"
if
(
options
.
include
)
{
var
tableNames
=
[
options
.
table
]
,
optAttributes
=
[
options
.
table
+
'.*'
]
for
(
var
daoName
in
options
.
include
)
{
if
(
options
.
include
.
hasOwnProperty
(
daoName
))
{
var
dao
=
options
.
include
[
daoName
]
,
_tableName
=
Utils
.
addTicks
(
dao
.
tableName
)
tableNames
.
push
(
_tableName
)
optAttributes
=
optAttributes
.
concat
(
Utils
.
_
.
keys
(
dao
.
attributes
).
map
(
function
(
attr
)
{
var
identifer
=
[
_tableName
,
Utils
.
addTicks
(
attr
)]
return
identifer
.
join
(
'.'
)
+
' AS '
+
Utils
.
addTicks
(
identifer
.
join
(
'.'
))
})
)
}
}
options
.
table
=
tableNames
.
join
(
', '
).
replace
(
/`/g
,
'"'
)
options
.
attributes
=
optAttributes
.
join
(
', '
).
replace
(
/`/g
,
'"'
)
}
if
(
options
.
where
)
{
if
(
options
.
where
)
{
options
.
where
=
QueryGenerator
.
getWhereConditions
(
options
.
where
)
options
.
where
=
QueryGenerator
.
getWhereConditions
(
options
.
where
)
query
+=
" WHERE <%= where %>"
query
+=
" WHERE <%= where %>"
}
}
if
(
options
.
order
)
{
if
(
options
.
order
)
{
options
.
order
=
options
.
order
.
replace
(
/
([^
]
+
)(
.*
)
/
,
function
(
m
,
g1
,
g2
)
{
return
addQuotes
(
g1
)
+
g2
})
options
.
order
=
options
.
order
.
replace
(
/
([^
]
+
)(
.*
)
/
,
function
(
m
,
g1
,
g2
)
{
return
addQuotes
(
g1
)
+
g2
})
query
+=
" ORDER BY <%= order %>"
query
+=
" ORDER BY <%= order %>"
}
}
if
(
options
.
group
)
{
if
(
options
.
group
)
{
options
.
group
=
addQuotes
(
options
.
group
)
options
.
group
=
addQuotes
(
options
.
group
)
query
+=
" GROUP BY <%= group %>"
query
+=
" GROUP BY <%= group %>"
}
}
if
(
options
.
limit
)
query
+=
" LIMIT <%= limit %>"
if
(
options
.
offset
)
query
+=
" OFFSET <%= offset %>"
if
(
!
(
options
.
include
&&
(
options
.
limit
===
1
)))
{
if
(
options
.
limit
)
{
query
+=
" LIMIT <%= limit %>"
}
if
(
options
.
offset
)
{
query
+=
" OFFSET <%= offset %>"
}
}
query
+=
";"
query
+=
";"
return
Utils
.
_
.
template
(
query
)(
options
)
return
Utils
.
_
.
template
(
query
)(
options
)
},
},
...
...
lib/dialects/postgres/query.js
View file @
dd8bb8b
...
@@ -17,71 +17,69 @@ module.exports = (function() {
...
@@ -17,71 +17,69 @@ module.exports = (function() {
Utils
.
inherit
(
Query
,
AbstractQuery
)
Utils
.
inherit
(
Query
,
AbstractQuery
)
Query
.
prototype
.
run
=
function
(
sql
)
{
Query
.
prototype
.
run
=
function
(
sql
)
{
var
self
=
this
this
.
sql
=
sql
this
.
sql
=
sql
if
(
this
.
options
.
logging
!==
false
)
{
if
(
this
.
options
.
logging
!==
false
)
{
this
.
options
.
logging
(
'Executing: '
+
this
.
sql
)
this
.
options
.
logging
(
'Executing: '
+
this
.
sql
)
}
}
var
results
=
[];
var
receivedError
=
false
var
receivedError
=
false
;
,
query
=
this
.
client
.
query
(
sql
)
,
rows
=
[]
var
query
=
this
.
client
.
query
(
sql
)
query
.
on
(
'row'
,
function
(
row
)
{
query
.
on
(
'row'
,
function
(
row
)
{
if
(
self
.
callee
&&
(
self
.
sql
.
indexOf
(
'INSERT INTO'
)
==
0
||
self
.
sql
.
indexOf
(
'UPDATE'
)
==
0
))
{
rows
.
push
(
row
)
Utils
.
_
.
forEach
(
row
,
function
(
value
,
key
)
{
})
self
.
callee
[
key
]
=
value
})
results
.
push
(
self
.
callee
)
}
if
(
self
.
sql
.
indexOf
(
'SELECT table_name FROM information_schema.tables'
)
==
0
)
{
query
.
on
(
'error'
,
function
(
err
)
{
results
.
push
(
Utils
.
_
.
values
(
row
))
receivedError
=
true
}
else
if
(
self
.
sql
.
indexOf
(
'SELECT relname FROM pg_class WHERE oid IN'
)
==
0
)
{
this
.
emit
(
'error'
,
err
,
this
.
callee
)
results
.
push
(
Utils
.
_
.
values
(
row
))
}.
bind
(
this
))
}
else
if
(
self
.
sql
.
indexOf
(
'SELECT'
)
==
0
)
{
// transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find)
if
(
self
.
options
.
raw
)
{
results
.
push
(
row
);
}
else
{
results
.
push
(
self
.
callee
.
build
(
row
,
{
isNewRecord
:
false
}))
}
}
else
if
((
self
.
sql
.
indexOf
(
'SHOW'
)
==
0
)
||
(
self
.
sql
.
indexOf
(
'DESCRIBE'
)
==
0
))
{
results
.
push
(
row
)
}
});
query
.
on
(
'end'
,
function
()
{
query
.
on
(
'end'
,
function
()
{
self
.
emit
(
'sql'
,
self
.
sql
)
this
.
emit
(
'sql'
,
this
.
sql
)
if
(
receivedError
)
return
;
if
(
receivedError
)
{
if
(
self
.
sql
.
indexOf
(
'SELECT'
)
==
0
)
{
return
if
(
self
.
options
.
plain
)
{
self
.
emit
(
'success'
,
(
results
.
length
==
0
)
?
null
:
results
[
0
])
}
else
{
self
.
emit
(
'success'
,
results
)
}
}
else
if
((
self
.
sql
.
indexOf
(
'SHOW'
)
==
0
)
||
(
self
.
sql
.
indexOf
(
'DESCRIBE'
)
==
0
))
{
self
.
emit
(
'success'
,
results
)
}
else
if
(
self
.
sql
.
indexOf
(
'INSERT INTO'
)
==
0
)
{
self
.
emit
(
'success'
,
results
[
0
])
}
else
if
(
self
.
sql
.
indexOf
(
'UPDATE'
)
==
0
)
{
self
.
emit
(
'success'
,
self
.
callee
)
}
else
{
self
.
emit
(
'success'
,
results
)
}
}
});
query
.
on
(
'error'
,
function
(
err
)
{
onSuccess
.
call
(
this
,
rows
)
receivedError
=
true
}.
bind
(
this
))
self
.
emit
(
'error'
,
err
,
self
.
callee
)
});
return
this
return
this
}
}
Query
.
prototype
.
getInsertIdField
=
function
()
{
return
'id'
}
var
onSuccess
=
function
(
rows
)
{
var
results
=
[]
,
isTableNameQuery
=
(
this
.
sql
.
indexOf
(
'SELECT table_name FROM information_schema.tables'
)
===
0
)
,
isRelNameQuery
=
(
this
.
sql
.
indexOf
(
'SELECT relname FROM pg_class WHERE oid IN'
)
===
0
)
if
(
isTableNameQuery
||
isRelNameQuery
)
{
return
this
.
emit
(
'success'
,
rows
.
map
(
function
(
row
)
{
return
Utils
.
_
.
values
(
row
)
}))
}
if
(
this
.
send
(
'isSelectQuery'
))
{
this
.
emit
(
'success'
,
this
.
send
(
'handleSelectQuery'
,
rows
))
}
else
if
(
this
.
send
(
'isShowOrDescribeQuery'
))
{
this
.
emit
(
'success'
,
results
)
}
else
if
(
this
.
send
(
'isInsertQuery'
))
{
for
(
var
key
in
rows
[
0
])
{
if
(
rows
[
0
].
hasOwnProperty
(
key
))
{
this
.
callee
[
key
]
=
rows
[
0
][
key
]
}
}
this
.
emit
(
'success'
,
this
.
callee
)
}
else
if
(
this
.
send
(
'isUpdateQuery'
))
{
this
.
emit
(
'success'
,
this
.
callee
)
}
else
{
this
.
emit
(
'success'
,
results
)
}
}
return
Query
return
Query
})()
})()
spec/dao-factory.spec.js
View file @
dd8bb8b
...
@@ -360,349 +360,343 @@ dialects.forEach(function(dialect) {
...
@@ -360,349 +360,343 @@ dialects.forEach(function(dialect) {
})
})
})
})
if
([
'sqlite'
,
'mysql'
].
indexOf
(
dialect
)
!==
-
1
)
{
describe
(
'association fetching'
,
function
()
{
before
(
function
()
{
describe
(
'association fetching'
,
function
()
{
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
before
(
function
()
{
title
:
Sequelize
.
STRING
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
})
title
:
Sequelize
.
STRING
})
this
.
User
=
this
.
sequelize
.
define
(
'UserWithName'
,
{
this
.
User
=
this
.
sequelize
.
define
(
'UserWithName'
,
{
name
:
Sequelize
.
STRING
name
:
Sequelize
.
STRING
})
})
})
})
it
(
'fetches associated objects for 1:1 associations (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
user
.
setTask
(
task
).
success
(
function
()
{
this
.
User
.
find
({
where
:
{
'UserWithNames.id'
:
1
},
include
:
[
'Task'
]
}).
success
(
function
(
user
)
{
expect
(
user
.
task
).
toBeDefined
()
expect
(
user
.
task
.
id
).
toEqual
(
task
.
id
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for 1:1 associations (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
user
.
setTask
(
task
).
success
(
function
()
{
this
.
Task
.
find
({
where
:
{
'Tasks.id'
:
1
},
include
:
[
'UserWithName'
]
}).
success
(
function
(
task
)
{
expect
(
task
.
userWithName
).
toBeDefined
()
expect
(
task
.
userWithName
.
id
).
toEqual
(
user
.
id
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for 1:1
associations (1st direction)'
,
function
(
done
)
{
it
(
'fetches associated objects for 1:N
associations (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
user
.
setTask
(
task
).
success
(
function
()
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
User
.
find
({
this
.
User
.
find
({
where
:
{
'UserWithNames.id'
:
1
},
where
:
{
'UserWithNames.id'
:
1
},
include
:
[
'Task'
]
include
:
[
'Task'
]
}).
success
(
function
(
user
)
{
}).
success
(
function
(
user
)
{
expect
(
user
.
task
).
toBeDefined
()
expect
(
user
.
tasks
).
toBeDefined
()
expect
(
user
.
task
.
id
).
toEqual
(
task
.
id
)
expect
(
user
.
tasks
.
map
(
function
(
t
)
{
return
t
.
id
})
).
toEqual
(
[
task1
.
id
,
task2
.
id
]
)
done
()
done
()
})
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- User.create
})
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for 1:1
associations (2nd direction)'
,
function
(
done
)
{
it
(
'fetches associated objects for 1:N
associations (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
user
.
setTask
(
task
).
success
(
function
()
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
Task
.
find
({
this
.
Task
.
find
({
where
:
{
'Tasks.id'
:
1
},
where
:
{
'Tasks.id'
:
1
},
include
:
[
'UserWithName'
]
include
:
[
'UserWithName'
]
}).
success
(
function
(
task
)
{
}).
success
(
function
(
task
)
{
expect
(
task
.
userWithName
).
toBeDefined
()
expect
(
task
.
userWithName
).
toBeDefined
()
expect
(
task
.
userWithName
.
id
).
toEqual
(
user
.
id
)
expect
(
task
.
userWithName
.
name
).
toEqual
(
user
.
name
)
done
()
done
()
})
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- User.create
})
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for 1:N associations (1st direction)'
,
function
(
done
)
{
it
(
'fetches associated objects for N:M associations (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
Task
.
hasMany
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
User
.
find
({
where
:
{
'UserWithNames.id'
:
1
},
include
:
[
'Task'
]
}).
success
(
function
(
user
)
{
expect
(
user
.
tasks
).
toBeDefined
()
expect
(
user
.
tasks
.
map
(
function
(
t
)
{
return
t
.
id
})
).
toEqual
(
[
task1
.
id
,
task2
.
id
]
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for 1:N associations (2nd direction)'
,
function
(
done
)
{
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user1
)
{
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
Task
.
find
({
where
:
{
'Tasks.id'
:
1
},
include
:
[
'UserWithName'
]
}).
success
(
function
(
task
)
{
expect
(
task
.
userWithName
).
toBeDefined
()
expect
(
task
.
userWithName
.
name
).
toEqual
(
user
.
name
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for N:M associations (1st direction)'
,
function
(
done
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
this
.
Task
.
hasMany
(
this
.
User
)
user1
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
User
.
find
({
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
where
:
{
'UserWithNames.id'
:
user1
.
id
},
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user1
)
{
include
:
[
'Task'
]
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
expect
(
user
.
tasks
).
toBeDefined
()
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
expect
(
user1
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
user
.
tasks
.
map
(
function
(
t
)
{
return
t
.
id
})
this
.
User
.
find
({
).
toEqual
(
where
:
{
'UserWithNames.id'
:
user1
.
id
},
[
task1
.
id
,
task2
.
id
]
include
:
[
'Task'
]
)
}).
success
(
function
(
user
)
{
done
()
expect
(
user
.
tasks
).
toBeDefined
()
})
expect
(
}.
bind
(
this
))
//- setTask
user
.
tasks
.
map
(
function
(
t
)
{
return
t
.
id
})
).
toEqual
(
[
task1
.
id
,
task2
.
id
]
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- sequelize.sync
})
})
it
(
'fetches associated objects for N:M associations (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
hasMany
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user1
)
{
it
(
'fetches associated objects for N:M associations (2nd direction)'
,
function
(
done
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
this
.
Task
.
hasMany
(
this
.
User
)
user1
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
Task
.
find
({
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
where
:
{
'Tasks.id'
:
task1
.
id
},
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user1
)
{
include
:
[
'UserWithName'
]
}).
success
(
function
(
task
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
expect
(
task
.
userWithNames
).
toBeDefined
()
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
expect
(
user1
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
task
.
userWithNames
.
map
(
function
(
u
)
{
return
u
.
id
})
this
.
Task
.
find
({
).
toEqual
(
where
:
{
'Tasks.id'
:
task1
.
id
},
[
user1
.
id
]
include
:
[
'UserWithName'
]
)
}).
success
(
function
(
task
)
{
done
()
expect
(
task
.
userWithNames
).
toBeDefined
()
})
expect
(
}.
bind
(
this
))
//- setTask
task
.
userWithNames
.
map
(
function
(
u
)
{
return
u
.
id
})
).
toEqual
(
[
user1
.
id
]
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- sequelize.sync
})
})
})
}
}
)
})
//- describe: find
})
//- describe: find
describe
(
'findAll'
,
function
findAll
()
{
describe
(
'findAll'
,
function
findAll
()
{
if
([
'sqlite'
,
'mysql'
].
indexOf
(
dialect
)
!==
-
1
)
{
describe
(
'association fetching'
,
function
()
{
before
(
function
()
{
describe
(
'association fetching'
,
function
()
{
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
before
(
function
()
{
title
:
Sequelize
.
STRING
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
})
title
:
Sequelize
.
STRING
})
this
.
User
=
this
.
sequelize
.
define
(
'UserWithName'
,
{
this
.
User
=
this
.
sequelize
.
define
(
'UserWithName'
,
{
name
:
Sequelize
.
STRING
name
:
Sequelize
.
STRING
})
})
})
})
it
(
'fetches associated objects for 1:1 associations (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
user
.
setTask
(
task
).
success
(
function
()
{
this
.
User
.
findAll
({
where
:
{
'UserWithNames.id'
:
1
},
include
:
[
'Task'
]
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
task
).
toBeDefined
()
expect
(
users
[
0
].
task
.
id
).
toEqual
(
task
.
id
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for 1:1 associations (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
user
.
setTask
(
task
).
success
(
function
()
{
this
.
Task
.
findAll
({
where
:
{
'Tasks.id'
:
1
},
include
:
[
'UserWithName'
]
}).
success
(
function
(
tasks
)
{
expect
(
tasks
[
0
].
userWithName
).
toBeDefined
()
expect
(
tasks
[
0
].
userWithName
.
id
).
toEqual
(
user
.
id
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for 1:1
associations (1st direction)'
,
function
(
done
)
{
it
(
'fetches associated objects for 1:N
associations (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
user
.
setTask
(
task
).
success
(
function
()
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
User
.
findAll
({
this
.
User
.
findAll
({
where
:
{
'UserWithNames.id'
:
1
},
where
:
{
'UserWithNames.id'
:
1
},
include
:
[
'Task'
]
include
:
[
'Task'
]
}).
success
(
function
(
users
)
{
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
task
).
toBeDefined
()
expect
(
users
[
0
].
tasks
).
toBeDefined
()
expect
(
users
[
0
].
task
.
id
).
toEqual
(
task
.
id
)
expect
(
users
[
0
].
tasks
.
map
(
function
(
t
)
{
return
t
.
id
})
).
toEqual
(
[
task1
.
id
,
task2
.
id
]
)
done
()
done
()
})
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- User.create
})
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for 1:1
associations (2nd direction)'
,
function
(
done
)
{
it
(
'fetches associated objects for 1:N
associations (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
user
.
setTask
(
task
).
success
(
function
()
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
Task
.
findAll
({
this
.
Task
.
findAll
({
where
:
{
'Tasks.id'
:
1
},
where
:
{
'Tasks.id'
:
1
},
include
:
[
'UserWithName'
]
include
:
[
'UserWithName'
]
}).
success
(
function
(
tasks
)
{
}).
success
(
function
(
tasks
)
{
expect
(
tasks
[
0
].
userWithName
).
toBeDefined
()
expect
(
tasks
[
0
].
userWithName
).
toBeDefined
()
expect
(
tasks
[
0
].
userWithName
.
id
).
toEqual
(
user
.
id
)
expect
(
tasks
[
0
].
userWithName
.
name
).
toEqual
(
user
.
name
)
done
()
done
()
})
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- User.create
})
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for 1:N associations (1st direction)'
,
function
(
done
)
{
it
(
'fetches associated objects for N:M associations (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
Task
.
hasMany
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
User
.
findAll
({
where
:
{
'UserWithNames.id'
:
1
},
include
:
[
'Task'
]
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
tasks
).
toBeDefined
()
expect
(
users
[
0
].
tasks
.
map
(
function
(
t
)
{
return
t
.
id
})
).
toEqual
(
[
task1
.
id
,
task2
.
id
]
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for 1:N associations (2nd direction)'
,
function
(
done
)
{
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user1
)
{
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
Task
.
findAll
({
where
:
{
'Tasks.id'
:
1
},
include
:
[
'UserWithName'
]
}).
success
(
function
(
tasks
)
{
expect
(
tasks
[
0
].
userWithName
).
toBeDefined
()
expect
(
tasks
[
0
].
userWithName
.
name
).
toEqual
(
user
.
name
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for N:M associations (1st direction)'
,
function
(
done
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
this
.
Task
.
hasMany
(
this
.
User
)
user1
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
User
.
findAll
({
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
where
:
{
'UserWithNames.id'
:
user1
.
id
},
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user1
)
{
include
:
[
'Task'
]
}).
success
(
function
(
users
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
expect
(
users
[
0
].
tasks
).
toBeDefined
()
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
expect
(
user1
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
users
[
0
].
tasks
.
map
(
function
(
t
)
{
return
t
.
id
})
this
.
User
.
findAll
({
).
toEqual
(
where
:
{
'UserWithNames.id'
:
user1
.
id
},
[
task1
.
id
,
task2
.
id
]
include
:
[
'Task'
]
)
}).
success
(
function
(
users
)
{
done
()
expect
(
users
[
0
].
tasks
).
toBeDefined
()
})
expect
(
}.
bind
(
this
))
//- setTask
users
[
0
].
tasks
.
map
(
function
(
t
)
{
return
t
.
id
})
).
toEqual
(
[
task1
.
id
,
task2
.
id
]
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- sequelize.sync
})
})
it
(
'fetches associated objects for N:M associations (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
hasMany
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user1
)
{
it
(
'fetches associated objects for N:M associations (2nd direction)'
,
function
(
done
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
this
.
Task
.
hasMany
(
this
.
User
)
user1
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
Task
.
findAll
({
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
where
:
{
'Tasks.id'
:
task1
.
id
},
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user1
)
{
include
:
[
'UserWithName'
]
}).
success
(
function
(
tasks
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
expect
(
tasks
[
0
].
userWithNames
).
toBeDefined
()
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
expect
(
user1
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
tasks
[
0
].
userWithNames
.
map
(
function
(
u
)
{
return
u
.
id
})
this
.
Task
.
findAll
({
).
toEqual
(
where
:
{
'Tasks.id'
:
task1
.
id
},
[
user1
.
id
]
include
:
[
'UserWithName'
]
)
}).
success
(
function
(
tasks
)
{
done
()
expect
(
tasks
[
0
].
userWithNames
).
toBeDefined
()
})
expect
(
}.
bind
(
this
))
//- setTask
tasks
[
0
].
userWithNames
.
map
(
function
(
u
)
{
return
u
.
id
})
).
toEqual
(
[
user1
.
id
]
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- sequelize.sync
})
})
})
}
}
)
})
//- describe: findAll
})
//- describe: findAll
describe
(
'min'
,
function
()
{
describe
(
'min'
,
function
()
{
...
...
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