|
| 1 | +import { Cluster, } from "../index.js" |
| 2 | + |
| 3 | +const nodes = process.env.CLUSTER_NODES?.split(",") ?? ["127.0.0.1:9042"]; |
| 4 | + |
| 5 | +console.log(`Connecting to ${nodes}`); |
| 6 | + |
| 7 | +const cluster = new Cluster({ nodes }); |
| 8 | +const session = await cluster.connect(); |
| 9 | + |
| 10 | +await session.execute("CREATE KEYSPACE IF NOT EXISTS basic WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }"); |
| 11 | +await session.useKeyspace("basic"); |
| 12 | + |
| 13 | +await session.execute("CREATE TABLE IF NOT EXISTS basic (a int, b int, c text, primary key (a, b))"); |
| 14 | + |
| 15 | +await session.execute("INSERT INTO basic (a, b, c) VALUES (1, 2, 'abc')"); |
| 16 | +await session.execute("INSERT INTO basic (a, b, c) VALUES (?, ?, ?)", [3, 4, "def"]); |
| 17 | + |
| 18 | +const prepared = await session.prepare("INSERT INTO basic (a, b, c) VALUES (?, 7, ?)"); |
| 19 | +await session.execute(prepared, [42, "I'm prepared!"]); |
| 20 | +await session.execute(prepared, [43, "I'm prepared 2!"]); |
| 21 | +await session.execute(prepared, [44, "I'm prepared 3!"]); |
| 22 | + |
| 23 | +interface RowData { |
| 24 | + a: number; |
| 25 | + b: number; |
| 26 | + c: string; |
| 27 | +} |
| 28 | +const result = await session.execute<RowData>("SELECT a, b, c FROM basic"); |
| 29 | +console.log(result); |
| 30 | + |
| 31 | +const metrics = session.metrics(); |
| 32 | +console.log(`Queries requested: ${metrics.getQueriesNum()}`); |
| 33 | +console.log(`Iter queries requested: ${metrics.getQueriesIterNum()}`); |
| 34 | +console.log(`Errors occurred: ${metrics.getErrorsNum()}`); |
| 35 | +console.log(`Iter errors occurred: ${metrics.getErrorsIterNum()}`); |
| 36 | +console.log(`Average latency: ${metrics.getLatencyAvgMs()}`); |
| 37 | +console.log(`99.9 latency percentile: ${metrics.getLatencyPercentileMs(99.9)}`); |
0 commit comments