mcp-quickbase
Version:
Work with Quickbase via Model Context Protocol
126 lines • 4.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetRelationshipsTool = void 0;
const base_1 = require("../base");
const logger_1 = require("../../utils/logger");
const logger = (0, logger_1.createLogger)("GetRelationshipsTool");
/**
* Tool for retrieving all table-to-table relationships for a specified table
*/
class GetRelationshipsTool extends base_1.BaseTool {
/**
* Constructor
* @param client Quickbase client
*/
constructor(client) {
super(client);
this.name = "get_relationships";
this.description = "Gets all table-to-table relationships for a specified table. Returns both relationships " +
"where this table is the child (has reference fields pointing to parents) and where this " +
"table is the parent (has child tables referencing it). Use this tool to explore table " +
"structure and understand data connections before modifying relationships.";
/**
* Parameter schema for get_relationships
*/
this.paramSchema = {
type: "object",
properties: {
table_id: {
type: "string",
description: "The ID of the Quickbase table (DBID)",
},
skip: {
type: "number",
description: "Number of relationships to skip for pagination (default: 0)",
},
},
required: ["table_id"],
};
}
/**
* Run the get_relationships tool
* @param params Tool parameters
* @returns Relationships for the table
*/
async run(params) {
const { table_id, skip = 0 } = params;
logger.info("Getting relationships for table", { tableId: table_id, skip });
// Prepare query parameters
const queryParams = {};
if (skip > 0) {
queryParams.skip = skip.toString();
}
// Get relationships for the table
const response = await this.client.request({
method: "GET",
path: `/tables/${table_id}/relationships`,
params: Object.keys(queryParams).length > 0 ? queryParams : undefined,
});
if (!response.success || !response.data) {
logger.error("Failed to get relationships", {
error: response.error,
tableId: table_id,
});
throw new Error(response.error?.message || "Failed to get relationships");
}
// Parse and validate the API response
const data = response.data;
// Validate relationships array exists
const rawRelationships = data.relationships;
if (!Array.isArray(rawRelationships)) {
logger.error("Relationships response missing relationships array", {
data,
});
throw new Error("Relationships response does not contain relationships array");
}
// Transform API response to our interface
const relationships = rawRelationships.map((rel) => {
const foreignKeyField = rel.foreignKeyField;
const lookupFields = rel.lookupFields;
const summaryFields = rel.summaryFields;
return {
id: rel.id,
parentTableId: rel.parentTableId,
childTableId: rel.childTableId,
foreignKeyField: foreignKeyField
? {
id: foreignKeyField.id,
label: foreignKeyField.label,
type: foreignKeyField.type,
}
: { id: 0, label: "", type: "" },
isCrossApp: rel.isCrossApp || false,
lookupFields: (lookupFields || []).map((field) => ({
id: field.id,
label: field.label,
type: field.type,
})),
summaryFields: (summaryFields || []).map((field) => ({
id: field.id,
label: field.label,
type: field.type,
})),
};
});
// Extract metadata from response
const metadata = data.metadata;
const totalRelationships = metadata?.totalRelationships ?? relationships.length;
const numRelationships = metadata?.numRelationships ?? relationships.length;
logger.info(`Found ${relationships.length} relationships for table`, {
tableId: table_id,
totalRelationships,
numRelationships,
skip,
});
return {
relationships,
metadata: {
totalRelationships,
numRelationships,
skip,
},
};
}
}
exports.GetRelationshipsTool = GetRelationshipsTool;
//# sourceMappingURL=get_relationships.js.map