Type relationship endpoints

This commit is contained in:
Andrew Kingston 2024-12-03 10:06:57 +00:00
parent ac05ccd6a8
commit e938fddeed
No known key found for this signature in database
4 changed files with 43 additions and 28 deletions

View File

@ -1,21 +0,0 @@
export const buildRelationshipEndpoints = API => ({
/**
* Fetches related rows for a certain field of a certain row.
* @param tableId the ID of the table to fetch from
* @param rowId the ID of the row to fetch related rows for
* @param fieldName the name of the relationship field
*/
fetchRelationshipData: async ({ tableId, rowId, fieldName }) => {
if (!tableId || !rowId) {
return []
}
const response = await API.get({
url: `/api/${tableId}/${rowId}/enrich?field=${fieldName}`,
})
if (!fieldName) {
return response || []
} else {
return response[fieldName] || []
}
},
})

View File

@ -0,0 +1,31 @@
import { Row } from "@budibase/types"
import { BaseAPIClient } from "./types"
export interface RelationshipEndpoints {
fetchRelationshipData: (
sourceId: string,
rowId: string,
fieldName?: string
) => Promise<Row[]>
}
export const buildRelationshipEndpoints = (
API: BaseAPIClient
): RelationshipEndpoints => ({
/**
* Fetches related rows for a certain field of a certain row.
* @param sourceId the ID of the table to fetch from
* @param rowId the ID of the row to fetch related rows for
* @param fieldName the name of the relationship field
*/
fetchRelationshipData: async (sourceId, rowId, fieldName) => {
const response = await API.get<Row>({
url: `/api/${sourceId}/${rowId}/enrich?field=${fieldName}`,
})
if (!fieldName) {
return [response]
} else {
return response[fieldName] || []
}
},
})

View File

@ -20,6 +20,7 @@ import { OtherEndpoints } from "./other"
import { PermissionEndpoints } from "./permissions" import { PermissionEndpoints } from "./permissions"
import { PluginEndpoins } from "./plugins" import { PluginEndpoins } from "./plugins"
import { QueryEndpoints } from "./queries" import { QueryEndpoints } from "./queries"
import { RelationshipEndpoints } from "./relationships"
export enum HTTPMethod { export enum HTTPMethod {
POST = "POST", POST = "POST",
@ -111,4 +112,5 @@ export type APIClient = BaseAPIClient &
OtherEndpoints & OtherEndpoints &
PermissionEndpoints & PermissionEndpoints &
PluginEndpoins & PluginEndpoins &
QueryEndpoints & { [key: string]: any } QueryEndpoints &
RelationshipEndpoints & { [key: string]: any }

View File

@ -3,13 +3,16 @@ import DataFetch from "./DataFetch.js"
export default class RelationshipFetch extends DataFetch { export default class RelationshipFetch extends DataFetch {
async getData() { async getData() {
const { datasource } = this.options const { datasource } = this.options
if (!datasource?.rowId || !datasource?.rowTableId) {
return { rows: [] }
}
try { try {
const res = await this.API.fetchRelationshipData({ const res = await this.API.fetchRelationshipData(
rowId: datasource?.rowId, datasource.rowTableId,
tableId: datasource?.rowTableId, datasource.rowId,
fieldName: datasource?.fieldName, datasource.fieldName
}) )
return { rows: res || [] } return { rows: res }
} catch (error) { } catch (error) {
return { rows: [] } return { rows: [] }
} }