mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-02 03:02:19 +08:00
fix: data source load
This commit is contained in:
parent
f3d20b19a5
commit
c2ab0ac7be
@ -9,7 +9,9 @@
|
||||
|
||||
import { Plugin } from '../application/Plugin';
|
||||
|
||||
import { InheritanceCollectionMixin } from './mixins/InheritanceCollectionMixin';
|
||||
import { name } from 'packages/core/database/src/__tests__/fixtures/collections/tags';
|
||||
import { DataSource } from '../data-source/data-source/DataSource';
|
||||
import { DEFAULT_DATA_SOURCE_KEY, DEFAULT_DATA_SOURCE_TITLE } from '../data-source/data-source/DataSourceManager';
|
||||
import {
|
||||
CheckboxFieldInterface,
|
||||
CheckboxGroupFieldInterface,
|
||||
@ -53,14 +55,13 @@ import {
|
||||
UrlFieldInterface,
|
||||
UUIDFieldInterface,
|
||||
} from './interfaces';
|
||||
import { InheritanceCollectionMixin } from './mixins/InheritanceCollectionMixin';
|
||||
import {
|
||||
GeneralCollectionTemplate,
|
||||
SqlCollectionTemplate,
|
||||
TreeCollectionTemplate,
|
||||
ViewCollectionTemplate,
|
||||
} from './templates';
|
||||
import { DEFAULT_DATA_SOURCE_KEY, DEFAULT_DATA_SOURCE_TITLE } from '../data-source/data-source/DataSourceManager';
|
||||
import { DataSource } from '../data-source/data-source/DataSource';
|
||||
|
||||
class MainDataSource extends DataSource {
|
||||
async getDataSource() {
|
||||
@ -72,6 +73,7 @@ class MainDataSource extends DataSource {
|
||||
const collections = service?.data?.data || [];
|
||||
|
||||
return {
|
||||
key: 'main',
|
||||
collections,
|
||||
};
|
||||
}
|
||||
|
@ -83,8 +83,18 @@ export abstract class DataSource {
|
||||
|
||||
abstract getDataSource(): Promise<Omit<Partial<DataSourceOptions>, 'key'>> | Omit<Partial<DataSourceOptions>, 'key'>;
|
||||
|
||||
get flowEngineDataSourceManager() {
|
||||
return this.app.flowEngine?.context?.dataSourceManager;
|
||||
}
|
||||
|
||||
async reload() {
|
||||
const dataSource = await this.getDataSource();
|
||||
const flowEngineDataSourceManager = this.flowEngineDataSourceManager;
|
||||
if (flowEngineDataSourceManager) {
|
||||
flowEngineDataSourceManager.upsertDataSource(this.options);
|
||||
const ds = flowEngineDataSourceManager.getDataSource(this.key);
|
||||
ds.upsertCollections(dataSource.collections || []);
|
||||
}
|
||||
this.setOptions(dataSource);
|
||||
this.collectionManager.setCollections(dataSource.collections || []);
|
||||
this.reloadCallbacks.forEach((callback) => callback(dataSource.collections));
|
||||
|
@ -34,25 +34,12 @@ export class PluginFlowEngine extends Plugin {
|
||||
this.flowEngine.context['app'] = this.app;
|
||||
this.flowEngine.context['api'] = this.app.apiClient;
|
||||
this.flowEngine.context['dataSourceManager'] = dataSourceManager;
|
||||
try {
|
||||
const response = await this.app.apiClient.request<any>({
|
||||
url: '/collections:listMeta',
|
||||
});
|
||||
const mainDataSource = new DataSource({
|
||||
name: 'main',
|
||||
displayName: 'Main',
|
||||
});
|
||||
dataSourceManager.addDataSource(mainDataSource);
|
||||
const collections = response.data?.data || [];
|
||||
collections.forEach((collection) => {
|
||||
mainDataSource.addCollection(collection);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to load collections:', error);
|
||||
// Optionally, you can throw an error or handle it as needed
|
||||
}
|
||||
const mainDataSource = new DataSource({
|
||||
key: 'main',
|
||||
displayName: 'Main',
|
||||
});
|
||||
dataSourceManager.addDataSource(mainDataSource);
|
||||
this.app.addProvider(FlowEngineRunner, {});
|
||||
|
||||
// 注册通用 flow
|
||||
this.flowEngine.registerAction(DateTimeFormat);
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ RefreshActionModel.registerFlow({
|
||||
ctx.globals.message.error('No resource selected for refresh.');
|
||||
return;
|
||||
}
|
||||
currentResource.loading = true;
|
||||
await currentResource.refresh();
|
||||
},
|
||||
},
|
||||
|
@ -11,8 +11,7 @@ import { Schema } from '@formily/json-schema';
|
||||
import { observable } from '@formily/reactive';
|
||||
|
||||
export interface DataSourceOptions extends Record<string, any> {
|
||||
name: string;
|
||||
use?: typeof DataSource;
|
||||
key: string;
|
||||
displayName?: string;
|
||||
description?: string;
|
||||
[key: string]: any;
|
||||
@ -25,20 +24,28 @@ export class DataSourceManager {
|
||||
this.dataSources = observable.shallow<Map<string, DataSource>>(new Map());
|
||||
}
|
||||
|
||||
addDataSource(ds: DataSource | DataSourceOptions) {
|
||||
if (this.dataSources.has(ds.name)) {
|
||||
throw new Error(`DataSource with name ${ds.name} already exists`);
|
||||
addDataSource(ds: DataSource | DataSourceOptions, options: Record<string, any> = {}) {
|
||||
if (this.dataSources.has(ds.key)) {
|
||||
throw new Error(`DataSource with name ${ds.key} already exists`);
|
||||
}
|
||||
if (ds instanceof DataSource) {
|
||||
this.dataSources.set(ds.name, ds);
|
||||
this.dataSources.set(ds.key, ds);
|
||||
} else {
|
||||
const clz = ds.use || DataSource;
|
||||
this.dataSources.set(ds.name, new clz(ds));
|
||||
this.dataSources.set(ds.key, new clz(ds));
|
||||
}
|
||||
}
|
||||
|
||||
removeDataSource(name: string) {
|
||||
this.dataSources.delete(name);
|
||||
upsertDataSource(ds: DataSource | DataSourceOptions) {
|
||||
if (this.dataSources.has(ds.key)) {
|
||||
this.dataSources.get(ds.key)?.setOptions(ds);
|
||||
} else {
|
||||
this.addDataSource(ds);
|
||||
}
|
||||
}
|
||||
|
||||
removeDataSource(key: string) {
|
||||
this.dataSources.delete(key);
|
||||
}
|
||||
|
||||
clearDataSources() {
|
||||
@ -49,19 +56,19 @@ export class DataSourceManager {
|
||||
return Array.from(this.dataSources.values());
|
||||
}
|
||||
|
||||
getDataSource(name: string): DataSource | undefined {
|
||||
return this.dataSources.get(name);
|
||||
getDataSource(key: string): DataSource | undefined {
|
||||
return this.dataSources.get(key);
|
||||
}
|
||||
|
||||
getCollection(dataSourceName: string, collectionName: string): Collection | undefined {
|
||||
const ds = this.getDataSource(dataSourceName);
|
||||
getCollection(dataSourceKey: string, collectionName: string): Collection | undefined {
|
||||
const ds = this.getDataSource(dataSourceKey);
|
||||
if (!ds) return undefined;
|
||||
return ds.collectionManager.getCollection(collectionName);
|
||||
}
|
||||
|
||||
getCollectionField(fieldPathWithDataSource: string) {
|
||||
const [dataSourceName, ...otherKeys] = fieldPathWithDataSource.split('.');
|
||||
const ds = this.getDataSource(dataSourceName);
|
||||
const [dataSourceKey, ...otherKeys] = fieldPathWithDataSource.split('.');
|
||||
const ds = this.getDataSource(dataSourceKey);
|
||||
if (!ds) return undefined;
|
||||
return ds.getCollectionField(otherKeys.join('.'));
|
||||
}
|
||||
@ -76,8 +83,8 @@ export class DataSource {
|
||||
this.collectionManager = new CollectionManager(this);
|
||||
}
|
||||
|
||||
get name() {
|
||||
return this.options.name;
|
||||
get key() {
|
||||
return this.options.key;
|
||||
}
|
||||
|
||||
getCollections(): Collection[] {
|
||||
@ -96,6 +103,14 @@ export class DataSource {
|
||||
return this.collectionManager.updateCollection(newOptions);
|
||||
}
|
||||
|
||||
upsertCollection(options: CollectionOptions) {
|
||||
return this.collectionManager.upsertCollection(options);
|
||||
}
|
||||
|
||||
upsertCollections(collections: CollectionOptions[]) {
|
||||
return this.collectionManager.upsertCollections(collections);
|
||||
}
|
||||
|
||||
removeCollection(name: string) {
|
||||
return this.collectionManager.removeCollection(name);
|
||||
}
|
||||
@ -114,7 +129,7 @@ export class DataSource {
|
||||
const fieldName = otherKeys.join('.');
|
||||
const collection = this.getCollection(collectionName);
|
||||
if (!collection) {
|
||||
throw new Error(`Collection ${collectionName} not found in data source ${this.name}`);
|
||||
throw new Error(`Collection ${collectionName} not found in data source ${this.key}`);
|
||||
}
|
||||
const field = collection.getField(fieldName);
|
||||
if (!field) {
|
||||
@ -122,10 +137,6 @@ export class DataSource {
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
refresh() {
|
||||
// 刷新数据源
|
||||
}
|
||||
}
|
||||
|
||||
export interface CollectionOptions {
|
||||
@ -166,6 +177,25 @@ export class CollectionManager {
|
||||
collection.setOptions(newOptions);
|
||||
}
|
||||
|
||||
upsertCollection(options: CollectionOptions) {
|
||||
if (this.collections.has(options.name)) {
|
||||
this.updateCollection(options);
|
||||
} else {
|
||||
this.addCollection(options);
|
||||
}
|
||||
return this.getCollection(options.name);
|
||||
}
|
||||
|
||||
upsertCollections(collections: CollectionOptions[]) {
|
||||
for (const collection of collections) {
|
||||
if (this.collections.has(collection.name)) {
|
||||
this.updateCollection(collection);
|
||||
} else {
|
||||
this.addCollection(collection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getCollection(name: string): Collection | undefined {
|
||||
return this.collections.get(name);
|
||||
}
|
||||
@ -228,6 +258,7 @@ export class Collection {
|
||||
Object.keys(this.options).forEach((key) => delete this.options[key]);
|
||||
Object.assign(this.options, newOptions);
|
||||
this.initInherits();
|
||||
this.upsertFields(this.options.fields || []);
|
||||
}
|
||||
|
||||
getFields(): CollectionField[] {
|
||||
@ -258,12 +289,22 @@ export class Collection {
|
||||
}
|
||||
}
|
||||
|
||||
upsertFields(fields: Record<string, any>[] = []) {
|
||||
for (const field of fields) {
|
||||
if (this.fields.has(field.name)) {
|
||||
this.fields.get(field.name).setOptions(field);
|
||||
} else {
|
||||
this.addField(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getField(fieldName: string): CollectionField | undefined {
|
||||
return this.fields.get(fieldName);
|
||||
}
|
||||
|
||||
getFullFieldPath(name: string): string {
|
||||
return this.dataSource.name + '.' + this.name + '.' + name;
|
||||
return this.dataSource.key + '.' + this.name + '.' + name;
|
||||
}
|
||||
|
||||
addField(field: CollectionField | Record<string, any>) {
|
||||
@ -311,7 +352,7 @@ export class CollectionField {
|
||||
}
|
||||
|
||||
get fullpath() {
|
||||
return this.collection.dataSource.name + '.' + this.collection.name + '.' + this.name;
|
||||
return this.collection.dataSource.key + '.' + this.collection.name + '.' + this.name;
|
||||
}
|
||||
|
||||
get name() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user