Create a table (TypeScript)

Creates a new table in a keyspace in a database.

After you create a table, index columns that you want to sort or filter. This optimizes your queries and avoids resource intensive, long running allow filtering operations.

You can also modify the table columns later. To add data to your table, insert rows.

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

Creates a table with the specified parameters.

Returns a promise that resolves to a <Table<Schema, PKey>> object. You can use this object to work with rows in the table.

Unless you specify the Schema, the table is typed as Table<Record<string, any>>.

Parameters

Use the createTable method, which belongs to the Db class.

Method signature
async createTable<const Def extends CreateTableDefinition>(
  name: string,
  options: {
    definition: CreateTableDefinition,
    ifNotExists?: boolean,
    embeddingApiKey?: string | EmbeddingHeadersProvider,
    logging?: DataAPILoggingConfig,
    serdes?: TableSerDesConfig,
    timeoutDefaults?: Partial<TimeoutDescriptor>,
    keyspace?: string,
  }
): Table<InferTableSchema<Def>, InferTablePrimaryKey<Def>>

Parameters:

Name Type Summary

name

string

The name of the table.

Table names must follow these rules:

  • Can contain letters, numbers, and underscores

  • Cannot exceed 48 characters

  • Must be unique within the keyspace

options

CreateTableOptions

The options for this operation. See Properties of options for more details.

Properties of options
Name Type Summary

definition

CreateTableDefinition

The full schema for the table, including column names, column data types, and the primary key.

See the examples for usage.

All column names used in the schema must be unique within the table.

ifNotExists

boolean

Optional. Whether the command should silently succeed even if a table with the given name already exists in the keyspace and no new table was created.

This option only checks table names. It does not check table schemas.

Default: false

keyspace

string

Optional. The keyspace in which to create the table.

For an example, see Create a table and specify the keyspace.

Default: The working keyspace for the database. This is default_keyspace unless you set a different working keyspace when you created the Db object.

embeddingApiKey

string | EmbeddingHeadersProvider

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 embeddingApiKey option must instead use the AWSEmbeddingHeadersProvider class to pass your access ID and secret ID.

logging

DataAPILoggingConfig

Optional. The configuration for logging events emitted by the DataAPIClient. For more information, see Logging.

timeoutDefaults

Partial<TimeoutDescriptor>

Optional. The default timeout options for any operation performed on this Table instance. For more information, see TypeScript client internals: TimeoutDescriptor.

serdes

TableSerDesConfig

Optional. Lower-level serialization/deserialization configuration for this table. For more information, see Custom Ser/Des.

Examples

The following examples demonstrate how to create a table.

Create a table with a single-column primary key

A single-column primary key is a primary key consisting of one column. For more information, see Primary keys in tables (TypeScript).

The TypeScript client supports multiple ways to create a table. The method you choose depends on your typing preferences and whether you modified the ser/des configuration.

For more information, see Collection and table typing.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

The TypeScript client can automatically infer the TypeScript-equivalent type of the table’s schema and primary key.

To do this, first create the table definition. Then, use InferTableSchema and InferTablePrimaryKey to infer the type of the table and of the primary key. To create the table, provide the table definition and the inferred types to the createTable method.

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    title: "text",
    number_of_pages: "int",
    rating: "float",
    genres: { type: "set", valueType: "text" },
    metadata: {
      type: "map",
      keyType: "text",
      valueType: "text",
    },
    is_checked_out: "boolean",
    due_date: "date",
  },
  // Define the primary key for the table.
  // In this case, the table uses a single-column primary key.
  primaryKey: {
    partitionBy: ["title"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  // Provide the types and the definition
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "example_table",
    { definition: tableDefinition },
  );
})();

You can use the TableSchema type as you would any other type. For example, this gives a type error since the TableSchema type from the previous example does not include bad_field:

  const row: TableSchema = {
    title: "Wind with No Name",
    number_of_pages: 193,
    bad_field: "I will error",
  };

You can manually define the type for your table’s schema and primary key. To create the table, provide the table definition and the types to the createTable method.

This may be necessary if you modify the table’s default ser/des configuration.

