Untitled

 avatar
unknown
javascript
a year ago
1.1 kB
5
Indexable
async function connect() {
    if (global.connection)
        return global.connection.connect();

    const { Pool } = require('pg');
    const pool = new Pool({
        connectionString: 'postgres://postgres:secret@localhost:5432/example_app',
        max: 20,
        idleTimeoutMillis: 30000,
        connectionTimeoutMillis: 2000,
    });

    return pool.connect();
}

async function selectEquipments() {
    const client = await connect();
    const res = await client.query('SELECT * FROM equipaments');
    client.release();
    return res.rows;
}

async function equipmentOnOff(message, mac_address) {
    const client = await connect();
    const msg = JSON.parse(message.toString());
    let online = '';
    
    if(msg.message=='online') {
        online = true ;
    } else {
        online = false ;
    }

    const query = 'UPDATE equipaments SET online = $1 WHERE mac_address = $2';
    const res = await client.query(query, [online, mac_address]);
    client.release();
    return res.rows;
    
}




module.exports = { selectEquipments, equipmentOnOff }
Editor is loading...
Leave a Comment