Skip to content

Commit 1637629

Browse files
authored
feat(session): add useKeyspace function (#26)
Signed-off-by: Daniel Boll <danielboll.academico@gmail.com>
1 parent bdcf1bd commit 1637629

File tree

4 files changed

+99
-13
lines changed

4 files changed

+99
-13
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,5 @@ base.mts
201201
.vscode
202202

203203
pack
204+
205+
debug.mts

debug.mts

Lines changed: 0 additions & 13 deletions
This file was deleted.

index.d.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,46 @@ export class ScyllaSession {
9494
execute(query: string | ScyllaPreparedStatement, parameters?: Array<number | string | Uuid> | undefined | null): Promise<any>
9595
query(scyllaQuery: Query, parameters?: Array<number | string | Uuid> | undefined | null): Promise<any>
9696
prepare(query: string): Promise<ScyllaPreparedStatement>
97+
/**
98+
* Sends `USE <keyspace_name>` request on all connections\
99+
* This allows to write `SELECT * FROM table` instead of `SELECT * FROM keyspace.table`\
100+
*
101+
* Note that even failed `useKeyspace` can change currently used keyspace - the request is sent on all connections and
102+
* can overwrite previously used keyspace.
103+
*
104+
* Call only one `useKeyspace` at a time.\
105+
* Trying to do two `useKeyspace` requests simultaneously with different names
106+
* can end with some connections using one keyspace and the rest using the other.
107+
*
108+
* # Arguments
109+
*
110+
* * `keyspaceName` - keyspace name to use,
111+
* keyspace names can have up to 48 alphanumeric characters and contain underscores
112+
* * `caseSensitive` - if set to true the generated query will put keyspace name in quotes
113+
*
114+
* # Errors
115+
*
116+
* * `InvalidArg` - if the keyspace name is invalid
117+
*
118+
* # Example
119+
*
120+
* ```javascript
121+
* import { Cluster } from ".";
122+
*
123+
* const cluster = new Cluster({
124+
* nodes: ["127.0.0.1:9042"],
125+
* });
126+
*
127+
* const session = await cluster.connect();
128+
*
129+
* await session.useKeyspace("system_schema");
130+
*
131+
* const result = await session
132+
* .execute("SELECT * FROM scylla_tables limit ?", [1])
133+
* .catch((err) => console.error(err));
134+
* ```
135+
*/
136+
useKeyspace(keyspaceName: string, caseSensitive?: boolean | undefined | null): Promise<void>
97137
}
98138
export class Uuid {
99139
/** Generates a random UUID v4. */

src/session/scylla_session.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,61 @@ impl ScyllaSession {
8989

9090
Ok(ScyllaPreparedStatement::new(prepared))
9191
}
92+
93+
/// Sends `USE <keyspace_name>` request on all connections\
94+
/// This allows to write `SELECT * FROM table` instead of `SELECT * FROM keyspace.table`\
95+
///
96+
/// Note that even failed `useKeyspace` can change currently used keyspace - the request is sent on all connections and
97+
/// can overwrite previously used keyspace.
98+
///
99+
/// Call only one `useKeyspace` at a time.\
100+
/// Trying to do two `useKeyspace` requests simultaneously with different names
101+
/// can end with some connections using one keyspace and the rest using the other.
102+
///
103+
/// # Arguments
104+
///
105+
/// * `keyspaceName` - keyspace name to use,
106+
/// keyspace names can have up to 48 alphanumeric characters and contain underscores
107+
/// * `caseSensitive` - if set to true the generated query will put keyspace name in quotes
108+
///
109+
/// # Errors
110+
///
111+
/// * `InvalidArg` - if the keyspace name is invalid
112+
///
113+
/// # Example
114+
///
115+
/// ```javascript
116+
/// import { Cluster } from ".";
117+
///
118+
/// const cluster = new Cluster({
119+
/// nodes: ["127.0.0.1:9042"],
120+
/// });
121+
///
122+
/// const session = await cluster.connect();
123+
///
124+
/// await session.useKeyspace("system_schema");
125+
///
126+
/// const result = await session
127+
/// .execute("SELECT * FROM scylla_tables limit ?", [1])
128+
/// .catch((err) => console.error(err));
129+
/// ```
130+
#[napi]
131+
pub async fn use_keyspace(
132+
&self,
133+
keyspace_name: String,
134+
case_sensitive: Option<bool>,
135+
) -> napi::Result<()> {
136+
self
137+
.session
138+
.use_keyspace(keyspace_name.clone(), case_sensitive.unwrap_or(false))
139+
.await
140+
.map_err(|e| {
141+
napi::Error::new(
142+
napi::Status::InvalidArg,
143+
format!("Something went wrong with your keyspace. - [{keyspace_name}]\n{e}"),
144+
)
145+
})?;
146+
147+
Ok(())
148+
}
92149
}

0 commit comments

Comments
 (0)