.Update[T](table, data)
Updates all records in a table, or a specific record, in the database.
Method Syntax
db.Update[T](table, data)
This function replaces the current document / record data with the specified data.
Arguments
| Arguments | Description |
|---|
thing required | The table name or the specific RecordId to update. |
data optional | The document / record data to update. |
Example usage
package main
import (
"fmt"
"time"
surrealdb "github.com/surrealdb/surrealdb.go"
"github.com/surrealdb/surrealdb.go/pkg/models"
)
func ExampleUpdate() {
db := newSurrealDBConnection("examples", "update", "persons")
type NestedStruct struct {
City string `json:"city"`
}
type Person struct {
ID *models.RecordID `json:"id,omitempty"`
Name string `json:"name"`
NestedMap map[string]any `json:"nested_map,omitempty"`
NestedStruct `json:"nested_struct,omitempty"`
CreatedAt models.CustomDateTime `json:"created_at,omitempty"`
UpdatedAt *models.CustomDateTime `json:"updated_at,omitempty"`
}
createdAt, err := time.Parse(time.RFC3339, "2023-10-01T12:00:00Z")
if err != nil {
panic(err)
}
recordID := models.NewRecordID("persons", "yusuke")
created, err := surrealdb.Create[Person](db, recordID, map[string]any{
"name": "Yusuke",
"nested_struct": NestedStruct{
City: "Tokyo",
},
"created_at": models.CustomDateTime{
Time: createdAt,
},
})
if err != nil {
panic(err)
}
fmt.Printf("Created persons: %+v\n", *created)
updatedAt, err := time.Parse(time.RFC3339, "2023-10-02T12:00:00Z")
if err != nil {
panic(err)
}
updated, err := surrealdb.Update[Person](db, recordID, map[string]any{
"name": "Yusuke",
"nested_map": map[string]any{
"key1": "value1",
},
"nested_struct": NestedStruct{
City: "Kagawa",
},
"updated_at": models.CustomDateTime{
Time: updatedAt,
},
})
if err != nil {
panic(err)
}
fmt.Printf("Updated persons: %+v\n", *updated)
}