Using batch queries in typescript with cassandra , inserting only one row in table in Nodejs

78 views Asked by At

Using this way

const client = new cassandra.Client({ contactPoints: ['127.0.0.1'], localDataCenter: 'eu-west-3', keyspace: 'test',

});

const query1 = 'INSERT INTO user_track (key, text, date) VALUES (?, ?, ?)';
const query2 = 'INSERT INTO user_track (key, text, date) VALUES (?, ?, ?)';
const queries = [
   { query: query1, params: ['hendrix1', 'Changed email1', new Date()]},
   { query: query2, params: ['hendrix2', 'Changed email2', new Date()] } 
];
// Promise-based call
client.batch(queries, { prepare: true })
  .then(function() {
    // All queries have been executed successfully
  })
  .catch(function(err) {
    // None of the changes have been applied
  });

Inserting only one row..

My expectation is to insert all rows.

1

There are 1 answers

0
Aaron On

Ok, so I'm curious to know what your underlying table user_track looks like.

Based on what you described, I did a npm install cassandra-driver, started Cassandra locally, and created my user_track table like this, with key as my PRIMARY KEY:

CREATE TABLE user_track (
    key TEXT PRIMARY KEY,
    text TEXT,
    date DATE);

I then ran your code, and I have two rows:

aaron@cqlsh:stackoverflow> SELECT * FROm user_track ;

 key      | date       | text
----------+------------+----------------
 hendrix1 | 2023-04-10 | Changed email1
 hendrix2 | 2023-04-10 | Changed email2

(2 rows)

Here's my Git repo, if you'd like to have a look: https://github.com/aar0np/nodejsCassandraTest/tree/main

So everything looks correct from a code standpoint. Maybe reverify that you're on the latest version of the Nodejs cassandra-driver?