import { DataAPIClient, DataAPIDate, Table } from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    title: "text",
    number_of_pages: "int",
    rating: "float",
    genres: { type: "set", valueType: "text" },
    metadata: {
      type: "map",
      keyType: "text",
      valueType: "text",
    },
    is_checked_out: "boolean",
    due_date: "date",
  },
  // Define the primary key for the table.
  // In this case, the table uses a single-column primary key.
  primaryKey: {
    partitionBy: ["title"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  title: 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">;

(async function () {
  // Provide the types and the definition to create the table
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "example_table",
    { definition: tableDefinition },
  );
})();

You can use the TableSchema type as you would any other type. For example, this gives a type error since the TableSchema type from the previous example does not include bad_field:

  const row: TableSchema = {
    title: "Wind with No Name",
    number_of_pages: 193,
    bad_field: "I will error",
  };

To create a table without any typing, pass SomeRow as the single generic type parameter to the createTable method. This types the table’s rows as Record<string, any>.

This is the most flexible but least type-safe option.

import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    title: "text",
    number_of_pages: "int",
    rating: "float",
    genres: { type: "set", valueType: "text" },
    metadata: {
      type: "map",
      keyType: "text",
      valueType: "text",
    },
    is_checked_out: "boolean",
    due_date: "date",
  },
  // Define the primary key for the table.
  // In this case, the table uses a single-column primary key.
  primaryKey: {
    partitionBy: ["title"],
  },
});

(async function () {
  // Provide the types and the definition to create the table
  const table = await database.createTable<SomeRow>("example_table", {
    definition: tableDefinition,
  });
})();

Create a table with a composite primary key

A composite primary key is a primary key consisting of multiple columns. For more information, see Primary keys in tables (TypeScript).

The TypeScript client supports multiple ways to create a table. The method you choose depends on your typing preferences and whether you modified the ser/des configuration.

For more information, see Collection and table typing.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

The TypeScript client can automatically infer the TypeScript-equivalent type of the table’s schema and primary key.

To do this, first create the table definition. Then, use InferTableSchema and InferTablePrimaryKey to infer the type of the table and of the primary key. To create the table, provide the table definition and the inferred types to the createTable method.

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    title: "text",
    number_of_pages: "int",
    rating: "float",
    genres: { type: "set", valueType: "text" },
    metadata: {
      type: "map",
      keyType: "text",
      valueType: "text",
    },
    is_checked_out: "boolean",
    due_date: "date",
  },
  // Define the primary key for the table.
  // In this case, the table uses a composite primary key.
  primaryKey: {
    partitionBy: ["title", "rating"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  // Provide the types and the definition
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "example_table",
    { definition: tableDefinition },
  );
})();

You can use the TableSchema type as you would any other type. For example, this gives a type error since the TableSchema type from the previous example does not include bad_field:

  const row: TableSchema = {
    title: "Wind with No Name",
    number_of_pages: 193,
    bad_field: "I will error",
  };

You can manually define the type for your table’s schema and primary key. To create the table, provide the table definition and the types to the createTable method.

This may be necessary if you modify the table’s default ser/des configuration.

import { DataAPIClient, DataAPIDate, Table } from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    title: "text",
    number_of_pages: "int",
    rating: "float",
    genres: { type: "set", valueType: "text" },
    metadata: {
      type: "map",
      keyType: "text",
      valueType: "text",
    },
    is_checked_out: "boolean",
    due_date: "date",
  },
  // Define the primary key for the table.
  // In this case, the table uses a composite primary key.
  primaryKey: {
    partitionBy: ["title", "rating"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  title: 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" | "rating">;

(async function () {
  // Provide the types and the definition to create the table
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "example_table",
    { definition: tableDefinition },
  );
})();

You can use the TableSchema type as you would any other type. For example, this gives a type error since the TableSchema type from the previous example does not include bad_field:

  const row: TableSchema = {
    title: "Wind with No Name",
    number_of_pages: 193,
    bad_field: "I will error",
  };

To create a table without any typing, pass SomeRow as the single generic type parameter to the createTable method. This types the table’s rows as Record<string, any>.

This is the most flexible but least type-safe option.

import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    title: "text",
    number_of_pages: "int",
    rating: "float",
    genres: { type: "set", valueType: "text" },
    metadata: {
      type: "map",
      keyType: "text",
      valueType: "text",
    },
    is_checked_out: "boolean",
    due_date: "date",
  },
  // Define the primary key for the table.
  // In this case, the table uses a composite primary key.
  primaryKey: {
    partitionBy: ["title", "rating"],
  },
});

(async function () {
  // Provide the types and the definition to create the table
  const table = await database.createTable<SomeRow>("example_table", {
    definition: tableDefinition,
  });
})();

