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?: {
embeddingApiKey?: string | EmbeddingHeadersProvider,
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. The keyspace containing the table. For an example, see Get a table and specify the keyspace. Default: The working keyspace for the database.
This is |
|
|
Optional. This only applies to tables that have a vector column with a vectorize embedding provider integration. Use this option to provide the embedding provider API key directly with headers instead of using an API key in the Astra DB KMS. The API key is sent to the Data API for every operation on the table. It is useful when a vectorize integration is configured but no credentials are stored, or when you want to override the stored credentials. For more information, see Manage embedding provider integrations for vectorize. You can use this authentication method only if all affected columns use the same embedding provider. If you use an AWS embedding provider, 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 } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const table = database.table("TABLE_NAME");
Get a table and specify the keyspace
By default, this method uses the working keyspace of your database. If you want to use a different keyspace, you must specify the keyspace.
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_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, 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();
const database = client.db("API_ENDPOINT", {
token: "APPLICATION_TOKEN",
});
const table = database.table<TableSchema, TablePrimaryKey>("TABLE_NAME");
Client reference
For more information, see the client reference.