Documentation ¶
Overview ¶
Example ¶
package main import ( "github.com/miyamo2/dynmgrm" "github.com/miyamo2/sqldav" "gorm.io/gorm" ) type Event struct { Name string `gorm:"primaryKey"` Date string `gorm:"primaryKey"` Host string Guest sqldav.Set[string] } func main() { db, err := gorm.Open(dynmgrm.New()) if err != nil { panic(err) } var dynamoDBWorkshop Event db.Table("events"). Where(`name=?`, "DynamoDB Workshop"). Where(`date=?`, "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=?`, "2024/4/1"). Where(`( ? )`, db.Where(`host=?`, "Dave").Or(`CONTAINS("guest", ?)`, "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, ?)", sqldav.Set[string]{"Dave"})) } } tx.Model(&carolBirthday).Update("guest", gorm.Expr("set_add(guest, ?)", sqldav.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 BaseMigrator
- type CallbacksRegisterer
- type CapacityUnitsSpecifier
- type DBForMigrator
- 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.Migrator
- 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 Migrator
- func (m Migrator) AddColumn(dst interface{}, field string) errordeprecated
- func (m Migrator) AlterColumn(dst interface{}, field string) errordeprecated
- func (m Migrator) AutoMigrate(dst ...interface{}) error
- func (m Migrator) ColumnTypes(dst interface{}) ([]gorm.ColumnType, error)
- func (m Migrator) CreateConstraint(dst interface{}, name string) errordeprecated
- func (m Migrator) CreateIndex(dst interface{}, name string) error
- func (m Migrator) CreateTable(models ...interface{}) error
- func (m Migrator) CreateView(name string, option gorm.ViewOption) errordeprecated
- func (m Migrator) CurrentDatabase() string
- func (m Migrator) DropColumn(dst interface{}, field string) errordeprecated
- func (m Migrator) DropConstraint(dst interface{}, name string) errordeprecated
- func (m Migrator) DropIndex(dst interface{}, name string) error
- func (m Migrator) DropTable(dst ...interface{}) error
- func (m Migrator) DropView(name string) errordeprecated
- 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) booldeprecated
- 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) errordeprecated
- func (m Migrator) MigrateColumnUnique(dst interface{}, field *schema.Field, columnType gorm.ColumnType) errordeprecated
- func (m Migrator) RenameColumn(dst interface{}, oldName, field string) errordeprecated
- func (m Migrator) RenameIndex(dst interface{}, oldName, newName string) errordeprecated
- func (m Migrator) RenameTable(oldName, newName interface{}) errordeprecated
- func (m Migrator) TableType(dst interface{}) (gorm.TableType, error)
- type SecondaryIndexOption
- type TableClass
- type TableClassSpecifier
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 ErrDynmgrmAreNotSupported = errors.New("dynmgrm are not supported this operation")
var ErrIncompatibleNestedStruct = errors.New("incompatible nested struct")
ErrIncompatibleNestedStruct occurs when an incompatible with nested-struct.
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(sqldav.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.
Default: https://github.com/miyamo2/godynamo?tab=readme-ov-file#data-source-name-dsn-format-for-aws-dynamodb
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.
Default: https://github.com/miyamo2/godynamo?tab=readme-ov-file#data-source-name-dsn-format-for-aws-dynamodb
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.
Default: https://github.com/miyamo2/godynamo?tab=readme-ov-file#data-source-name-dsn-format-for-aws-dynamodb
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.
Default: https://github.com/miyamo2/godynamo?tab=readme-ov-file#data-source-name-dsn-format-for-aws-dynamodb
Example ¶
package main import ( "github.com/miyamo2/dynmgrm" ) func main() { dynmgrm.WithTimeout(30000) }
Output:
Types ¶
type BaseMigrator ¶ added in v0.6.0
type CallbacksRegisterer ¶
type CapacityUnitsSpecifier ¶ added in v0.6.0
CapacityUnitsSpecifier could specify WCUs and RCU
type DBForMigrator ¶ added in v0.6.0
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 Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator is gorm.Migrator implementation for dynamodb
func (Migrator) AlterColumn
deprecated
AlterColumn See: gorm.migrator.Migrator.AlterColumn
Deprecated: DynamoDB are schema less, so this operation is not supported.
func (Migrator) AutoMigrate ¶
func (Migrator) ColumnTypes ¶
func (m Migrator) ColumnTypes(dst interface{}) ([]gorm.ColumnType, error)
func (Migrator) CreateConstraint
deprecated
CreateConstraint See: gorm.migrator.Migrator.CreateConstraint
Deprecated: DynamoDB does not provide constraint feature, so this operation is not supported.
func (Migrator) CreateIndex ¶
func (Migrator) CreateTable ¶
func (Migrator) CreateView
deprecated
func (m Migrator) CreateView(name string, option gorm.ViewOption) error
CreateView See: gorm.migrator.Migrator.CreateView
Deprecated: DynamoDB does not provide View feature, so this operation is not supported.
func (Migrator) CurrentDatabase ¶
func (Migrator) DropColumn
deprecated
DropColumn See: gorm.migrator.Migrator.DropColumn
Deprecated: DynamoDB are schema less, so this operation is not supported.
func (Migrator) DropConstraint
deprecated
DropConstraint See: gorm.migrator.Migrator.DropConstraint
Deprecated: DynamoDB does not provide constraint feature, so this operation is not supported.
func (Migrator) DropView
deprecated
DropView See: gorm.migrator.Migrator.DropView
Deprecated: DynamoDB does not provide View feature, so this operation is not supported.
func (Migrator) GetTypeAliases ¶
func (Migrator) HasConstraint
deprecated
HasConstraint See: gorm.migrator.Migrator.HasConstraint
Deprecated: DynamoDB does not provide constraint feature, so this operation is not supported.
func (Migrator) MigrateColumn
deprecated
func (m Migrator) MigrateColumn(dst interface{}, field *schema.Field, columnType gorm.ColumnType) error
MigrateColumn See: gorm.migrator.Migrator.MigrateColumn
Deprecated: DynamoDB are schema less, so this operation is not supported.
func (Migrator) MigrateColumnUnique
deprecated
func (m Migrator) MigrateColumnUnique(dst interface{}, field *schema.Field, columnType gorm.ColumnType) error
MigrateColumnUnique See: gorm.migrator.Migrator.MigrateColumnUnique
Deprecated: DynamoDB are schema less, so this operation is not supported.
func (Migrator) RenameColumn
deprecated
RenameColumn See: gorm.migrator.Migrator.RenameColumn
Deprecated: DynamoDB are schema less, so this operation is not supported.
func (Migrator) RenameIndex
deprecated
RenameIndex See: gorm.migrator.Migrator.RenameIndex
Deprecated: DynamoDB does not possibly rename an existing index, so this operation is not supported.
func (Migrator) RenameTable
deprecated
RenameTable See: gorm.migrator.Migrator.RenameTable
Deprecated: DynamoDB does not possibly rename an existing table, so this operation is not supported.
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 TableClass ¶ added in v0.6.0
type TableClass int
TableClass is the type of table class
const ( TableClassStandard TableClass = iota TableClassStandardIA )
TableClassStandard and TableClassStandardIA are the supported table classes
func (TableClass) String ¶ added in v0.6.0
func (t TableClass) String() string
type TableClassSpecifier ¶ added in v0.6.0
type TableClassSpecifier interface {
TableClass() TableClass
}
TableClassSpecifier could specify table class.