Create a table with a compound primary key

A compound primary key is a primary key consisting of partition (grouping) columns and clustering (sorting) columns. For more information, see Primary keys in tables (TypeScript).

The TypeScript client supports multiple ways to create a table. The method you choose depends on your typing preferences and whether you modified the ser/des configuration.

For more information, see Collection and table typing.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

The TypeScript client can automatically infer the TypeScript-equivalent type of the table’s schema and primary key.

To do this, first create the table definition. Then, use InferTableSchema and InferTablePrimaryKey to infer the type of the table and of the primary key. To create the table, provide the table definition and the inferred types to the createTable method.

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    title: "text",
    number_of_pages: "int",
    rating: "float",
    genres: { type: "set", valueType: "text" },
    metadata: {
      type: "map",
      keyType: "text",
      valueType: "text",
    },
    is_checked_out: "boolean",
    due_date: "date",
  },
  // Define the primary key for the table.
  // In this case, the table uses a compound primary key.
  primaryKey: {
    partitionBy: ["title", "rating"],
    partitionSort: { number_of_pages: 1, is_checked_out: -1 },
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  // Provide the types and the definition
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "example_table",
    { definition: tableDefinition },
  );
})();

You can use the TableSchema type as you would any other type. For example, this gives a type error since the TableSchema type from the previous example does not include bad_field:

  const row: TableSchema = {
    title: "Wind with No Name",
    number_of_pages: 193,
    bad_field: "I will error",
  };

You can manually define the type for your table’s schema and primary key. To create the table, provide the table definition and the types to the createTable method.

This may be necessary if you modify the table’s default ser/des configuration.

import { DataAPIClient, DataAPIDate, Table } from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    title: "text",
    number_of_pages: "int",
    rating: "float",
    genres: { type: "set", valueType: "text" },
    metadata: {
      type: "map",
      keyType: "text",
      valueType: "text",
    },
    is_checked_out: "boolean",
    due_date: "date",
  },
  // Define the primary key for the table.
  // In this case, the table uses a compound primary key.
  primaryKey: {
    partitionBy: ["title", "rating"],
    partitionSort: { number_of_pages: 1, is_checked_out: -1 },
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  title: 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" | "rating">;

(async function () {
  // Provide the types and the definition to create the table
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "example_table",
    { definition: tableDefinition },
  );
})();

You can use the TableSchema type as you would any other type. For example, this gives a type error since the TableSchema type from the previous example does not include bad_field:

  const row: TableSchema = {
    title: "Wind with No Name",
    number_of_pages: 193,
    bad_field: "I will error",
  };

To create a table without any typing, pass SomeRow as the single generic type parameter to the createTable method. This types the table’s rows as Record<string, any>.

This is the most flexible but least type-safe option.

import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    title: "text",
    number_of_pages: "int",
    rating: "float",
    genres: { type: "set", valueType: "text" },
    metadata: {
      type: "map",
      keyType: "text",
      valueType: "text",
    },
    is_checked_out: "boolean",
    due_date: "date",
  },
  // Define the primary key for the table.
  // In this case, the table uses a compound primary key.
  primaryKey: {
    partitionBy: ["title", "rating"],
    partitionSort: { number_of_pages: 1, is_checked_out: -1 },
  },
});

(async function () {
  // Provide the types and the definition to create the table
  const table = await database.createTable<SomeRow>("example_table", {
    definition: tableDefinition,
  });
})();

Create a table with a column to store vector embeddings

If you want to store pre-generated vector embeddings in a table, create a table with a vector column. A table can include more than one vector column.

The TypeScript client supports multiple ways to create a table. The method you choose depends on your typing preferences and whether you modified the ser/des configuration.

For more information, see Collection and table typing.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

The TypeScript client can automatically infer the TypeScript-equivalent type of the table’s schema and primary key.

To do this, first create the table definition. Then, use InferTableSchema and InferTablePrimaryKey to infer the type of the table and of the primary key. To create the table, provide the table definition and the inferred types to the createTable method.

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    example_vector: { type: "vector", dimension: 1024 },
    example_non_vector: "text",
  },
  // Define the primary key for the table.
  // In this case, the table uses a single-column primary key.
  primaryKey: {
    partitionBy: ["example_non_vector"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  // Provide the types and the definition
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "example_table",
    { definition: tableDefinition },
  );
})();

You can manually define the type for your table’s schema and primary key. To create the table, provide the table definition and the types to the createTable method.

