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 1dde34ec
authored
Jul 10, 2012
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
association options added to hasMany
1 parent
5d053be7
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
108 additions
and
11 deletions
lib/associations/has-many-double-linked.js
lib/associations/has-many-single-linked.js
lib/associations/has-many.js
spec/associations/has-many.spec.js
lib/associations/has-many-double-linked.js
View file @
1dde34e
...
...
@@ -6,11 +6,11 @@ module.exports = (function() {
this
.
instance
=
instance
}
HasManyDoubleLinked
.
prototype
.
injectGetter
=
function
()
{
var
self
=
this
HasManyDoubleLinked
.
prototype
.
injectGetter
=
function
(
options
)
{
var
self
=
this
,
_options
=
options
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
where
=
{}
var
where
=
{}
,
options
=
_options
||
{};
//fully qualify
where
[
self
.
__factory
.
connectorDAO
.
tableName
+
"."
+
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
...
...
@@ -19,7 +19,19 @@ module.exports = (function() {
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__factory
.
identifier
})[
0
]
where
[
self
.
__factory
.
connectorDAO
.
tableName
+
"."
+
foreignKey
]
=
{
join
:
self
.
__factory
.
target
.
tableName
+
".id"
}
self
.
__factory
.
target
.
findAllJoin
(
self
.
__factory
.
connectorDAO
.
tableName
,
{
where
:
where
})
if
(
options
.
where
)
{
Utils
.
_
.
each
(
options
.
where
,
function
(
value
,
index
)
{
delete
options
.
where
[
index
];
options
.
where
[
self
.
__factory
.
target
.
tableName
+
"."
+
index
]
=
value
;
});
options
.
where
=
options
.
where
?
Utils
.
merge
(
options
.
where
,
where
)
:
where
}
else
{
options
.
where
=
where
;
}
self
.
__factory
.
target
.
findAllJoin
(
self
.
__factory
.
connectorDAO
.
tableName
,
options
)
.
on
(
'success'
,
function
(
objects
)
{
customEventEmitter
.
emit
(
'success'
,
objects
)
})
.
on
(
'error'
,
function
(
err
){
customEventEmitter
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
customEventEmitter
.
emit
(
'sql'
,
sql
)})
...
...
lib/associations/has-many-single-linked.js
View file @
1dde34e
...
...
@@ -6,11 +6,13 @@ module.exports = (function() {
this
.
instance
=
instance
}
HasManySingleLinked
.
prototype
.
injectGetter
=
function
()
{
var
where
=
{}
HasManySingleLinked
.
prototype
.
injectGetter
=
function
(
options
)
{
var
where
=
{}
,
options
=
options
||
{}
where
[
this
.
__factory
.
identifier
]
=
this
.
instance
.
id
return
this
.
__factory
.
target
.
findAll
({
where
:
where
})
options
.
where
=
options
.
where
?
Utils
.
merge
(
options
.
where
,
where
)
:
where
return
this
.
__factory
.
target
.
findAll
(
options
)
}
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
...
...
lib/associations/has-many.js
View file @
1dde34e
...
...
@@ -72,9 +72,9 @@ module.exports = (function() {
HasMany
.
prototype
.
injectGetter
=
function
(
obj
)
{
var
self
=
this
obj
[
this
.
accessors
.
get
]
=
function
()
{
obj
[
this
.
accessors
.
get
]
=
function
(
options
)
{
var
Class
=
self
.
connectorDAO
?
HasManyMultiLinked
:
HasManySingleLinked
return
new
Class
(
self
,
this
).
injectGetter
()
return
new
Class
(
self
,
this
).
injectGetter
(
options
)
}
obj
[
this
.
accessors
.
hasAll
]
=
function
(
objects
)
{
...
...
spec/associations/has-many.spec.js
View file @
1dde34e
...
...
@@ -162,9 +162,91 @@ describe('HasMany', function() {
})
})
})
describe
(
"getting assocations with options"
,
function
()
{
before
(
function
(
done
)
{
var
self
=
this
;
this
.
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
this
.
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
,
active
:
Sequelize
.
BOOLEAN
})
self
.
User
.
hasMany
(
self
.
Task
)
sequelize
.
sync
({
force
:
true
}).
done
(
function
()
{
var
chainer
=
new
Sequelize
.
Utils
.
QueryChainer
([
self
.
User
.
create
({
username
:
'John'
}),
self
.
Task
.
create
({
title
:
'Get rich'
,
active
:
true
}),
self
.
Task
.
create
({
title
:
'Die trying'
,
active
:
false
})
])
chainer
.
run
().
success
(
function
(
results
,
john
,
task1
,
task2
)
{
john
.
setTasks
([
task1
,
task2
]).
success
(
done
)
})
})
})
it
(
"gets all associated objects when no options are passed"
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
username
:
'John'
}}).
success
(
function
(
john
)
{
john
.
getTasks
().
success
(
function
(
tasks
)
{
expect
(
tasks
.
length
).
toEqual
(
2
)
done
();
})
})
})
it
(
"only get objects that fullfil the options"
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
username
:
'John'
}}).
success
(
function
(
john
)
{
john
.
getTasks
({
where
:
{
active
:
true
},
limit
:
10
,
order
:
'ID DESC'
}).
success
(
function
(
tasks
)
{
expect
(
tasks
.
length
).
toEqual
(
1
)
done
();
})
})
})
})
})
describe
(
'(N:M)'
,
function
()
{
describe
(
"getting assocations with options"
,
function
()
{
before
(
function
(
done
)
{
var
self
=
this
;
this
.
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
this
.
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
,
active
:
Sequelize
.
BOOLEAN
})
self
.
User
.
hasMany
(
self
.
Task
)
self
.
Task
.
hasMany
(
self
.
User
)
sequelize
.
sync
({
force
:
true
}).
done
(
function
()
{
var
chainer
=
new
Sequelize
.
Utils
.
QueryChainer
([
self
.
User
.
create
({
username
:
'John'
}),
self
.
Task
.
create
({
title
:
'Get rich'
,
active
:
true
}),
self
.
Task
.
create
({
title
:
'Die trying'
,
active
:
false
})
])
chainer
.
run
().
success
(
function
(
results
,
john
,
task1
,
task2
)
{
john
.
setTasks
([
task1
,
task2
]).
success
(
done
)
})
})
})
it
(
"gets all associated objects when no options are passed"
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
username
:
'John'
}}).
success
(
function
(
john
)
{
john
.
getTasks
().
success
(
function
(
tasks
)
{
expect
(
tasks
.
length
).
toEqual
(
2
)
done
();
})
})
})
it
(
"only get objects that fullfil the options"
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
username
:
'John'
}}).
success
(
function
(
john
)
{
john
.
getTasks
({
where
:
{
active
:
true
}}).
success
(
function
(
tasks
)
{
expect
(
tasks
.
length
).
toEqual
(
1
)
done
();
})
})
})
})
it
(
"removes the reference id, which was added in the first place"
,
function
()
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
,
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
...
...
@@ -220,4 +302,4 @@ describe('HasMany', function() {
})
})
})
})
})
\ No newline at end of file
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