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 6343ed2f
authored
Sep 16, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(associations): add options.fields support to N:M#createAssoication (closes #2259)
1 parent
c58bc413
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
25 deletions
lib/associations/has-many.js
test/associations/has-many.test.js
lib/associations/has-many.js
View file @
6343ed2
...
@@ -472,8 +472,15 @@ module.exports = (function() {
...
@@ -472,8 +472,15 @@ module.exports = (function() {
HasMany
.
prototype
.
injectCreator
=
function
(
obj
)
{
HasMany
.
prototype
.
injectCreator
=
function
(
obj
)
{
var
association
=
this
;
var
association
=
this
;
obj
[
this
.
accessors
.
create
]
=
function
(
values
,
fieldsOrO
ptions
)
{
obj
[
this
.
accessors
.
create
]
=
function
(
values
,
o
ptions
)
{
var
instance
=
this
;
var
instance
=
this
;
options
=
options
||
{};
if
(
Array
.
isArray
(
options
))
{
options
=
{
fields
:
options
};
}
if
(
values
===
undefined
)
{
if
(
values
===
undefined
)
{
values
=
{};
values
=
{};
...
@@ -482,17 +489,19 @@ module.exports = (function() {
...
@@ -482,17 +489,19 @@ module.exports = (function() {
if
(
association
.
scope
)
{
if
(
association
.
scope
)
{
Object
.
keys
(
association
.
scope
).
forEach
(
function
(
attribute
)
{
Object
.
keys
(
association
.
scope
).
forEach
(
function
(
attribute
)
{
values
[
attribute
]
=
association
.
scope
[
attribute
];
values
[
attribute
]
=
association
.
scope
[
attribute
];
if
(
options
.
fields
)
options
.
fields
.
push
(
attribute
);
});
});
}
}
if
(
Object
(
association
.
through
.
model
)
===
association
.
through
.
model
)
{
if
(
Object
(
association
.
through
.
model
)
===
association
.
through
.
model
)
{
// Create the related model instance
// Create the related model instance
return
association
.
target
.
create
(
values
,
fieldsOrO
ptions
).
then
(
function
(
newAssociatedObject
)
{
return
association
.
target
.
create
(
values
,
o
ptions
).
then
(
function
(
newAssociatedObject
)
{
return
instance
[
association
.
accessors
.
add
](
newAssociatedObject
,
fieldsOrOptions
).
return
(
newAssociatedObject
);
return
instance
[
association
.
accessors
.
add
](
newAssociatedObject
,
_
.
omit
(
options
,
[
'fields'
])
).
return
(
newAssociatedObject
);
});
});
}
else
{
}
else
{
values
[
association
.
identifier
]
=
instance
.
get
(
association
.
source
.
primaryKeyAttribute
);
values
[
association
.
identifier
]
=
instance
.
get
(
association
.
source
.
primaryKeyAttribute
);
return
association
.
target
.
create
(
values
,
fieldsOrOptions
);
if
(
options
.
fields
)
options
.
fields
.
push
(
association
.
identifier
);
return
association
.
target
.
create
(
values
,
options
);
}
}
};
};
...
...
test/associations/has-many.test.js
View file @
6343ed2
...
@@ -550,6 +550,31 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -550,6 +550,31 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
return
this
.
t
.
rollback
();
return
this
.
t
.
rollback
();
});
});
});
});
it
(
'supports passing the field option'
,
function
()
{
var
Article
=
this
.
sequelize
.
define
(
'Article'
,
{
'title'
:
DataTypes
.
STRING
});
var
Label
=
this
.
sequelize
.
define
(
'Label'
,
{
'text'
:
DataTypes
.
STRING
});
Article
.
hasMany
(
Label
);
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
Article
.
create
();
}).
then
(
function
(
article
)
{
return
article
.
createLabel
({
text
:
'yolo'
},
{
fields
:
[
'text'
]
}).
return
(
article
);
}).
then
(
function
(
article
)
{
return
article
.
getLabels
();
}).
then
(
function
(
labels
)
{
expect
(
labels
.
length
).
to
.
be
.
ok
;
});
});
});
});
describe
(
"getting assocations with options"
,
function
()
{
describe
(
"getting assocations with options"
,
function
()
{
...
@@ -1087,28 +1112,49 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -1087,28 +1112,49 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
isAdmin
:
Sequelize
.
BOOLEAN
isAdmin
:
Sequelize
.
BOOLEAN
});
});
User
.
hasMany
(
Group
,
{
through
:
UserGroups
});
User
.
hasMany
(
Group
,
{
through
:
UserGroups
});
Group
.
hasMany
(
User
,
{
through
:
UserGroups
});
Group
.
hasMany
(
User
,
{
through
:
UserGroups
});
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
Group
.
create
({});
return
Group
.
create
({});
}).
then
(
function
(
group
)
{
}).
then
(
function
(
group
)
{
return
Promise
.
join
(
return
Promise
.
join
(
group
.
createUser
({
id
:
1
},
{
isAdmin
:
true
}),
group
.
createUser
({
id
:
1
},
{
isAdmin
:
true
}),
group
.
createUser
({
id
:
2
},
{
isAdmin
:
false
}),
group
.
createUser
({
id
:
2
},
{
isAdmin
:
false
}),
function
()
{
function
()
{
return
UserGroups
.
findAll
();
return
UserGroups
.
findAll
();
}
}
);
);
}).
then
(
function
(
userGroups
)
{
}).
then
(
function
(
userGroups
)
{
userGroups
.
sort
(
function
(
a
,
b
)
{
userGroups
.
sort
(
function
(
a
,
b
)
{
return
a
.
userId
<
b
.
userId
?
-
1
:
1
;
return
a
.
userId
<
b
.
userId
?
-
1
:
1
;
});
expect
(
userGroups
[
0
].
userId
).
to
.
equal
(
1
);
expect
(
userGroups
[
0
].
isAdmin
).
to
.
be
.
ok
;
expect
(
userGroups
[
1
].
userId
).
to
.
equal
(
2
);
expect
(
userGroups
[
1
].
isAdmin
).
not
.
to
.
be
.
ok
;
});
});
expect
(
userGroups
[
0
].
userId
).
to
.
equal
(
1
);
expect
(
userGroups
[
0
].
isAdmin
).
to
.
be
.
ok
;
expect
(
userGroups
[
1
].
userId
).
to
.
equal
(
2
);
expect
(
userGroups
[
1
].
isAdmin
).
not
.
to
.
be
.
ok
;
});
});
it
(
'supports using the field parameter'
,
function
()
{
var
User
=
this
.
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
})
,
Task
=
this
.
sequelize
.
define
(
'Task'
,
{
title
:
DataTypes
.
STRING
});
User
.
hasMany
(
Task
);
Task
.
hasMany
(
User
);
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(
function
()
{
return
Task
.
create
({
title
:
'task'
});
}).
bind
({}).
then
(
function
(
task
)
{
this
.
task
=
task
;
return
task
.
createUser
({
username
:
'foo'
},
{
fields
:
[
'username'
]});
}).
then
(
function
(
createdUser
)
{
expect
(
createdUser
.
Model
).
to
.
equal
(
User
);
expect
(
createdUser
.
username
).
to
.
equal
(
'foo'
);
return
this
.
task
.
getUsers
();
}).
then
(
function
(
_users
)
{
expect
(
_users
).
to
.
have
.
length
(
1
);
});
});
});
});
});
...
...
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