This may be necessary if you modify the table’s default ser/des configuration.

import { DataAPIClient, DataAPIVector, Table } from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    example_vector: { type: "vector", dimension: 1024 },
    example_non_vector: "text",
  },
  // Define the primary key for the table.
  // In this case, the table uses a single-column primary key.
  primaryKey: {
    partitionBy: ["example_non_vector"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  example_vector: DataAPIVector;
  example_non_vector: string;
};

type TablePrimaryKey = Pick<TableSchema, "example_non_vector">;

(async function () {
  // Provide the types and the definition to create the table
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "example_table",
    { definition: tableDefinition },
  );
})();

To create a table without any typing, pass SomeRow as the single generic type parameter to the createTable method. This types the table’s rows as Record<string, any>.

This is the most flexible but least type-safe option.

import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    example_vector: { type: "vector", dimension: 1024 },
    example_non_vector: "text",
  },
  // Define the primary key for the table.
  // In this case, the table uses a single-column primary key.
  primaryKey: {
    partitionBy: ["example_non_vector"],
  },
});

(async function () {
  // Provide the types and the definition to create the table
  const table = await database.createTable<SomeRow>("example_table", {
    definition: tableDefinition,
  });
})();

Create a table with a column to automatically generate vector embeddings

If you want to automatically generate vector embeddings, create a table with a vector column and configure an embedding provider integration for the column.

The configuration depends on the embedding provider.

You can also configure an embedding provider integration after table creation. For more information, see Alter a table (TypeScript).

If you want to store the original text in addition to the vector embeddings that were generated from the text, then you need to create a separate column to store the text.

You can configure a different embedding provider for each vector column in the table. If you want to use the same embedding provider for all vector columns in the table, you must still configure the embedding provider for each vector column.

Configure Azure OpenAI as the embedding provider

