Search code examples
node.jscassandrarowcql3

How to get generated Id after inserting row in Cassandra


I am trying to insert insert some rows sharing a row-id and decided to stick with time-based uuids. All documentation i could find explained how to create such a row:

INSERT INTO users (id, name) VALUES (now(), 'Florian')

I'm using DataStax's cassandra-driver for Node.js to execute my queries (where insertUser is a string containing the query from above):

var r = await client.execute(insertUser)
console.dir(r.rows)

The result looks like this:

ResultSet {
  info:
   { queriedHost: '127.0.0.1:9042',
     triedHosts: { '127.0.0.1:9042': null },
     speculativeExecutions: 0,
     achievedConsistency: 10,
     traceId: undefined,
     warnings: undefined,
     customPayload: undefined,
     isSchemaInAgreement: true },
  rows: undefined,
  rowLength: undefined,
  columns: null,
  pageState: null,
  nextPage: undefined }

As we can see there is no id in the result which i could use to create dependent rows.

Is there a Cassandra-idiomatic way to create multiple rows depending on the same id without generating the id local?


Solution

  • You should provide it in your query parameter instead of relying on the CQL now() function (which returns an UUID v1).

    const cassandra = require('cassandra-driver');
    const Uuid = cassandra.types.Uuid;
    
    // ...
    const query = 'INSERT INTO users (id, name) VALUES (?, ?)';
    const id = Uuid.random();
    const options = { prepare: true, isIdempotent: true };
    const result = await client.execute(query, [ id, 'Florian' ], options);
    
    

    The added benefit of generating the id from the client side is that it makes your query idempotent.

    The DataStax driver has a rich type system, you can check out the CQL types to JavaScript types representation in this table: https://docs.datastax.com/en/developer/nodejs-driver/latest/features/datatypes/