Skip to content

Commit 1a7c343

Browse files
committed
fix(errors): add error context and docs to some functions
Signed-off-by: Daniel Boll <danielboll.academico@gmail.com>
1 parent 303195e commit 1a7c343

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed

index.d.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,46 @@ export class Metrics {
122122
export class ScyllaSession {
123123
metrics(): Metrics
124124
execute<T = unknown>(query: string | Query | PreparedStatement, parameters?: Array<number | string | Uuid> | undefined | null): Promise<T>
125-
query(scyllaQuery: Query, parameters?: Array<number | string | Uuid> | undefined | null): Promise<any>
125+
query<T = unknown>(scyllaQuery: Query, parameters?: Array<number | string | Uuid> | undefined | null): Promise<T>
126126
prepare(query: string): Promise<PreparedStatement>
127-
batch(batch: BatchStatement, parameters: Array<Array<number | string | Uuid> | undefined | null>): Promise<any>
127+
/**
128+
* Perform a batch query\
129+
* Batch contains many `simple` or `prepared` queries which are executed at once\
130+
* Batch doesn't return any rows
131+
*
132+
* Batch values must contain values for each of the queries
133+
*
134+
* See [the book](https://rust-driver.docs.scylladb.com/stable/queries/batch.html) for more information
135+
*
136+
* # Arguments
137+
* * `batch` - Batch to be performed
138+
* * `values` - List of values for each query, it's the easiest to use an array of arrays
139+
*
140+
* # Example
141+
* ```javascript
142+
* const nodes = process.env.CLUSTER_NODES?.split(",") ?? ["127.0.0.1:9042"];
143+
*
144+
* const cluster = new Cluster({ nodes });
145+
* const session = await cluster.connect();
146+
*
147+
* const batch = new BatchStatement();
148+
*
149+
* await session.execute("CREATE KEYSPACE IF NOT EXISTS batch_statements WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
150+
* await session.useKeyspace("batch_statements");
151+
* await session.execute("CREATE TABLE IF NOT EXISTS users (id UUID PRIMARY KEY, name TEXT)");
152+
*
153+
* const simpleStatement = new Query("INSERT INTO users (id, name) VALUES (?, ?)");
154+
* const preparedStatement = await session.prepare("INSERT INTO users (id, name) VALUES (?, ?)");
155+
*
156+
* batch.appendStatement(simpleStatement);
157+
* batch.appendStatement(preparedStatement);
158+
*
159+
* await session.batch(batch, [[Uuid.randomV4(), "Alice"], [Uuid.randomV4(), "Bob"]]);
160+
*
161+
* console.log(await session.execute("SELECT * FROM users"));
162+
* ```
163+
*/
164+
batch<T = unknown>(batch: BatchStatement, parameters: Array<Array<number | string | Uuid> | undefined | null>): Promise<T>
128165
/**
129166
* Sends `USE <keyspace_name>` request on all connections\
130167
* This allows to write `SELECT * FROM table` instead of `SELECT * FROM keyspace.table`\
@@ -161,7 +198,7 @@ export class ScyllaSession {
161198
*
162199
* const result = await session
163200
* .execute("SELECT * FROM scylla_tables limit ?", [1])
164-
* .catch((err) => console.error(err));
201+
* .catch(console.error);
165202
* ```
166203
*/
167204
useKeyspace(keyspaceName: string, caseSensitive?: boolean | undefined | null): Promise<void>

src/session/scylla_session.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl ScyllaSession {
4040
Either3::B(query) => self.session.query(query.query.clone(), values).await,
4141
Either3::C(prepared) => self.session.execute(&prepared.prepared, values).await,
4242
}
43-
.map_err(|_| {
43+
.map_err(|e| {
4444
let query = match query {
4545
Either3::A(query) => query,
4646
Either3::B(query) => query.query.contents.clone(),
@@ -49,7 +49,7 @@ impl ScyllaSession {
4949

5050
napi::Error::new(
5151
napi::Status::InvalidArg,
52-
format!("Something went wrong with your query. - [{query}] - {parameters:?}"),
52+
format!("Something went wrong with your query. - [{query}] - {parameters:?}\n{e}"),
5353
)
5454
})?;
5555

@@ -71,10 +71,10 @@ impl ScyllaSession {
7171
.session
7272
.query(scylla_query.query.clone(), values)
7373
.await
74-
.map_err(|_| {
74+
.map_err(|e| {
7575
napi::Error::new(
7676
napi::Status::InvalidArg,
77-
format!("Something went wrong with your query. - [{scylla_query}] - {parameters:?}"),
77+
format!("Something went wrong with your query. - [{scylla_query}] - {parameters:?}\n{e}"),
7878
)
7979
})?;
8080

@@ -83,16 +83,51 @@ impl ScyllaSession {
8383

8484
#[napi]
8585
pub async fn prepare(&self, query: String) -> napi::Result<PreparedStatement> {
86-
let prepared = self.session.prepare(query.clone()).await.map_err(|_| {
86+
let prepared = self.session.prepare(query.clone()).await.map_err(|e| {
8787
napi::Error::new(
8888
napi::Status::InvalidArg,
89-
format!("Something went wrong with your prepared statement. - [{query}]"),
89+
format!("Something went wrong with your prepared statement. - [{query}]\n{e}"),
9090
)
9191
})?;
9292

9393
Ok(PreparedStatement::new(prepared))
9494
}
9595

96+
/// Perform a batch query\
97+
/// Batch contains many `simple` or `prepared` queries which are executed at once\
98+
/// Batch doesn't return any rows
99+
///
100+
/// Batch values must contain values for each of the queries
101+
///
102+
/// See [the book](https://rust-driver.docs.scylladb.com/stable/queries/batch.html) for more information
103+
///
104+
/// # Arguments
105+
/// * `batch` - Batch to be performed
106+
/// * `values` - List of values for each query, it's the easiest to use an array of arrays
107+
///
108+
/// # Example
109+
/// ```javascript
110+
/// const nodes = process.env.CLUSTER_NODES?.split(",") ?? ["127.0.0.1:9042"];
111+
///
112+
/// const cluster = new Cluster({ nodes });
113+
/// const session = await cluster.connect();
114+
///
115+
/// const batch = new BatchStatement();
116+
///
117+
/// await session.execute("CREATE KEYSPACE IF NOT EXISTS batch_statements WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
118+
/// await session.useKeyspace("batch_statements");
119+
/// await session.execute("CREATE TABLE IF NOT EXISTS users (id UUID PRIMARY KEY, name TEXT)");
120+
///
121+
/// const simpleStatement = new Query("INSERT INTO users (id, name) VALUES (?, ?)");
122+
/// const preparedStatement = await session.prepare("INSERT INTO users (id, name) VALUES (?, ?)");
123+
///
124+
/// batch.appendStatement(simpleStatement);
125+
/// batch.appendStatement(preparedStatement);
126+
///
127+
/// await session.batch(batch, [[Uuid.randomV4(), "Alice"], [Uuid.randomV4(), "Bob"]]);
128+
///
129+
/// console.log(await session.execute("SELECT * FROM users"));
130+
/// ```
96131
#[napi]
97132
#[allow(clippy::type_complexity)]
98133
pub async fn batch(
@@ -159,7 +194,7 @@ impl ScyllaSession {
159194
///
160195
/// const result = await session
161196
/// .execute("SELECT * FROM scylla_tables limit ?", [1])
162-
/// .catch((err) => console.error(err));
197+
/// .catch(console.error);
163198
/// ```
164199
#[napi]
165200
pub async fn use_keyspace(

0 commit comments

Comments
 (0)