For more detailed instructions, see Integrate Azure OpenAI as an embedding provider.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "azureOpenAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
        parameters: {
          resourceName: "RESOURCE_NAME",
          deploymentId: "DEPLOYMENT_ID",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, DataAPIVector, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "azureOpenAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
        parameters: {
          resourceName: "RESOURCE_NAME",
          deploymentId: "DEPLOYMENT_ID",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  VECTOR_COLUMN_NAME: DataAPIVector;
  TEXT_COLUMN_NAME: string;
};

type TablePrimaryKey = Pick<TableSchema, "TEXT_COLUMN_NAME">;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "azureOpenAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
        parameters: {
          resourceName: "RESOURCE_NAME",
          deploymentId: "DEPLOYMENT_ID",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

(async function () {
  const table = await database.createTable<SomeRow>("TABLE_NAME", {
    definition: tableDefinition,
  });
})();

Replace the following:

  • TABLE_NAME: The name for your table.

  • VECTOR_COLUMN_NAME: The name for your vector column.

  • TEXT_COLUMN_NAME: The name for the text column that will store the original text. Omit this column if you won’t store the original text in addition to the generated embeddings.

  • API_KEY_NAME: The name of the Azure OpenAI API key that you want to use. Must be the name of an existing Azure OpenAI API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embeddingApiKey parameter when you instantiate a Table object with the commands to create a table or get a table. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search. You can use this authentication method only if all affected columns use the same embedding provider.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: text-embedding-3-small, text-embedding-3-large, text-embedding-ada-002.

    For Azure OpenAI, you must select the model that matches the one deployed to your DEPLOYMENT_ID in Azure.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • RESOURCE_NAME: The name of your Azure OpenAI Service resource, as defined in the resource’s Instance details. For more information, see the Azure OpenAI documentation.

  • DEPLOYMENT_ID: Your Azure OpenAI resource’s Deployment name. For more information, see the Azure OpenAI documentation.

Configure Hugging Face (Dedicated) as the embedding provider

For more detailed instructions, see Integrate Hugging Face Dedicated as an embedding provider.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "huggingfaceDedicated",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
        parameters: {
          endpointName: "ENDPOINT_NAME",
          regionName: "REGION",
          cloudName: "CLOUD_PROVIDER",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, DataAPIVector, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "huggingfaceDedicated",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
        parameters: {
          endpointName: "ENDPOINT_NAME",
          regionName: "REGION",
          cloudName: "CLOUD_PROVIDER",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  VECTOR_COLUMN_NAME: DataAPIVector;
  TEXT_COLUMN_NAME: string;
};

type TablePrimaryKey = Pick<TableSchema, "TEXT_COLUMN_NAME">;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "huggingfaceDedicated",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
        parameters: {
          endpointName: "ENDPOINT_NAME",
          regionName: "REGION",
          cloudName: "CLOUD_PROVIDER",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

(async function () {
  const table = await database.createTable<SomeRow>("TABLE_NAME", {
    definition: tableDefinition,
  });
})();

Replace the following:

  • TABLE_NAME: The name for your table.

  • VECTOR_COLUMN_NAME: The name for your vector column.

  • TEXT_COLUMN_NAME: The name for the text column that will store the original text. Omit this column if you won’t store the original text in addition to the generated embeddings.

  • API_KEY_NAME: The name of the Hugging Face Dedicated user access token that you want to use. Must be the name of an existing Hugging Face Dedicated user access token in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embeddingApiKey parameter when you instantiate a Table object with the commands to create a table or get a table. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search. You can use this authentication method only if all affected columns use the same embedding provider.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: endpoint-defined-model.

    For Hugging Face Dedicated, you must deploy the model as a text embeddings inference (TEI) container.

    You must set MODEL_NAME to endpoint-defined-model because this integration uses the model specified in your dedicated endpoint configuration.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ENDPOINT_NAME: The programmatically-generated name of your Hugging Face Dedicated endpoint. This is the first part of the endpoint URL. For example, if your endpoint URL is https://mtp1x7muf6qyn3yh.us-east-2.aws.endpoints.huggingface.cloud, the endpoint name is mtp1x7muf6qyn3yh.

  • REGION: The cloud provider region your Hugging Face Dedicated endpoint is deployed to. For example, us-east-2.

  • CLOUD_PROVIDER: The cloud provider your Hugging Face Dedicated endpoint is deployed to. For example, aws.

Configure Hugging Face (Serverless) as the embedding provider

For more detailed instructions, see Integrate Hugging Face Serverless as an embedding provider.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "huggingface",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, DataAPIVector, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "huggingface",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  VECTOR_COLUMN_NAME: DataAPIVector;
  TEXT_COLUMN_NAME: string;
};

type TablePrimaryKey = Pick<TableSchema, "TEXT_COLUMN_NAME">;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "huggingface",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

(async function () {
  const table = await database.createTable<SomeRow>("TABLE_NAME", {
    definition: tableDefinition,
  });
})();

Replace the following:

  • TABLE_NAME: The name for your table.

  • VECTOR_COLUMN_NAME: The name for your vector column.

  • TEXT_COLUMN_NAME: The name for the text column that will store the original text. Omit this column if you won’t store the original text in addition to the generated embeddings.

  • API_KEY_NAME: The name of the Hugging Face Serverless user access token that you want to use. Must be the name of an existing Hugging Face Serverless user access token in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embeddingApiKey parameter when you instantiate a Table object with the commands to create a table or get a table. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search. You can use this authentication method only if all affected columns use the same embedding provider.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: sentence-transformers/all-MiniLM-L6-v2, intfloat/multilingual-e5-large, intfloat/multilingual-e5-large-instruct, BAAI/bge-small-en-v1.5, BAAI/bge-base-en-v1.5, BAAI/bge-large-en-v1.5.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

Configure Jina AI as the embedding provider

For more detailed instructions, see Integrate Jina AI as an embedding provider.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "jinaAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, DataAPIVector, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "jinaAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  VECTOR_COLUMN_NAME: DataAPIVector;
  TEXT_COLUMN_NAME: string;
};

type TablePrimaryKey = Pick<TableSchema, "TEXT_COLUMN_NAME">;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "jinaAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

(async function () {
  const table = await database.createTable<SomeRow>("TABLE_NAME", {
    definition: tableDefinition,
  });
})();

Replace the following:

  • TABLE_NAME: The name for your table.

  • VECTOR_COLUMN_NAME: The name for your vector column.

  • TEXT_COLUMN_NAME: The name for the text column that will store the original text. Omit this column if you won’t store the original text in addition to the generated embeddings.

  • API_KEY_NAME: The name of the Jina AI API key that you want to use. Must be the name of an existing Jina AI API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embeddingApiKey parameter when you instantiate a Table object with the commands to create a table or get a table. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search. You can use this authentication method only if all affected columns use the same embedding provider.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: jina-embeddings-v2-base-en, jina-embeddings-v2-base-de, jina-embeddings-v2-base-es, jina-embeddings-v2-base-code, jina-embeddings-v2-base-zh.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

Configure Mistral AI as the embedding provider

For more detailed instructions, see Integrate Mistral AI as an embedding provider.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "mistral",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, DataAPIVector, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "mistral",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  VECTOR_COLUMN_NAME: DataAPIVector;
  TEXT_COLUMN_NAME: string;
};

type TablePrimaryKey = Pick<TableSchema, "TEXT_COLUMN_NAME">;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "mistral",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

(async function () {
  const table = await database.createTable<SomeRow>("TABLE_NAME", {
    definition: tableDefinition,
  });
})();

Replace the following:

  • TABLE_NAME: The name for your table.

  • VECTOR_COLUMN_NAME: The name for your vector column.

  • TEXT_COLUMN_NAME: The name for the text column that will store the original text. Omit this column if you won’t store the original text in addition to the generated embeddings.

  • API_KEY_NAME: The name of the Mistral AI API key that you want to use. Must be the name of an existing Mistral AI API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embeddingApiKey parameter when you instantiate a Table object with the commands to create a table or get a table. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search. You can use this authentication method only if all affected columns use the same embedding provider.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: mistral-embed.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

Configure NVIDIA as the embedding provider

For more detailed instructions, see Integrate NVIDIA as an embedding provider. Your database must be in a supported region.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      service: {
        provider: "nvidia",
        modelName: "nvidia/nv-embedqa-e5-v5",
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, DataAPIVector, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      service: {
        provider: "nvidia",
        modelName: "nvidia/nv-embedqa-e5-v5",
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  VECTOR_COLUMN_NAME: DataAPIVector;
  TEXT_COLUMN_NAME: string;
};

type TablePrimaryKey = Pick<TableSchema, "TEXT_COLUMN_NAME">;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      service: {
        provider: "nvidia",
        modelName: "nvidia/nv-embedqa-e5-v5",
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

(async function () {
  const table = await database.createTable<SomeRow>("TABLE_NAME", {
    definition: tableDefinition,
  });
})();

Configure OpenAI as the embedding provider

For more detailed instructions, see Integrate OpenAI as an embedding provider.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "openai",
        modelName: "MODEL_NAME}",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
        parameters: {
          organizationId: "ORGANIZATION_ID",
          projectId: "PROJECT_ID",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, DataAPIVector, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "openai",
        modelName: "MODEL_NAME}",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
        parameters: {
          organizationId: "ORGANIZATION_ID",
          projectId: "PROJECT_ID",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  VECTOR_COLUMN_NAME: DataAPIVector;
  TEXT_COLUMN_NAME: string;
};

type TablePrimaryKey = Pick<TableSchema, "TEXT_COLUMN_NAME">;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "openai",
        modelName: "MODEL_NAME}",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
        parameters: {
          organizationId: "ORGANIZATION_ID",
          projectId: "PROJECT_ID",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

(async function () {
  const table = await database.createTable<SomeRow>("TABLE_NAME", {
    definition: tableDefinition,
  });
})();

Replace the following:

  • TABLE_NAME: The name for your table.

  • VECTOR_COLUMN_NAME: The name for your vector column.

  • TEXT_COLUMN_NAME: The name for the text column that will store the original text. Omit this column if you won’t store the original text in addition to the generated embeddings.

  • API_KEY_NAME: The name of the OpenAI API key that you want to use. Must be the name of an existing OpenAI API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embeddingApiKey parameter when you instantiate a Table object with the commands to create a table or get a table. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search. You can use this authentication method only if all affected columns use the same embedding provider.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: text-embedding-3-small, text-embedding-3-large, text-embedding-ada-002.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

  • ORGANIZATION_ID: Optional. The ID of the OpenAI organization that owns the API key. Only required if your OpenAI account belongs to multiple organizations or if you are using a legacy user API key to access projects. For more information about organization IDs, see the OpenAI API reference.

  • PROJECT_ID: Optional. The ID of the OpenAI project that owns the API key. This cannot use the default project. Only required if your OpenAI account belongs to multiple organizations or if you are using a legacy user API key to access projects. For more information about project IDs, see the OpenAI API reference.

Configure Upstage as the embedding provider

For more detailed instructions, see Integrate Upstage as an embedding provider.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "upstageAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, DataAPIVector, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "upstageAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  VECTOR_COLUMN_NAME: DataAPIVector;
  TEXT_COLUMN_NAME: string;
};

type TablePrimaryKey = Pick<TableSchema, "TEXT_COLUMN_NAME">;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "upstageAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

(async function () {
  const table = await database.createTable<SomeRow>("TABLE_NAME", {
    definition: tableDefinition,
  });
})();

Replace the following:

  • TABLE_NAME: The name for your table.

  • VECTOR_COLUMN_NAME: The name for your vector column.

  • TEXT_COLUMN_NAME: The name for the text column that will store the original text. Omit this column if you won’t store the original text in addition to the generated embeddings.

  • API_KEY_NAME: The name of the Upstage API key that you want to use. Must be the name of an existing Upstage API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embeddingApiKey parameter when you instantiate a Table object with the commands to create a table or get a table. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search. You can use this authentication method only if all affected columns use the same embedding provider.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: solar-embedding-1-large.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

Configure Voyage AI as the embedding provider

For more detailed instructions, see Integrate Voyage AI as an embedding provider.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "voyageAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, DataAPIVector, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "voyageAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

// Manually define the type of the table's schema and primary key
type TableSchema = {
  VECTOR_COLUMN_NAME: DataAPIVector;
  TEXT_COLUMN_NAME: string;
};

type TablePrimaryKey = Pick<TableSchema, "TEXT_COLUMN_NAME">;

(async function () {
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "TABLE_NAME",
    { definition: tableDefinition },
  );
})();
import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Instantiate the client
const client = new DataAPIClient();

// Connect to a database
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

// Define the columns and primary key for the table
const tableDefinition = Table.schema({
  columns: {
    // This column will store vector embeddings.
    // The configured vector service
    // will automatically generate vector embeddings
    // for any text inserted to this column.
    VECTOR_COLUMN_NAME: {
      type: "vector",
      dimension: MODEL_DIMENSIONS,
      service: {
        provider: "voyageAI",
        modelName: "MODEL_NAME",
        authentication: {
          providerKey: "API_KEY_NAME",
        },
      },
    },
    // If you want to store the original text
    // in addition to the generated embeddings
    // you must create a separate column.
    TEXT_COLUMN_NAME: "text",
  },
  // You should change the primary key definition to meet the needs of your data.
  primaryKey: {
    partitionBy: ["TEXT_COLUMN_NAME"],
  },
});

(async function () {
  const table = await database.createTable<SomeRow>("TABLE_NAME", {
    definition: tableDefinition,
  });
})();

Replace the following:

  • TABLE_NAME: The name for your table.

  • VECTOR_COLUMN_NAME: The name for your vector column.

  • TEXT_COLUMN_NAME: The name for the text column that will store the original text. Omit this column if you won’t store the original text in addition to the generated embeddings.

  • API_KEY_NAME: The name of the Voyage AI API key that you want to use. Must be the name of an existing Voyage AI API key in the Astra Portal. For more information, see Embedding provider authentication.

    Alternatively, you can omit this parameter and instead provide the authentication key in the embeddingApiKey parameter when you instantiate a Table object with the commands to create a table or get a table. The client will send the x-embedding-api-key header with the specified key to any underlying HTTP request that requires vectorize authentication. Header authentication overrides the API_KEY_NAME parameter if you set both. If you use the header instead of specifying the API_KEY_NAME parameter, you must include the header in every command that uses vectorize, including writes and vector search. You can use this authentication method only if all affected columns use the same embedding provider.

  • MODEL_NAME: The model that you want to use to generate embeddings. The available models are: voyage-2, voyage-code-2, voyage-finance-2, voyage-large-2, voyage-large-2-instruct, voyage-law-2, voyage-multilingual-2.

  • MODEL_DIMENSIONS: The number of dimensions that you want the generated vectors to have. Your chosen embedding model must support the specified number of dimensions.

    If you omit the dimension, Astra DB can use a default dimension value. However, some models don’t have default dimensions. You can use the Data API to find supported embedding providers and their configuration parameters, including dimensions ranges and default dimensions.

Create a table that uses a user-defined type (UDT)

In addition to the supported types, you can create a user-defined type to use in your table.

You can use a user-defined type as the type of a column or as the value type of a map, list, or set column. You can’t use a user-defined type as the key type of a map column or as a partitionKey or clustering key.

The following examples demonstrate how to use a user-defined type called person for the group_leader column, value type in the group_members set column, and value type in the group_roles map column.

The TypeScript client supports multiple ways to create a table. The method you choose depends on your typing preferences and whether you modified the ser/des configuration.

For more information, see Collection and table typing.

  • Automatic type inference

  • Manually typed tables

  • Untyped tables

The TypeScript client can automatically infer the TypeScript-equivalent type of the table’s schema and primary key.

To do this, first create the table definition. Then, use InferTableSchema and InferTablePrimaryKey to infer the type of the table and of the primary key. To create the table, provide the table definition and the inferred types to the createTable method.

import {
  DataAPIClient,
  InferTablePrimaryKey,
  InferTableSchema,
  Table,
} from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    id: "uuid",
    group_leader: {
      type: "userDefined",
      udtName: "person",
    },
    group_members: {
      type: "set",
      valueType: {
        type: "userDefined",
        udtName: "person",
      },
    },
    group_roles: {
      type: "map",
      keyType: "text",
      valueType: {
        type: "userDefined",
        udtName: "person",
      },
    },
  },
  // Define the primary key for the table.
  primaryKey: {
    partitionBy: ["id"],
  },
});

