Represents a Cassandra table, encapsulating functionality for schema definition, data manipulation, and querying. This class provides a high-level abstraction over Cassandra's table operations, including creating tables, inserting, updating, selecting, and deleting records. It leverages the CassandraClient for executing operations and supports asynchronous interactions with the database.

Key features include:

  • Table and keyspace management: Allows for specifying table schema, including primary keys, columns, and indices, and handles the creation of these elements within the specified keyspace.
  • Data manipulation: Offers methods for inserting (upserting) and deleting data in batches or individually, with support for asynchronous operation and concurrency control.
  • Querying: Enables selecting data with flexible filtering, sorting, and pagination options.

The class is designed to be instantiated with a set of configuration arguments (CassandraTableArgs) that define the table's structure and operational parameters, providing a streamlined interface for interacting with Cassandra tables in a structured and efficient manner.

Usage Example:

const tableArgs: CassandraTableArgs = {
table: 'my_table',
keyspace: 'my_keyspace',
primaryKey: [{ name: 'id', type: 'uuid', partition: true }],
nonKeyColumns: [{ name: 'data', type: 'text' }],
};
const cassandraClient = new CassandraClient(clientConfig);
const myTable = new CassandraTable(tableArgs, cassandraClient);

This class simplifies Cassandra database interactions, making it easier to perform robust data operations while maintaining clear separation of concerns and promoting code reusability.

Constructors

  • Initializes a new instance of the CassandraTable class with specified configuration. This includes setting up the table schema (primary key, columns, and indices) and preparing the environment for executing queries against a Cassandra database.

    Parameters

    • args: CassandraTableArgs

      Configuration arguments defining the table schema and operational settings.

    • Optionalclient: Client

      Optional. A Cassandra Client instance. If not provided, one will be created using the configuration specified in args.

    Returns CassandraTable

Methods

  • Deletes rows from the Cassandra table that match the specified WHERE clause conditions.

    Parameters

    • whereClause: WhereClause

      Defines the conditions that must be met for rows to be deleted. Can be a single filter, an array of filters, or a key-value map translating to filter conditions.

    Returns Promise<ResultSet>

    A Promise that resolves when the DELETE operation has completed.

  • Retrieves the Node.js Cassandra client instance associated with this table. This method ensures that the client is initialized and ready for use, returning the Cassandra client object that can be used for database operations directly. It initializes the client if it has not already been initialized.

    Returns Promise<Client>

    A Promise that resolves to the Cassandra Client instance used by this table for database interactions.

  • Executes a SELECT query on the Cassandra table with optional filtering, ordering, and pagination. Allows for specifying columns to return, filter conditions, sort order, and limits on the number of results.

    Parameters

    • Optionalcolumns: Column[]

      Optional. Columns to include in the result set. If omitted, all columns are selected.

    • Optionalfilter: WhereClause

      Optional. Conditions to apply to the query for filtering results.

    • OptionalorderBy: Filter[]

      Optional. Criteria to sort the result set.

    • Optionallimit: number

      Optional. Maximum number of records to return.

    • OptionalallowFiltering: boolean

      Optional. Enables ALLOW FILTERING option for queries that cannot be executed directly due to Cassandra's query restrictions.

    • OptionalfetchSize: number

      Optional. The number of rows to fetch per page (for pagination).

    • OptionalpagingState: string

      Optional. The paging state from a previous query execution, used for pagination.

    Returns Promise<ResultSet>

    A Promise resolving to the query result set.

  • Inserts or updates records in the Cassandra table in batches, managing concurrency and batching size. This method organizes the provided values into batches and uses _upsert to perform the database operations.

    Parameters

    • values: unknown[][]

      An array of arrays, where each inner array contains values for a single record.

    • Optionalcolumns: Column[]

      Optional. Columns to be included in the insert/update operations. Defaults to all table columns.

    • batchSize: number = ...

      Optional. The size of each batch for the operation. Defaults to the class's batchSize property.

    Returns Promise<void>

    A Promise that resolves once all records have been upserted.