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:
- Cassandra
- Couchbase
- CouchDB
- LevelDB
- MySQL
- MongoDB
- Neo4j
- Oracle
- PostgreSQL
- Redis
- SQL Server
- SQLite
- Elasticsearch
Caution
These database drivers are among many that are available. For other options, search on the npm site.
Cassandra
Module: cassandra-driver
Installation
npm install cassandra-driveryarn add cassandra-driverpnpm add cassandra-driverbun add cassandra-driverExample
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]);});import cassandra from '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
npm install couchbaseyarn add couchbasepnpm add couchbasebun add couchbaseExample
const couchbase = require('couchbase');const bucket = new couchbase.Cluster('http://localhost:8091').openBucket('bucketName');
// add a document to a bucketbucket.insert('document-key', { name: 'Matt', shoeSize: 13 }, (err, result) => { if (err) { console.log(err); } else { console.log(result); }});
// get all documents with shoe size 13const 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);