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 14ca4e6e
authored
Feb 26, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mysql is working :)
1 parent
4281e799
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
551 additions
and
345 deletions
lib/dao-factory.js
lib/query-interface.js
spec/dao-factory.spec.js
lib/dao-factory.js
View file @
14ca4e6
...
...
@@ -143,23 +143,16 @@ module.exports = (function() {
var
hasJoin
=
false
var
options
=
Utils
.
_
.
clone
(
options
)
if
(
(
typeof
options
===
'object'
)
&&
(
options
.
hasOwnProperty
(
'include'
))
)
{
var
includes
=
options
.
includ
e
if
(
typeof
options
===
'object'
)
{
hasJoin
=
tru
e
hasJoin
=
true
options
.
include
=
{}
/*options.include =*/
includes
.
map
(
function
(
include
)
{
console
.
log
(
include
instanceof
DAOFactory
)
})
includes
.
forEach
(
function
(
daoName
)
{
options
.
include
[
daoName
]
=
this
.
daoFactoryManager
.
getDAO
(
daoName
)
if
(
options
.
hasOwnProperty
(
'include'
))
{
hasJoin
=
true
if
(
!
options
.
include
[
daoName
]
)
{
options
.
include
[
daoName
]
=
this
.
getAssociationByAlias
(
daoName
).
target
}
}
.
bind
(
this
))
options
.
include
=
options
.
include
.
map
(
function
(
include
)
{
return
validateIncludedElement
.
call
(
this
,
include
)
}
.
bind
(
this
))
}
// whereCollection is used for non-primary key updates
this
.
options
.
whereCollection
=
options
.
where
||
null
...
...
lib/query-interface.js
View file @
14ca4e6
...
...
@@ -198,6 +198,8 @@ module.exports = (function() {
}
QueryInterface
.
prototype
.
select
=
function
(
factory
,
tableName
,
options
,
queryOptions
)
{
options
=
options
||
{}
var
sql
=
this
.
QueryGenerator
.
selectQuery
(
tableName
,
options
)
queryOptions
=
Utils
.
_
.
extend
({},
queryOptions
,
{
include
:
options
.
include
})
return
queryAndEmit
.
call
(
this
,
[
sql
,
factory
,
queryOptions
],
'select'
)
...
...
spec/dao-factory.spec.js
View file @
14ca4e6
...
...
@@ -472,7 +472,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
this
.
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{
name
:
Sequelize
.
STRING
})
})
describe
(
'
=>
belongsTo'
,
function
()
{
describe
(
'belongsTo'
,
function
()
{
before
(
function
(
done
)
{
this
.
Task
.
belongsTo
(
this
.
Worker
)
...
...
@@ -520,7 +520,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
describe
(
'
=>
hasOne'
,
function
()
{
describe
(
'hasOne'
,
function
()
{
before
(
function
(
done
)
{
this
.
Worker
.
hasOne
(
this
.
Task
)
...
...
@@ -556,7 +556,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
describe
(
'
=>
hasOne with alias'
,
function
()
{
describe
(
'hasOne with alias'
,
function
()
{
before
(
function
(
done
)
{
this
.
Worker
.
hasOne
(
this
.
Task
,
{
as
:
'ToDo'
})
...
...
@@ -598,7 +598,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
describe
(
'
=>
hasMany'
,
function
()
{
describe
(
'hasMany'
,
function
()
{
before
(
function
(
done
)
{
this
.
Worker
.
hasMany
(
this
.
Task
)
...
...
@@ -634,7 +634,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
describe
(
'
=>
hasMany with alias'
,
function
()
{
describe
(
'hasMany with alias'
,
function
()
{
before
(
function
(
done
)
{
this
.
Worker
.
hasMany
(
this
.
Task
,
{
as
:
'ToDos'
})
...
...
@@ -1091,357 +1091,568 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
//- describe: find
describe
(
'findAll'
,
function
findAll
()
{
describe
(
'
include
'
,
function
()
{
describe
(
'
eager loading
'
,
function
()
{
before
(
function
()
{
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
this
.
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
this
.
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{
name
:
Sequelize
.
STRING
})
})
describe
(
'belongsTo'
,
function
()
{
before
(
function
(
done
)
{
this
.
Task
.
belongsTo
(
this
.
Worker
)
this
.
sequelize
.
sync
({
force
:
true
}).
complete
(
function
()
{
this
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
this
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
this
.
worker
=
worker
this
.
task
=
task
this
.
task
.
setWorker
(
this
.
worker
).
success
(
done
)
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
})
it
(
'throws an error about unexpected input if include contains a non-object'
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
Worker
.
findAll
({
include
:
[
1
]
})
}.
bind
(
this
),
'Include unexpected. Element has to be either an instance of DAOFactory or an object.'
)
})
it
(
'throws an error about missing attributes if include contains an object with daoFactory'
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
Worker
.
findAll
({
include
:
[
{
daoFactory
:
this
.
Worker
}
]
})
}.
bind
(
this
),
'Include malformed. Expected attributes: daoFactory, as!'
)
})
it
(
'throws an error if included DaoFactory is not associated'
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
Worker
.
findAll
({
include
:
[
this
.
Task
]
})
}.
bind
(
this
),
'Task is not associated to Worker!'
)
})
this
.
User
=
this
.
sequelize
.
define
(
'UserWithName'
,
{
name
:
Sequelize
.
STRING
it
(
'returns the associated worker via task.worker'
,
function
(
done
)
{
this
.
Task
.
findAll
({
where
:
{
id
:
this
.
task
.
id
},
include
:
[
this
.
Worker
]
}).
complete
(
function
(
err
,
tasks
)
{
expect
(
err
).
toBeNull
()
expect
(
tasks
).
toBeDefined
()
expect
(
tasks
[
0
].
worker
).
toBeDefined
()
expect
(
tasks
[
0
].
worker
.
name
).
toEqual
(
'worker'
)
done
()
}.
bind
(
this
))
})
})
it
(
'fetches data only for the relevant where clause'
,
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
(
user1
)
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user2
)
{
this
.
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
var
where
=
[
Sequelize
.
Utils
.
addTicks
(
this
.
User
.
tableName
)
+
".`id`=?"
,
user1
.
id
]
if
(
dialect
===
'postgres'
)
{
where
=
[
'"'
+
this
.
User
.
tableName
+
'"."id"=?'
,
user1
.
id
]
}
this
.
User
.
findAll
({
where
:
where
,
include
:
[
'Task'
]
}).
success
(
function
(
users
){
expect
(
users
.
length
).
toEqual
(
1
)
// console.log(users[0])
done
()
}.
bind
(
this
))
describe
(
'hasOne'
,
function
()
{
before
(
function
(
done
)
{
this
.
Worker
.
hasOne
(
this
.
Task
)
this
.
sequelize
.
sync
({
force
:
true
}).
complete
(
function
()
{
this
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
this
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
this
.
worker
=
worker
this
.
task
=
task
this
.
worker
.
setTask
(
this
.
task
).
success
(
done
)
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
})
})
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
(
'throws an error if included DaoFactory is not associated'
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
Task
.
findAll
({
include
:
[
this
.
Worker
]
})
}.
bind
(
this
),
'Worker is not associated to Task!'
)
})
it
(
'fetches associated objects via "as" param for 1:1 associations (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
,
{
as
:
'Homework'
})
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
.
setHomework
(
task
).
success
(
function
()
{
this
.
User
.
findAll
({
where
:
{
'UserWithNames.id'
:
1
},
include
:
[
'Homework'
]
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
homework
).
toBeDefined
()
expect
(
users
[
0
].
homework
.
id
).
toEqual
(
task
.
id
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
it
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
findAll
({
where
:
{
id
:
this
.
worker
.
id
},
include
:
[
this
.
Task
]
}).
complete
(
function
(
err
,
workers
)
{
expect
(
err
).
toBeNull
()
expect
(
workers
).
toBeDefined
()
expect
(
workers
[
0
].
task
).
toBeDefined
()
expect
(
workers
[
0
].
task
.
title
).
toEqual
(
'homework'
)
done
()
}.
bind
(
this
))
})
})
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
})
describe
(
'hasOne with alias'
,
function
()
{
before
(
function
(
done
)
{
this
.
Worker
.
hasOne
(
this
.
Task
,
{
as
:
'ToDo'
})
it
(
'fetches associated objects for 1:1 associations (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
,
{
as
:
'Owner'
})
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
:
[
'Owner'
]
}).
success
(
function
(
tasks
)
{
expect
(
tasks
[
0
].
owner
).
toBeDefined
()
expect
(
tasks
[
0
].
owner
.
id
).
toEqual
(
user
.
id
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
this
.
sequelize
.
sync
({
force
:
true
}).
complete
(
function
()
{
this
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
this
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
this
.
worker
=
worker
this
.
task
=
task
it
(
'fetches associated objects for 1:N associations (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
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
:
'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
})
this
.
worker
.
setToDo
(
this
.
task
).
success
(
done
)
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
})
it
(
'fetches associated objects for 1:N associations (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
,
{
as
:
'Homeworks'
})
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
.
setHomeworks
([
task1
,
task2
]).
success
(
function
()
{
this
.
User
.
findAll
({
where
:
{
'UserWithNames.id'
:
1
},
include
:
[
'Homeworks'
]
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
homeworks
).
toBeDefined
()
expect
(
users
[
0
].
homeworks
.
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
(
'throws an error if included DaoFactory is not referenced by alias'
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
Worker
.
findAll
({
include
:
[
this
.
Task
]
})
}.
bind
(
this
),
'Task is not associated to Worker!'
)
})
it
(
'fetches associated objects for 1:N associations (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
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
:
'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
(
'throws an error if alias is not associated'
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
Worker
.
findAll
({
include
:
[
{
daoFactory
:
this
.
Task
,
as
:
'Work'
}
]
})
}.
bind
(
this
),
'Task (Work) is not associated to Worker!'
)
})
it
(
'fetches associated objects for 1:N associations (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
,
{
as
:
'Owner'
})
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
:
[
'Owner'
]
}).
success
(
function
(
tasks
)
{
expect
(
tasks
[
0
].
owner
).
toBeDefined
()
expect
(
tasks
[
0
].
owner
.
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
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
findAll
({
where
:
{
id
:
this
.
worker
.
id
},
include
:
[
{
daoFactory
:
this
.
Task
,
as
:
'ToDo'
}
]
}).
complete
(
function
(
err
,
workers
)
{
expect
(
err
).
toBeNull
()
expect
(
workers
).
toBeDefined
()
expect
(
workers
[
0
].
toDo
).
toBeDefined
()
expect
(
workers
[
0
].
toDo
.
title
).
toEqual
(
'homework'
)
done
()
}.
bind
(
this
))
})
})
it
(
'fetches associated objects for N:M associations (1st 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
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user1
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
User
.
findAll
({
where
:
{
'UserWithNames.id'
:
user1
.
id
},
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
})
describe
(
'hasMany'
,
function
()
{
before
(
function
(
done
)
{
this
.
Worker
.
hasMany
(
this
.
Task
)
it
(
'fetches associated objects for N:M associations (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
,
{
as
:
'Homeworks'
})
this
.
Task
.
hasMany
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user1
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user1
.
setHomeworks
([
task1
,
task2
]).
success
(
function
()
{
this
.
User
.
findAll
({
where
:
{
'UserWithNames.id'
:
user1
.
id
},
include
:
[
'Homeworks'
]
}).
success
(
function
(
users
)
{
expect
(
users
[
0
].
homeworks
).
toBeDefined
()
expect
(
users
[
0
].
homeworks
.
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
})
this
.
sequelize
.
sync
({
force
:
true
}).
complete
(
function
()
{
this
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
this
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
this
.
worker
=
worker
this
.
task
=
task
this
.
worker
.
setTasks
([
this
.
task
]).
success
(
done
)
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
})
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
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user1
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
Task
.
findAll
({
where
:
{
'Tasks.id'
:
task1
.
id
},
include
:
[
'UserWithName'
]
}).
success
(
function
(
tasks
)
{
expect
(
tasks
[
0
].
userWithNames
).
toBeDefined
()
expect
(
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
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
it
(
'throws an error if included DaoFactory is not associated'
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
Task
.
findAll
({
include
:
[
this
.
Worker
]
})
}.
bind
(
this
),
'Worker is not associated to Task!'
)
})
it
(
'returns the associated tasks via worker.tasks'
,
function
(
done
)
{
this
.
Worker
.
findAll
({
where
:
{
id
:
this
.
worker
.
id
},
include
:
[
this
.
Task
]
}).
complete
(
function
(
err
,
workers
)
{
expect
(
err
).
toBeNull
()
expect
(
workers
).
toBeDefined
()
expect
(
workers
[
0
].
tasks
).
toBeDefined
()
expect
(
workers
[
0
].
tasks
[
0
].
title
).
toEqual
(
'homework'
)
done
()
}.
bind
(
this
))
})
})
it
(
'fetches associated objects for N:M associations (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
hasMany
(
this
.
User
,
{
as
:
'Owners'
})
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user1
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
user1
.
setTasks
([
task1
,
task2
]).
success
(
function
()
{
this
.
Task
.
findAll
({
where
:
{
'Tasks.id'
:
task1
.
id
},
include
:
[
'Owners'
]
}).
success
(
function
(
tasks
)
{
expect
(
tasks
[
0
].
owners
).
toBeDefined
()
expect
(
tasks
[
0
].
owners
.
map
(
function
(
u
)
{
return
u
.
id
})
).
toEqual
(
[
user1
.
id
]
)
done
()
})
}.
bind
(
this
))
//- setTask
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
describe
(
'hasMany with alias'
,
function
()
{
before
(
function
(
done
)
{
this
.
Worker
.
hasMany
(
this
.
Task
,
{
as
:
'ToDos'
})
this
.
sequelize
.
sync
({
force
:
true
}).
complete
(
function
()
{
this
.
Worker
.
create
({
name
:
'worker'
}).
success
(
function
(
worker
)
{
this
.
Task
.
create
({
title
:
'homework'
}).
success
(
function
(
task
)
{
this
.
worker
=
worker
this
.
task
=
task
this
.
worker
.
setToDos
([
this
.
task
]).
success
(
done
)
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
})
it
(
'throws an error if included DaoFactory is not referenced by alias'
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
Worker
.
findAll
({
include
:
[
this
.
Task
]
})
}.
bind
(
this
),
'Task is not associated to Worker!'
)
})
it
(
'throws an error if alias is not associated'
,
function
()
{
Helpers
.
assertException
(
function
()
{
this
.
Worker
.
findAll
({
include
:
[
{
daoFactory
:
this
.
Task
,
as
:
'Work'
}
]
})
}.
bind
(
this
),
'Task (Work) is not associated to Worker!'
)
})
it
(
'returns the associated task via worker.task'
,
function
(
done
)
{
this
.
Worker
.
findAll
({
where
:
{
id
:
this
.
worker
.
id
},
include
:
[
{
daoFactory
:
this
.
Task
,
as
:
'ToDos'
}
]
}).
complete
(
function
(
err
,
workers
)
{
expect
(
err
).
toBeNull
()
expect
(
workers
).
toBeDefined
()
expect
(
workers
[
0
].
toDos
).
toBeDefined
()
expect
(
workers
[
0
].
toDos
[
0
].
title
).
toEqual
(
'homework'
)
done
()
}.
bind
(
this
))
})
})
})
// describe('include', function() {
// before(function() {
// this.Task = this.sequelize.define('Task', {
// title: Sequelize.STRING
// })
// this.User = this.sequelize.define('UserWithName', {
// name: Sequelize.STRING
// })
// })
// it('fetches data only for the relevant where clause', 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(user1) {
// this.User.create({ name: 'barfooz' }).success(function(user2) {
// this.Task.create({ title: 'task' }).success(function(task) {
// var where = [Sequelize.Utils.addTicks(this.User.tableName) + ".`id`=?", user1.id]
// if (dialect === 'postgres') {
// where = ['"' + this.User.tableName + '"."id"=?', user1.id]
// }
// this.User.findAll({
// where: where,
// include: [ 'Task' ]
// }).success(function(users){
// expect(users.length).toEqual(1)
// // console.log(users[0])
// done()
// }.bind(this))
// }.bind(this))
// }.bind(this))
// }.bind(this))
// }.bind(this))
// })
// 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 via "as" param for 1:1 associations (1st direction)', function(done) {
// this.User.hasOne(this.Task, { as: 'Homework' })
// 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.setHomework(task).success(function() {
// this.User.findAll({
// where: { 'UserWithNames.id': 1 },
// include: [ 'Homework' ]
// }).success(function(users) {
// expect(users[0].homework).toBeDefined()
// expect(users[0].homework.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 (2nd direction)', function(done) {
// this.User.hasOne(this.Task)
// this.Task.belongsTo(this.User, { as: 'Owner' })
// 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: [ 'Owner' ]
// }).success(function(tasks) {
// expect(tasks[0].owner).toBeDefined()
// expect(tasks[0].owner.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:N associations (1st direction)', function(done) {
// this.User.hasMany(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: '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 (1st direction)', function(done) {
// this.User.hasMany(this.Task, { as: 'Homeworks' })
// 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.setHomeworks([task1, task2]).success(function() {
// this.User.findAll({
// where: { 'UserWithNames.id': 1 },
// include: [ 'Homeworks' ]
// }).success(function(users) {
// expect(users[0].homeworks).toBeDefined()
// expect(
// users[0].homeworks.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.User.hasMany(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: '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 1:N associations (2nd direction)', function(done) {
// this.User.hasMany(this.Task)
// this.Task.belongsTo(this.User, { as: 'Owner' })
// 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: [ 'Owner' ]
// }).success(function(tasks) {
// expect(tasks[0].owner).toBeDefined()
// expect(tasks[0].owner.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.User.hasMany(this.Task)
// this.Task.hasMany(this.User)
// this.sequelize.sync({ force: true }).success(function() {
// this.User.create({ name: 'barfooz' }).success(function(user1) {
// this.Task.create({ title: 'task1' }).success(function(task1) {
// this.Task.create({ title: 'task2' }).success(function(task2) {
// user1.setTasks([task1, task2]).success(function() {
// this.User.findAll({
// where: { 'UserWithNames.id': user1.id },
// 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 N:M associations (1st direction)', function(done) {
// this.User.hasMany(this.Task, { as: 'Homeworks' })
// this.Task.hasMany(this.User)
// this.sequelize.sync({ force: true }).success(function() {
// this.User.create({ name: 'barfooz' }).success(function(user1) {
// this.Task.create({ title: 'task1' }).success(function(task1) {
// this.Task.create({ title: 'task2' }).success(function(task2) {
// user1.setHomeworks([task1, task2]).success(function() {
// this.User.findAll({
// where: { 'UserWithNames.id': user1.id },
// include: [ 'Homeworks' ]
// }).success(function(users) {
// expect(users[0].homeworks).toBeDefined()
// expect(
// users[0].homeworks.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 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) {
// this.Task.create({ title: 'task1' }).success(function(task1) {
// this.Task.create({ title: 'task2' }).success(function(task2) {
// user1.setTasks([task1, task2]).success(function() {
// this.Task.findAll({
// where: { 'Tasks.id': task1.id },
// include: [ 'UserWithName' ]
// }).success(function(tasks) {
// expect(tasks[0].userWithNames).toBeDefined()
// expect(
// 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)) //- User.create
// }.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, { as: 'Owners' })
// this.sequelize.sync({ force: true }).success(function() {
// this.User.create({ name: 'barfooz' }).success(function(user1) {
// this.Task.create({ title: 'task1' }).success(function(task1) {
// this.Task.create({ title: 'task2' }).success(function(task2) {
// user1.setTasks([task1, task2]).success(function() {
// this.Task.findAll({
// where: { 'Tasks.id': task1.id },
// include: [ 'Owners' ]
// }).success(function(tasks) {
// expect(tasks[0].owners).toBeDefined()
// expect(
// tasks[0].owners.map(function(u) { return u.id })
// ).toEqual(
// [ user1.id ]
// )
// done()
// })
// }.bind(this)) //- setTask
// }.bind(this)) //- Task.create
// }.bind(this)) //- Task.create
// }.bind(this)) //- User.create
// }.bind(this)) //- sequelize.sync
// })
// })
})
//- describe: findAll
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