fix(backend): Convert legacy queries and add translate wrapper
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* transform.js
|
||||
* Converte server.js de sintaxe MySQL/adapter para PostgreSQL nativo (pg).
|
||||
* Uso: node transform.js > server_new.js
|
||||
*/
|
||||
import { readFileSync, writeFileSync } from 'fs';
|
||||
|
||||
let code = readFileSync('./server.js', 'utf8');
|
||||
|
||||
// 1. Remove the dead initTables function (MySQL DDL, never called)
|
||||
code = code.replace(/\/\/ Initialization: Create tables if not exist[\s\S]*?^\/\/ initTables\(\); removed from here\s*/m, '');
|
||||
|
||||
// 2. Fix all simple ? placeholders in pool.query calls
|
||||
// Strategy: for each pool.query or client.query call, replace ? sequentially with $1, $2...
|
||||
function fixPlaceholders(sql) {
|
||||
let i = 0;
|
||||
return sql.replace(/\?/g, () => `$${++i}`);
|
||||
}
|
||||
|
||||
// 3. Replace INSERT INTO table SET ? with helper call marker
|
||||
// 4. Replace UPDATE table SET ? WHERE id = ? with helper call marker
|
||||
// These need manual per-call replacement, done below
|
||||
|
||||
// Fix simple queries with ? -> $n (for strings not containing SET ?)
|
||||
code = code.replace(/pool\.query\('([^']*?)'\s*,\s*\[([^\]]+)\]\)/g, (match, sql, params) => {
|
||||
if (sql.includes('SET ?') || sql.includes('SET?')) return match; // skip complex
|
||||
const fixed = fixPlaceholders(sql);
|
||||
return `pool.query('${fixed}', [${params}])`;
|
||||
});
|
||||
|
||||
code = code.replace(/pool\.query\(`([\s\S]*?)`\s*,\s*\[([^\]]+)\]\)/g, (match, sql, params) => {
|
||||
if (sql.includes('SET ?') || sql.includes('ON DUPLICATE')) return match;
|
||||
const fixed = fixPlaceholders(sql);
|
||||
return `pool.query(\`${fixed}\`, [${params}])`;
|
||||
});
|
||||
|
||||
// Fix client.query inside withTransaction
|
||||
code = code.replace(/client\.query\('([^']*?)'\s*,\s*\[([^\]]+)\]\)/g, (match, sql, params) => {
|
||||
if (sql.includes('SET ?')) return match;
|
||||
const fixed = fixPlaceholders(sql);
|
||||
return `client.query('${fixed}', [${params}])`;
|
||||
});
|
||||
|
||||
writeFileSync('./server_transformed.js', code, 'utf8');
|
||||
console.log('Done. Check server_transformed.js');
|
||||
Reference in New Issue
Block a user