Documentation ¶
Overview ¶
Example ¶
package main import ( "github.com/miyamo2/dynmgrm" "gorm.io/gorm" ) type Event struct { Name string `gorm:"primaryKey"` Date string `gorm:"primaryKey"` Host string Guest dynmgrm.Set[string] } func main() { db, err := gorm.Open(dynmgrm.New()) if err != nil { panic(err) } var dynamoDBWorkshop Event db.Table("events").Where(`name=? AND date=?`, "DynamoDB Workshop", "2024/3/25").Scan(&dynamoDBWorkshop) dynamoDBWorkshop.Guest = append(dynamoDBWorkshop.Guest, "Alice") db.Save(&dynamoDBWorkshop) carolBirthday := Event{ Name: "Carol's Birthday", Date: "2024/4/1", Host: "Charlie", Guest: []string{"Alice", "Bob"}, } db.Create(carolBirthday) var daveSchedule []Event db.Table("events"). Where(`date=? AND ( host=? OR CONTAINS("guest", ?) )`, "2024/4/1", "Dave", "Dave"). Scan(&daveSchedule) tx := db.Begin() for _, event := range daveSchedule { if event.Host == "Dave" { tx.Delete(&event) } else { tx.Model(&event).Update("guest", gorm.Expr("set_delete(guest, ?)", dynmgrm.Set[string]{"Dave"})) } } tx.Model(&carolBirthday).Update("guest", gorm.Expr("set_add(guest, ?)", dynmgrm.Set[string]{"Dave"})) tx.Commit() var hostDateIndex []Event db.Table("events").Clauses( dynmgrm.SecondaryIndex("host-date-index"), ).Where(`host=?`, "Bob").Scan(&hostDateIndex) }
Output:
Index ¶
- Constants
- Variables
- func ListAppend(item ...interface{}) *listAppend
- func New(option ...DialectorOption) gorm.Dialector
- func Open(dsn string) gorm.Dialector
- func SecondaryIndex(indexName string, options ...SecondaryIndexOption) secondaryIndexExpression
- func WithAccessKeyID(accessKeyId string) func(*config)
- func WithConnection(conn gorm.ConnPool) func(*config)
- func WithEndpoint(endpoint string) func(*config)
- func WithRegion(region string) func(*config)
- func WithSecretKey(secretKey string) func(*config)
- func WithTimeout(timeout int) func(*config)
- type CallbacksRegisterer
- type DBOpener
- type Dialector
- func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{})
- func (dialector Dialector) DataTypeOf(field *schema.Field) string
- func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression
- func (dialector Dialector) Explain(sql string, vars ...interface{}) string
- func (dialector Dialector) Initialize(db *gorm.DB) (err error)
- func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migratordeprecated
- func (dialector Dialector) Name() string
- func (dialector Dialector) QuoteTo(writer clause.Writer, str string)
- func (dialector Dialector) Translate(err error) error
- type DialectorOption
- type KeySchemaDataType
- type List
- type Map
- type Migratordeprecated
- func (m Migrator) AddColumn(dst interface{}, field string) error
- func (m Migrator) AlterColumn(dst interface{}, field string) error
- func (m Migrator) AutoMigrate(dst ...interface{}) error
- func (m Migrator) ColumnTypes(dst interface{}) ([]gorm.ColumnType, error)
- func (m Migrator) CreateConstraint(dst interface{}, name string) error
- func (m Migrator) CreateIndex(dst interface{}, name string) error
- func (m Migrator) CreateTable(dst ...interface{}) error
- func (m Migrator) CreateView(name string, option gorm.ViewOption) error
- func (m Migrator) CurrentDatabase() string
- func (m Migrator) DropColumn(dst interface{}, field string) error
- func (m Migrator) DropConstraint(dst interface{}, name string) error
- func (m Migrator) DropIndex(dst interface{}, name string) error
- func (m Migrator) DropTable(dst ...interface{}) error
- func (m Migrator) DropView(name string) error
- func (m Migrator) FullDataTypeOf(field *schema.Field) clause.Expr
- func (m Migrator) GetIndexes(dst interface{}) ([]gorm.Index, error)
- func (m Migrator) GetTables() (tableList []string, err error)
- func (m Migrator) GetTypeAliases(databaseTypeName string) []string
- func (m Migrator) HasColumn(dst interface{}, field string) bool
- func (m Migrator) HasConstraint(dst interface{}, name string) bool
- func (m Migrator) HasIndex(dst interface{}, name string) bool
- func (m Migrator) HasTable(dst interface{}) bool
- func (m Migrator) MigrateColumn(dst interface{}, field *schema.Field, columnType gorm.ColumnType) error
- func (m Migrator) MigrateColumnUnique(dst interface{}, field *schema.Field, columnType gorm.ColumnType) error
- func (m Migrator) RenameColumn(dst interface{}, oldName, field string) error
- func (m Migrator) RenameIndex(dst interface{}, oldName, newName string) error
- func (m Migrator) RenameTable(oldName, newName interface{}) error
- func (m Migrator) TableType(dst interface{}) (gorm.TableType, error)
- type SecondaryIndexOption
- type Set
- type SetSupportable
- type TypedList
Examples ¶
- Package
- ListAppend
- New
- New (WithAccessKeyID)
- New (WithEndpoint)
- New (WithRegion)
- New (WithSecretKey)
- New (WithTimeout)
- Open
- SecondaryIndex
- SecondaryIndex (WithSecondaryIndexOf_withString)
- SecondaryIndex (WithSecondaryIndexOf_withTableClause)
- WithAccessKeyID
- WithEndpoint
- WithRegion
- WithSecretKey
- WithTimeout
Constants ¶
const ( // DriverName is the driver name for DynamoDB. DriverName = "godynamo" DBName = "dynamodb" )
Variables ¶
var ( ErrCollectionAlreadyContainsItem = errors.New("collection already contains item") ErrFailedToCast = errors.New("failed to cast") ErrDynmgrmAreNotSupported = errors.New("dynmgrm are not supported this operation") )
var ( ErrValueIsIncompatibleOfStringSlice = errors.New("value is incompatible of string slice") ErrValueIsIncompatibleOfIntSlice = errors.New("value is incompatible of int slice") ErrValueIsIncompatibleOfFloat64Slice = errors.New("value is incompatible of float64 slice") ErrValueIsIncompatibleOfBinarySlice = errors.New("value is incompatible of []byte slice") )
var (
ErrInvalidColumnName = errors.New("column name contains invalid characters")
)
Functions ¶
func ListAppend ¶ added in v0.5.0
func ListAppend(item ...interface{}) *listAppend
ListAppend returns a functionForPartiQLUpdates implementation for `list_append` function.
Example ¶
db, err := gorm.Open( dynmgrm.New(), &gorm.Config{ SkipDefaultTransaction: true, }) if err != nil { log.Fatalf("failed to open database, got error %v", err) } db.Model(&TestTable{PK: "Partition1", SK: 1}). Update("list_type_attr", dynmgrm.ListAppend(dynmgrm.Map{"Foo": "Bar"}))
Output:
func New ¶
func New(option ...DialectorOption) gorm.Dialector
New returns a new DynamoDB dialector with options.
Example ¶
package main import ( "github.com/miyamo2/dynmgrm" "gorm.io/gorm" ) func main() { gorm.Open(dynmgrm.New()) }
Output:
Example (WithAccessKeyID) ¶
package main import ( "github.com/miyamo2/dynmgrm" "gorm.io/gorm" ) func main() { gorm.Open(dynmgrm.New(dynmgrm.WithAccessKeyID("YourAccess"))) }
Output:
Example (WithEndpoint) ¶
package main import ( "github.com/miyamo2/dynmgrm" "gorm.io/gorm" ) func main() { gorm.Open(dynmgrm.New(dynmgrm.WithEndpoint("http://localhost:8000"))) }
Output:
Example (WithRegion) ¶
package main import ( "github.com/miyamo2/dynmgrm" "gorm.io/gorm" ) func main() { gorm.Open(dynmgrm.New(dynmgrm.WithRegion("ap-northeast-1"))) }
Output:
Example (WithSecretKey) ¶
package main import ( "github.com/miyamo2/dynmgrm" "gorm.io/gorm" ) func main() { gorm.Open(dynmgrm.New(dynmgrm.WithSecretKey("YourSecretKey"))) }
Output:
Example (WithTimeout) ¶
package main import ( "github.com/miyamo2/dynmgrm" "gorm.io/gorm" ) func main() { gorm.Open(dynmgrm.New(dynmgrm.WithTimeout(30000))) }
Output:
func Open ¶
Open returns a new DynamoDB dialector based on the DSN.
e.g. "region=ap-northeast-1;AkId=<YOUR_ACCESS_KEY_ID>;SecretKey=<YOUR_SECRET_KEY>"
Example ¶
package main import ( "github.com/miyamo2/dynmgrm" "gorm.io/gorm" ) func main() { gorm.Open(dynmgrm.Open("region=ap-northeast-1;AkId=YourAccessKeyID;SecretKey=YourSecretKey")) }
Output:
func SecondaryIndex ¶
func SecondaryIndex(indexName string, options ...SecondaryIndexOption) secondaryIndexExpression
SecondaryIndex enables queries using a secondary index
Example ¶
package main import ( "github.com/miyamo2/dynmgrm" "gorm.io/gorm" ) type TestTable struct { PK string `gorm:"primaryKey"` SK int `gorm:"primaryKey"` GSIKey string } func main() { db, err := gorm.Open(dynmgrm.New(), &gorm.Config{}) if err != nil { panic("failed to connect database") } result := TestTable{} db.Table("something").Clauses( dynmgrm.SecondaryIndex("gsi_key-sk-index")). Where(`gsi_key = ?`, "1"). Scan(&result) }
Output:
Example (WithSecondaryIndexOf_withString) ¶
package main import ( "github.com/miyamo2/dynmgrm" "gorm.io/gorm" ) type TestTable struct { PK string `gorm:"primaryKey"` SK int `gorm:"primaryKey"` GSIKey string } func main() { db, err := gorm.Open(dynmgrm.New(), &gorm.Config{}) if err != nil { panic("failed to connect database") } result := TestTable{} db.Clauses( dynmgrm.SecondaryIndex("gsi_key-sk-index", dynmgrm.SecondaryIndexOf("something"))). Where(`gsi_key = ?`, "1"). Scan(&result) }
Output:
Example (WithSecondaryIndexOf_withTableClause) ¶
package main import ( "github.com/miyamo2/dynmgrm" "gorm.io/gorm" "gorm.io/gorm/clause" ) type TestTable struct { PK string `gorm:"primaryKey"` SK int `gorm:"primaryKey"` GSIKey string } func main() { db, err := gorm.Open(dynmgrm.New(), &gorm.Config{}) if err != nil { panic("failed to connect database") } result := TestTable{} db.Clauses( dynmgrm.SecondaryIndex("gsi_key-sk-index", dynmgrm.SecondaryIndexOf( clause.Table{ Name: "something", }))). Where(`gsi_key = ?`, "1"). Scan(&result) }
Output:
func WithAccessKeyID ¶
func WithAccessKeyID(accessKeyId string) func(*config)
WithAccessKeyID sets the access key ID for the DynamoDB connection.
Example ¶
package main import ( "github.com/miyamo2/dynmgrm" ) func main() { dynmgrm.WithAccessKeyID("YourAccess") }
Output:
func WithConnection ¶
WithConnection sets the exist connection for the DynamoDB.
func WithEndpoint ¶
func WithEndpoint(endpoint string) func(*config)
WithEndpoint sets the endpoint for the DynamoDB connection.
Example ¶
package main import ( "github.com/miyamo2/dynmgrm" ) func main() { dynmgrm.WithEndpoint("http://localhost:8000") }
Output:
func WithRegion ¶
func WithRegion(region string) func(*config)
WithRegion sets the region for the DynamoDB connection.
Example ¶
package main import ( "github.com/miyamo2/dynmgrm" ) func main() { dynmgrm.WithRegion("ap-northeast-1") }
Output:
func WithSecretKey ¶
func WithSecretKey(secretKey string) func(*config)
WithSecretKey sets the secret key for the DynamoDB connection.
Example ¶
package main import ( "github.com/miyamo2/dynmgrm" ) func main() { dynmgrm.WithSecretKey("YourSecretKey") }
Output:
func WithTimeout ¶
func WithTimeout(timeout int) func(*config)
WithTimeout sets the timeout milliseconds for the DynamoDB connection.
Example ¶
package main import ( "github.com/miyamo2/dynmgrm" ) func main() { dynmgrm.WithTimeout(30000) }
Output:
Types ¶
type CallbacksRegisterer ¶
type Dialector ¶
type Dialector struct {
// contains filtered or unexported fields
}
Dialector gorm dialector for DynamoDB
func (Dialector) BindVarTo ¶
BindVarTo writes the bind variable of goodynamo to clauses.Writer.
func (Dialector) DataTypeOf ¶
DataTypeOf maps GORM's data types to DynamoDB's data types. DataTypeOf works only with migration, so it will not return data types that are not allowed in PK, SK.
func (Dialector) DefaultValueOf ¶
func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression
DefaultValueOf returns the default value of the field.
func (Dialector) Explain ¶
Explain returns the SQL string with the variables replaced. Explain is typically used only for logging, dry runs, and migration.
func (Dialector) Initialize ¶
Initialize initializes the DynamoDB connection.
type DialectorOption ¶
type DialectorOption func(*config)
DialectorOption is the option for the DynamoDB dialector.
type KeySchemaDataType ¶ added in v0.3.0
type KeySchemaDataType string
KeySchemaDataType is the data type for the DynamoDB key schema.
const ( // KeySchemaDataTypeString is the data type for string. KeySchemaDataTypeString KeySchemaDataType = "string" // KeySchemaDataTypeNumber is the data type for number. KeySchemaDataTypeNumber KeySchemaDataType = "number" // KeySchemaDataTypeBinary is the data type for binary. KeySchemaDataTypeBinary KeySchemaDataType = "binary" )
Define KeySchemaDataType
func (KeySchemaDataType) String ¶ added in v0.3.0
func (d KeySchemaDataType) String() string
String returns the string representation of the KeySchemaDataType.
type List ¶
type List []interface{}
List is a DynamoDB list type.
func (*List) GormDataType ¶
GormDataType returns the data type for Gorm.
func (List) GormValue ¶
GormValue implements the gorm.Valuer interface.
func (*List) Scan ¶
Scan implements the sql.Scanner#Scan
type Map ¶
type Map map[string]interface{}
Map is a DynamoDB map type.
func (Map) GormDataType ¶
GormDataType returns the data type for Gorm.
func (Map) GormValue ¶
GormValue implements the gorm.Valuer interface.
type Migrator
deprecated
type Migrator struct{}
Migrator is gorm.Migrator implementation for dynamodb
Deprecated: Migrator is not implemented.
func (Migrator) AlterColumn ¶
func (Migrator) AutoMigrate ¶
func (Migrator) ColumnTypes ¶
func (m Migrator) ColumnTypes(dst interface{}) ([]gorm.ColumnType, error)
func (Migrator) CreateConstraint ¶
func (Migrator) CreateIndex ¶
func (Migrator) CreateTable ¶
func (Migrator) CreateView ¶
func (m Migrator) CreateView(name string, option gorm.ViewOption) error
func (Migrator) CurrentDatabase ¶
func (Migrator) DropColumn ¶
func (Migrator) DropConstraint ¶
func (Migrator) GetTypeAliases ¶
func (Migrator) HasConstraint ¶
func (Migrator) MigrateColumn ¶
func (Migrator) MigrateColumnUnique ¶
func (Migrator) RenameColumn ¶
func (Migrator) RenameIndex ¶
func (Migrator) RenameTable ¶
type SecondaryIndexOption ¶
type SecondaryIndexOption func(*secondaryIndexExpression)
SecondaryIndexOption is a functional option for secondaryIndexExpression
func SecondaryIndexOf ¶
func SecondaryIndexOf[T string | clause.Table](table T) SecondaryIndexOption
SecondaryIndexOf is the table with the index to be used.
type Set ¶
type Set[T SetSupportable] []T
Set is a DynamoDB set type.
func (*Set[T]) GormDataType ¶
GormDataType returns the data type for Gorm.
func (Set[T]) GormValue ¶
GormValue implements the gorm.Valuer interface.
func (*Set[T]) Scan ¶
Scan implements the sql.Scanner#Scan
type SetSupportable ¶
SetSupportable are the types that support the Set
type TypedList ¶ added in v0.4.0
type TypedList[T any] []T
TypedList is a DynamoDB list type with type specification.
func (*TypedList[T]) GormDataType ¶ added in v0.4.0
GormDataType returns the data type for Gorm.
func (TypedList[T]) GormValue ¶ added in v0.4.0
GormValue implements the gorm.Valuer interface.
func (*TypedList[T]) Scan ¶ added in v0.4.0
Scan implements the sql.Scanner#Scan