add source prefix to columns

This commit is contained in:
Fendy Heryanto
2026-01-03 06:18:57 +00:00
parent 0d1f5bbd54
commit 30713218cb
4 changed files with 48 additions and 6 deletions

View File

@@ -27,6 +27,7 @@ import type {
XcFilterWithAlias,
} from '~/db/sql-data-mapper/lib/BaseModel';
import type { Filter, GridViewColumn } from '~/models';
import { swaggerSanitizeSchemaName } from '~/helpers/stringHelpers';
import { NcError } from '~/helpers/catchError';
import { defaultLimitConfig } from '~/helpers/extractLimitAndOffset';
import {
@@ -813,3 +814,39 @@ export function transformObjectKeys(
});
return result;
}
/**
* Get database-specific array aggregation expression for team roles or similar use cases
* Returns a Knex raw expression that aggregates distinct values into an array/JSON array
* based on the database client (PostgreSQL, MySQL, SQLite)
*
* @param knex - Knex instance
* @param knexConnection - Knex connection instance (to get client type)
* @param columnName - Column name to aggregate (e.g., 'wta.roles')
* @param alias - Alias for the aggregated column
* @returns Knex raw expression for array aggregation
*/
export function getArrayAggExpression(
knex: CustomKnex,
knexConnection: any,
columnName: string,
alias: string,
): Knex.Raw {
const client = knexConnection.client.config.client;
// Note: columnName and alias are controlled by our code, so it's safe to use directly
const exprMap: Record<string, string> = {
pg: `ARRAY_AGG(DISTINCT ${columnName}) FILTER (WHERE ${columnName} IS NOT NULL) AS ${alias}`,
mysql2: `JSON_ARRAYAGG(DISTINCT ${columnName}) AS ${alias}`,
sqlite3: `json_group_array(DISTINCT ${columnName}) AS ${alias}`,
};
// fallback to mysql2 query
return knex.raw(exprMap[client] || exprMap.mysql2);
}
export function swaggerGetSourcePrefix(source?: Source) {
return source?.isMeta()
? ''
: `${swaggerSanitizeSchemaName(source?.alias || 'Source')}_`;
}

View File

@@ -12,6 +12,7 @@ import type {
} from '~/models';
import type { NcContext } from '~/interface/config';
import Noco from '~/Noco';
import { swaggerGetSourcePrefix } from '~/helpers/dbHelpers';
export interface SwaggerView {
view: View;
@@ -50,9 +51,7 @@ export async function prepareSwaggerGenerationData({
for (const model of models) {
const source = sourcesMap.get(model.source_id);
const sourcePrefix = source?.isMeta()
? ''
: `${swaggerSanitizeSchemaName(source?.alias || 'Source')}_`;
const sourcePrefix = swaggerGetSourcePrefix(source);
const tableName = `${sourcePrefix}${model.title}`;
// Handle duplicate table names by adding a number suffix

View File

@@ -48,14 +48,18 @@ async function processColumnToSwaggerField(
// skip if refTable undefined or cross base link
if (relTable && relTable.base_id === context.base_id) {
field.$ref = `#/components/schemas/${relTable.title}Request`;
field.$ref = `#/components/schemas/${swaggerGetSourcePrefix(
source,
)}${relTable.title}Request`;
}
} else {
field.type = 'array';
// skip if refTable undefined or cross base link
if (relTable && relTable.base_id === context.base_id) {
field.items = {
$ref: `#/components/schemas/${relTable.title}Request`,
$ref: `#/components/schemas/${swaggerGetSourcePrefix(source)}${
relTable.title
}Request`,
};
}
}

View File

@@ -46,7 +46,9 @@ async function processColumnToSwaggerField(
field.type = undefined;
// skip if refTable undefined or cross base link
if (relTable && relTable.base_id === context.base_id) {
field.$ref = `#/components/schemas/${relTable.title}Request`;
field.$ref = `#/components/schemas/${swaggerGetSourcePrefix(
source,
)}${relTable.title}Request`;
}
}
}