@A0533057932
async function insertRecord(/*connection, */tableName, data) {
// הגנה בסיסית נגד SQL injection
const allowedTables = ['ads', 'ad_cities'];
if (!allowedTables.includes(tableName)) {
throw new Error("Invalid table name");
}
// אם מדובר באובייקט יחיד → עטוף במערך
const records = Array.isArray(data) ? data : [data];
if (records.length === 0) return;
const validColumns = ['phone', 'type', 'profession', 'recording_path', 'min_max_price', 'ring_mode', 'ad_id', 'city'];
for (const record of records) {
const invalidKeys = Object.keys(record).filter(key => !validColumns.includes(key));
if (invalidKeys.length > 0) {
throw new Error(`Invalid columns in record: ${invalidKeys.join(', ')}`);
}
}
try {
const result = await knex(tableName).insert(data);
return result; // ב-MySQL מחזיר מערך עם ה-ID של הרשומה הראשונה שנוצרה
} catch (err) {
console.error("Error inserting record:", err);
throw err;
}
}
ככה?