37 lines
1.5 KiB
JavaScript
37 lines
1.5 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import { createPostgresMcp } from '../src/core/index.js';
|
|
const usage = () => {
|
|
console.error('Usage: npx ts-node scripts/test-connection.ts <configPath> <envName> [schema] [tableName]');
|
|
console.error('Example: npx ts-node scripts/test-connection.ts config/postgres.local.json shcis_drworks_cpoe_pg dbo appsettings');
|
|
};
|
|
const main = async () => {
|
|
const [, , configPathArg, envName, schema, tableName] = process.argv;
|
|
if (!configPathArg || !envName) {
|
|
usage();
|
|
process.exit(1);
|
|
}
|
|
const configPath = path.resolve(process.cwd(), configPathArg);
|
|
const raw = fs.readFileSync(configPath, 'utf8');
|
|
const parsed = JSON.parse(raw);
|
|
const envs = parsed.environments ?? [];
|
|
const pg = createPostgresMcp(envs);
|
|
try {
|
|
console.log(`Connecting to env "${envName}" using ${configPath} ...`);
|
|
const searchPath = await pg.metadata.describeSearchPath(envName, schema);
|
|
console.log('Current search_path:', searchPath);
|
|
// Replace with an existing table in your schema to validate data access
|
|
const testTable = tableName ?? 'pg_tables';
|
|
const result = await pg.queries.execute(envName, `SELECT * FROM ${testTable} LIMIT 5`, [], schema ? { schema } : undefined);
|
|
console.log(`Query ok, ${result.rowCount} rows:`);
|
|
console.log(result.rows);
|
|
}
|
|
finally {
|
|
await pg.connections.closeAll();
|
|
}
|
|
};
|
|
main().catch((error) => {
|
|
console.error('Test failed:', error.message);
|
|
process.exit(1);
|
|
});
|