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/prepared-statements-nodejs

by DataStax-Examples

This example shows the best practice of using prepared statements in the Node.js DataStax Driver to write and read data from a Cassandra database.Contributor(s): Dave BechbergerObjectivesLearn how to use prepared statements in Node.jsProject Layoutapp.js - The main application file, contains the example code for using prepared statementsHow this WorksThis example first creates a videos table in the examples keyspace.Once this table is created the code runs an INSERT and SELECT CQL query using a prepared statement. To create a statement as a prepared statement in Node.js you use a command like:client.execute(query, params, { prepare: true })The critical part of that command is the third parameter { prepare: true }. Adding this parameter makes this a prepared statement. Prepared statements are beneficial for several reasons:Prepared statements are parsed and stored on the Cassandra nodes which increase the performance of subsequent queries by preparing them once and executing them many times with bound parametersPrepared statements allow for proper mapping of Javascript types to Cassandra typesSetup and RunningPrerequisitesNode.Js version 8 ( download )A Cassandra, DDAC, DSE cluster or Apollo database ( docker is a nice option for local install - see docs )Note This application defaults to connecting to a cluster on localhost with a local data center of dc1. If this is not the case then you will need to change the following in app.js: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:Inserting Data Using a Prepared StatementSelecting Data Using a Prepared StatementVideo Id: 1665ffe0-77eb-4047-8791-f043accda4dd, User Id: 98462781-327b-40dc-9036-91702afdeb77, Name: Test Movie, Description: Test Description

Illustration Image

This example shows the best practice of using prepared statements in the Node.js DataStax Driver to write and read data from a Cassandra database.

Contributor(s): Dave Bechberger

Objectives

  • Learn how to use prepared statements in Node.js

Project Layout

  • app.js - The main application file, contains the example code for using prepared statements

How this Works

This example first creates a videos table in the examples keyspace.

Once this table is created the code runs an INSERT and SELECT CQL query using a prepared statement. To create a statement as a prepared statement in Node.js you use a command like:

client.execute(query, params, { prepare: true })

The critical part of that command is the third parameter { prepare: true }. Adding this parameter makes this a prepared statement. Prepared statements are beneficial for several reasons:

  • Prepared statements are parsed and stored on the Cassandra nodes which increase the performance of subsequent queries by preparing them once and executing them many times with bound parameters
  • Prepared statements allow for proper mapping of Javascript types to Cassandra types

Setup and Running

Prerequisites

  • Node.Js version 8 ( download )
  • A Cassandra, DDAC, DSE cluster or Apollo database ( docker is a nice option for local install - see docs )

Note This application defaults to connecting to a cluster on localhost with a local data center of dc1. If this is not the case then you will need to change the following in app.js:

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:

Inserting Data Using a Prepared Statement
Selecting Data Using a Prepared Statement
Video Id: 1665ffe0-77eb-4047-8791-f043accda4dd, User Id: 98462781-327b-40dc-9036-91702afdeb77, Name: Test Movie, Description: Test Description

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