Alter a table (TypeScript)
Alters a table by doing one of the following:
-
Adding one or more columns to a table
-
Dropping one or more columns from a table
You cannot change a column’s type. Instead, you must drop the column and add a new column.
After you add a column, you should index the column if you want to filter or sort on the column. For more information, see Create an index (TypeScript) and Create a vector index (TypeScript).
|
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
Adds or drops columns.
Returns a promise that resolves to a Table instance that represents the table after the modification.
Although the Table instance that you used to perform the alteration will still work, it will not reflect the updated typing if you added or dropped columns.
To reflect the new typing, provide the new type of the table to the alter method.
For example:
const newTable = await table.alter<NewSchema>({
operation: {
add: {
columns: {
venue: 'text',
},
},
},
});
Parameters
Use the alter method, which belongs to the Table class.
Method signature
async alter(
options: AlterTableOptions<Schema>
): Table<Schema, PKey>
| Name | Type | Summary |
|---|---|---|
|
|
The alter operation to perform. Can be one of the following:
|
|
|
The timeout(s) to apply to HTTP request(s) originating from this method. |
Examples
The following examples demonstrate how to alter a table.
Add columns to a table
When you add columns, the columns are defined in the same way as they are when you create a table.
After you add a column, you should index the column if you want to filter or sort on the column. For more information, see Create an index (TypeScript) and Create a vector index (TypeScript).
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
keyspace: "KEYSPACE_NAME",
});
const table = database.table("TABLE_NAME");
// Add columns
(async function () {
await table.alter({
operation: {
add: {
columns: {
is_summer_reading: "boolean",
library_branch: "text",
},
},
},
});
})();
Add vector columns to a table
After you add a vector column, you should index the column if you want run vector searches on the column. For more information, Create a vector index (TypeScript).
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
keyspace: "KEYSPACE_NAME",
});
const table = database.table("TABLE_NAME");
// Add a vector column
(async function () {
await table.alter({
operation: {
add: {
columns: {
example_vector: { type: "vector", dimension: 1024 },
},
},
},
});
})();
Drop columns from a table
Dropping columns produces tombstones. Excessive tombstones can impact query performance.
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
keyspace: "KEYSPACE_NAME",
});
const table = database.table("TABLE_NAME");
// Drop columns
(async function () {
await table.alter({
operation: {
drop: {
columns: ["is_summer_reading", "library_branch"],
},
},
});
})();
Client reference
For more information, see the client reference.