.Patch[T](what, patches)
Applies JSON Patch changes to all records, or a specific record, in the database.
Method Syntax
db.Patch[T](what, patches)
This function patches document / record data with the specified JSON Patch data.
Arguments
| Arguments | Description |
|---|
thing required | The table name or the specific RecordId to patch. |
data optional | The JSON Patch data with which to patch the records. |
Example usage
type PatchData struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value"`
}
patches := []PatchData{
{Op: "replace", Path: "/name", Value: "John Smith"},
{Op: "add", Path: "/tags", Value: []string{"developer", "engineer"}},
{Op: "remove", Path: "/oldField"},
}
recordID := models.NewRecordID("person", "tobie")
updatedPatches, err := surrealdb.Patch(db, recordID, patches)
if err != nil {
panic(err)
}
fmt.Printf("Patched person record with patches: %+v\n", updatedPatches)