Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getSourceKeyByAssocation #3947

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ export class CollectionManager {
return this.getCollection(collectionName)?.getFields(predicate) || [];
}

getSourceKeyByAssocation(associationName: string) {
if (!associationName) {
return;
}
const field = this.getCollectionField(associationName);
// 字段不存在,返回空
if (!field) {
return;
}
// hasOne 和 hasMany 和 belongsToMany 的字段存在 sourceKey,所以会直接返回 sourceKey;
if (field.sourceKey) {
return field.sourceKey;
}
// belongsTo 不存在 sourceKey,所以会使用 filterTargetKey;
const sourceCollection = this.getCollection(associationName.split('.')[0]);
// source collection 不存在,返回空
if (!sourceCollection) {
return;
}
// 后面的主键和 id 是为了保险起见加上的;
return sourceCollection.filterTargetKey || sourceCollection.getPrimaryKey() || 'id';
}

/**
* @internal
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { IResource } from '@nocobase/sdk';
import React, { FC, ReactNode, createContext, useContext, useMemo } from 'react';

import { useCollectionManager } from '../collection';
import { useDataBlockProps } from './DataBlockProvider';
import { useAPIClient } from '../../api-client';
import { useCollectionManager } from '../collection';
import { CollectionRecord } from '../collection-record';
import { useDataSourceHeaders } from '../utils';
import { useDataBlockProps } from './DataBlockProvider';

export const DataBlockResourceContext = createContext<IResource>(null);
DataBlockResourceContext.displayName = 'DataBlockResourceContext';
Expand All @@ -23,13 +23,11 @@ export const DataBlockResourceProvider: FC<{ children?: ReactNode }> = ({ childr
return sourceId;
}
if (association && parentRecord) {
const associationCollection = cm.getCollection(association);
if (associationCollection) {
const parentRecordData = parentRecord instanceof CollectionRecord ? parentRecord.data : parentRecord;
return parentRecordData[associationCollection.sourceKey || 'id'];
}
const sourceKey = cm.getSourceKeyByAssocation(association);
const parentRecordData = parentRecord instanceof CollectionRecord ? parentRecord.data : parentRecord;
return parentRecordData[sourceKey];
}
}, [sourceId, parentRecord]);
}, [association, sourceId, parentRecord]);

const resource = useMemo(() => {
if (association) {
Expand Down
17 changes: 1 addition & 16 deletions packages/core/client/src/modules/blocks/useSourceKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { InheritanceCollectionMixin } from '../../collection-manager/mixins/InheritanceCollectionMixin';
import { useCollectionManager } from '../../data-source/collection/CollectionManagerProvider';

/**
Expand All @@ -8,19 +7,5 @@ import { useCollectionManager } from '../../data-source/collection/CollectionMan
*/
export const useSourceKey = (association: string) => {
const cm = useCollectionManager();

if (!association) return;

const associationField = cm.getCollectionField(association);

if (!associationField) {
return;
}

const sourceCollection = cm.getCollection<InheritanceCollectionMixin>(association.split('.')[0]);

// 1. hasOne 和 hasMany 和 belongsToMany 的字段存在 sourceKey,所以会直接返回 sourceKey;
// 2. belongsTo 不存在 sourceKey,所以会使用 filterTargetKey;
// 3. 后面的主键和 id 是为了保险起见加上的;
return associationField.sourceKey || sourceCollection.filterTargetKey || sourceCollection.getPrimaryKey() || 'id';
return cm.getSourceKeyByAssocation(association);
};
Loading