|
| 1 | +"use strict"; |
| 2 | +const cassandra = require("scylladb-javascript-driver"); |
| 3 | +const { getClientArgs } = require("../util"); |
| 4 | + |
| 5 | +const client = new cassandra.Client(getClientArgs()); |
| 6 | + |
| 7 | +/** |
| 8 | + * Creates a table with a Tuple type, inserts a row and selects a row. |
| 9 | + */ |
| 10 | +client |
| 11 | + .connect() |
| 12 | + .then(function () { |
| 13 | + const query = |
| 14 | + "CREATE KEYSPACE IF NOT EXISTS examples WITH replication =" + |
| 15 | + "{'class': 'SimpleStrategy', 'replication_factor': '1' }"; |
| 16 | + return client.execute(query); |
| 17 | + }) |
| 18 | + .then(function () { |
| 19 | + const query = |
| 20 | + "CREATE TABLE IF NOT EXISTS examples.tuple_forex " + |
| 21 | + "(name text, time timeuuid, currencies frozen<tuple<text, text>>, PRIMARY KEY (name, time))"; |
| 22 | + return client.execute(query); |
| 23 | + }) |
| 24 | + .then(function () { |
| 25 | + console.log("Inserting"); |
| 26 | + // Create a new instance of a Tuple |
| 27 | + const currencies = new cassandra.types.Tuple("USD", "EUR"); |
| 28 | + const query = |
| 29 | + "INSERT INTO examples.tuple_forex (name, time, currencies) VALUES (?, ?, ?)"; |
| 30 | + const params = ["market1", cassandra.types.TimeUuid.now(), currencies]; |
| 31 | + return client.execute(query, params, { prepare: true }); |
| 32 | + }) |
| 33 | + .then(function () { |
| 34 | + const query = |
| 35 | + "SELECT name, time, currencies, FROM examples.tuple_forex where name = ?"; |
| 36 | + return client.execute(query, ["market1"], { prepare: true }); |
| 37 | + }) |
| 38 | + .then(function (result) { |
| 39 | + const row = result.first(); |
| 40 | + console.log( |
| 41 | + "%s to %s", |
| 42 | + row["currencies"].get(0), |
| 43 | + row["currencies"].get(1), |
| 44 | + ); |
| 45 | + }) |
| 46 | + .catch(function (err) { |
| 47 | + console.error("There was an error", err); |
| 48 | + }); |
0 commit comments