Get a table (TypeScript)
Gets a reference to a table for use with the Data API clients.
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Returns a Table<Schema> object that corresponds to the specified table name.
This method returns a Table object even for tables that don’t exist.
Unless you specify the Schema, the table is typed as Table<Record<string, any>>.
Parameters
Use the table method, which belongs to the Db class.
Method signature
table(
name: string,
options?: {
logging?: DataAPILoggingConfig,
serdes?: TableSerDesConfig,
timeoutDefaults?: Partial<TimeoutDescriptor>,
keyspace?: string,
},
): Table<Schema, PKeys>
| Name | Type | Summary |
|---|---|---|
|
|
The name of the table. |
|
|
The options for this operation. See Properties of |
| Name | Type | Summary |
|---|---|---|
|
|
Optional if you specified a working keyspace when you created the Default: The working keyspace set when you created the |
|
|
Optional.
The configuration for logging events emitted by the |
|
|
Optional.
The default timeout options for any operation performed on this |
|
|
Optional. Lower-level serialization/deserialization configuration for this table. For more information, see Custom Ser/Des. |
Examples
The following examples demonstrate how to get a table.
Get a table
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
keyspace: "KEYSPACE_NAME",
});
const table = database.table("TABLE_NAME");
Get a table and specify typing
When you get a table, you can specify the table schema type.
If you specify the table schema type, you can also specify the primary key type.
The primary key type is only used in the return types of the insertOne and insertMany methods.
You don’t need to specify the primary key type if you don’t need to use the returned values from the insertOne and insertMany methods.
If you don’t specify any typing, the table rows are typed as Record<string, any>.
This is the most flexible but least type-safe option.
For more information, see Collection and table typing.
import {
DataAPIClient,
UsernamePasswordTokenProvider,
DataAPIDate,
} from "@datastax/astra-db-ts";
// Manually define the type of the table's schema and primary key
type TableSchema = {
title: string;
author: string;
number_of_pages?: number | null | undefined;
rating?: number | null | undefined;
genres?: Set<string> | undefined;
metadata?: Map<string, string> | undefined;
is_checked_out?: boolean | null | undefined;
due_date?: DataAPIDate | null | undefined;
};
type TablePrimaryKey = Pick<TableSchema, "title" | "author">;
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
keyspace: "KEYSPACE_NAME",
});
const table = database.table<TableSchema, TablePrimaryKey>("TABLE_NAME");
Client reference
For more information, see the client reference.