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 7673b2fa
authored
Jan 07, 2014
by
Sascha Gehlich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added HasMany#injectCreator and Model.createAssociatedModel()
1 parent
e1031475
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
81 additions
and
10 deletions
lib/associations/has-many.js
lib/associations/mixin.js
test/associations/has-many.test.js
lib/associations/has-many.js
View file @
7673b2f
...
@@ -4,7 +4,7 @@ var Utils = require("./../utils")
...
@@ -4,7 +4,7 @@ var Utils = require("./../utils")
,
_
=
require
(
'lodash'
)
,
_
=
require
(
'lodash'
)
var
HasManySingleLinked
=
require
(
"./has-many-single-linked"
)
var
HasManySingleLinked
=
require
(
"./has-many-single-linked"
)
,
HasMany
Multi
Linked
=
require
(
"./has-many-double-linked"
)
,
HasMany
Double
Linked
=
require
(
"./has-many-double-linked"
)
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
HasMany
=
function
(
source
,
target
,
options
)
{
var
HasMany
=
function
(
source
,
target
,
options
)
{
...
@@ -50,7 +50,7 @@ module.exports = (function() {
...
@@ -50,7 +50,7 @@ module.exports = (function() {
this
.
associationAccessor
=
this
.
options
.
as
this
.
associationAccessor
=
this
.
options
.
as
if
(
!
this
.
associationAccessor
&&
(
typeof
this
.
through
===
"string"
||
Object
(
this
.
through
)
===
this
.
through
))
{
if
(
!
this
.
associationAccessor
&&
(
typeof
this
.
through
===
"string"
||
Object
(
this
.
through
)
===
this
.
through
))
{
this
.
associationAccessor
=
this
.
through
.
tableName
||
this
.
through
this
.
associationAccessor
=
this
.
through
.
tableName
||
this
.
through
}
}
else
if
(
!
this
.
associationAccessor
)
{
else
if
(
!
this
.
associationAccessor
)
{
this
.
associationAccessor
=
this
.
combinedTableName
this
.
associationAccessor
=
this
.
combinedTableName
}
}
...
@@ -82,7 +82,7 @@ module.exports = (function() {
...
@@ -82,7 +82,7 @@ module.exports = (function() {
if
(
paired
)
{
if
(
paired
)
{
self
.
doubleLinked
=
true
self
.
doubleLinked
=
true
association
.
doubleLinked
=
true
association
.
doubleLinked
=
true
self
.
targetAssociation
=
association
self
.
targetAssociation
=
association
association
.
targetAssociation
=
self
association
.
targetAssociation
=
self
}
}
...
@@ -115,6 +115,7 @@ module.exports = (function() {
...
@@ -115,6 +115,7 @@ module.exports = (function() {
get
:
Utils
.
_
.
camelize
(
'get_'
+
as
),
get
:
Utils
.
_
.
camelize
(
'get_'
+
as
),
set
:
Utils
.
_
.
camelize
(
'set_'
+
as
),
set
:
Utils
.
_
.
camelize
(
'set_'
+
as
),
add
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'add_'
+
as
,
this
.
target
.
options
.
language
)),
add
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'add_'
+
as
,
this
.
target
.
options
.
language
)),
create
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'create_'
+
as
,
this
.
target
.
options
.
language
)),
remove
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'remove_'
+
as
,
this
.
target
.
options
.
language
)),
remove
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'remove_'
+
as
,
this
.
target
.
options
.
language
)),
hasSingle
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'has_'
+
as
,
this
.
target
.
options
.
language
)),
hasSingle
:
Utils
.
_
.
camelize
(
Utils
.
singularize
(
'has_'
+
as
,
this
.
target
.
options
.
language
)),
hasAll
:
Utils
.
_
.
camelize
(
'has_'
+
as
)
hasAll
:
Utils
.
_
.
camelize
(
'has_'
+
as
)
...
@@ -188,7 +189,7 @@ module.exports = (function() {
...
@@ -188,7 +189,7 @@ module.exports = (function() {
var
self
=
this
var
self
=
this
obj
[
this
.
accessors
.
get
]
=
function
(
options
)
{
obj
[
this
.
accessors
.
get
]
=
function
(
options
)
{
var
Class
=
Object
(
self
.
through
)
===
self
.
through
?
HasMany
Multi
Linked
:
HasManySingleLinked
var
Class
=
Object
(
self
.
through
)
===
self
.
through
?
HasMany
Double
Linked
:
HasManySingleLinked
return
new
Class
(
self
,
this
).
injectGetter
(
options
)
return
new
Class
(
self
,
this
).
injectGetter
(
options
)
}
}
...
@@ -246,7 +247,7 @@ module.exports = (function() {
...
@@ -246,7 +247,7 @@ module.exports = (function() {
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
]()
instance
[
self
.
accessors
.
get
]()
.
success
(
function
(
oldAssociatedObjects
)
{
.
success
(
function
(
oldAssociatedObjects
)
{
var
Class
=
Object
(
self
.
through
)
===
self
.
through
?
HasMany
Multi
Linked
:
HasManySingleLinked
var
Class
=
Object
(
self
.
through
)
===
self
.
through
?
HasMany
Double
Linked
:
HasManySingleLinked
new
Class
(
self
,
instance
).
injectSetter
(
emitter
,
oldAssociatedObjects
,
newAssociatedObjects
,
defaultAttributes
)
new
Class
(
self
,
instance
).
injectSetter
(
emitter
,
oldAssociatedObjects
,
newAssociatedObjects
,
defaultAttributes
)
})
})
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]})
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]})
...
@@ -266,7 +267,7 @@ module.exports = (function() {
...
@@ -266,7 +267,7 @@ module.exports = (function() {
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]})
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]})
.
success
(
function
(
currentAssociatedObjects
)
{
.
success
(
function
(
currentAssociatedObjects
)
{
if
(
currentAssociatedObjects
.
length
===
0
||
Object
(
self
.
through
)
===
self
.
through
)
{
if
(
currentAssociatedObjects
.
length
===
0
||
Object
(
self
.
through
)
===
self
.
through
)
{
var
Class
=
Object
(
self
.
through
)
===
self
.
through
?
HasMany
Multi
Linked
:
HasManySingleLinked
var
Class
=
Object
(
self
.
through
)
===
self
.
through
?
HasMany
Double
Linked
:
HasManySingleLinked
new
Class
(
self
,
instance
).
injectAdder
(
emitter
,
newAssociatedObject
,
additionalAttributes
,
!!
currentAssociatedObjects
.
length
)
new
Class
(
self
,
instance
).
injectAdder
(
emitter
,
newAssociatedObject
,
additionalAttributes
,
!!
currentAssociatedObjects
.
length
)
}
else
{
}
else
{
emitter
.
emit
(
'success'
,
newAssociatedObject
);
emitter
.
emit
(
'success'
,
newAssociatedObject
);
...
@@ -323,6 +324,26 @@ module.exports = (function() {
...
@@ -323,6 +324,26 @@ module.exports = (function() {
return
this
return
this
}
}
HasMany
.
prototype
.
injectCreator
=
function
(
obj
)
{
var
self
=
this
obj
[
this
.
accessors
.
create
]
=
function
(
values
,
fieldsOrOptions
)
{
var
instance
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
// Create the related model instance
self
.
target
.
create
(
values
,
fieldsOrOptions
)
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]
})
.
success
(
function
(
newAssociatedObject
)
{
instance
[
self
.
accessors
.
add
](
newAssociatedObject
)
.
proxy
(
emitter
)
})
}).
run
()
}
return
this
};
/**
/**
* The method checks if it is ok to delete the previously defined foreign key.
* The method checks if it is ok to delete the previously defined foreign key.
* This is done because we need to keep the foreign key if another association
* This is done because we need to keep the foreign key if another association
...
...
lib/associations/mixin.js
View file @
7673b2f
...
@@ -50,6 +50,7 @@ Mixin.hasMany = function(associatedDAOFactory, options) {
...
@@ -50,6 +50,7 @@ Mixin.hasMany = function(associatedDAOFactory, options) {
association
.
injectGetter
(
this
.
DAO
.
prototype
)
association
.
injectGetter
(
this
.
DAO
.
prototype
)
association
.
injectSetter
(
this
.
DAO
.
prototype
)
association
.
injectSetter
(
this
.
DAO
.
prototype
)
association
.
injectCreator
(
this
.
DAO
.
prototype
)
return
this
return
this
}
}
...
...
test/associations/has-many.test.js
View file @
7673b2f
...
@@ -295,6 +295,31 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -295,6 +295,31 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
})
})
describe
(
'createAssociations'
,
function
()
{
it
(
'creates a new associated object'
,
function
(
done
)
{
var
Article
=
this
.
sequelize
.
define
(
'Article'
,
{
'title'
:
DataTypes
.
STRING
})
,
Label
=
this
.
sequelize
.
define
(
'Label'
,
{
'text'
:
DataTypes
.
STRING
})
Article
.
hasMany
(
Label
)
Article
.
sync
({
force
:
true
}).
success
(
function
()
{
Label
.
sync
({
force
:
true
}).
success
(
function
()
{
Article
.
create
({
title
:
'foo'
}).
success
(
function
(
article
)
{
article
.
createLabel
({
text
:
'bar'
}).
success
(
function
(
label
)
{
Label
.
findAll
({
where
:
{
ArticleId
:
article
.
id
}})
.
success
(
function
(
labels
)
{
expect
(
labels
.
length
).
to
.
equal
(
1
)
done
()
})
})
})
})
})
})
})
describe
(
"getting assocations with options"
,
function
()
{
describe
(
"getting assocations with options"
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
(
done
)
{
var
self
=
this
var
self
=
this
...
@@ -620,6 +645,30 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -620,6 +645,30 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
})
})
describe
(
'createAssociations'
,
function
()
{
it
(
'creates a new associated object'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
DataTypes
.
STRING
})
User
.
hasMany
(
Task
)
Task
.
hasMany
(
User
)
User
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
task
.
createUser
({
username
:
'foo'
}).
success
(
function
()
{
task
.
getUsers
().
success
(
function
(
_users
)
{
expect
(
_users
).
to
.
have
.
length
(
1
)
done
()
})
})
})
})
})
})
})
describe
(
'optimizations using bulk create, destroy and update'
,
function
()
{
describe
(
'optimizations using bulk create, destroy and update'
,
function
()
{
beforeEach
(
function
(
done
)
{
beforeEach
(
function
(
done
)
{
var
self
=
this
var
self
=
this
...
@@ -820,7 +869,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -820,7 +869,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
Worker
.
hasMany
(
Task
,
{
through
:
WorkerTasks
})
Worker
.
hasMany
(
Task
,
{
through
:
WorkerTasks
})
Task
.
hasMany
(
Worker
,
{
through
:
WorkerTasks
})
Task
.
hasMany
(
Worker
,
{
through
:
WorkerTasks
})
this
.
sequelize
.
sync
().
done
(
function
(
err
)
{
this
.
sequelize
.
sync
().
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
err
).
not
.
to
.
be
.
ok
Worker
.
create
().
done
(
function
(
err
,
worker
)
{
Worker
.
create
().
done
(
function
(
err
,
worker
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
err
).
not
.
to
.
be
.
ok
...
@@ -878,7 +927,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -878,7 +927,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
Worker
.
hasMany
(
Task
,
{
through
:
WorkerTasks
})
Worker
.
hasMany
(
Task
,
{
through
:
WorkerTasks
})
Task
.
hasMany
(
Worker
,
{
through
:
WorkerTasks
})
Task
.
hasMany
(
Worker
,
{
through
:
WorkerTasks
})
this
.
sequelize
.
sync
().
done
(
function
(
err
)
{
this
.
sequelize
.
sync
().
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
err
).
not
.
to
.
be
.
ok
Worker
.
create
().
done
(
function
(
err
,
worker
)
{
Worker
.
create
().
done
(
function
(
err
,
worker
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
err
).
not
.
to
.
be
.
ok
...
@@ -909,7 +958,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -909,7 +958,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
Worker
.
hasMany
(
Task
,
{
through
:
WorkerTasks
})
Worker
.
hasMany
(
Task
,
{
through
:
WorkerTasks
})
Task
.
hasMany
(
Worker
,
{
through
:
WorkerTasks
})
Task
.
hasMany
(
Worker
,
{
through
:
WorkerTasks
})
this
.
sequelize
.
sync
().
done
(
function
(
err
)
{
this
.
sequelize
.
sync
().
done
(
function
(
err
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
err
).
not
.
to
.
be
.
ok
Worker
.
create
({}).
done
(
function
(
err
,
worker
)
{
Worker
.
create
({}).
done
(
function
(
err
,
worker
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
err
).
not
.
to
.
be
.
ok
...
@@ -924,7 +973,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -924,7 +973,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
worker
.
getTasks
().
done
(
function
(
err
,
tasks
)
{
worker
.
getTasks
().
done
(
function
(
err
,
tasks
)
{
expect
(
tasks
.
length
).
to
.
equal
(
1
)
expect
(
tasks
.
length
).
to
.
equal
(
1
)
done
()
done
()
})
})
})
})
})
})
})
})
...
...
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