From d5f93dcc2380edf21242c5945c6f065064f3bf59 Mon Sep 17 00:00:00 2001 From: Daniel Boll <43689101+Daniel-Boll@users.noreply.github.com> Date: Mon, 29 Jul 2024 20:18:34 -0300 Subject: [PATCH] example(basic): replicate rust's driver `basic.rs` example (#32) closes #31 Signed-off-by: Daniel Boll --- examples/basic.mts | 37 +++++++++++++++++++++++++++++++++++++ index.d.ts | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 examples/basic.mts diff --git a/examples/basic.mts b/examples/basic.mts new file mode 100644 index 0000000..5fff6ba --- /dev/null +++ b/examples/basic.mts @@ -0,0 +1,37 @@ +import { Cluster, } from "../index.js" + +const nodes = process.env.CLUSTER_NODES?.split(",") ?? ["127.0.0.1:9042"]; + +console.log(`Connecting to ${nodes}`); + +const cluster = new Cluster({ nodes }); +const session = await cluster.connect(); + +await session.execute("CREATE KEYSPACE IF NOT EXISTS basic WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }"); +await session.useKeyspace("basic"); + +await session.execute("CREATE TABLE IF NOT EXISTS basic (a int, b int, c text, primary key (a, b))"); + +await session.execute("INSERT INTO basic (a, b, c) VALUES (1, 2, 'abc')"); +await session.execute("INSERT INTO basic (a, b, c) VALUES (?, ?, ?)", [3, 4, "def"]); + +const prepared = await session.prepare("INSERT INTO basic (a, b, c) VALUES (?, 7, ?)"); +await session.execute(prepared, [42, "I'm prepared!"]); +await session.execute(prepared, [43, "I'm prepared 2!"]); +await session.execute(prepared, [44, "I'm prepared 3!"]); + +interface RowData { + a: number; + b: number; + c: string; +} +const result = await session.execute("SELECT a, b, c FROM basic"); +console.log(result); + +const metrics = session.metrics(); +console.log(`Queries requested: ${metrics.getQueriesNum()}`); +console.log(`Iter queries requested: ${metrics.getQueriesIterNum()}`); +console.log(`Errors occurred: ${metrics.getErrorsNum()}`); +console.log(`Iter errors occurred: ${metrics.getErrorsIterNum()}`); +console.log(`Average latency: ${metrics.getLatencyAvgMs()}`); +console.log(`99.9 latency percentile: ${metrics.getLatencyPercentileMs(99.9)}`); diff --git a/index.d.ts b/index.d.ts index d0421ed..a05e667 100644 --- a/index.d.ts +++ b/index.d.ts @@ -121,7 +121,7 @@ export class Metrics { } export class ScyllaSession { metrics(): Metrics - execute(query: string | Query | PreparedStatement, parameters?: Array | undefined | null): Promise + execute(query: string | Query | PreparedStatement, parameters?: Array | undefined | null): Promise query(scyllaQuery: Query, parameters?: Array | undefined | null): Promise prepare(query: string): Promise batch(batch: BatchStatement, parameters: Array | undefined | null>): Promise