Back in March 2022, we announced a new feature, Heroku Data in Salesforce Functions, which lets you securely access Heroku Postgres, Heroku Data for Redis, and Apache Kafka on Heroku directly from your function. Today, we are releasing a new set of recipes on our Functions Recipes sample application showing you how to write code in Java and Node.js to access Postgres and Redis resources from a function, and how to write unit tests for them.

Screenshot of the Functions Recipes sample application showing the Heroku Postgres recipe

Heroku Postgres

Node.js

To access Heroku Postgres from a Node.js function, we are using the node-postgres npm package. There is no restriction on using a different one, or relying on a query generator or ORM, if that’s what you need.

Two things to keep in mind:

  • Install the dependency on your Functions project by running npm install
  • Access the Heroku Data resource by using the DATABASE_URL environment variable, which can be set manually on the local development experience, or by attaching the resource to the compute environment

You can find the full source for the Postgres recipe with Node.js in the Functions Recipes GitHub repository.

Java

For Java, we are using the org.postgresql JDBC driver. Similar to the Node.js recipe, we will need to add this dependency to the pom.xml file and rely on the DATABASE_URL to create the connection to the database.

You can find the full source for the Postgres recipe with Java in the Functions Recipes GitHub repository.

Heroku Data for Redis

Node.js

To access Heroku Data for Redis from a Node.js function, we are using the node-redis official npm package, but there is no restriction on using any other library from the npm ecosystem, for example, the popular ioredis client.

Make sure to install the dependency of your choice and connect using the REDIS_URL environment variable provided by Heroku Data.

You can find the full source for the Redis recipe with Node.js in the Functions Recipes GitHub repository.

Java

For Java, we are using the Jedis client, but you can rely on any other client of your choice (see a list of available clients per programming language). Just make sure to reference the library in your pom.xml file.

You can find the full source for the Redis recipe with Java in the Functions Recipes GitHub repository.

Bonus: Tests!

We’ve implemented a new feature in the Functions Recipes sample application! Now, you will be able to see the source code of all the test files within the app. Remember, it is a best practice to always write tests for your code. With these recipes, you now have a good way to see how functions can be tested, and external resources like database connections can be mocked, using tools recommended by the Salesforce Functions team.

Screenshot of the Functions Recipes sample app showing the unit tests file for the Heroku Data for Redis recipe

What’s next?

Learn more about Salesforce Functions in our Developer Center with curated content and resources created by our team and other folks at Salesforce.

I also presented a series of videos on how to connect to Heroku Data from Functions, including Apache Kafka. Take a look to learn more:

About the author

Julian Duque

Julián Duque is a Principal Developer Advocate at Salesforce. He is a developer and educator and spends his time running TTRPG games online, as well as playing and training his Mini Aussie, Cumbia.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS