55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
const { Client } = require('pg');
|
|
require('dotenv').config({ quiet: true });
|
|
|
|
const client = new Client({
|
|
host: process.env.PGHOST || 'localhost',
|
|
user: process.env.PGUSER || 'postgres',
|
|
port: Number(process.env.PGPORT) || 5432,
|
|
password: process.env.PGPASSWORD || '1234',
|
|
database: process.env.PGDATABASE || 'invima'
|
|
});
|
|
|
|
(async () => {
|
|
try {
|
|
await client.connect();
|
|
const { rows } = await client.query(
|
|
"SELECT * FROM productos WHERE nombre_comercial ILIKE '%rx%'"
|
|
);
|
|
console.log(rows);
|
|
|
|
const hacerCambios = false;
|
|
if (hacerCambios) {
|
|
try {
|
|
await client.query('BEGIN');
|
|
|
|
const sqlInsert = `
|
|
INSERT INTO productos (id_producto, nombre_comercial, categoria_general)
|
|
VALUES ($1, $2, $3)
|
|
RETURNING *;
|
|
`;
|
|
const valoresInsert = ['Prueba API', 'nombre API', 'Dispositivo'];
|
|
const ins = await client.query(sqlInsert, valoresInsert);
|
|
console.log('Insertado:', ins.rows[0]);
|
|
|
|
const insert2 = ['Prueba API 2', 'nombre API 2', 'Dispositivo'];
|
|
const insertar2 = await client.query(sqlInsert, insert2);
|
|
console.log('Exito insertar segunda cosa', insertar2.rows[0]);
|
|
|
|
await client.query('COMMIT');
|
|
|
|
} catch (e) {
|
|
await client.query('ROLLBACK');
|
|
console.error('Error en cambios (rollback):', e.message);
|
|
}
|
|
}
|
|
|
|
const r2 = await client.query("SELECT * FROM productos WHERE id_producto ILIKE '%prueba%'");
|
|
console.table(r2.rows);
|
|
|
|
} catch (err) {
|
|
console.error('DB error:', err.message);
|
|
} finally {
|
|
await client.end();
|
|
}
|
|
})();
|