-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example(basic): replicate rust's driver
basic.rs
example (#32)
closes #31 Signed-off-by: Daniel Boll <[email protected]>
- Loading branch information
1 parent
ee55a44
commit d5f93dc
Showing
2 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<RowData>("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)}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters