update

package
v0.0.0-...-eac1318 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(name string, value interface{}) func(*Opts)

Add is the global variant of Opts.Add.

This is useful if you only have one or two actions in the update expression and don't need chaining.

func Delete

func Delete(name string, value interface{}) func(*Opts)

Delete is the global variant of Opts.Delete.

This is useful if you only have one or two actions in the update expression and don't need chaining.

func DisableAutoGenerateTimestamps

func DisableAutoGenerateTimestamps(flag timestamp.AutoGenerateFlag) func(*Opts)

DisableAutoGenerateTimestamps disables logic around model.HasCreatedTimestamp and model.HasModifiedTimestamp.

func DisableOptimisticLocking

func DisableOptimisticLocking() func(*Opts)

DisableOptimisticLocking disables logic around model.Versioned.

func Remove

func Remove(name string) func(*Opts)

Remove is the global variant of Opts.Remove.

This is useful if you only have one or two actions in the update expression and don't need chaining.

func ReturnAllNewValues

func ReturnAllNewValues() func(*Opts)

ReturnAllNewValues sets the dynamodb.UpdateItemInput's ReturnValues to ALL_NEW.

func ReturnAllOldValues

func ReturnAllOldValues() func(*Opts)

ReturnAllOldValues sets the dynamodb.UpdateItemInput's ReturnValues to ALL_OLD.

func ReturnUpdatedNewValues

func ReturnUpdatedNewValues() func(*Opts)

ReturnUpdatedNewValues sets the dynamodb.UpdateItemInput's ReturnValues to UPDATED_NEW.

func ReturnUpdatedOldValues

func ReturnUpdatedOldValues() func(*Opts)

ReturnUpdatedOldValues sets the dynamodb.UpdateItemInput's ReturnValues to UPDATED_OLD.

func Set

func Set(name string, value interface{}) func(*Opts)

Set is the global variant of Opts.Set.

This is useful if you only have one or two actions in the update expression and don't need chaining.

func SetOrRemove

func SetOrRemove(set, remove bool, name string, value interface{}) func(*Opts)

SetOrRemove is the global variant of Opts.SetOrRemove.

This is useful if you only have one or two actions in the update expression and don't need chaining.

func SetOrRemoveStringPointer

func SetOrRemoveStringPointer(name string, ptr *string) func(*Opts)

SetOrRemoveStringPointer is the global variant of Opts.SetOrRemoveStringPointer.

This is useful if you only have one or two actions in the update expression and don't need chaining.

func WithTableName

func WithTableName(tableName string) func(*Opts)

WithTableName changes the table name in Opts.Input.

Types

type Opts

type Opts struct {
	Item                          model.Item
	Input                         *dynamodb.UpdateItemInput
	Condition                     *expression.ConditionBuilder
	Update                        *expression.UpdateBuilder
	DisableOptimisticLocking      bool
	DisableAutoGenerateTimestamps timestamp.AutoGenerateFlag
}

Opts provides customisation to the dynamodb.UpdateItemInput made with github.com/nguyengg/golambda/ddb.Wrapper.Update.

Opts.Input is guaranteed to exist when passed into the first modifier. Opts.Item is the original reference item. Changes to Opts.Item don't automatically update Opts.Input. Changes to Opts.Condition and Opts.Update will affect the final Opts.Input.

func (*Opts) Add

func (opts *Opts) Add(name string, value interface{}) *Opts

Add is a helper method to expr.Add to add an ADD action.

func (*Opts) Delete

func (opts *Opts) Delete(name string, value interface{}) *Opts

Delete is a helper method to expr.Delete to add a DELETE action.

func (*Opts) Remove

func (opts *Opts) Remove(name string) *Opts

Remove is a helper method to expr.Remove to add a REMOVE action.

func (*Opts) Set

func (opts *Opts) Set(name string, value interface{}) *Opts

Set is a helper method to expr.Set to add a SET action.

func (*Opts) SetOrRemove

func (opts *Opts) SetOrRemove(set, remove bool, name string, value interface{}) *Opts

SetOrRemove adds either Set or Remove action to the update expression.

If set is true, a SET action will be added. If set is false, only when remove is true then a REMOVE action will be added.

| set | remove | action | true | * | SET | false | true | REMOVE | false | false | no-op

This is useful for distinguishing between PUT/POST (remove=true) that replaces attributes with clobbering behaviour vs. PATCH (remove=false) that will only update attributes that are non-nil. An example is given:

func PUT(body Struct) {
	// because it's a PUT request, if notes is empty, instead of writing empty string to database, we'll remove it.
	wrapper.Update(..., SetOrRemove(true, true, "notes", body.Notes))
}

func PATCH(body Struct) {
	// only when notes is non-empty that we'll update it. an empty notes just means caller didn't try to update it.
	wrapper.Update(..., opts.SetOrRemove(body.Notes != "", false, "notes", body.Notes))
}

func Update(method string, body Struct) {
	// an attempt to unify the methods may look like this.
	wrapper.Update(..., opts.SetOrRemove(body.Notes != "", method != "PATCH", "notes", body.Notes))
}

If you have multiple actions to be added to the update expression, use this function. If you only have one or two actions, you can use the global SetOrRemove as well.

func (*Opts) SetOrRemoveStringPointer

func (opts *Opts) SetOrRemoveStringPointer(name string, ptr *string) *Opts

SetOrRemoveStringPointer is a specialization of SetOrRemove for string pointer value.

If ptr is a nil pointer, no action is taken. If ptr dereferences to an empty string, a REMOVE action is used. A non-empty string otherwise will result in a SET action.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL