Refactor NocoDB settings to support dynamic table configuration and update related validation
All checks were successful
Build And Push Image / docker (push) Successful in 3m13s
All checks were successful
Build And Push Image / docker (push) Successful in 3m13s
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { readFile, writeFile, mkdir, access, constants } from 'fs/promises';
|
||||
import { existsSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { createCipheriv, createDecipheriv, randomBytes, pbkdf2Sync } from 'crypto';
|
||||
import type { NocoDBSettings } from '~/utils/types';
|
||||
@@ -13,10 +14,20 @@ interface EffectiveNocoDB {
|
||||
url: string;
|
||||
token: string;
|
||||
baseId: string;
|
||||
tableId: string;
|
||||
tables: { [tableName: string]: string };
|
||||
}
|
||||
|
||||
const CONFIG_DIR = '/app/data';
|
||||
// Support both Docker (/app/data) and local development (./data) paths
|
||||
const getConfigDir = () => {
|
||||
// Check if running in Docker container
|
||||
if (process.env.NODE_ENV === 'production' || existsSync('/app/data')) {
|
||||
return '/app/data';
|
||||
}
|
||||
// Local development
|
||||
return './data';
|
||||
};
|
||||
|
||||
const CONFIG_DIR = getConfigDir();
|
||||
const CONFIG_FILE = join(CONFIG_DIR, 'admin-config.json');
|
||||
const BACKUP_FILE = join(CONFIG_DIR, 'admin-config.backup.json');
|
||||
const LOG_FILE = join(CONFIG_DIR, 'admin-config.log');
|
||||
@@ -169,6 +180,15 @@ export async function loadAdminConfig(): Promise<AdminConfiguration | null> {
|
||||
|
||||
console.log('[admin-config] Configuration loaded from file');
|
||||
configCache = config;
|
||||
|
||||
// Update global nocodb configuration
|
||||
try {
|
||||
const { setGlobalNocoDBConfig } = await import('./nocodb');
|
||||
setGlobalNocoDBConfig(getEffectiveNocoDBConfig());
|
||||
} catch (error) {
|
||||
console.error('[admin-config] Failed to update global configuration:', error);
|
||||
}
|
||||
|
||||
return config;
|
||||
} catch (error) {
|
||||
console.log('[admin-config] No configuration file found, using defaults');
|
||||
@@ -207,6 +227,15 @@ export async function saveAdminConfig(config: NocoDBSettings, updatedBy: string)
|
||||
console.log('[admin-config] Configuration saved to file');
|
||||
await logConfigChange('CONFIG_SAVED', updatedBy, { url: config.url, baseId: config.baseId });
|
||||
|
||||
// Update global nocodb configuration immediately
|
||||
try {
|
||||
const { setGlobalNocoDBConfig } = await import('./nocodb');
|
||||
setGlobalNocoDBConfig(getEffectiveNocoDBConfig());
|
||||
console.log('[admin-config] Global configuration updated immediately');
|
||||
} catch (error) {
|
||||
console.error('[admin-config] Failed to update global configuration after save:', error);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('[admin-config] Failed to save configuration:', error);
|
||||
await logConfigChange('CONFIG_SAVE_FAILED', updatedBy, { error: error instanceof Error ? error.message : String(error) });
|
||||
@@ -225,7 +254,7 @@ export function getEffectiveNocoDBConfig(): EffectiveNocoDB {
|
||||
url: runtimeConfig.nocodb?.url || 'https://database.monacousa.org',
|
||||
token: runtimeConfig.nocodb?.token || '',
|
||||
baseId: runtimeConfig.nocodb?.baseId || '',
|
||||
tableId: 'members-table-id' // This would be configured
|
||||
tables: { members: 'members-table-id' } // Default table mapping
|
||||
};
|
||||
|
||||
// Override with file configuration if available
|
||||
@@ -234,7 +263,7 @@ export function getEffectiveNocoDBConfig(): EffectiveNocoDB {
|
||||
url: configCache.nocodb.url || envConfig.url,
|
||||
token: configCache.nocodb.apiKey || envConfig.token,
|
||||
baseId: configCache.nocodb.baseId || envConfig.baseId,
|
||||
tableId: configCache.nocodb.tableId || envConfig.tableId
|
||||
tables: configCache.nocodb.tables || envConfig.tables
|
||||
};
|
||||
}
|
||||
|
||||
@@ -253,7 +282,7 @@ export async function getCurrentConfig(): Promise<NocoDBSettings> {
|
||||
url: config.nocodb.url || runtimeConfig.nocodb?.url || 'https://database.monacousa.org',
|
||||
apiKey: config.nocodb.apiKey ? '••••••••••••••••' : '',
|
||||
baseId: config.nocodb.baseId || runtimeConfig.nocodb?.baseId || '',
|
||||
tableId: config.nocodb.tableId || 'members-table-id'
|
||||
tables: config.nocodb.tables || { members: 'members-table-id' }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -262,7 +291,7 @@ export async function getCurrentConfig(): Promise<NocoDBSettings> {
|
||||
url: runtimeConfig.nocodb?.url || 'https://database.monacousa.org',
|
||||
apiKey: runtimeConfig.nocodb?.token ? '••••••••••••••••' : '',
|
||||
baseId: runtimeConfig.nocodb?.baseId || '',
|
||||
tableId: 'members-table-id'
|
||||
tables: { members: 'members-table-id' }
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -28,17 +28,13 @@ export interface EntityResponse<T> {
|
||||
|
||||
// Dynamic table ID getter - will use configured table ID from admin panel
|
||||
export const getTableId = (tableName: 'Members'): string => {
|
||||
try {
|
||||
// Try to get table ID from persistent configuration
|
||||
const { getEffectiveNocoDBConfig } = require('./admin-config');
|
||||
const effectiveConfig = getEffectiveNocoDBConfig();
|
||||
|
||||
if (tableName === 'Members' && effectiveConfig.tableId) {
|
||||
console.log(`[nocodb] Using configured table ID for ${tableName}:`, effectiveConfig.tableId);
|
||||
return effectiveConfig.tableId;
|
||||
// Try to get table ID from global configuration first
|
||||
if (globalNocoDBConfig?.tables && tableName === 'Members') {
|
||||
const tableId = globalNocoDBConfig.tables['members'] || globalNocoDBConfig.tables['Members'];
|
||||
if (tableId) {
|
||||
console.log(`[nocodb] Using global table ID for ${tableName}:`, tableId);
|
||||
return tableId;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`[nocodb] Admin config not available for table ID, using fallback`);
|
||||
}
|
||||
|
||||
// Fallback to default
|
||||
@@ -102,31 +98,35 @@ export const formatNationalitiesAsString = (nationalities: string[]): string =>
|
||||
return nationalities.filter(n => n && n.trim()).join(',');
|
||||
};
|
||||
|
||||
// Global variable to store effective configuration
|
||||
let globalNocoDBConfig: any = null;
|
||||
|
||||
// Function to set the global configuration (called by the admin-config system)
|
||||
export const setGlobalNocoDBConfig = (config: any) => {
|
||||
globalNocoDBConfig = config;
|
||||
console.log('[nocodb] Global configuration updated:', config.url);
|
||||
};
|
||||
|
||||
export const getNocoDbConfiguration = () => {
|
||||
try {
|
||||
// Try to use the persistent configuration system
|
||||
const { getEffectiveNocoDBConfig } = require('./admin-config');
|
||||
const effectiveConfig = getEffectiveNocoDBConfig();
|
||||
|
||||
const config = {
|
||||
url: effectiveConfig.url,
|
||||
token: effectiveConfig.token,
|
||||
baseId: effectiveConfig.baseId
|
||||
// Try to use the global configuration first
|
||||
if (globalNocoDBConfig) {
|
||||
console.log('[nocodb] Using global configuration - URL:', globalNocoDBConfig.url);
|
||||
return {
|
||||
url: globalNocoDBConfig.url,
|
||||
token: globalNocoDBConfig.token,
|
||||
baseId: globalNocoDBConfig.baseId
|
||||
};
|
||||
|
||||
console.log('[nocodb] Using effective configuration - URL:', config.url);
|
||||
return config;
|
||||
} catch (error) {
|
||||
// Fallback to runtime config if admin config is not available
|
||||
console.log('[nocodb] Admin config not available, using runtime config');
|
||||
const config = useRuntimeConfig().nocodb;
|
||||
const fallbackConfig = {
|
||||
...config,
|
||||
url: config.url || 'https://database.monacousa.org'
|
||||
};
|
||||
console.log('[nocodb] Fallback configuration URL:', fallbackConfig.url);
|
||||
return fallbackConfig;
|
||||
}
|
||||
|
||||
// Fallback to runtime config
|
||||
console.log('[nocodb] Global config not available, using runtime config');
|
||||
const config = useRuntimeConfig().nocodb;
|
||||
const fallbackConfig = {
|
||||
...config,
|
||||
url: config.url || 'https://database.monacousa.org'
|
||||
};
|
||||
console.log('[nocodb] Fallback configuration URL:', fallbackConfig.url);
|
||||
return fallbackConfig;
|
||||
};
|
||||
|
||||
export const createTableUrl = (table: Table | string) => {
|
||||
|
||||
Reference in New Issue
Block a user