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
d2c85b55
Commit
d2c85b55
authored
2 years ago
by
lyf-coder
Browse files
Options
Download
Email Patches
Plain Diff
fix(collection-manager): when del foreign key field, relate fields will be del too
parent
33f0df50
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
packages/plugins/collection-manager/src/hooks/afterCreateForForeignKeyField.ts
+10
-1
...ection-manager/src/hooks/afterCreateForForeignKeyField.ts
packages/plugins/collection-manager/src/hooks/afterDestroyForForeignKeyField.ts
+71
-0
...ction-manager/src/hooks/afterDestroyForForeignKeyField.ts
packages/plugins/collection-manager/src/server.ts
+2
-0
packages/plugins/collection-manager/src/server.ts
with
83 additions
and
1 deletion
+83
-1
packages/plugins/collection-manager/src/hooks/afterCreateForForeignKeyField.ts
+
10
-
1
View file @
d2c85b55
import
Database
from
'
@nocobase/database
'
;
import
Database
,
{
UpdateOptions
}
from
'
@nocobase/database
'
;
export
function
afterCreateForForeignKeyField
(
db
:
Database
)
{
function
generateFkOptions
(
collectionName
:
string
,
foreignKey
:
string
)
{
...
...
@@ -104,6 +104,15 @@ export function afterCreateForForeignKeyField(db: Database) {
},
transaction
,
});
}
else
{
const
options
=
instance
.
get
(
'
options
'
);
if
(
!
options
?.
isThrough
)
{
options
.
isThrough
=
true
;
instance
.
set
(
'
options
'
,
options
);
await
instance
.
save
({
transaction
,
});
}
}
const
opts1
=
generateFkOptions
(
through
,
foreignKey
);
const
opts2
=
generateFkOptions
(
through
,
otherKey
);
...
...
This diff is collapsed.
Click to expand it.
packages/plugins/collection-manager/src/hooks/afterDestroyForForeignKeyField.ts
0 → 100644
+
71
-
0
View file @
d2c85b55
import
Database
,
{
FindOneOptions
,
FindOptions
,
Model
}
from
'
@nocobase/database
'
;
import
{
Transaction
}
from
'
sequelize
'
;
async
function
destroyFields
(
db
:
Database
,
transaction
:
Transaction
,
fieldRecords
:
Model
[])
{
const
fieldsRepo
=
db
.
getRepository
(
'
fields
'
);
for
(
const
fieldRecord
of
fieldRecords
)
{
await
fieldsRepo
.
destroy
({
filter
:
{
name
:
fieldRecord
.
get
(
'
name
'
),
collectionName
:
fieldRecord
.
get
(
'
collectionName
'
),
},
transaction
,
});
}
}
export
function
afterDestroyForForeignKeyField
(
db
:
Database
)
{
return
async
(
model
,
opts
)
=>
{
const
{
transaction
}
=
opts
;
const
options
=
model
.
get
(
'
options
'
);
if
(
!
options
?.
isForeignKey
)
{
return
;
}
const
collectionRepo
=
db
.
getRepository
(
'
collections
'
);
const
foreignKey
=
model
.
get
(
'
name
'
);
const
foreignKeyCollectionName
=
model
.
get
(
'
collectionName
'
);
const
collectionRecord
=
await
collectionRepo
.
findOne
({
filter
:
{
name
:
foreignKeyCollectionName
,
},
transaction
,
}
as
FindOneOptions
);
const
collectionOptions
=
collectionRecord
.
get
(
'
options
'
);
const
fieldsRepo
=
db
.
getRepository
(
'
fields
'
);
if
(
collectionOptions
?.
isThrough
)
{
// through collection
const
fieldRecords
=
await
fieldsRepo
.
find
({
filter
:
{
options
:
{
through
:
foreignKeyCollectionName
,
foreignKey
:
foreignKey
},
},
transaction
,
}
as
FindOptions
);
await
destroyFields
(
db
,
transaction
,
fieldRecords
);
}
else
{
await
destroyFields
(
db
,
transaction
,
await
fieldsRepo
.
find
({
filter
:
{
collectionName
:
foreignKeyCollectionName
,
options
:
{
foreignKey
:
foreignKey
},
},
transaction
,
}
as
FindOptions
),
);
await
destroyFields
(
db
,
transaction
,
await
fieldsRepo
.
find
({
filter
:
{
options
:
{
foreignKey
:
foreignKey
,
target
:
foreignKeyCollectionName
},
},
transaction
,
}
as
FindOptions
),
);
}
};
}
This diff is collapsed.
Click to expand it.
packages/plugins/collection-manager/src/server.ts
+
2
-
0
View file @
d2c85b55
...
...
@@ -15,6 +15,7 @@ import {
beforeInitOptions
}
from
'
./hooks
'
;
import
{
CollectionModel
,
FieldModel
}
from
'
./models
'
;
import
{
afterDestroyForForeignKeyField
}
from
"
./hooks/afterDestroyForForeignKeyField
"
;
export
class
CollectionManagerPlugin
extends
Plugin
{
...
...
@@ -115,6 +116,7 @@ export class CollectionManagerPlugin extends Plugin {
this
.
app
.
db
.
on
(
'
fields.beforeDestroy
'
,
async
(
model
,
options
)
=>
{
await
model
.
remove
(
options
);
});
this
.
app
.
db
.
on
(
'
fields.afterDestroy
'
,
afterDestroyForForeignKeyField
(
this
.
app
.
db
));
this
.
app
.
db
.
on
(
'
collections.beforeDestroy
'
,
async
(
model
,
options
)
=>
{
await
model
.
remove
(
options
);
...
...
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