-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathtuple-insert-select.js
40 lines (38 loc) · 1.63 KB
/
tuple-insert-select.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use strict";
const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['127.0.0.1'], localDataCenter: 'dc1' });
/**
* Creates a table with a Tuple type, inserts a row and selects a row.
*/
client.connect()
.then(function () {
const query = "CREATE KEYSPACE IF NOT EXISTS examples WITH replication =" +
"{'class': 'SimpleStrategy', 'replication_factor': '1' }";
return client.execute(query);
})
.then(function () {
const query = "CREATE TABLE IF NOT EXISTS examples.tuple_forex " +
"(name text, time timeuuid, currencies frozen<tuple<text, text>>, value decimal, PRIMARY KEY (name, time))";
return client.execute(query);
})
.then(function () {
console.log('Inserting');
// Create a new instance of a Tuple
const currencies = new cassandra.types.Tuple('USD', 'EUR');
const query = 'INSERT INTO examples.tuple_forex (name, time, currencies, value) VALUES (?, ?, ?, ?)';
const params = [ 'market1', cassandra.types.TimeUuid.now(), currencies, new cassandra.types.BigDecimal(11, 1) ];
return client.execute(query, params, { prepare: true});
})
.then(function () {
const query = 'SELECT name, time, currencies, value FROM examples.tuple_forex where name = ?';
return client.execute(query, ['market1'], { prepare: true });
})
.then(function (result) {
const row = result.first();
console.log('%s to %s: %s', row['currencies'].get(0), row['currencies'].get(1), row['value']);
return client.shutdown();
})
.catch(function (err) {
console.error('There was an error', err);
return client.shutdown().then(() => { throw err; });
});