Edit on GitHub

Database integration

Adding the capability to connect databases to Express apps is just a matter of loading an appropriate Node.js driver for the database in your app. This document briefly explains how to add and use some of the most popular Node.js modules for database systems in your Express app:

Caution

These database drivers are among many that are available. For other options, search on the npm site.

Cassandra

Module: cassandra-driver

Installation

Terminal window
npm install cassandra-driver

Example

index.cjs
const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['localhost'] });
client.execute('select key from system.local', (err, result) => {
if (err) throw err;
console.log(result.rows[0]);
});

Couchbase

Module: couchnode

Installation

Terminal window
npm install couchbase

Example

index.cjs
const couchbase = require('couchbase');
const bucket = new couchbase.Cluster('http://localhost:8091').openBucket('bucketName');
// add a document to a bucket
bucket.insert('document-key', { name: 'Matt', shoeSize: 13 }, (err, result) => {
if (err) {
console.log(err);
} else {
console.log(result);
}
});
// get all documents with shoe size 13
const n1ql = 'SELECT d.* FROM `bucketName` d WHERE shoeSize = $1';
const query = N1qlQuery.fromString(n1ql);
bucket.query(query, [13], (err, result) => {
if (err) {
console.log(err);