Skip to content

Commit

Permalink
example(basic): replicate rust's driver basic.rs example (#32)
Browse files Browse the repository at this point in the history
closes #31

Signed-off-by: Daniel Boll <[email protected]>
  • Loading branch information
Daniel-Boll authored Jul 29, 2024
1 parent ee55a44 commit d5f93dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions examples/basic.mts
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)}`);
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class Metrics {
}
export class ScyllaSession {
metrics(): Metrics
execute(query: string | Query | PreparedStatement, parameters?: Array<number | string | Uuid> | undefined | null): Promise<any>
execute<T = unknown>(query: string | Query | PreparedStatement, parameters?: Array<number | string | Uuid> | undefined | null): Promise<T>
query(scyllaQuery: Query, parameters?: Array<number | string | Uuid> | undefined | null): Promise<any>
prepare(query: string): Promise<PreparedStatement>
batch(batch: BatchStatement, parameters: Array<Array<number | string | Uuid> | undefined | null>): Promise<any>
Expand Down

0 comments on commit d5f93dc

Please sign in to comment.