Create a keyspace (TypeScript)
Creates a new keyspace in a database.
Keyspaces are used to store collections and tables in Hyper-Converged Database (HCD).
Result
Creates the specified keyspace.
Returns a promise that resolves once the operation completes.
Signature
Use the createKeyspace method, which belongs to the DataAPIDbAdmin class.
async createKeyspace(
keyspace: string,
options?: {
replication?: KeyspaceReplicationOptions,
updateDbKeyspace?: boolean,
timeout?: number | TimeoutDescriptor,
}
): void
Parameters
| Name | Type | Summary |
|---|---|---|
|
|
The name of the keyspace to create. If a keyspace with the name already exists, a new keyspace isn’t created, and the command succeeds. |
|
|
Optional.
The options for this operation. See Properties of |
| Name | Type | Summary |
|---|---|---|
|
|
Optional. Specifies the options for data replication across database nodes. For an example, see Create a keyspace and specify replication options. Default: |
|
|
Optional.
Whether to set the working keyspace of the |
|
|
Optional. The timeout(s) to apply to this method.
You can specify For more information about the |
Examples
The following examples demonstrate how to create a keyspace.
Create a keyspace
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get a database
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
// Get an admin object
const admin = database.admin({ environment: "hcd" });
// Create a keyspace
(async function () {
await admin.createKeyspace("KEYSPACE_NAME");
})();
Create a keyspace and set it as the working keyspace for the database
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get a database
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
// Get an admin object
const admin = database.admin({ environment: "hcd" });
// Create a keyspace
(async function () {
await admin.createKeyspace("KEYSPACE_NAME", { updateDbKeyspace: true });
})();
Create a keyspace and specify replication options
For more information about data replication, see Data replication.
Example using SimpleStrategy:
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get a database
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
// Get an admin object
const admin = database.admin({ environment: "hcd" });
// Create a keyspace
(async function () {
await admin.createKeyspace("KEYSPACE_NAME", {
replication: {
class: "SimpleStrategy",
replicationFactor: 3,
},
});
})();
Example using NetworkTopologyStrategy:
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get a database
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
// Get an admin object
const admin = database.admin({ environment: "hcd" });
// Create a keyspace
(async function () {
await admin.createKeyspace("KEYSPACE_NAME", {
replication: {
class: "NetworkTopologyStrategy",
datacenter1: 3,
datacenter2: 2,
},
});
})();
Errors
Specific error codes for this command include UNSUPPORTED_SCHEMA_NAME.
For a full list of Data API error codes and their corresponding messages, see Data API errors and error codes.
For more information about how the client handles underlying Data API errors, see TypeScript client error handling.
Client reference
For more information, see the client reference.