The Cassandra connector enables LoopBack applications to connect to Cassandra data sources.Note: This page was generated from the loopback-connector-cassandra/README.md.

loopback-connector-cassandra

The official Cassandra Connector module for loopback-datasource-juggler.

Please also see LoopBack Cassandra Connector in LoopBack documentation.

Installation

In your application root directory, enter this command to install the connector:

  1. npm install loopback-connector-cassandra --save

This installs the module from npm and adds it as a dependency to the application’s package.json file.

If you create a Cassandra data source using the data source generator as described below, you don’t have to do this, since the generator will run npm install for you.

Creating a Cassandra data source

Use the Data source generator to add a Cassandra data source to your application. Select Cassandra connector as follows:

  1. $ lb datasource
  2. ? Enter the data-source name: mycass
  3. ? Select the connector for mycass:
  4. IBM Cloudant DB (supported by StrongLoop)
  5. IBM DB2 for z/OS (supported by StrongLoop)
  6. IBM WebSphere eXtreme Scale key-value connector (supported by StrongLoop)
  7. Cassandra (supported by StrongLoop)
  8. Redis key-value connector (supported by StrongLoop)
  9. MongoDB (supported by StrongLoop)
  10. MySQL (supported by StrongLoop)
  11. (Move up and down to reveal more choices)

The generator will then prompt for the database server hostname, port, and other settingsrequired to connect to a Cassandra database. It will also run the npm install command for you.

  1. $ lb datasource
  2. ? Enter the data-source name: mycass
  3. ? Select the connector for mycass: Cassandra (supported by StrongLoop)
  4. Connector-specific configuration:
  5. ? host: localhost
  6. ? port: 9042
  7. ? user:
  8. ? password:
  9. ? database: test
  10. ? connectTimeout(ms): 30000
  11. ? readTimeout(ms): 30000
  12. ? Install loopback-connector-cassandra@^1.0.0 Yes
  13. loopback-connector-cassandra@1.0.0 node_modules/loopback-connector-cassandra
  14. ...

The entry in the application’s /server/datasources.json will look like this:

  1. "mycass": {
  2. "host": "localhost",
  3. "port": 9042,
  4. "database": "test",
  5. "password": "",
  6. "name": "mycass",
  7. "user": "",
  8. "connectTimeout": 30000,
  9. "readTimeout": 30000,
  10. "connector": "cassandra"
  11. }

Edit datasources.json to add any other additional properties supported by cassandra-driver.

Type mappings

See LoopBack types for details on LoopBack’s data types.

LoopBack to/from Cassandra types

In addition to the standard data types such as String, Boolean, and Number, several Cassandra specific types are supported as shown in the table blow.

LoopBack TypeCassandra Type
UuidUUID
TimeUuidTIMEUUID
TupleTUPLE

Primary Keys

Auto generated partition keys

In case no id is defined, LoopBack adds id with Cassandra connector’s default type: Uuid.

LoopBack notation:

  1. zipCodes = db.define('zipCodes', {
  2. state: String,
  3. zipCode: Number,
  4. });

Cql equivalent:

  1. CREATE TABLE zipCodes (
  2. state TEXT,
  3. zipCode INT,
  4. id UUID,
  5. PRIMARY KEY (id)
  6. );

User defined partition keys

LoopBack notation:

When id: true is defined, LoopBack does not add id and uses it as a partition key.

  1. customers = db.define('customers', {
  2. name: String,
  3. state: String,
  4. zipCode: Number,
  5. userId: {type: 'TimeUuid', id: true},
  6. });

Cql equivalent:

  1. CREATE TABLE customers (
  2. name TEXT,
  3. state TEXT,
  4. zipCode INT,
  5. userId TIMEUUID,
  6. PRIMARY KEY (userId)
  7. );

Compound partition keys

LoopBack notation:

id value can be either boolean or number (base 1). Compound partition key is created by combining the ones with number in ascending orderthen boolean. In case conflict, first-come first-served.

  1. customers = db.define('customers', {
  2. isSignedUp: {type: Boolean, id: 2},
  3. state: String,
  4. contactSalesRep: {type: String, id: true},
  5. zipCode: Number,
  6. userId: {type: Number, id: 1},
  7. });

