diff --git a/packages/core/client/src/acl/ACLProvider.tsx b/packages/core/client/src/acl/ACLProvider.tsx index b5bca5b1b4..b5f10cd71b 100644 --- a/packages/core/client/src/acl/ACLProvider.tsx +++ b/packages/core/client/src/acl/ACLProvider.tsx @@ -10,8 +10,7 @@ import { useCollection, useCollectionManager } from '../collection-manager'; import { useResourceActionContext } from '../collection-manager/ResourceActionProvider'; import { useRecord } from '../record-provider'; import { SchemaComponentOptions, useDesignable } from '../schema-component'; -import { useApp } from '../application'; -import { useDataSourceName } from '../block-provider/BlockProvider'; +import { useApp, useCollectionDataSourceName } from '../application'; export const ACLContext = createContext({}); @@ -93,7 +92,7 @@ export const ACLActionParamsContext = createContext({}); export const useACLRolesCheck = () => { const ctx = useContext(ACLContext); - const dataSourceName = useDataSourceName(); + const dataSourceName = useCollectionDataSourceName(); const { dataSources: dataSourcesAcl } = ctx?.data?.meta || {}; const data = { ...ctx?.data?.data, ...omit(dataSourcesAcl?.[dataSourceName], 'snippets') }; const getActionAlias = (actionPath: string) => { diff --git a/packages/core/client/src/application/collection/CollectionManager.tsx b/packages/core/client/src/application/collection/CollectionManager.tsx index 9f41d641a9..21a1aead9d 100644 --- a/packages/core/client/src/application/collection/CollectionManager.tsx +++ b/packages/core/client/src/application/collection/CollectionManager.tsx @@ -60,7 +60,7 @@ interface DataSource { [key: string]: any; } -type DataSourceName = string; +type DataSourceNameType = string; export interface CollectionManagerOptionsV2 { collections?: CollectionOptionsV2[]; @@ -76,11 +76,11 @@ type ReloadCallback = (collections: CollectionOptionsV2[]) => void; export class CollectionManagerV2 { public app: Application; - protected collections: Record> = {}; + protected collections: Record> = {}; protected collectionTemplateInstances: Record = {}; protected fieldInterfaceInstances: Record = {}; protected collectionMixins: CollectionMixinConstructor[] = []; - protected dataSourceMap: Record> = {}; + protected dataSourceMap: Record> = {}; protected collectionFieldGroups: Record = {}; protected mainDataSourceFn: MainDataSOurceFn; protected thirdDataSourceFn: ThirdDataResourceFn; diff --git a/packages/core/client/src/application/data-block/CollectionDataSourceProvider.tsx b/packages/core/client/src/application/data-block/CollectionDataSourceProvider.tsx new file mode 100644 index 0000000000..7b3348ed6b --- /dev/null +++ b/packages/core/client/src/application/data-block/CollectionDataSourceProvider.tsx @@ -0,0 +1,15 @@ +import React, { FC, ReactNode, createContext, useContext } from 'react'; + +export const CollectionDataSourceName = createContext(undefined); +CollectionDataSourceName.displayName = 'CollectionDataSourceName'; + +export const CollectionDataSourceProvider: FC<{ dataSource: string; children: ReactNode }> = ({ + dataSource, + children, +}) => { + return {children}; +}; + +export const useCollectionDataSourceName = () => { + return useContext(CollectionDataSourceName); +}; diff --git a/packages/core/client/src/application/data-block/DataBlockProvider.tsx b/packages/core/client/src/application/data-block/DataBlockProvider.tsx index 4051e32555..01560523ab 100644 --- a/packages/core/client/src/application/data-block/DataBlockProvider.tsx +++ b/packages/core/client/src/application/data-block/DataBlockProvider.tsx @@ -6,10 +6,12 @@ import { withDynamicSchemaProps } from '../hoc'; import { DataBlockResourceProviderV2 } from './DataBlockResourceProvider'; import { AssociationProviderV2, CollectionProviderV2, RecordV2 } from '../collection'; import { UseRequestOptions, UseRequestService } from '../../api-client'; +import { CollectionDataSourceProvider } from './CollectionDataSourceProvider'; export interface AllDataBlockProps { collection: string; association: string; + dataSource?: string; sourceId: string | number; filterByTk: string | number; record: RecordV2; @@ -21,26 +23,39 @@ export interface AllDataBlockProps { [index: string]: any; } -type CollectionCreate = Pick; +type CollectionCreate = Pick; interface CollectionGet - extends Pick { + extends Pick< + AllDataBlockProps, + 'collection' | 'dataSource' | 'filterByTk' | 'params' | 'requestService' | 'requestOptions' + > { action: 'get'; } interface CollectionList - extends Pick { + extends Pick { action: 'list'; } -type CollectionRecord = Pick; +type CollectionRecord = Pick< + AllDataBlockProps, + 'collection' | 'dataSource' | 'record' | 'requestService' | 'requestOptions' +>; -type AssociationCreate = Pick; +type AssociationCreate = Pick; interface AssociationGet extends Pick< AllDataBlockProps, - 'association' | 'sourceId' | 'parentRecord' | 'filterByTk' | 'params' | 'requestService' | 'requestOptions' + | 'association' + | 'dataSource' + | 'sourceId' + | 'parentRecord' + | 'filterByTk' + | 'params' + | 'requestService' + | 'requestOptions' > { action: 'get'; } @@ -48,14 +63,14 @@ interface AssociationGet interface AssociationList extends Pick< AllDataBlockProps, - 'association' | 'sourceId' | 'parentRecord' | 'params' | 'requestService' | 'requestOptions' + 'association' | 'dataSource' | 'sourceId' | 'parentRecord' | 'params' | 'requestService' | 'requestOptions' > { action: 'list'; } type AssociationRecord = Pick< AllDataBlockProps, - 'association' | 'record' | 'parentRecord' | 'requestService' | 'requestOptions' + 'association' | 'dataSource' | 'record' | 'parentRecord' | 'requestService' | 'requestOptions' >; type AllDataBlockType = { @@ -73,7 +88,7 @@ export type DataBlockProviderProps = AllDataBlockType[keyof AllDataBlockType]; export type UseDataBlockProps = ( props: DataBlockProviderProps & { [index: string]: any }, -) => Omit & { [index: string]: any }; +) => Omit & { [index: string]: any }; export interface DataBlockContextValue { props: AllDataBlockProps & T; @@ -85,7 +100,7 @@ DataBlockContextV2.displayName = 'DataBlockContextV2'; export const DataBlockProviderV2: FC = withDynamicSchemaProps( (props) => { - const { collection, association, children, ...resets } = props as Partial; + const { collection, association, dataSource, children, ...resets } = props as Partial; const AssociationOrCollection = useMemo(() => { if (association) { return { @@ -105,14 +120,16 @@ export const DataBlockProviderV2: FC - - - {children} - - + + + + {children} + + + ); }, diff --git a/packages/core/client/src/application/data-block/DataBlockResourceProvider.tsx b/packages/core/client/src/application/data-block/DataBlockResourceProvider.tsx index e073e2315b..7a5e03a605 100644 --- a/packages/core/client/src/application/data-block/DataBlockResourceProvider.tsx +++ b/packages/core/client/src/application/data-block/DataBlockResourceProvider.tsx @@ -3,20 +3,26 @@ import { IResource } from '@nocobase/sdk'; import { useAPIClient } from '../../api-client'; import { useDataBlockPropsV2 } from './DataBlockProvider'; +import { DEFAULT_DATA_SOURCE_NAME } from '../collection'; export const DataBlockResourceContextV2 = createContext(null); DataBlockResourceContextV2.displayName = 'DataBlockResourceContextV2'; export const DataBlockResourceProviderV2: FC<{ children?: ReactNode }> = ({ children }) => { const dataBlockProps = useDataBlockPropsV2(); - const { association, collection, sourceId } = dataBlockProps; + const { association, collection, dataSource, sourceId } = dataBlockProps; const api = useAPIClient(); + const headers = useMemo(() => { + if (dataSource && dataSource !== DEFAULT_DATA_SOURCE_NAME) { + return { 'x-connection': dataSource }; + } + }, [dataSource]); const resource = useMemo(() => { if (association) { - return api.resource(association, sourceId); + return api.resource(association, sourceId, headers); } - return api.resource(collection); - }, [api, association, collection, sourceId]); + return api.resource(collection, undefined, headers); + }, [api, association, collection, sourceId, headers]); return {children}; }; diff --git a/packages/core/client/src/application/data-block/index.ts b/packages/core/client/src/application/data-block/index.ts index ae2ee6dd4e..6dd5220486 100644 --- a/packages/core/client/src/application/data-block/index.ts +++ b/packages/core/client/src/application/data-block/index.ts @@ -1,3 +1,4 @@ export * from './DataBlockResourceProvider'; export * from './DataBlockRequestProvider'; export * from './DataBlockProvider'; +export * from './CollectionDataSourceProvider'; diff --git a/packages/core/client/src/block-provider/BlockProvider.tsx b/packages/core/client/src/block-provider/BlockProvider.tsx index bd78f1f773..ce5ca0bd90 100644 --- a/packages/core/client/src/block-provider/BlockProvider.tsx +++ b/packages/core/client/src/block-provider/BlockProvider.tsx @@ -23,6 +23,7 @@ import { useRecordIndex } from '../record-provider'; import { SharedFilterProvider } from './SharedFilterProvider'; import { useTemplateBlockContext } from './TemplateBlockProvider'; import { useAssociationNames } from './hooks'; +import { CollectionDataSourceProvider } from '../application/data-block'; export const BlockResourceContext = createContext(null); export const BlockAssociationContext = createContext(null); @@ -303,11 +304,6 @@ export const useBlockContext = () => { return useContext(BlockContext); }; -export const DataSourceName = createContext(undefined); -export const useDataSourceName = () => { - return useContext(DataSourceName); -}; - export const BlockProvider = (props: { name: string; resource: any; @@ -331,7 +327,7 @@ export const BlockProvider = (props: { return ( - + @@ -345,7 +341,7 @@ export const BlockProvider = (props: { - + ); }; diff --git a/packages/core/client/src/collection-manager/CollectionProvider.tsx b/packages/core/client/src/collection-manager/CollectionProvider.tsx index f7f8cdf4ff..b8791a9bd1 100644 --- a/packages/core/client/src/collection-manager/CollectionProvider.tsx +++ b/packages/core/client/src/collection-manager/CollectionProvider.tsx @@ -4,7 +4,7 @@ import { CollectionOptions } from './types'; import React from 'react'; import { DeletedPlaceholder } from '../application/collection/DeletedPlaceholder'; import { CollectionExtendsProvider } from './CollectionManagerProvider'; -import { useDataSourceName } from '../block-provider/BlockProvider'; +import { useCollectionDataSourceName } from '../application/data-block'; function getCollectionName(name?: string | CollectionOptions): string { if (!name) return undefined; @@ -20,7 +20,7 @@ export const CollectionProvider: FC<{ dataSource?: string; }> = ({ children, allowNull, name, dataSource, collection }) => { const collectionName = getCollectionName(name || collection); - const dataSourceName = useDataSourceName(); + const dataSourceName = useCollectionDataSourceName(); const dataSourceValue = dataSource || dataSourceName || undefined; const cm = useCollectionManagerV2(); const hasCollection = cm.getCollection(collectionName, { dataSource: dataSourceValue }); diff --git a/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts b/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts index 0071e36720..c73794df8d 100644 --- a/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts +++ b/packages/core/client/src/collection-manager/hooks/useCollectionManager.ts @@ -6,11 +6,11 @@ import { CollectionFieldOptions, CollectionOptions } from '../types'; import { useCollectionManagerV2 } from '../../application'; import { InheritanceCollectionMixin } from '../mixins/InheritanceCollectionMixin'; import { uid } from '@formily/shared'; -import { useDataSourceName } from '../../block-provider/BlockProvider'; +import { useCollectionDataSourceName } from '../../application/data-block'; export const useCollectionManager = (dataSourceName?: string) => { const cm = useCollectionManagerV2(); - const blockDataSourceName = useDataSourceName(); + const blockDataSourceName = useCollectionDataSourceName(); const dataSourceNameValue = dataSourceName || blockDataSourceName || undefined; const [random, setRandom] = useState(uid()); const { refresh } = useSchemaComponentContext(); diff --git a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockProvider.tsx b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockProvider.tsx index d6580f57f5..e2d0cb0150 100644 --- a/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockProvider.tsx +++ b/packages/plugins/@nocobase/plugin-workflow-manual/src/client/instruction/FormBlockProvider.tsx @@ -4,7 +4,7 @@ import { BlockRequestContext, CollectionProvider, DEFAULT_DATA_SOURCE_NAME, - DataSourceName, + CollectionDataSourceProvider, FormActiveFieldsProvider, FormBlockContext, FormV2, @@ -75,7 +75,7 @@ export function FormBlockProvider(props) { }, [field, form, params, service, updateAssociationValues]); return !userJob.status || values ? ( - + @@ -92,6 +92,6 @@ export function FormBlockProvider(props) { - + ) : null; }