The 2024 CrateDB architecture guide covering all key concepts is out.

Download now
Skip to content
Product

How to Connect with Node.js

Effortlessly connect to CrateDB with Node.js through the node-postgres collection.

590px-Node.js_logo.svg

Connecting to CrateDB with Node.js is made effortless through the node-postgres collection, providing extensive functionalities for seamless integration between Node.js applications and CrateDB databases. This module supports various programming paradigms and rich capabilities, facilitating efficient communication and interaction tailored for Node.js environments and CrateDB.

There are two primary Node.js modules and drivers available for connecting to CrateDB. These modules offer distinct approaches for Node.js developers to interface with CrateDB, each with unique features and functionalities.

        

const { Client } = require("pg");

const crateClient = new Client({
  host: "scarlet-corde.aks1.eastus2.azure.cratedb.net",
  port: 5432,
  user: "admin",
  password: "",
  ssl: true,
});

(async () => {
  await crateClient.connect();
  const result = await crateClient.query("SELECT name FROM sys.cluster");
  console.log(result.rows[0]);
})();
        
        
        
        

const crate = require("node-crate");

crate.connect(`https://admin:${encodeURIComponent("")}@scarlet-corde.aks1.eastus2.azure.cratedb.net:4200`);

(async () => {
  const result = await crate.execute("SELECT name FROM sys.cluster");
  console.log(result.rows[0]);
})();
        
        
        

The node-postgres collection enables smooth interaction between Node.js applications and CrateDB databases. With comprehensive functionalities, including support for various programming paradigms (callbacks, promises, async/await), connection pooling, prepared statements, cursors, and streaming results, developers experience seamless integration when interfacing Node.js with CrateDB. For detailed insights and setup guidance, refer to the node-postgres documentation, empowering efficient utilization of CrateDB's full potential within Node.js applications.

The node-crate Node.js independent driver offers a tailored approach specifically designed for CRATE. Leveraging the _sql endpoint REST API, this alternative implementation provides developers with an additional option for establishing connections and interactions with CrateDB, catering to diverse development preferences and scenarios. Explore the node-crate driver documentation

Additional resources