chore: migrate sortable options to sort field (#4011)

* chore: migrate sortable options to sort field

* chore: association sortable field
This commit is contained in:
ChengLei Shao 2024-04-19 11:58:43 +08:00 committed by GitHub
parent c460354b69
commit 252bb14ae4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,90 @@
import { Migration } from '@nocobase/server';
export default class extends Migration {
on = 'afterLoad'; // 'beforeLoad' or 'afterLoad'
appVersion = '<0.21.0-alpha.7';
private async syncCollectionsSortField() {
const collections = await this.db.getRepository('collections').find();
for (const collection of collections) {
const sortable = collection.get('sortable');
if (!sortable) {
continue;
}
const sortFieldName = sortable === true ? 'sort' : sortable;
const fields = await collection.getFields();
const sortField = fields.find((item) => item.get('type') === 'sort' && item.get('name') === sortFieldName);
if (!sortField) {
await this.db.getRepository('fields').create({
values: {
name: sortFieldName,
type: 'sort',
interface: 'sort',
uiSchema: {
title: sortFieldName,
type: 'number',
'x-component': 'InputNumber',
'x-component-props': { stringMode: true, step: '1' },
'x-validator': 'integer',
},
collectionName: collection.get('name'),
},
});
}
}
}
private async syncAssociationSortField() {
const sortableAssociations = await this.db.getRepository('fields').find({
filter: {
'options.sortable': true,
},
});
for (const field of sortableAssociations) {
const collectionName = field.get('target');
const collection = await this.db.getRepository('collections').findOne({
filter: {
name: collectionName,
},
});
if (!collection) {
continue;
}
const sortFieldName = field.get('sortBy') ? field.get('sortBy') : field.get('name') + '_sort';
const fields = await collection.getFields();
const sortField = fields.find((item) => item.get('type') === 'sort' && item.get('name') === sortFieldName);
if (!sortField) {
await this.db.getRepository('fields').create({
values: {
name: sortFieldName,
type: 'sort',
interface: 'sort',
uiSchema: {
title: sortFieldName,
type: 'number',
'x-component': 'InputNumber',
'x-component-props': { stringMode: true, step: '1' },
'x-validator': 'integer',
},
collectionName: collection.get('name'),
},
});
}
}
}
async up() {
await this.syncCollectionsSortField();
await this.syncAssociationSortField();
}
}