// Infer the TypeScript-equivalent type of the table's schema and primary key
type TableSchema = InferTableSchema<typeof tableDefinition>;
type TablePrimaryKey = InferTablePrimaryKey<typeof tableDefinition>;

(async function () {
  // Provide the types and the definition
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "example_table",
    { definition: tableDefinition },
  );
})();

You can manually define the type for your table’s schema and primary key. To create the table, provide the table definition and the types to the createTable method.

This may be necessary if you modify the table’s default ser/des configuration.

import { DataAPIClient, DataAPIDate, Table, UUID } from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    id: "uuid",
    group_leader: {
      type: "userDefined",
      udtName: "person",
    },
    group_members: {
      type: "set",
      valueType: {
        type: "userDefined",
        udtName: "person",
      },
    },
    group_roles: {
      type: "map",
      keyType: "text",
      valueType: {
        type: "userDefined",
        udtName: "person",
      },
    },
  },
  // Define the primary key for the table.
  primaryKey: {
    partitionBy: ["id"],
  },
});

// Manually define the type of the table's schema and primary key
type Person = { name: string; level: number };
type TableSchema = {
  id: UUID;
  group_leader: Person;
  group_members: Set<Person>;
  group_roles: Map<string, Person>;
};
type TablePrimaryKey = Pick<TableSchema, "id">;

