File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -897,6 +897,63 @@ module.exports = session => {
897
897
} ) ;
898
898
} ) ;
899
899
900
+ it ( 'should be able to automatically convert children that are plain JS objects into entities' , ( ) => {
901
+ const parent = Model1 . fromJson ( {
902
+ id : 2 ,
903
+ model1Prop1 : null
904
+ } ) ;
905
+
906
+ const children = [
907
+ {
908
+ idCol : 1 ,
909
+ model2Relation1 : [
910
+ {
911
+ id : 4 ,
912
+ model1Prop1 : null
913
+ }
914
+ ]
915
+ }
916
+ ] ;
917
+ parent . model1Relation2 = children ;
918
+
919
+ return transaction ( session . knex , trx => {
920
+ return Model1 . query ( trx )
921
+ . upsertGraph ( parent , { unrelate : true , relate : true } )
922
+ . then ( result => {
923
+ // Fetch the graph from the database.
924
+ return Model1 . query ( trx )
925
+ . findById ( 2 )
926
+ . eager ( '[model1Relation2.model2Relation1]' )
927
+ . modifyEager ( 'model1Relation2' , qb => qb . orderBy ( 'id_col' ) )
928
+ . modifyEager ( 'model1Relation2.model2Relation1' , qb => qb . orderBy ( 'id' ) ) ;
929
+ } )
930
+ . then ( omitIrrelevantProps )
931
+ . then ( result => {
932
+ expect ( result ) . to . eql ( {
933
+ id : 2 ,
934
+ model1Id : 3 ,
935
+ model1Prop1 : null ,
936
+
937
+ model1Relation2 : [
938
+ {
939
+ idCol : 1 ,
940
+ model1Id : 2 ,
941
+ model2Prop1 : 'hasMany 1' ,
942
+
943
+ model2Relation1 : [
944
+ {
945
+ id : 4 ,
946
+ model1Id : null ,
947
+ model1Prop1 : null
948
+ }
949
+ ]
950
+ }
951
+ ]
952
+ } ) ;
953
+ } ) ;
954
+ } ) ;
955
+ } ) ;
956
+
900
957
it ( 'should respect noRelate and noUnrelate flags' , ( ) => {
901
958
const upsert = {
902
959
// the root gets updated because it has an id
You can’t perform that action at this time.
0 commit comments