Insert a row (TypeScript)
|
Tables with the Data API are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms. |
Inserts a single row into a table.
This method can insert a row in an existing CQL table, but the Data API does not support all CQL data types or modifiers. For more information, see Data type compatibility in tables (TypeScript).
For general information about working with tables and rows, see About tables with the Data API (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
Inserts the specified row and returns a promise that resolves to a TableInsertOneResult<PKey> object that includes the primary key of the inserted row.
The primary key type is inferred from the PKey of the table’s type. If it cannot be inferred from the PKey, it is instead inferred from Partial<Schema>.
If a row with the specified primary key already exists in the table, the row will be overwritten with the specified column values. Unspecified columns will remain unchanged.
If any of the inserted columns use the wrong datatype or improper encoding, then the entire insert fails.
Parameters
Use the insertOne method, which belongs to the Table class.
Method signature
async insertOne(
row: Schema,
options?: {
timeout?: number | TimeoutDescriptor,
},
): TableInsertOneResult<PKey>
| Name | Type | Summary |
|---|---|---|
|
|
An object that defines the row to insert. All primary key values are required. To reduce tombstones, you should not explicitly set a column to The table definition determines the columns in the row, the type for each column, and the primary key. To get this information, see List table metadata (TypeScript). |
|
|
Optional. A timeout to impose on the underlying API request. |
Examples
The following examples demonstrate how to insert a row into a table.
Insert a row
When you insert a row, you must specify a non-null value for each primary key column.
Non-primary key columns are optional.
To reduce tombstones, you should not explicitly set a column to null.
import {
DataAPIClient,
UsernamePasswordTokenProvider,
date,
} 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");
// Insert a row into the table
(async function () {
const result = await table.insertOne({
title: "Computed Wilderness",
author: "Ryan Eau",
number_of_pages: 432,
due_date: date("2024-12-18"),
genres: new Set(["History", "Biography"]),
});
})();
Insert a row with vector embeddings
You can only insert vector embeddings into vector columns.
To create a table with a vector column, see Create a table (TypeScript). To add a vector column to an existing table, see Alter a table (TypeScript).
All embeddings in the column should use the same provider, model, and dimensions. Mismatched embeddings can cause inaccurate vector searches.
You can use the DataAPIVector class to binary-encode your vector embeddings.
DataStax recommends that you always use a DataAPIVector object instead of a list of floats to improve performance.
import {
DataAPIClient,
UsernamePasswordTokenProvider,
DataAPIVector,
} 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");
// Insert a row into the table
(async function () {
const result = await table.insertOne({
title: "Computed Wilderness",
author: "Ryan Eau",
summary_genres_vector: new DataAPIVector([0.08, -0.62, 0.39]),
});
})();
Client reference
For more information, see the client reference.