(async function () {
  // Provide the types and the definition to create the table
  const table = await database.createTable<TableSchema, TablePrimaryKey>(
    "example_table",
    { definition: tableDefinition },
  );
})();

To create a table without any typing, pass SomeRow as the single generic type parameter to the createTable method. This types the table’s rows as Record<string, any>.

This is the most flexible but least type-safe option.

import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    id: "uuid",
    group_leader: {
      type: "userDefined",
      udtName: "person",
    },
    group_members: {
      type: "set",
      valueType: {
        type: "userDefined",
        udtName: "person",
      },
    },
    group_roles: {
      type: "map",
      keyType: "text",
      valueType: {
        type: "userDefined",
        udtName: "person",
      },
    },
  },
  // Define the primary key for the table.
  primaryKey: {
    partitionBy: ["id"],
  },
});

(async function () {
  // Provide the types and the definition to create the table
  const table = await database.createTable<SomeRow>("example_table", {
    definition: tableDefinition,
  });
})();

Create a table and specify the keyspace

import { DataAPIClient, SomeRow, Table } from "@datastax/astra-db-ts";

// Get an existing database
const client = new DataAPIClient();
const database = client.db("API_ENDPOINT", {
  token: "APPLICATION_TOKEN",
});

const tableDefinition = Table.schema({
  // Define all of the columns in the table
  columns: {
    title: "text",
    number_of_pages: "int",
    rating: "float",
    genres: { type: "set", valueType: "text" },
    metadata: {
      type: "map",
      keyType: "text",
      valueType: "text",
    },
    is_checked_out: "boolean",
    due_date: "date",
  },
  // Define the primary key for the table.
  // In this case, the table uses a single-column primary key.
  primaryKey: {
    partitionBy: ["title"],
  },
});

(async function () {
  // Provide the types and the definition to create the table
  const table = await database.createTable<SomeRow>("example_table", {
    definition: tableDefinition,
    keyspace: "KEYSPACE_NAME",
  });
})();

Client reference

For more information, see the client reference.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM