/** * SQL literal helpers * Build literal SQL values for direct interpolation into query strings. * Queries are fully formed strings — easy to copy and run in psql for debugging. */ // Escape a single value for safe interpolation into SQL function lit(val) { if (val === null || val === undefined) return 'NULL'; if (typeof val === 'boolean') return val ? 'TRUE' : 'FALSE'; if (typeof val === 'number') return String(Math.trunc(val)); if (typeof val === 'object') return `'${JSON.stringify(val).replace(/'/g, "''")}'`; return `'${String(val).replace(/'/g, "''")}'`; } // Format a value for a JSONB parameter — always JSON.stringifies first so that // plain strings become valid JSON ("ROBLOX" not ROBLOX), arrays and objects work too. function jsonLit(val) { if (val === null || val === undefined) return 'NULL'; return `'${JSON.stringify(val).replace(/'/g, "''")}'`; } // Format a JS array as a PostgreSQL text array literal: ARRAY['a','b'] function arr(val) { if (!Array.isArray(val) || val.length === 0) return "ARRAY[]::text[]"; return `ARRAY[${val.map(s => `'${String(s).replace(/'/g, "''")}'`).join(',')}]`; } module.exports = { lit, jsonLit, arr };