Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Nocobase
Commits
02a03d15
Commit
02a03d15
authored
3 years ago
by
Chareice
Browse files
Options
Download
Email Patches
Plain Diff
feat: reverseAssociationPair with HasMany && HasOne
parent
0d4add54
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
packages/database/src/__tests__/repository/count.test.ts
+0
-0
packages/database/src/__tests__/repository/count.test.ts
packages/database/src/__tests__/repository/create.test.ts
+58
-0
packages/database/src/__tests__/repository/create.test.ts
packages/database/src/__tests__/repository/destroy.test.ts
+0
-0
packages/database/src/__tests__/repository/destroy.test.ts
packages/database/src/__tests__/repository/find.test.ts
+0
-0
packages/database/src/__tests__/repository/find.test.ts
packages/database/src/update-associations.ts
+43
-12
packages/database/src/update-associations.ts
with
101 additions
and
12 deletions
+101
-12
packages/database/src/__tests__/re
s
psitory/count.test.ts
→
packages/database/src/__tests__/rep
o
sitory/count.test.ts
+
0
-
0
View file @
02a03d15
File moved
This diff is collapsed.
Click to expand it.
packages/database/src/__tests__/re
s
psitory/create.test.ts
→
packages/database/src/__tests__/rep
o
sitory/create.test.ts
+
58
-
0
View file @
02a03d15
import
{
mockDatabase
}
from
'
../index
'
;
import
Database
from
'
../../database
'
;
describe
(
'
create with hasMany
'
,
()
=>
{
let
db
:
Database
;
let
Post
;
let
User
;
afterEach
(
async
()
=>
{
await
db
.
close
();
});
beforeEach
(
async
()
=>
{
db
=
mockDatabase
();
await
db
.
clean
({
drop
:
true
});
User
=
db
.
collection
({
name
:
'
users
'
,
fields
:
[
{
type
:
'
string
'
,
name
:
'
name
'
,
},
{
type
:
'
hasMany
'
,
name
:
'
posts
'
,
},
],
});
Post
=
db
.
collection
({
name
:
'
posts
'
,
fields
:
[
{
type
:
'
string
'
,
name
:
'
title
'
,
},
{
type
:
'
belongsTo
'
,
name
:
'
user
'
,
},
],
});
await
db
.
sync
();
});
it
(
'
should save associations with reverseField value
'
,
async
()
=>
{
const
u1
=
await
db
.
getRepository
(
'
users
'
).
create
({
values
:
{
name
:
'
u1
'
,
posts
:
[{
title
:
'
t1
'
,
user
:
null
}],
},
});
const
p1
=
await
db
.
getRepository
(
'
posts
'
).
findOne
();
// @ts-ignore
expect
(
await
p1
.
getUser
()).
not
.
toBeNull
();
});
});
describe
(
'
create with belongsToMany
'
,
()
=>
{
let
db
:
Database
;
let
Post
;
...
...
This diff is collapsed.
Click to expand it.
packages/database/src/__tests__/re
s
psitory/destroy.test.ts
→
packages/database/src/__tests__/rep
o
sitory/destroy.test.ts
+
0
-
0
View file @
02a03d15
File moved
This diff is collapsed.
Click to expand it.
packages/database/src/__tests__/re
s
psitory/find.test.ts
→
packages/database/src/__tests__/rep
o
sitory/find.test.ts
+
0
-
0
View file @
02a03d15
File moved
This diff is collapsed.
Click to expand it.
packages/database/src/update-associations.ts
+
43
-
12
View file @
02a03d15
...
...
@@ -165,16 +165,32 @@ export async function updateAssociations(instance: Model, values: any, options:
}
}
function
isReverseAssociationPair
(
a
:
Association
,
b
:
Association
)
{
if
(
a
.
associationType
==
'
BelongsToMany
'
&&
b
.
associationType
==
'
BelongsToMany
'
)
{
// @ts-ignore
if
(
(
a
as
any
).
through
.
tableName
===
(
b
as
any
).
through
.
tableName
&&
function
isReverseAssociationPair
(
a
:
any
,
b
:
any
)
{
const
typeSet
=
new
Set
();
typeSet
.
add
(
a
.
associationType
);
typeSet
.
add
(
b
.
associationType
);
if
(
typeSet
.
size
==
1
&&
typeSet
.
has
(
'
BelongsToMany
'
))
{
return
(
a
.
through
.
tableName
===
b
.
through
.
tableName
&&
a
.
target
.
name
===
b
.
source
.
name
&&
b
.
target
.
name
===
a
.
source
.
name
)
{
return
true
;
}
b
.
target
.
name
===
a
.
source
.
name
&&
a
.
foreignKey
===
b
.
otherKey
&&
a
.
sourceKey
===
b
.
targetKey
&&
a
.
otherKey
===
b
.
foreignKey
&&
a
.
targetKey
===
b
.
sourceKey
);
}
if
((
typeSet
.
has
(
'
HasOne
'
)
&&
typeSet
.
has
(
'
BelongsTo
'
))
||
(
typeSet
.
has
(
'
HasMany
'
)
&&
typeSet
.
has
(
'
BelongsTo
'
)))
{
const
sourceAssoc
=
a
.
associationType
==
'
BelongsTo
'
?
b
:
a
;
const
targetAssoc
=
sourceAssoc
==
a
?
b
:
a
;
return
(
sourceAssoc
.
source
.
name
===
targetAssoc
.
target
.
name
&&
sourceAssoc
.
foreignKey
===
targetAssoc
.
foreignKey
&&
sourceAssoc
.
sourceKey
===
targetAssoc
.
targetKey
);
}
return
false
;
...
...
@@ -300,7 +316,12 @@ export async function updateSingleAssociation(
await
instance
.
update
(
value
,
{
...
options
,
transaction
});
}
await
updateAssociations
(
instance
,
value
,
{
...
options
,
transaction
,
updateAssociationValues
:
keys
});
await
updateAssociations
(
instance
,
value
,
{
...
options
,
transaction
,
associationContext
:
association
,
updateAssociationValues
:
keys
,
});
model
.
setDataValue
(
key
,
instance
);
if
(
!
options
.
transaction
)
{
await
transaction
.
commit
();
...
...
@@ -310,7 +331,12 @@ export async function updateSingleAssociation(
}
const
instance
=
await
model
[
createAccessor
](
value
,
{
context
,
transaction
});
await
updateAssociations
(
instance
,
value
,
{
...
options
,
transaction
,
updateAssociationValues
:
keys
});
await
updateAssociations
(
instance
,
value
,
{
...
options
,
transaction
,
associationContext
:
association
,
updateAssociationValues
:
keys
,
});
model
.
setDataValue
(
key
,
instance
);
// @ts-ignore
if
(
association
.
targetKey
)
{
...
...
@@ -412,7 +438,12 @@ export async function updateMultipleAssociation(
if
(
isUndefinedOrNull
(
item
[
pk
]))
{
// create new record
const
instance
=
await
model
[
createAccessor
](
item
,
accessorOptions
);
await
updateAssociations
(
instance
,
item
,
{
...
options
,
transaction
,
updateAssociationValues
:
keys
});
await
updateAssociations
(
instance
,
item
,
{
...
options
,
transaction
,
associationContext
:
association
,
updateAssociationValues
:
keys
,
});
list3
.
push
(
instance
);
}
else
{
// set & update record
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
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 comment
Menu
Projects
Groups
Snippets
Help