2
votes

Thank you for taking the time to look at my issue. My team and I just started dabbling with NodeJS and this is the first time we have run into a problem that we cannot find a solution for ourselves thus far.

Simply put, we have a Controller that needs to query Cassandra with a UUID primary key.

Controller:

'use strict';

const express = require('express');
const models = require('../dbConnection');
const router = express.Router();
var UUID = require('uuid-js');

// Store / create checklist definition.
router.post('/', function(req, res) {

});

// Show checklist definition.
router.get('/:id', function(req, res) {

    let checklistId = req.params.id;

    console.log(checklistId);

    models.instance.ChecklistDefinition.findOne({id: checklistId}, function(err, checklist) {
       if (err) {
           console.log(err);
           return;
       }

       res.json(checklist);
    });
});

// Update checklist definition.
router.put('/:id', function(req, res) {

});

// Delete checklist definition.
router.delete('/:id', function(req, res) {

});

module.exports = router;

Model:

module.exports = {
    fields: {
        id : "uuid",
        client_id : "int",
        name : "text"
    },
    key : ["id"],
    indexes: ["name"],
    table_name: "checklist_definition"
};

DB Connection:

const models = require('express-cassandra');

//Tell express-cassandra to use the models-directory, and
//use bind() to load the models using cassandra configurations.
models.setDirectory( __dirname + '/models').bind(
    {
        clientOptions: {
            contactPoints: ['127.0.0.1'],
            protocolOptions: { port: 9042 },
            keyspace: 'audit_checklist',
            queryOptions: {consistency: models.consistencies.one}
        },
        ormOptions: {
            //If your keyspace doesn't exist it will be created automatically
            //using the default replication strategy provided here.
            defaultReplicationStrategy : {
                class: 'SimpleStrategy',
                replication_factor: 1
            },
            createKeyspace: true
        }
    },
    function(err) {
        if(err) console.log(err.message);
        else console.log(models.timeuuid());
    }
);

module.exports = models;

Cassandra Keyspace Schema:

CREATE KEYSPACE audit_checklist
WITH durable_writes = true
AND replication = {
    'class' : 'SimpleStrategy',
    'replication_factor' : 1
};

CREATE TABLE audit_checklist.checklist_definition (
    id uuid,
    client_id int,
    name text,
    PRIMARY KEY (id)
) WITH bloom_filter_fp_chance = 0.01
AND comment = ''
AND crc_check_chance = 1.0
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99.0PERCENTILE'
AND caching = {
    'keys' : 'ALL',
    'rows_per_partition' : 'NONE'
}
AND compression = {
    'chunk_length_in_kb' : 64,
    'class' : 'LZ4Compressor',
    'enabled' : true
}
AND compaction = {
    'class' : 'SizeTieredCompactionStrategy',
    'max_threshold' : 32,
    'min_threshold' : 4
};

CREATE INDEX checklist_definition_name_index ON audit_checklist.checklist_definition (name);

On the line models.instance.ChecklistDefinition.findOne, I cannot figure out how to get the checklistId (which is a UUID in the URL) to query properly against the Cassandra table. I'd imagine that I have to do something to the checklistId before I can use it for comparison with what is in the id column for this table in Cassandra.

The error that I am getting is:

apollo.model.validator.invalidvalue: Invalid Value: "6c84fb90-12c4-11e1-840d-7b25c5ee775a" for Field: id (Type: uuid)
at f (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/apollo_error.js:174:15)
at Function.f [as _get_db_value_expression] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:999:11)
at /Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:1144:34
at Array.forEach (native)
at Function.f [as _create_where_clause] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:1018:28)
at Function.f [as _create_find_query] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:1196:26)
at Function.f [as find] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:1482:26)
at Function.f [as findOne] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:1541:15)
at /Users/shanejeffery/Desktop/compli-docker/audit-checklist/controllers/ChecklistDefinitionController.js:21:41
at Layer.handle [as handle_request] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/layer.js:95:5)
at /Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/index.js:277:22
at param (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/index.js:349:14)
at param (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/index.js:365:14) name: 'apollo.model.validator.invalidvalue' }

Any help would be greatly appreciated!

3
Please edit your post by adding your cassandra keyspace schema. - Maximilien Belinga
Added the Keyspace schema to the main post. - Shane

3 Answers

1
votes

Uuid type should be queried without quotes (").

I'm not a NodeJS specialist, but maybe you should use cassandra type uuid?

There's Datastax article on uuid:

http://docs.datastax.com/en/developer/nodejs-driver/latest/features/datatypes/uuids/

2
votes

@nevsv -- You were exactly right. I feel like that should be a part of the documentation for express-cassandra, but glad I figured it out anyways.

To help others, this is what needed to happen in the model:

module.exports = {
    fields: {
        id : {
            type: "uuid",
            default: {"$db_function": "uuid()"}
        },
        client_id : "int",
        name : "text"
    },
    key : ["id"],
    indexes: ["name"],
    table_name: "checklist_definition"
};

Hope that helps someone else!

0
votes

I came across this issue today while coding an Express app using Casandra. The solution was to use the method uuidFromStringdefined in the models object. You can find more here: https://express-cassandra.readthedocs.io/en/stable/datatypes/