CQL data types to JavaScript types
When retrieving the value of a column from a Row
object, the value is typed according to the following table.
CQL data type | JavaScript type |
---|---|
ascii | String |
bigint | Long / BigInt |
blob | Buffer |
boolean | Boolean |
counter | Long / BigInt |
date | LocalDate |
decimal | BigDecimal |
double | Number |
duration | Duration |
float | Number |
inet | InetAddress |
int | Number |
list | Array |
map | Object / ECMAScript 6 Map |
set | Array / ECMAScript 6 Set |
smallint | Number |
text | String |
time | LocalTime |
timestamp | Date |
timeuuid | TimeUuid |
tinyint | Number |
tuple | Tuple |
uuid | Uuid |
varchar | String |
varint | Integer |
vector | Float32Array |
Encoding data
When encoding data, on a normal execute with parameters, the driver tries to guess the target type based on the input
type. Values of type Number
will be encoded as double
(because Number
is IEEE 754 double).
Consider the following example:
const key = 1000;
client.execute('SELECT * FROM table1 where key = ?', [ key ]);
If the key column is of type int
, the execution fails. There are two possible ways to avoid this type of problem, as
detailed below.
Prepare your queries (recommended)
Using prepared statements provides multiple benefits. Prepared statements are parsed and prepared on the Cassandra nodes and are ready for future execution. Also, the driver retrieves information about the parameter types which allows an accurate mapping between a JavaScript type and a Cassandra type.
Using the previous example, setting the prepare
flag in the queryOptions will fix it:
// Prepare the query before execution
client.execute('SELECT * FROM table1 where key = ?', [ key ], { prepare : true });
When using prepared statements, the driver prepares the statement once on each host to execute multiple times.
Hinting the target data type
Providing parameter hints in the query options is another way around it.
// Hint that the first parameter is an integer
client.execute('SELECT * FROM table1 where key = ?', [ key ], { hints : ['int'] });