Node.js querying
A simple query can be performed by passing a CQL query to the client using the
executeQuery()
function for standard query execution:
// For Stargate OSS: SELECT the data to read from the table
const query = new Query();
const queryString = 'SELECT firstname, lastname FROM test.users;'
// Set the CQL statement using the string defined in the last line
query.setCql(queryString);
// For Stargate OSS: execute the query statement
const response = await promisifiedClient.executeQuery(
query,
authenticationMetadata
);
console.log("select executed")
Data definition (DDL) queries are supported in the same manner:
// For Stargate OSS: Create a new keyspace
const createKeyspaceStatement = new Query();
// Set the CQL statement
createKeyspaceStatement.setCql("CREATE KEYSPACE IF NOT EXISTS test WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1};");
await promisifiedClient.executeQuery(createKeyspaceStatement, authenticationMetadata);
console.log("created keyspace");
// For Stargate OSS: Create a new table
const createTableStatement = new Query();
// Set the CQL statement
createTableStatement.setCql("CREATE TABLE IF NOT EXISTS test.users (firstname text PRIMARY KEY, lastname text);");
await promisifiedClient.executeQuery(
createTableStatement,
authenticationMetadata
);
console.log("created table");
Parameterized queries are also supported:
const query = new Query();
query.setCql("select * from system_schema.keyspaces where keyspace_name = ?");
const keyspaceNameValue = new Value();
keyspaceNameValue.setString("system");
const queryValues = new Values();
queryValues.setValuesList([keyspaceNameValue]);
query.setValues(queryValues);
const queryParameters = new QueryParameters();
queryParameters.setTracing(false);
queryParameters.setSkipMetadata(false);
query.setParameters(queryParameters);
const response = await promisifiedClient.executeQuery(query, metadata);
If you would like to use a
batch statement,
the client also provides an executeBatch()
function to execute a batch query:
// For Stargate OSS: INSERT two rows/records
// Create two queries that will be run in a batch statement
const insertOne = new BatchQuery();
const insertTwo = new BatchQuery();
// Set the CQL statement
insertOne.setCql(`INSERT INTO test.users (firstname, lastname) VALUES('Jane', 'Doe')`);
insertTwo.setCql(`INSERT INTO test.users (firstname, lastname) VALUES('Serge', 'Provencio')`);
// Define the new batch to include the 2 insertions
const batch = new Batch();
batch.setQueriesList([insertOne, insertTwo]);
// For Stargate OSS: execute the batch statement
const batchResult = await promisifiedClient.executeBatch(
batch,
authenticationMetadata
);
console.log("inserted data");
Promise support
The Node gRPC implementation uses callbacks by default. If you’d prefer promises, this library provides a utility function to create a promisified version of the Stargate gRPC client. The promise will reject if an error occurs:
import {
StargateClient,
promisifyStargateClient,
} from "@stargate-oss/stargate-grpc-node-client";
const stargateClient = new StargateClient(
"localhost:8090",
grpc.credentials.createInsecure()
);
const promisifiedClient = promisifyStargateClient(stargateClient);
try {
const queryResult = await promisifiedClient.executeQuery(
query,
metadata,
callOptions
);
const batchResult = await promisifiedClient.executeBatch(
query,
metadata,
callOptions
);
} catch (e) {
// something went wrong
}
The metadata
and callOptions
arguments are both optional.