Create a user-defined type (UDT) (Go)
Creates a user-defined type (UDT) to use in a table.
|
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 user-defined type with the specified definition.
A successful operation does not return anything.
Parameters
Use the CreateType method, which belongs to the Db type.
Method signature
func (d *Db) CreateType(
ctx context.Context,
name string,
definition table.UDTDefinition,
opts ...options.CreateTypeOption
) error
| Name | Type | Summary |
|---|---|---|
|
|
The context for the operation. |
|
|
The name of the new user-defined type. The name must be unique within the keyspace. |
|
|
The definition for the new user-defined type. The definition describes the name and data type of each field in the user-defined type. You can use any supported data type, except for maps, lists, sets, or other user-defined types. See the examples for usage. |
|
Optional. The options for this method. See Methods of the |
| Method | Summary |
|---|---|
|
Optional. Whether the command should silently succeed even if a user-defined type with the given name already exists in the keyspace and no new type was created. This option only checks type names. It does not check type definitions. Default: false |
|
Optional if you specified a working keyspace when you created the Default: The working keyspace set when you created the |
|
Optional. General API options for this operation, including the timeout. |
Examples
The following examples demonstrate how to create a user-defined type.
Create a user-defined type
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/table"
)
func main() {
ctx := context.Background()
// Get a database
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
// Create a user-defined type
definition := table.UDTDefinition{
Fields: table.Columns{
{Name: "name", Column: table.Text()},
{Name: "is_active", Column: table.Boolean()},
{Name: "date_joined", Column: table.Date()},
},
}
err := database.CreateType(ctx, "member", definition)
if err != nil {
log.Fatal(err)
}
}
Create a user-defined type only if the type does not exist
Use this option to silently do nothing if a user-defined type with the specified name already exists.
This option only checks type names. It doesn’t check the definition of any existing types.
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/table"
)
func main() {
ctx := context.Background()
// Get a database
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
// Create a user-defined type
definition := table.UDTDefinition{
Fields: table.Columns{
{Name: "name", Column: table.Text()},
{Name: "is_active", Column: table.Boolean()},
{Name: "date_joined", Column: table.Date()},
},
}
err := database.CreateType(
ctx,
"member",
definition,
options.CreateType().SetIfNotExists(true),
)
if err != nil {
log.Fatal(err)
}
}
Client reference
For more information, see the client reference.