mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 21:49:25 +08:00
chore: support update field attribute in collection (#5643)
This commit is contained in:
parent
6198c99706
commit
9fe1775ca3
@ -76,6 +76,15 @@ export class Collection implements ICollection {
|
|||||||
return this.fields.get(name);
|
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() {
|
getFields() {
|
||||||
return [...this.fields.values()];
|
return [...this.fields.values()];
|
||||||
}
|
}
|
||||||
|
@ -65,6 +65,8 @@ export interface ICollection {
|
|||||||
|
|
||||||
getField(name: string): IField;
|
getField(name: string): IField;
|
||||||
|
|
||||||
|
getFieldByField(field: string): IField;
|
||||||
|
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
|
|
||||||
unavailableActions?: () => string[];
|
unavailableActions?: () => string[];
|
||||||
|
@ -369,6 +369,10 @@ export class Collection<
|
|||||||
return this.fields.get(name);
|
return this.fields.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getFieldByField(field: string): Field {
|
||||||
|
return this.findField((f) => f.options.field === field);
|
||||||
|
}
|
||||||
|
|
||||||
getFields() {
|
getFields() {
|
||||||
return [...this.fields.values()];
|
return [...this.fields.values()];
|
||||||
}
|
}
|
||||||
|
@ -20,13 +20,27 @@ export class DataSourcesFieldModel extends MagicAttributeModel {
|
|||||||
const { app } = loadOptions;
|
const { app } = loadOptions;
|
||||||
|
|
||||||
const options = this.get();
|
const options = this.get();
|
||||||
const { collectionName, name, dataSourceKey } = options;
|
const { collectionName, name, dataSourceKey, field } = options;
|
||||||
const dataSource = app.dataSourceManager.dataSources.get(dataSourceKey);
|
const dataSource = app.dataSourceManager.dataSources.get(dataSourceKey);
|
||||||
const collection = dataSource.collectionManager.getCollection(collectionName);
|
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);
|
const newOptions = mergeOptions(oldField ? oldField.options : {}, options);
|
||||||
|
|
||||||
collection.setField(name, newOptions);
|
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) {
|
unload(loadOptions: LoadOptions) {
|
||||||
|
@ -67,20 +67,22 @@ export default {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
await mainDb.getRepository('dataSourcesFields').update({
|
fieldRecord = (
|
||||||
filter: {
|
await mainDb.getRepository('dataSourcesFields').update({
|
||||||
name,
|
filter: {
|
||||||
collectionName,
|
name,
|
||||||
dataSourceKey,
|
collectionName,
|
||||||
},
|
dataSourceKey,
|
||||||
values,
|
},
|
||||||
});
|
values,
|
||||||
|
})
|
||||||
|
)[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
const field = ctx.app.dataSourceManager.dataSources
|
const field = ctx.app.dataSourceManager.dataSources
|
||||||
.get(dataSourceKey)
|
.get(dataSourceKey)
|
||||||
.collectionManager.getCollection(collectionName)
|
.collectionManager.getCollection(collectionName)
|
||||||
.getField(name);
|
.getField(fieldRecord.get('name'));
|
||||||
|
|
||||||
ctx.body = field.options;
|
ctx.body = field.options;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user