Cql equivalent:

  1. CREATE TABLE customers (
  2. isSignedUp BOOLEAN,
  3. state TEXT,
  4. contactSalesRep TEXT,
  5. zipCode INT,
  6. userId INT,
  7. PRIMARY KEY ((userId, isSignedUp, contactSalesRep))
  8. );

Clustering keys and Sorting

Cassandra stores data on each node according to the hashed TOKEN value of the partition key in the range that the node is responsible for.Since hashed TOKEN values are generally random, find with limit: 10 filter will return apparently random 10 (or less) rows.The Cassandra connector supports on-disk sorting by setting clustering key as ASCending or DESCending at table creation time.order filter is ignored. Since sorting is done on node by node basis, the returned result is property sorted only when the partition key is specified.

For example, in case you want to find the most recently added row, create a table with time-based column as a clustering key with DESC property.Then, use find with limit: 1 or findOne.

Concrete example is as follows assuming all the rows fall in the same partition range.Note that clusteringKeys is defined as an array because the order of the sorting keys is important:

isSignedUpstatecontactSalesRepzipCodeuserId
trueArizonaTed Johnson850032003
trueArizonaDavid Smith8500216002
trueArizonaMary Parker8500115001
trueCaliforniaDavid Smith9000121002
trueColoradoMary Parker800022010
trueColoradoJane Miller8000112002
trueNevadaTed Johnson7517328006

LoopBack notation:

Cassandra connector supports clustering key as a custom option. Sorting order can be associated with clustering keys as ASC or DESC.

  1. customers = db.define('customers', {
  2. isSignedUp: {type: Boolean, id: true},
  3. state: String,
  4. contactSalesRep: String,
  5. zipCode: Number,
  6. userId: Number,
  7. }, {
  8. cassandra: {
  9. clusteringKeys: ['state', 'zipCode DESC'],
  10. },
  11. });

Cql equivalent:

  1. CREATE TABLE customers (
  2. isSignedUp BOOLEAN,
  3. state TEXT,
  4. contactSalesRep TEXT,
  5. zipCode INT,
  6. userId INT,
  7. PRIMARY KEY (isSignedUp, state, zipCode)
  8. ) WITH CLUSTERING ORDER BY (state ASC, zipCode DESC);

Secondary Indexes

Additional searchable fields can be defined as secondary indexes. For example, in case the table customers below is defined with name as just {type: String}, then find with where: {name: "Martin Taylor"} filter will fail. However, find with where: {namee: "Martin Taylor"} filter will succeed on the table defined with index: true as follows:

LoopBack notation:

  1. customers = db.define('customers', {
  2. name: {type: String, index: true},
  3. userId: {type: Number, id: true},
  4. });

Cql equivalent:

  1. CREATE TABLE customers (
  2. name TEXT,
  3. userId INT,
  4. PRIMARY KEY (userId)
  5. );
  6. CREATE INDEX ON customers (name);

V1 Limitations

Because of the Cassandra architecture, Cassandra connector V1 supports where and limit.Other filter conditions are not supported.

order filter not supported

Use clustering keys for sorting. The database side sorting determines the order or rows to be returnwhen ordering matters such as where limit or findOne. Ad hoc sorting with sort filter is not supported.

or filter not supported

and is supported, but or is not in where filter.

offset is not supported

Pagination is not supported in V1.

Running tests

Own instance

If you have a local or remote Cassandra instance and would like to use that to run the test suite, use the following command:

  • Linux
  1. CASSANDRA_HOST=<HOST> CASSANDRA_PORT=<PORT> CASSANDRA_KEYSPACE=<KEYSPACE> CI=true npm test
  • Windows
  1. SET CASSANDRA_HOST=<HOST>
  2. SET CASSANDRA_PORT=<PORT>
  3. SET CASSANDRA_KEYSPACE=<KEYSPACE>
  4. SET CI=true
  5. npm test

Docker

If you do not have a local Cassandra instance, you can also run the test suite with very minimal requirements.

  • Assuming you have Docker installed, run the following script which would spawn a Cassandra instance on your local:
  1. source setup.sh <HOST> <PORT> <KEYSPACE>

where <HOST>, <PORT> and <KEYSPACE> are optional parameters. The default values are localhost, 9042 and test respectively.

  • Run the test:
  1. npm test

Tags: readme