Apache Cassanda database adapter for Sails/Waterline. It works with 2.x and 3.x versions of Cassandra.
Install from NPM:
$ npm install sails-cassandra --save
2. Configuring Sails
cassandra configuration to the
config/connections.js file. The basic
options are as follows:
moduleexportsconnections =my-cassandra-connection:module : 'sails-cassandra'// typical sails/waterline options (see comment below)user : 'username'password : 'password'// cassandra driver optionscontactPoints : '127.0.0.1'keyspace : 'keyspace name'...;
And then change default model configuration in the
moduleexportsmodels =connection: 'my-cassandra-connection';
Adapter configuration may contain any of Cassandra client options.
However, you probably will only need
keyspace to get
started and the adapter will provide reasonable defaults for the rest.
Authentication information for
cassandra-driver is typically supplied in
authProvider option, however
sails-cassandra adapter will also recognize
password options and convert them into
an existing value. This also means that if you wish to use your own
you will need to remove
password from the configuration.
3. Running Tests
You can set environment variables to override the default database configuration for running tests as follows:
$ WATERLINE_ADAPTER_TESTS_PASSWORD=yourpass npm test
Default settings are:
contactPoints: processenvWATERLINE_ADAPTER_TESTS_HOST || '127.0.0.1'user: processenvWATERLINE_ADAPTER_TESTS_USER || 'root'password: processenvWATERLINE_ADAPTER_TESTS_PASSWORD || ''keyspace: processenvWATERLINE_ADAPTER_TESTS_DATABASE || 'test'
Note: Default name of the keyspace for running tests is
test. Make sure you created it in your database before executing
4. Implementation Notes
This section describes behaviour of Apache Cassandra adapter that is distinct from other database types.
4.1. Naming of tables and columns
Column and table names in Cassandra are case insensitive and this ambiguity makes it difficult to map between attribute names that are case sensitive and column names that are not. There are two possible workarounds for this:
There is a mechanism in Apache Cassandra to make table/column names case sensitive by including them in double quotes. This may seem as a good idea on the surface but it does not cover a use case when tables are not created by sails/waterline but by an external process.
Converting table/column names to lower case is an another approach. This way we always convert table/column names to lower case before mapping them to/from attribute names. This is not very elegant but it works and this is the current preferred approach.
The autoincrement feature was plaguing ORM frameworks right from their inseption as it requires 1-2 extra queries in order to retrieve new record identifier from underlying database into the framework. It also does not work very well with sharding and replication.
Cassandra database does not support autoincrement, however it achieves the same functionality in a much more efficient way by using time based UUIDs (a.k.a. Type 1 UUID) for primary keys.
Sails/Waterline supports autoincrement and its implementation is heavily
influenced by MySQL database. The
sails-cassandra adapter makes an attempt to
achieve the same functionality using the following rules:
Model attribute that represents primary key may have
autoIncrementproperty set to
This automatically forces attribute type to
stringand supersedes any other declarations. The adapter will give a warning message is there is a discrepancy.
The value of the primary key cannot be overridden by
autoIncrementproperty is enabled. You will see a (non-lethal) warning message if such attempt is made.
Note: This logic is inconsistent with the current Sails/Waterline specifications as it requires
autoIncrementfield to be of type
integer. Please use discretion. Also, see this issue.
4.3. Type conversion between Cassandra and Sails/Waterline
The following table represents mappings between Sails/Waterline model data types and Apache Cassandra data types:
|Sails/Waterline Type||JS Type||Cassandra Type|
|string||String||text (UTF-8 text)|
|text||String||text (UTF-8 text)|
|integer||Number||bigint (64-bit signed integer)|
|float||Number||double (64-bit float)|
|json||???||text (UTF-8 text)|
|String||ascii (US-ASCII character string)|
The following table may be used as a guideline when creating Sails/Waterline models for existing tables:
|Cassandra Type||Type Id||Driver JS type||Waterline JS Type||Waterline Type|
|bigint||2||Long||Number or NaN||integer|
|counter||5||Long||Number or NaN||integer|
|decimal||6||BigDecimal||Number or NaN||float|
|map||33||Object/ES6 Map||Null||not supported (null)|
|set||34||Array/ES6 Set||Null||not supported (null)|
|timestamp||11||Date||Date||datetime or date|
|varint||14||Integer||Number or NaN||integer|
4.4. Use of indexes
Apache Cassandra require index on a column that is used in
where clause of
select statement and unlike other database it will produce and exception if
the index is missing.
Sails/Waterline allows to set
unique properties on model
sails-cassanda adapter will respect these attributes and it
will create indexes for attributes with
unique attributes set to
Note: that Apache Cassandra have no notion of
uniqueconstraint and the uniqueness has to be enforced either by Sails/Waterline core or in your own code. The
uniqueattribute property is considered an alias for
indexand both are treated in the exactly same way.
4.5. Search criteria
Apache Cassandra only supports subset of operation in selection criteria in comparison to relational databases and this section describes what is currently supported.
4.5.1. Key Pairs
This is an exact match criteria and it is declared as follows:
It is supported and it will be executed as follows:
SELECT id, first_name, last_name FROM users WHERE first_name = 'Joe' AND last_name = 'Doe' ALLOW FILTERING;
Please also refer to Use of Indexes above.
4.5.2. Modified Pair
will be converted to CQL query that may look like this:
SELECT id,first_name,last_name FROM users WHERE age > 18 AND age <= 65 ALLOW FILTERING;
and supported operations are as follows:
4.5.3. In Pairs
will be rendered into the following CQL statement:
SELECT id, first_name, last_name FROM users WHERE title IN ( 'Mr', 'Mrs' ) ALLOW FILTERING;
INcriterion works differently in Apache Cassandra. It is subject of certain limitations and is considered a pattern to be avoided.
4.5.4. Not-In Pair
Not supported since Apache Cassandra does not support
NOT IN criterion,
so this construct:
will cause adapter to throw an exception.
4.5.5. Or Pairs
Not supported since Apache Cassandra has no
OR criterion, so this construct:
will cause the adapter to throw an exception.
4.5.6. Limit, Sort, Skip
limit is curently implemented and works as expected.
not supported and silently ignored if provided.
5. Version History
- 0.12.x - Supports Apache Cassandra v2.x and v3.x, uses cassandra-driver v3.x, requires Node.js v4+
- 0.10.x - Supports Apache Cassandra v2.x, uses cassandra-driver v2.x, requires Node.js v0.10+
See LICENSE.md file for details.