完善文档预览的效果修改
This commit is contained in:
+3
-114
@@ -42,123 +42,12 @@ function logPostgrestQuery(endpoint: string, params?: QueryParams, method: strin
|
||||
// 以可读格式单独打印每个参数
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined) {
|
||||
if (key === 'select' && typeof value === 'string') {
|
||||
// 美化 select 参数,使其看起来像 SQL 查询
|
||||
console.log(` - ${key}:`);
|
||||
const fields = value.replace(/\s+/g, ' ').trim().split(',');
|
||||
fields.forEach(field => {
|
||||
console.log(` ${field.trim()}`);
|
||||
});
|
||||
} else if (key === 'order' && typeof value === 'string') {
|
||||
// 格式化排序参数
|
||||
console.log(` - ${key}: ${value.replace(/\./g, ' ')}`); // 例如:created_at desc
|
||||
} else if (key === 'or' && typeof value === 'string') {
|
||||
// 格式化OR条件
|
||||
console.log(` - ${key}: ${value.replace(/\./g, ' -> ').replace(/,/g, ' 或 ')}`);
|
||||
} else {
|
||||
console.log(` - ${key}: ${JSON.stringify(value)}`);
|
||||
}
|
||||
console.log(` - ${key}: ${JSON.stringify(value)}`);
|
||||
}
|
||||
});
|
||||
|
||||
// 构建人类可读的简化URL
|
||||
const readableQueryString = Object.entries(params)
|
||||
.filter(([, value]) => value !== undefined)
|
||||
.map(([key, value]) => {
|
||||
if (key === 'select' && typeof value === 'string') {
|
||||
// 简化select查询
|
||||
return `${key}=...`;
|
||||
}
|
||||
return `${key}=${value}`;
|
||||
})
|
||||
.join('&');
|
||||
|
||||
console.log(`\n📦 可读URL: ${baseUrl}/${normalizedEndpoint}${readableQueryString ? '?' + readableQueryString : ''}`);
|
||||
|
||||
// 格式化查询为 PostgreSQL 风格的查询
|
||||
let postgrestQuery = `${method.toUpperCase()} `;
|
||||
|
||||
if (params.select && typeof params.select === 'string') {
|
||||
postgrestQuery += params.select.replace(/\s+/g, ' ').trim();
|
||||
} else {
|
||||
postgrestQuery += '*';
|
||||
}
|
||||
|
||||
postgrestQuery += ` FROM ${normalizedEndpoint}`;
|
||||
|
||||
const conditions: string[] = [];
|
||||
|
||||
// 添加过滤条件
|
||||
if (params.filter) {
|
||||
Object.entries(params.filter).forEach(([key, value]) => {
|
||||
if (value !== undefined && typeof value === 'string') {
|
||||
// 解析 eq.X, neq.X, gt.X 等
|
||||
const parts = value.toString().split('.');
|
||||
if (parts.length >= 2) {
|
||||
const operator = parts[0];
|
||||
const operatorMap: Record<string, string> = {
|
||||
'eq': '=',
|
||||
'neq': '!=',
|
||||
'gt': '>',
|
||||
'gte': '>=',
|
||||
'lt': '<',
|
||||
'lte': '<=',
|
||||
'like': 'LIKE',
|
||||
'ilike': 'ILIKE'
|
||||
};
|
||||
|
||||
const sqlOperator = operatorMap[operator] || operator;
|
||||
const value = parts.slice(1).join('.');
|
||||
conditions.push(`${key} ${sqlOperator} '${value}'`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 添加 OR 条件
|
||||
if (params.or) {
|
||||
if (typeof params.or === 'string') {
|
||||
if (params.or.startsWith('(') && params.or.endsWith(')')) {
|
||||
// 处理 or=(cond1,cond2) 格式
|
||||
const orConditions = params.or.slice(1, -1).split(',').map(cond => {
|
||||
const [field, operator] = cond.split('.');
|
||||
return `${field} ${operator.replace(/ilike/i, 'ILIKE')}`;
|
||||
});
|
||||
|
||||
if (orConditions.length > 0) {
|
||||
conditions.push(`(${orConditions.join(' OR ')})`);
|
||||
}
|
||||
} else {
|
||||
// 简单 or 条件
|
||||
conditions.push(params.or);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 添加WHERE子句
|
||||
if (conditions.length > 0) {
|
||||
postgrestQuery += ` WHERE ${conditions.join(' AND ')}`;
|
||||
}
|
||||
|
||||
// 添加ORDER BY
|
||||
if (params.order && typeof params.order === 'string') {
|
||||
const [field, direction] = params.order.split('.');
|
||||
postgrestQuery += ` ORDER BY ${field} ${direction.toUpperCase()}`;
|
||||
}
|
||||
|
||||
// 添加LIMIT和OFFSET
|
||||
if (params.limit !== undefined) {
|
||||
postgrestQuery += ` LIMIT ${params.limit}`;
|
||||
}
|
||||
|
||||
if (params.offset !== undefined) {
|
||||
postgrestQuery += ` OFFSET ${params.offset}`;
|
||||
}
|
||||
|
||||
console.log('\n📦 等效SQL查询:');
|
||||
console.log(postgrestQuery);
|
||||
console.log('=========================================\n');
|
||||
}
|
||||
|
||||
console.log('=========================================\n');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user