feat: support add collection to collectionManager

This commit is contained in:
sealday 2024-03-14 18:46:45 +08:00
parent 8ccfdf5828
commit cc09966d63
3 changed files with 1222 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,180 @@
import { defineCollection } from '@nocobase/database';
export default defineCollection({
dumpRules: 'required',
name: 'undetermined_projects',
title: '测算项目',
inherit: false,
hidden: false,
description: null,
fields: [
{
name: 'parentId',
type: 'bigInt',
interface: 'integer',
description: null,
parentKey: null,
reverseKey: null,
isForeignKey: true,
uiSchema: {
type: 'number',
title: '{{t("Parent ID")}}',
'x-component': 'InputNumber',
'x-read-pretty': true,
},
target: 'undetermined_projects',
},
{
name: 'parent',
type: 'belongsTo',
interface: 'm2o',
description: null,
parentKey: null,
reverseKey: null,
foreignKey: 'parentId',
treeParent: true,
onDelete: 'CASCADE',
uiSchema: {
title: '{{t("Parent")}}',
'x-component': 'AssociationField',
'x-component-props': {
multiple: false,
fieldNames: {
label: 'id',
value: 'id',
},
},
},
target: 'undetermined_projects',
targetKey: 'id',
},
{
name: 'children',
type: 'hasMany',
interface: 'o2m',
description: null,
parentKey: null,
reverseKey: null,
foreignKey: 'parentId',
treeChildren: true,
onDelete: 'CASCADE',
uiSchema: {
title: '{{t("Children")}}',
'x-component': 'AssociationField',
'x-component-props': {
multiple: true,
fieldNames: {
label: 'id',
value: 'id',
},
},
},
target: 'undetermined_projects',
targetKey: 'id',
sourceKey: 'id',
},
{
name: 'id',
type: 'bigInt',
interface: 'integer',
description: null,
parentKey: null,
reverseKey: null,
autoIncrement: true,
primaryKey: true,
allowNull: false,
uiSchema: {
type: 'number',
title: '{{t("ID")}}',
'x-component': 'InputNumber',
'x-read-pretty': true,
},
target: 'undetermined_projects',
},
{
name: 'createdAt',
type: 'date',
interface: 'createdAt',
description: null,
parentKey: null,
reverseKey: null,
field: 'createdAt',
uiSchema: {
type: 'datetime',
title: '{{t("Created at")}}',
'x-component': 'DatePicker',
'x-component-props': {},
'x-read-pretty': true,
},
target: 'undetermined_projects',
},
{
name: 'createdBy',
type: 'belongsTo',
interface: 'createdBy',
description: null,
parentKey: null,
reverseKey: null,
target: 'users',
foreignKey: 'createdById',
uiSchema: {
type: 'object',
title: '{{t("Created by")}}',
'x-component': 'AssociationField',
'x-component-props': {
fieldNames: {
value: 'id',
label: 'nickname',
},
},
'x-read-pretty': true,
},
targetKey: 'id',
},
{
name: 'updatedAt',
type: 'date',
interface: 'updatedAt',
description: null,
parentKey: null,
reverseKey: null,
field: 'updatedAt',
uiSchema: {
type: 'string',
title: '{{t("Last updated at")}}',
'x-component': 'DatePicker',
'x-component-props': {},
'x-read-pretty': true,
},
target: 'undetermined_projects',
},
{
name: 'updatedBy',
type: 'belongsTo',
interface: 'updatedBy',
description: null,
parentKey: null,
reverseKey: null,
target: 'users',
foreignKey: 'updatedById',
uiSchema: {
type: 'object',
title: '{{t("Last updated by")}}',
'x-component': 'AssociationField',
'x-component-props': {
fieldNames: {
value: 'id',
label: 'nickname',
},
},
'x-read-pretty': true,
},
targetKey: 'id',
},
],
template: 'tree',
view: false,
tree: 'adjacencyList',
schema: 'public',
filterTargetKey: 'id',
});

View File

@ -8,6 +8,8 @@ import { Container } from '@nocobase/utils';
import './actions'; import './actions';
import { SqlLoader } from '@hera/plugin-core'; import { SqlLoader } from '@hera/plugin-core';
import { DetailCheckService } from './services/detail-check-service'; import { DetailCheckService } from './services/detail-check-service';
import { CollectionRepository } from '@nocobase/plugin-collection-manager';
import { Repository } from '@nocobase/database';
export class PluginRentalServer extends Plugin { export class PluginRentalServer extends Plugin {
async afterAdd() {} async afterAdd() {}
@ -23,6 +25,39 @@ export class PluginRentalServer extends Plugin {
Container.get(ContractService).load(); Container.get(ContractService).load();
} }
async syncCollections(collectionName: string, categoryNames: string[]) {
const repo = this.db.getRepository<CollectionRepository>('collections');
await repo.db2cm(collectionName);
const categoriesRepo = this.db.getRepository<Repository>('collectionCategories');
const categories = await Promise.all(
categoryNames.map(async (name) => {
return await categoriesRepo.findOne({
filter: {
name,
},
});
}),
);
const collectionRepo = this.db.getRepository<Repository>('collectionCategory');
// 删除之前建立的所有分类
await collectionRepo.destroy({
filter: {
collectionName,
},
});
await collectionRepo.createMany({
records: categories
.filter((item) => typeof item.id !== 'undefined')
.map((item) => ({ collectionName, categoryId: item.id })),
});
}
async upgrade() {
await this.syncCollections('transfer_orders', ['测算相关']);
await this.syncCollections('undetermined_projects', ['测算相关']);
}
async install() {} async install() {}
async afterEnable() {} async afterEnable() {}