Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

10/23/2020

Reading time:1 min

DataStax-Examples/object-mapper-nodejs

by DataStax-Examples

The Node.js DataStax Driver comes with an object mapper that removes boilerplate of writing queries and lets you focus on your application objects. This example shows how to use the mapper to access Apache Cassandra™ in a Node.js application.Contributor(s): Jorge Bay Gondra, Andrew Tolbert - derived from hereObjectivesDemonstrate how to write and read data using the Node.js Driver's object mapperProject Layoutapp.js - The main application file which contains the example code for using the object mapperHow this WorksThis example starts by creating a mapper instance that maps to three tables in the examples keyspace.usersuser_videoslatest_videosIt then uses that mapper instance to write and read data to / from the tables above.Setup and RunningPrerequisitesNode.Js version 8A Cassandra cluster is running and accessible through the contacts points and data centerNote This application defaults to connecting to a cluster on localhost with a local data center of dc1. These parameters can be changed in app.js by modifying the following to your settings.const client = new cassandra.Client({ contactPoints: ['127.0.0.1'], localDataCenter: 'dc1' });RunningTo run this application use the following command:node app.jsThis will produce the following output:--Obtained video by id { videoId: Uuid: 31ff5550-da75-47f4-8b41-926512f74691, addedDate: 2019-11-06T22:47:00.369Z, description: 'My desc', location: null, locationType: null, name: 'My video', previewThumbnails: null, tags: null, userId: Uuid: 7cd9f42f-6a37-42b6-a8d6-08c6ecdbf75f }--Obtained video by user id { userId: Uuid: 7cd9f42f-6a37-42b6-a8d6-08c6ecdbf75f, addedDate: 2019-11-06T22:47:00.369Z, videoId: Uuid: 31ff5550-da75-47f4-8b41-926512f74691, name: 'My video', previewImageLocation: null }```

Illustration Image

The Node.js DataStax Driver comes with an object mapper that removes boilerplate of writing queries and lets you focus on your application objects. This example shows how to use the mapper to access Apache Cassandra™ in a Node.js application.

Contributor(s): Jorge Bay Gondra, Andrew Tolbert - derived from here

Objectives

  • Demonstrate how to write and read data using the Node.js Driver's object mapper

Project Layout

  • app.js - The main application file which contains the example code for using the object mapper

How this Works

This example starts by creating a mapper instance that maps to three tables in the examples keyspace.

  • users
  • user_videos
  • latest_videos

It then uses that mapper instance to write and read data to / from the tables above.

Setup and Running

Prerequisites

  • Node.Js version 8
  • A Cassandra cluster is running and accessible through the contacts points and data center

Note This application defaults to connecting to a cluster on localhost with a local data center of dc1. These parameters can be changed in app.js by modifying the following to your settings.

const client = new cassandra.Client({ contactPoints: ['127.0.0.1'], localDataCenter: 'dc1' });

Running

To run this application use the following command:

node app.js

This will produce the following output:

--Obtained video by id
 { videoId: Uuid: 31ff5550-da75-47f4-8b41-926512f74691,
  addedDate: 2019-11-06T22:47:00.369Z,
  description: 'My desc',
  location: null,
  locationType: null,
  name: 'My video',
  previewThumbnails: null,
  tags: null,
  userId: Uuid: 7cd9f42f-6a37-42b6-a8d6-08c6ecdbf75f }
--Obtained video by user id
 { userId: Uuid: 7cd9f42f-6a37-42b6-a8d6-08c6ecdbf75f,
  addedDate: 2019-11-06T22:47:00.369Z,
  videoId: Uuid: 31ff5550-da75-47f4-8b41-926512f74691,
  name: 'My video',
  previewImageLocation: null }```

Related Articles

api.management
node
api

LoopBack

John Doe

3/7/2024

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

node