Query YugabyteDB from NodeJS using PostgreSQL driver

Vladimir Novick
InstructorVladimir Novick
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

YugabyteDB is an open source, high-performance distributed SQL database for powering global, internet-scale applications.YugabyteDB is also a cloud-native database, so it can be deployed across both public and private clouds, including Kubernetes environments. In regards to serving as a backend for microservices, YugabyteDB brings together three must-haves: a PostgreSQL-compatible SQL API, low-latency read performance, and globally distributed write scalability. YugabyteDB with its global data distribution brings data close to users for multi-region and multi-cloud deployments.

In this lesson we will see how we can query YugabyteDB from NodeJS using PostgreSQL driver

Vladimir Novick: [0:00] Let's verify that our Yugabyte cluster is running by executing Yugabyte CTL status. We indeed can see the cluster's running. Let's see that we can connect to a SQL shell and indeed we can do that.

[0:15] Now, let's develop a node app that will connect to a SQL shell and the query things. First of all, let's run npm init to set package JSON. Let's install postgres driver and start creating our app by creating index JS.

[0:39] Now in our index JS let's require postgres. Now let's define connection string to be postgres and we want to connect to postgres on local host both 5433 and we want to use demo database. Let us also create postgres client by executing client method on postgres package, provide an input connection string.

[1:24] Now we need to create a async function. Let's call it query. Inside our function, let's try and catch block to catch possible errors. In case we have an error, we want to log there was an error with all the message and stock trace and everything.

[1:48] Now we'll try a block. Let's first connect to our database. We can execute query method on the client providing it with a SQL. Now, I will get the result. I want to write it into console.

[2:11] Let's try at our query. In our query, we want to get all those data for specific users, also specify user's name and email. Because we have two tables, we need to join both of them using user ID.

[2:31] Let's also limit to 10 rows because we probably have more data in database. What we also need to do is shutdown our client and we will do so by executing await client.end. I also want to get rows from the response to see the actual data. Now if I get and run index JS, I will indeed have data for my SQL query.

egghead
egghead
~ 30 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today