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 39c35012
authored
Apr 19, 2020
by
Andy Edwards
Committed by
GitHub
Apr 19, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(associations): asyncify methods (#12123)
1 parent
5a1472ba
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
147 additions
and
147 deletions
lib/associations/belongs-to-many.js
lib/associations/belongs-to.js
lib/associations/has-many.js
lib/associations/has-one.js
test/integration/associations/belongs-to.test.js
lib/associations/belongs-to-many.js
View file @
39c3501
...
@@ -608,13 +608,12 @@ class BelongsToMany extends Association {
...
@@ -608,13 +608,12 @@ class BelongsToMany extends Association {
}
}
if
(
obsoleteAssociations
.
length
>
0
)
{
if
(
obsoleteAssociations
.
length
>
0
)
{
const
where
=
Object
.
assign
({
[
identifier
]:
sourceInstance
.
get
(
sourceKey
),
[
foreignIdentifier
]:
obsoleteAssociations
.
map
(
obsoleteAssociation
=>
obsoleteAssociation
[
foreignIdentifier
])
},
this
.
through
.
scope
);
promises
.
push
(
promises
.
push
(
this
.
through
.
model
.
destroy
(
_
.
defaults
({
this
.
through
.
model
.
destroy
(
_
.
defaults
({
where
where
:
Object
.
assign
({
[
identifier
]:
sourceInstance
.
get
(
sourceKey
),
[
foreignIdentifier
]:
obsoleteAssociations
.
map
(
obsoleteAssociation
=>
obsoleteAssociation
[
foreignIdentifier
])
},
this
.
through
.
scope
)
},
options
))
},
options
))
);
);
}
}
...
@@ -724,13 +723,11 @@ class BelongsToMany extends Association {
...
@@ -724,13 +723,11 @@ class BelongsToMany extends Association {
if
(
throughAttributes
instanceof
association
.
through
.
model
)
{
if
(
throughAttributes
instanceof
association
.
through
.
model
)
{
throughAttributes
=
{};
throughAttributes
=
{};
}
}
const
where
=
{
promises
.
push
(
association
.
through
.
model
.
update
(
attributes
,
Object
.
assign
(
options
,
{
where
:
{
[
identifier
]:
sourceInstance
.
get
(
sourceKey
),
[
identifier
]:
sourceInstance
.
get
(
sourceKey
),
[
foreignIdentifier
]:
assoc
.
get
(
targetKey
)
[
foreignIdentifier
]:
assoc
.
get
(
targetKey
)
};
}
})));
promises
.
push
(
association
.
through
.
model
.
update
(
attributes
,
Object
.
assign
(
options
,
{
where
})));
}
}
return
Utils
.
Promise
.
all
(
promises
);
return
Utils
.
Promise
.
all
(
promises
);
...
...
lib/associations/belongs-to.js
View file @
39c3501
...
@@ -123,7 +123,7 @@ class BelongsTo extends Association {
...
@@ -123,7 +123,7 @@ class BelongsTo extends Association {
*
*
* @returns {Promise<Model>}
* @returns {Promise<Model>}
*/
*/
get
(
instances
,
options
)
{
async
get
(
instances
,
options
)
{
const
where
=
{};
const
where
=
{};
let
Target
=
this
.
target
;
let
Target
=
this
.
target
;
let
instance
;
let
instance
;
...
@@ -149,7 +149,7 @@ class BelongsTo extends Association {
...
@@ -149,7 +149,7 @@ class BelongsTo extends Association {
if
(
instances
)
{
if
(
instances
)
{
where
[
this
.
targetKey
]
=
{
where
[
this
.
targetKey
]
=
{
[
Op
.
in
]:
instances
.
map
(
instance
=>
instance
.
get
(
this
.
foreignKey
))
[
Op
.
in
]:
instances
.
map
(
_instance
=>
_
instance
.
get
(
this
.
foreignKey
))
};
};
}
else
{
}
else
{
if
(
this
.
targetKeyIsPrimary
&&
!
options
.
where
)
{
if
(
this
.
targetKeyIsPrimary
&&
!
options
.
where
)
{
...
@@ -164,18 +164,17 @@ class BelongsTo extends Association {
...
@@ -164,18 +164,17 @@ class BelongsTo extends Association {
where
;
where
;
if
(
instances
)
{
if
(
instances
)
{
return
Target
.
findAll
(
options
).
then
(
results
=>
{
const
results
=
await
Target
.
findAll
(
options
);
const
result
=
{};
const
result
=
{};
for
(
const
instance
of
instances
)
{
for
(
const
_instance
of
instances
)
{
result
[
instance
.
get
(
this
.
foreignKey
,
{
raw
:
true
})]
=
null
;
result
[
_instance
.
get
(
this
.
foreignKey
,
{
raw
:
true
})]
=
null
;
}
}
for
(
const
instance
of
results
)
{
for
(
const
_instance
of
results
)
{
result
[
instance
.
get
(
this
.
targetKey
,
{
raw
:
true
})]
=
instance
;
result
[
_instance
.
get
(
this
.
targetKey
,
{
raw
:
true
})]
=
_instance
;
}
}
return
result
;
return
result
;
});
}
}
return
Target
.
findOne
(
options
);
return
Target
.
findOne
(
options
);
...
@@ -191,7 +190,7 @@ class BelongsTo extends Association {
...
@@ -191,7 +190,7 @@ class BelongsTo extends Association {
*
*
* @returns {Promise}
* @returns {Promise}
*/
*/
set
(
sourceInstance
,
associatedInstance
,
options
=
{})
{
async
set
(
sourceInstance
,
associatedInstance
,
options
=
{})
{
let
value
=
associatedInstance
;
let
value
=
associatedInstance
;
if
(
associatedInstance
instanceof
this
.
target
)
{
if
(
associatedInstance
instanceof
this
.
target
)
{
...
@@ -209,7 +208,7 @@ class BelongsTo extends Association {
...
@@ -209,7 +208,7 @@ class BelongsTo extends Association {
},
options
);
},
options
);
// passes the changed field to save, so only that field get updated.
// passes the changed field to save, so only that field get updated.
return
sourceInstance
.
save
(
options
);
return
await
sourceInstance
.
save
(
options
);
}
}
/**
/**
...
@@ -224,14 +223,14 @@ class BelongsTo extends Association {
...
@@ -224,14 +223,14 @@ class BelongsTo extends Association {
*
*
* @returns {Promise<Model>} The created target model
* @returns {Promise<Model>} The created target model
*/
*/
create
(
sourceInstance
,
values
,
options
)
{
async
create
(
sourceInstance
,
values
,
options
)
{
values
=
values
||
{};
values
=
values
||
{};
options
=
options
||
{};
options
=
options
||
{};
return
this
.
target
.
create
(
values
,
options
)
const
newAssociatedObject
=
await
this
.
target
.
create
(
values
,
options
);
.
then
(
newAssociatedObject
=>
sourceInstance
[
this
.
accessors
.
set
](
newAssociatedObject
,
options
)
await
sourceInstance
[
this
.
accessors
.
set
](
newAssociatedObject
,
options
);
.
then
(()
=>
newAssociatedObject
)
)
;
return
newAssociatedObject
;
}
}
verifyAssociationAlias
(
alias
)
{
verifyAssociationAlias
(
alias
)
{
...
...
lib/associations/has-many.js
View file @
39c3501
...
@@ -168,7 +168,7 @@ class HasMany extends Association {
...
@@ -168,7 +168,7 @@ class HasMany extends Association {
*
*
* @returns {Promise<Array<Model>>}
* @returns {Promise<Array<Model>>}
*/
*/
get
(
instances
,
options
=
{})
{
async
get
(
instances
,
options
=
{})
{
const
where
=
{};
const
where
=
{};
let
Model
=
this
.
target
;
let
Model
=
this
.
target
;
...
@@ -187,7 +187,7 @@ class HasMany extends Association {
...
@@ -187,7 +187,7 @@ class HasMany extends Association {
}
}
if
(
instances
)
{
if
(
instances
)
{
values
=
instances
.
map
(
instance
=>
instance
.
get
(
this
.
sourceKey
,
{
raw
:
true
}));
values
=
instances
.
map
(
_instance
=>
_
instance
.
get
(
this
.
sourceKey
,
{
raw
:
true
}));
if
(
options
.
limit
&&
instances
.
length
>
1
)
{
if
(
options
.
limit
&&
instances
.
length
>
1
)
{
options
.
groupedLimit
=
{
options
.
groupedLimit
=
{
...
@@ -223,20 +223,19 @@ class HasMany extends Association {
...
@@ -223,20 +223,19 @@ class HasMany extends Association {
Model
=
Model
.
schema
(
options
.
schema
,
options
.
schemaDelimiter
);
Model
=
Model
.
schema
(
options
.
schema
,
options
.
schemaDelimiter
);
}
}
return
Model
.
findAll
(
options
).
then
(
results
=>
{
const
results
=
await
Model
.
findAll
(
options
);
if
(
instance
)
return
results
;
if
(
instance
)
return
results
;
const
result
=
{};
const
result
=
{};
for
(
const
instance
of
instances
)
{
for
(
const
_
instance
of
instances
)
{
result
[
instance
.
get
(
this
.
sourceKey
,
{
raw
:
true
})]
=
[];
result
[
_
instance
.
get
(
this
.
sourceKey
,
{
raw
:
true
})]
=
[];
}
}
for
(
const
instance
of
results
)
{
for
(
const
_
instance
of
results
)
{
result
[
instance
.
get
(
this
.
foreignKey
,
{
raw
:
true
})].
push
(
instance
);
result
[
_instance
.
get
(
this
.
foreignKey
,
{
raw
:
true
})].
push
(
_
instance
);
}
}
return
result
;
return
result
;
});
}
}
/**
/**
...
@@ -249,7 +248,7 @@ class HasMany extends Association {
...
@@ -249,7 +248,7 @@ class HasMany extends Association {
*
*
* @returns {Promise<number>}
* @returns {Promise<number>}
*/
*/
count
(
instance
,
options
)
{
async
count
(
instance
,
options
)
{
options
=
Utils
.
cloneDeep
(
options
);
options
=
Utils
.
cloneDeep
(
options
);
options
.
attributes
=
[
options
.
attributes
=
[
...
@@ -264,7 +263,9 @@ class HasMany extends Association {
...
@@ -264,7 +263,9 @@ class HasMany extends Association {
options
.
raw
=
true
;
options
.
raw
=
true
;
options
.
plain
=
true
;
options
.
plain
=
true
;
return
this
.
get
(
instance
,
options
).
then
(
result
=>
parseInt
(
result
.
count
,
10
));
const
result
=
await
this
.
get
(
instance
,
options
);
return
parseInt
(
result
.
count
,
10
);
}
}
/**
/**
...
@@ -276,7 +277,7 @@ class HasMany extends Association {
...
@@ -276,7 +277,7 @@ class HasMany extends Association {
*
*
* @returns {Promise}
* @returns {Promise}
*/
*/
has
(
sourceInstance
,
targetInstances
,
options
)
{
async
has
(
sourceInstance
,
targetInstances
,
options
)
{
const
where
=
{};
const
where
=
{};
if
(
!
Array
.
isArray
(
targetInstances
))
{
if
(
!
Array
.
isArray
(
targetInstances
))
{
...
@@ -305,7 +306,9 @@ class HasMany extends Association {
...
@@ -305,7 +306,9 @@ class HasMany extends Association {
]
]
};
};
return
this
.
get
(
sourceInstance
,
options
).
then
(
associatedObjects
=>
associatedObjects
.
length
===
targetInstances
.
length
);
const
associatedObjects
=
await
this
.
get
(
sourceInstance
,
options
);
return
associatedObjects
.
length
===
targetInstances
.
length
;
}
}
/**
/**
...
@@ -318,68 +321,69 @@ class HasMany extends Association {
...
@@ -318,68 +321,69 @@ class HasMany extends Association {
*
*
* @returns {Promise}
* @returns {Promise}
*/
*/
set
(
sourceInstance
,
targetInstances
,
options
)
{
async
set
(
sourceInstance
,
targetInstances
,
options
)
{
if
(
targetInstances
===
null
)
{
if
(
targetInstances
===
null
)
{
targetInstances
=
[];
targetInstances
=
[];
}
else
{
}
else
{
targetInstances
=
this
.
toInstanceArray
(
targetInstances
);
targetInstances
=
this
.
toInstanceArray
(
targetInstances
);
}
}
return
this
.
get
(
sourceInstance
,
_
.
defaults
({
scope
:
false
,
raw
:
true
},
options
)).
then
(
oldAssociations
=>
{
const
oldAssociations
=
await
this
.
get
(
sourceInstance
,
_
.
defaults
({
scope
:
false
,
raw
:
true
},
options
));
const
promises
=
[];
const
promises
=
[];
const
obsoleteAssociations
=
oldAssociations
.
filter
(
old
=>
const
obsoleteAssociations
=
oldAssociations
.
filter
(
old
=>
!
targetInstances
.
find
(
obj
=>
!
targetInstances
.
find
(
obj
=>
obj
[
this
.
target
.
primaryKeyAttribute
]
===
old
[
this
.
target
.
primaryKeyAttribute
]
obj
[
this
.
target
.
primaryKeyAttribute
]
===
old
[
this
.
target
.
primaryKeyAttribute
]
)
)
);
);
const
unassociatedObjects
=
targetInstances
.
filter
(
obj
=>
const
unassociatedObjects
=
targetInstances
.
filter
(
obj
=>
!
oldAssociations
.
find
(
old
=>
!
oldAssociations
.
find
(
old
=>
obj
[
this
.
target
.
primaryKeyAttribute
]
===
old
[
this
.
target
.
primaryKeyAttribute
]
obj
[
this
.
target
.
primaryKeyAttribute
]
===
old
[
this
.
target
.
primaryKeyAttribute
]
)
)
);
);
let
updateWhere
;
let
updateWhere
;
let
update
;
let
update
;
if
(
obsoleteAssociations
.
length
>
0
)
{
if
(
obsoleteAssociations
.
length
>
0
)
{
update
=
{};
update
=
{};
update
[
this
.
foreignKey
]
=
null
;
update
[
this
.
foreignKey
]
=
null
;
updateWhere
=
{
updateWhere
=
{
[
this
.
target
.
primaryKeyAttribute
]:
obsoleteAssociations
.
map
(
associatedObject
=>
[
this
.
target
.
primaryKeyAttribute
]:
obsoleteAssociations
.
map
(
associatedObject
=>
associatedObject
[
this
.
target
.
primaryKeyAttribute
]
associatedObject
[
this
.
target
.
primaryKeyAttribute
]
)
)
};
};
promises
.
push
(
this
.
target
.
unscoped
().
update
(
promises
.
push
(
this
.
target
.
unscoped
().
update
(
update
,
update
,
_
.
defaults
({
_
.
defaults
({
where
:
updateWhere
where
:
updateWhere
},
options
)
},
options
)
));
));
}
}
if
(
unassociatedObjects
.
length
>
0
)
{
if
(
unassociatedObjects
.
length
>
0
)
{
updateWhere
=
{};
updateWhere
=
{};
update
=
{};
update
=
{};
update
[
this
.
foreignKey
]
=
sourceInstance
.
get
(
this
.
sourceKey
);
update
[
this
.
foreignKey
]
=
sourceInstance
.
get
(
this
.
sourceKey
);
Object
.
assign
(
update
,
this
.
scope
);
Object
.
assign
(
update
,
this
.
scope
);
updateWhere
[
this
.
target
.
primaryKeyAttribute
]
=
unassociatedObjects
.
map
(
unassociatedObject
=>
updateWhere
[
this
.
target
.
primaryKeyAttribute
]
=
unassociatedObjects
.
map
(
unassociatedObject
=>
unassociatedObject
[
this
.
target
.
primaryKeyAttribute
]
unassociatedObject
[
this
.
target
.
primaryKeyAttribute
]
);
);
promises
.
push
(
this
.
target
.
unscoped
().
update
(
promises
.
push
(
this
.
target
.
unscoped
().
update
(
update
,
update
,
_
.
defaults
({
_
.
defaults
({
where
:
updateWhere
where
:
updateWhere
},
options
)
},
options
)
));
));
}
}
return
Utils
.
Promise
.
all
(
promises
).
then
(()
=>
sourceInstance
);
await
Utils
.
Promise
.
all
(
promises
);
});
return
sourceInstance
;
}
}
/**
/**
...
@@ -392,7 +396,7 @@ class HasMany extends Association {
...
@@ -392,7 +396,7 @@ class HasMany extends Association {
*
*
* @returns {Promise}
* @returns {Promise}
*/
*/
add
(
sourceInstance
,
targetInstances
,
options
=
{})
{
a
sync
a
dd
(
sourceInstance
,
targetInstances
,
options
=
{})
{
if
(
!
targetInstances
)
return
Utils
.
Promise
.
resolve
();
if
(
!
targetInstances
)
return
Utils
.
Promise
.
resolve
();
const
update
=
{};
const
update
=
{};
...
@@ -408,7 +412,9 @@ class HasMany extends Association {
...
@@ -408,7 +412,9 @@ class HasMany extends Association {
)
)
};
};
return
this
.
target
.
unscoped
().
update
(
update
,
_
.
defaults
({
where
},
options
)).
then
(()
=>
sourceInstance
);
await
this
.
target
.
unscoped
().
update
(
update
,
_
.
defaults
({
where
},
options
));
return
sourceInstance
;
}
}
/**
/**
...
@@ -420,7 +426,7 @@ class HasMany extends Association {
...
@@ -420,7 +426,7 @@ class HasMany extends Association {
*
*
* @returns {Promise}
* @returns {Promise}
*/
*/
remove
(
sourceInstance
,
targetInstances
,
options
=
{})
{
async
remove
(
sourceInstance
,
targetInstances
,
options
=
{})
{
const
update
=
{
const
update
=
{
[
this
.
foreignKey
]:
null
[
this
.
foreignKey
]:
null
};
};
...
@@ -434,7 +440,9 @@ class HasMany extends Association {
...
@@ -434,7 +440,9 @@ class HasMany extends Association {
)
)
};
};
return
this
.
target
.
unscoped
().
update
(
update
,
_
.
defaults
({
where
},
options
)).
then
(()
=>
this
);
await
this
.
target
.
unscoped
().
update
(
update
,
_
.
defaults
({
where
},
options
));
return
this
;
}
}
/**
/**
...
@@ -446,7 +454,7 @@ class HasMany extends Association {
...
@@ -446,7 +454,7 @@ class HasMany extends Association {
*
*
* @returns {Promise}
* @returns {Promise}
*/
*/
create
(
sourceInstance
,
values
,
options
=
{})
{
async
create
(
sourceInstance
,
values
,
options
=
{})
{
if
(
Array
.
isArray
(
options
))
{
if
(
Array
.
isArray
(
options
))
{
options
=
{
options
=
{
fields
:
options
fields
:
options
...
@@ -466,7 +474,7 @@ class HasMany extends Association {
...
@@ -466,7 +474,7 @@ class HasMany extends Association {
values
[
this
.
foreignKey
]
=
sourceInstance
.
get
(
this
.
sourceKey
);
values
[
this
.
foreignKey
]
=
sourceInstance
.
get
(
this
.
sourceKey
);
if
(
options
.
fields
)
options
.
fields
.
push
(
this
.
foreignKey
);
if
(
options
.
fields
)
options
.
fields
.
push
(
this
.
foreignKey
);
return
this
.
target
.
create
(
values
,
options
);
return
await
this
.
target
.
create
(
values
,
options
);
}
}
verifyAssociationAlias
(
alias
)
{
verifyAssociationAlias
(
alias
)
{
...
...
lib/associations/has-one.js
View file @
39c3501
...
@@ -122,7 +122,7 @@ class HasOne extends Association {
...
@@ -122,7 +122,7 @@ class HasOne extends Association {
*
*
* @returns {Promise<Model>}
* @returns {Promise<Model>}
*/
*/
get
(
instances
,
options
)
{
async
get
(
instances
,
options
)
{
const
where
=
{};
const
where
=
{};
let
Target
=
this
.
target
;
let
Target
=
this
.
target
;
...
@@ -149,7 +149,7 @@ class HasOne extends Association {
...
@@ -149,7 +149,7 @@ class HasOne extends Association {
if
(
instances
)
{
if
(
instances
)
{
where
[
this
.
foreignKey
]
=
{
where
[
this
.
foreignKey
]
=
{
[
Op
.
in
]:
instances
.
map
(
instance
=>
instance
.
get
(
this
.
sourceKey
))
[
Op
.
in
]:
instances
.
map
(
_instance
=>
_
instance
.
get
(
this
.
sourceKey
))
};
};
}
else
{
}
else
{
where
[
this
.
foreignKey
]
=
instance
.
get
(
this
.
sourceKey
);
where
[
this
.
foreignKey
]
=
instance
.
get
(
this
.
sourceKey
);
...
@@ -164,18 +164,17 @@ class HasOne extends Association {
...
@@ -164,18 +164,17 @@ class HasOne extends Association {
where
;
where
;
if
(
instances
)
{
if
(
instances
)
{
return
Target
.
findAll
(
options
).
then
(
results
=>
{
const
results
=
await
Target
.
findAll
(
options
);
const
result
=
{};
const
result
=
{};
for
(
const
instance
of
instances
)
{
for
(
const
_
instance
of
instances
)
{
result
[
instance
.
get
(
this
.
sourceKey
,
{
raw
:
true
})]
=
null
;
result
[
_
instance
.
get
(
this
.
sourceKey
,
{
raw
:
true
})]
=
null
;
}
}
for
(
const
instance
of
results
)
{
for
(
const
_
instance
of
results
)
{
result
[
instance
.
get
(
this
.
foreignKey
,
{
raw
:
true
})]
=
instance
;
result
[
_instance
.
get
(
this
.
foreignKey
,
{
raw
:
true
})]
=
_
instance
;
}
}
return
result
;
return
result
;
});
}
}
return
Target
.
findOne
(
options
);
return
Target
.
findOne
(
options
);
...
@@ -190,45 +189,42 @@ class HasOne extends Association {
...
@@ -190,45 +189,42 @@ class HasOne extends Association {
*
*
* @returns {Promise}
* @returns {Promise}
*/
*/
set
(
sourceInstance
,
associatedInstance
,
options
)
{
async
set
(
sourceInstance
,
associatedInstance
,
options
)
{
let
alreadyAssociated
;
options
=
Object
.
assign
({},
options
,
{
options
=
Object
.
assign
({},
options
,
{
scope
:
false
scope
:
false
});
});
return
sourceInstance
[
this
.
accessors
.
get
](
options
).
then
(
oldInstance
=>
{
const
oldInstance
=
await
sourceInstance
[
this
.
accessors
.
get
](
options
);
// TODO Use equals method once #5605 is resolved
// TODO Use equals method once #5605 is resolved
alreadyAssociated
=
oldInstance
&&
associatedInstance
&&
this
.
target
.
primaryKeyAttributes
.
every
(
attribute
=>
const
alreadyAssociated
=
oldInstance
&&
associatedInstance
&&
this
.
target
.
primaryKeyAttributes
.
every
(
attribute
=>
oldInstance
.
get
(
attribute
,
{
raw
:
true
})
===
(
associatedInstance
.
get
?
associatedInstance
.
get
(
attribute
,
{
raw
:
true
})
:
associatedInstance
)
oldInstance
.
get
(
attribute
,
{
raw
:
true
})
===
(
associatedInstance
.
get
?
associatedInstance
.
get
(
attribute
,
{
raw
:
true
})
:
associatedInstance
)
);
);
if
(
oldInstance
&&
!
alreadyAssociated
)
{
if
(
oldInstance
&&
!
alreadyAssociated
)
{
oldInstance
[
this
.
foreignKey
]
=
null
;
oldInstance
[
this
.
foreignKey
]
=
null
;
return
oldInstance
.
save
(
Object
.
assign
({},
options
,
{
fields
:
[
this
.
foreignKey
],
await
oldInstance
.
save
(
Object
.
assign
({},
options
,
{
allowNull
:
[
this
.
foreignKey
],
fields
:
[
this
.
foreignKey
],
association
:
true
allowNull
:
[
this
.
foreignKey
],
}));
association
:
true
}));
}
if
(
associatedInstance
&&
!
alreadyAssociated
)
{
if
(
!
(
associatedInstance
instanceof
this
.
target
))
{
const
tmpInstance
=
{};
tmpInstance
[
this
.
target
.
primaryKeyAttribute
]
=
associatedInstance
;
associatedInstance
=
this
.
target
.
build
(
tmpInstance
,
{
isNewRecord
:
false
});
}
}
}).
then
(()
=>
{
if
(
associatedInstance
&&
!
alreadyAssociated
)
{
if
(
!
(
associatedInstance
instanceof
this
.
target
))
{
const
tmpInstance
=
{};
tmpInstance
[
this
.
target
.
primaryKeyAttribute
]
=
associatedInstance
;
associatedInstance
=
this
.
target
.
build
(
tmpInstance
,
{
isNewRecord
:
false
});
}
Object
.
assign
(
associatedInstance
,
this
.
scope
);
Object
.
assign
(
associatedInstance
,
this
.
scope
);
associatedInstance
.
set
(
this
.
foreignKey
,
sourceInstance
.
get
(
this
.
sourceKeyAttribute
));
associatedInstance
.
set
(
this
.
foreignKey
,
sourceInstance
.
get
(
this
.
sourceKeyAttribute
));
return
associatedInstance
.
save
(
options
);
return
associatedInstance
.
save
(
options
);
}
}
return
null
;
return
null
;
});
}
}
/**
/**
...
@@ -243,7 +239,7 @@ class HasOne extends Association {
...
@@ -243,7 +239,7 @@ class HasOne extends Association {
*
*
* @returns {Promise<Model>} The created target model
* @returns {Promise<Model>} The created target model
*/
*/
create
(
sourceInstance
,
values
,
options
)
{
async
create
(
sourceInstance
,
values
,
options
)
{
values
=
values
||
{};
values
=
values
||
{};
options
=
options
||
{};
options
=
options
||
{};
...
@@ -261,7 +257,7 @@ class HasOne extends Association {
...
@@ -261,7 +257,7 @@ class HasOne extends Association {
options
.
fields
.
push
(
this
.
foreignKey
);
options
.
fields
.
push
(
this
.
foreignKey
);
}
}
return
this
.
target
.
create
(
values
,
options
);
return
await
this
.
target
.
create
(
values
,
options
);
}
}
verifyAssociationAlias
(
alias
)
{
verifyAssociationAlias
(
alias
)
{
...
...
test/integration/associations/belongs-to.test.js
View file @
39c3501
...
@@ -369,10 +369,10 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
...
@@ -369,10 +369,10 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
return
Promise
.
join
(
return
Promise
.
join
(
Post
.
create
(),
Post
.
create
(),
Comment
.
create
()
Comment
.
create
()
).
then
(([
post
,
comment
])
=>
{
).
then
(
async
([
post
,
comment
])
=>
{
expect
(
comment
.
get
(
'post_id'
)).
not
.
to
.
be
.
ok
;
expect
(
comment
.
get
(
'post_id'
)).
not
.
to
.
be
.
ok
;
const
setter
=
comment
.
setPost
(
post
,
{
save
:
false
});
const
setter
=
await
comment
.
setPost
(
post
,
{
save
:
false
});
expect
(
setter
).
to
.
be
.
undefined
;
expect
(
setter
).
to
.
be
.
undefined
;
expect
(
comment
.
get
(
'post_id'
)).
to
.
equal
(
post
.
get
(
'id'
));
expect
(
comment
.
get
(
'post_id'
)).
to
.
equal
(
post
.
get
(
'id'
));
...
...
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