nodejs pg salesforce platform events unable to insert into database while processing messages what is wrong with my script

0 votes

I am using node v16.14.0.

The below is a node script that is trying to insert message data into Postgres db after pulling a message from a platform event, but some how it is not able to.

It hits the line "let client = await pool.connect();" and continues with the next message. It never inserts into the table.

I am not sure what I am doing wrong here?

const jsforce = require("jsforce");
const { Pool }= require("pg");
const channel = "/event/Short_Url_Click_Notification__e";

let replayId = -2 // -1 = Only New messages | -2 = All Window and New

console.log('db = ' +process.env.DATABASE_URL);
console.log('sfu= ' +process.env.SF_USERNAME);
console.log('sfp= ' +process.env.SF_PASSWORD);

// sf connection
const sfConnection = new jsforce.Connection();

const pool = new Pool({
    connectionString:  process.env.DATABASE_URL,
    ssl:{
        rejectUnauthorized: false
    }
});

try {

    pool.query("SELECT replay_id, date_str, channel FROM platform_event_setting WHERE channel = $1 LIMIT 1", [channel], (err, res) => {
        if (err) {
            console.error("Unable to Query platform_event_setting: ", err);
            pgClient.end();
            process.exit(1);
        }

        if (res.rowCount > 0) {
            replayId = res.rows[0].replay_id;
        }

        sfConnection.login(process.env.SF_USERNAME, process.env.SF_PASSWORD,  (err, res) => {
            if (err) {
                console.error(err);
                return process.exit(1);
            }

            const streamingClient = sfConnection.streaming.createClient([
                new jsforce.StreamingExtension.Replay(channel, replayId),
                new jsforce.StreamingExtension.AuthFailure(function () {
                    return process.exit(1);
                }),
            ]);

            subscription = streamingClient.subscribe(channel, async (message) => {
                try {
                    console.log("received data", JSON.stringify(message));
                    event_values = [channel, message.event.replayId, message.payload.CreatedDate];
                    let client = await pool.connect();
                    client.query("BEGIN");
                    try {
                        // process message
                        await client.query(`INSERT INTO platform_event_setting
                                            (channel, replay_id, date_str)
                                        VALUES ($1, $2, $3)
                                        ON CONFLICT ON CONSTRAINT platform_event_setting_pkey
                                        DO UPDATE SET (replay_id, date_str) = (EXCLUDED.replay_id, EXCLUDED.date_str)
                                        `, event_values);
                        client.query("COMMIT");
                    } catch (e) {
                        console.error(e);
                        client.query("ROLLBACK");
                        return process.exit(1);
                    }
                } finally {
                    client.release();
                }
            }); // subscritpion
        }); // sf connection
    }); // pool
} catch (e) {
    console.error(e);
}

The above script pulls a message from Salesforce platform events bus and trying to insert into a Postgres database.

Aug 19, 2022 in Node-js by Neha
• 9,060 points
294 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Node-js

0 votes
1 answer

How do I get the path to the current script with Node.js?

Hello @kartik, you can do this: fs.readFile(path.resolve(__dirname, 'settings.json'), 'UTF-8', ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
2,462 views
0 votes
1 answer

How do I add a custom script to my package.json file that runs a javascript file?

Hello @kartik, I have created the following, and ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,880 points
39,794 views
0 votes
1 answer

What is the best way to run npm install for nested folders?

Hello @kartik, If you want to run a ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,880 points
13,472 views
0 votes
1 answer

How to get the _id of inserted document in Mongo database in NodeJS?

Hello @kartik, A shorter way than using second ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,880 points
13,141 views
0 votes
1 answer

How to create an Excel File with Nodejs?

Hello @kartik, Just create a file with Tabs ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,880 points
2,137 views
0 votes
1 answer

How to save a stream into multiple destinations with Gulp.js?

Hello @kartik, Currently you have to use two ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,880 points
1,302 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,692 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,233 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,450 points
1,145 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP