chore: support update field attribute in collection (#5643)

This commit is contained in:
ChengLei Shao 2024-11-13 19:09:02 +08:00 committed by GitHub
parent 6198c99706
commit 9fe1775ca3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 42 additions and 11 deletions

View File

@ -76,6 +76,15 @@ export class Collection implements ICollection {
return this.fields.get(name);
}
getFieldByField(field: string): IField {
for (const item of this.fields.values()) {
if (item.options.field === field) {
return item;
}
}
return null;
}
getFields() {
return [...this.fields.values()];
}

View File

@ -65,6 +65,8 @@ export interface ICollection {
getField(name: string): IField;
getFieldByField(field: string): IField;
[key: string]: any;
unavailableActions?: () => string[];

View File

@ -369,6 +369,10 @@ export class Collection<
return this.fields.get(name);
}
getFieldByField(field: string): Field {
return this.findField((f) => f.options.field === field);
}
getFields() {
return [...this.fields.values()];
}

View File

@ -20,13 +20,27 @@ export class DataSourcesFieldModel extends MagicAttributeModel {
const { app } = loadOptions;
const options = this.get();
const { collectionName, name, dataSourceKey } = options;
const { collectionName, name, dataSourceKey, field } = options;
const dataSource = app.dataSourceManager.dataSources.get(dataSourceKey);
const collection = dataSource.collectionManager.getCollection(collectionName);
const oldField = collection.getField(name);
const oldFieldByName = collection.getField(name);
const oldFieldByField = field ? collection.getFieldByField(field) : null;
const oldField = oldFieldByField || oldFieldByName;
const newOptions = mergeOptions(oldField ? oldField.options : {}, options);
collection.setField(name, newOptions);
if (oldFieldByField && !oldFieldByName) {
const filedShouldRemove = collection
.getFields()
.filter((f) => f.options.field === field && f.options.name !== name);
for (const f of filedShouldRemove) {
collection.removeField(f.options.name);
}
}
}
unload(loadOptions: LoadOptions) {

View File

@ -67,20 +67,22 @@ export default {
},
});
} else {
await mainDb.getRepository('dataSourcesFields').update({
filter: {
name,
collectionName,
dataSourceKey,
},
values,
});
fieldRecord = (
await mainDb.getRepository('dataSourcesFields').update({
filter: {
name,
collectionName,
dataSourceKey,
},
values,
})
)[0];
}
const field = ctx.app.dataSourceManager.dataSources
.get(dataSourceKey)
.collectionManager.getCollection(collectionName)
.getField(name);
.getField(fieldRecord.get('name'));
ctx.body = field.options;