dynamodb

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2022 License: MIT Imports: 12 Imported by: 1

README

godal/dynamodb

PkgGoDev codecov

Generic AWS DynamoDB DAO implementation.

Guideline

General

  • DAOs must implement IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt.
  • Row-mapper's ColumnsList(table string) []string must return all attribute names of specified table's primary key.

Use GenericDaoDynamodb (and godal.IGenericBo) directly

  • Define a DAO struct that implements IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt.
  • Use a row-mapper whose ColumnsList(table string) []string returns all attribute names of specified table's primary key.

Implement custom DynamoDB business DAOs and BOs

  • Define and implement the business DAO (Note: DAOs must implement IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt and its row-mapper's ColumnsList(table string) []string function must return all attribute names of specified table's primary key).
  • Define functions to transform godal.IGenericBo to business BO and vice versa.

Optionally, create a helper function to create DAO instances.

Examples: see examples and examples_sta.

This package uses github.com/aws/aws-sdk-go to interact with AWS DynamoDB service.

Documentation

Overview

Package dynamodb provides a generic AWS DynamoDB implementation of godal.IGenericDao.

General guideline:

  • DAOs must implement IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt.
  • Row-mapper's function 'ColumnsList(table string) []string' must return all attribute names of specified table's primary key.

Guideline: Use GenericDaoDynamodb (and godal.IGenericBo) directly

  • Define a DAO struct that implements IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt.

  • Use a row-mapper whose 'ColumnsList(table string) []string' returns all attribute names of specified table's primary key.

  • Optionally, create a helper function to create DAO instances.

    import ( //"github.com/aws/aws-sdk-go/aws" //"github.com/aws/aws-sdk-go/aws/credentials" //awsdynamodb "github.com/aws/aws-sdk-go/service/dynamodb" //"github.com/aws/aws-sdk-go/service/dynamodb/expression"

    "github.com/btnguyen2k/consu/reddo" "github.com/btnguyen2k/godal" godaldynamodb "github.com/btnguyen2k/godal/dynamodb" promdynamodb "github.com/btnguyen2k/prom/dynamodb" )

    type myGenericDaoDynamodb struct { *godaldynamodb.GenericDaoDynamodb }

    // GdaoCreateFilter implements godal.IGenericDao.GdaoCreateFilter. func (dao *myGenericDaoDynamodb) GdaoCreateFilter(table string, bo godal.IGenericBo) godal.FilterOpt { id := bo.GboGetAttrUnsafe(fieldId, reddo.TypeString) return &godal.FilterOptFieldOpValue{FieldName: fieldId, Operator: godal.FilterOpEqual, Value: id} }

    // newGenericDaoDynamodb is convenient method to create myGenericDaoDynamodb instances. func newGenericDaoDynamodb(adc *promdynamodb.AwsDynamodbConnect, tableName string) godal.IGenericDao { dao := &myGenericDaoDynamodb{} dao.GenericDaoDynamodb = godaldynamodb.NewGenericDaoDynamodb(adc, godal.NewAbstractGenericDao(dao)) dao.SetRowMapper(&godaldynamodb.GenericRowMapperDynamodb{ColumnsListMap: map[string][]string{tableName: {fieldId}}}) return dao }

    Since AWS DynamoDB is schema-less, GenericRowMapperDynamodb should be sufficient. However, it must be configured so that its function 'ColumnsList(table string) []string' returns all attribute names of specified table's primary key.

Guideline: Implement custom AWS DynamoDB business DAOs and BOs

  • Define and implement the business dao (Note: DAOs must implement IGenericDao.GdaoCreateFilter(string, IGenericBo) FilterOpt).

  • Optionally, create a helper function to create DAO instances.

  • Define functions to transform godal.IGenericBo to business BO and vice versa.

    import ( //"github.com/aws/aws-sdk-go/aws" //"github.com/aws/aws-sdk-go/aws/credentials" //awsdynamodb "github.com/aws/aws-sdk-go/service/dynamodb" //"github.com/aws/aws-sdk-go/service/dynamodb/expression"

    "github.com/btnguyen2k/consu/reddo" "github.com/btnguyen2k/godal" godaldynamodb "github.com/btnguyen2k/godal/dynamodb" promdynamodb "github.com/btnguyen2k/prom/dynamodb" )

    // BoApp defines business object app type BoApp struct { Id string `json:"id"` Description string `json:"desc"` Value int `json:"val"` }

    func (app *BoApp) ToGbo() godal.IGenericBo { gbo := godal.NewGenericBo()

    // method 1: populate attributes one by one gbo.GboSetAttr("id" , app.Id) gbo.GboSetAttr("desc", app.Description) gbo.GboSetAttr("val" , app.Value)

    // method 2: transfer all attributes at once if err := gbo.GboImportViaJson(app); err!=nil { panic(err) }

    return gbo }

    func NewBoAppFromGbo(gbo godal.IGenericBo) *BoApp { app := BoApp{}

    // method 1: populate attributes one by one app.Id = gbo.GboGetAttrUnsafe("id", reddo.TypeString).(string) app.Description = gbo.GboGetAttrUnsafe("desc", reddo.TypeString).(string) app.Value = int(gbo.GboGetAttrUnsafe("val", reddo.TypeInt).(int64))

    // method 2: transfer all attributes at once if err := gbo.GboTransferViaJson(&app); err!=nil { panic(err) }

    return &app }

    // DaoAppDynamodb is AWS DynamoDB-implementation of business dao type DaoAppDynamodb struct { *godaldynamodb.GenericDaoDynamodb tableName string }

    // NewDaoAppDynamodb is convenient method to create DaoAppDynamodb instances. func NewDaoAppDynamodb(adc *promdynamodb.AwsDynamodbConnect, tableName string) *NewDaoAppDynamodb { dao := &DaoAppDynamodb{tableName: tableName} dao.GenericDaoDynamodb = godaldynamodb.NewGenericDaoDynamodb(adc, godal.NewAbstractGenericDao(dao)) dao.SetRowMapper(&godaldynamodb.GenericRowMapperDynamodb{ColumnsListMap: map[string][]string{tableName: {"id"}}}) return dao }

    Since AWS DynamoDB is schema-less, GenericRowMapperDynamodb should be sufficient. However, it must be configured so that its function 'ColumnsList(table string) []string' must return all attribute names of specified table's primary key.

See more examples in 'examples' directory on project's GitHub: https://github.com/btnguyen2k/godal/tree/master/examples

To create AwsDynamodbConnect instances, see package github.com/btnguyen2k/prom/dynamodb

Index

Constants

This section is empty.

Variables

View Source
var (
	// GenericRowMapperDynamodbInstance is a pre-created instance of GenericRowMapperDynamodb that is ready to use.
	GenericRowMapperDynamodbInstance godal.IRowMapper = &GenericRowMapperDynamodb{}
)

Functions

This section is empty.

Types

type GenericDaoDynamodb

type GenericDaoDynamodb struct {
	*godal.AbstractGenericDao
	// contains filtered or unexported fields
}

GenericDaoDynamodb is AWS DynamoDB implementation of godal.IGenericDao.

Function implementations (n = No, y = Yes, i = inherited):

  • (n) GdaoCreateFilter(tableName string, bo godal.IGenericBo) godal.FilterOpt
  • (y) GdaoDelete(tableName string, bo godal.IGenericBo) (int, error)
  • (y) GdaoDeleteMany(tableName string, filter godal.FilterOpt) (int, error)
  • (y) GdaoFetchOne(tableName string, filter godal.FilterOpt) (godal.IGenericBo, error)
  • (y) GdaoFetchMany(tableName string, filter godal.FilterOpt, sorting *godal.SortingOpt, startOffset, numItems int) ([]godal.IGenericBo, error)
  • (y) GdaoCreate(tableName string, bo godal.IGenericBo) (int, error)
  • (y) GdaoUpdate(tableName string, bo godal.IGenericBo) (int, error)
  • (y) GdaoSave(tableName string, bo godal.IGenericBo) (int, error)

Available: since v0.2.0

func NewGenericDaoDynamodb

func NewGenericDaoDynamodb(dynamodbConnect *dynamodb.AwsDynamodbConnect, agdao *godal.AbstractGenericDao) *GenericDaoDynamodb

NewGenericDaoDynamodb constructs a new AWS DynamoDB implementation of 'godal.IGenericDao'.

func (*GenericDaoDynamodb) BuildConditionBuilder added in v0.5.1

func (dao *GenericDaoDynamodb) BuildConditionBuilder(tableName string, filter godal.FilterOpt) (*expression.ConditionBuilder, error)

BuildConditionBuilder transforms a godal.FilterOpt to expression.ConditionBuilder.

Available since v0.5.1

func (*GenericDaoDynamodb) GdaoCreate

func (dao *GenericDaoDynamodb) GdaoCreate(table string, bo godal.IGenericBo) (int, error)

GdaoCreate implements godal.IGenericDao.GdaoCreate.

func (*GenericDaoDynamodb) GdaoCreateWithContext

func (dao *GenericDaoDynamodb) GdaoCreateWithContext(ctx aws.Context, table string, bo godal.IGenericBo) (int, error)

GdaoCreateWithContext is is AWS DynamoDB variant of GdaoCreate.

func (*GenericDaoDynamodb) GdaoDelete

func (dao *GenericDaoDynamodb) GdaoDelete(table string, bo godal.IGenericBo) (int, error)

GdaoDelete implements godal.IGenericDao.GdaoDelete.

func (*GenericDaoDynamodb) GdaoDeleteMany

func (dao *GenericDaoDynamodb) GdaoDeleteMany(table string, filter godal.FilterOpt) (int, error)

GdaoDeleteMany implements godal.IGenericDao.GdaoDeleteMany.

  • table name format: <table_name>[:<index_name>]:
  • table_name: name of the table to delete rows from.
  • index_name: (optional) name of the table's index (local or global) to search for rows.

This function uses "scan" operation by default, which is expensive! To force "query" operation, prefix the table name with character '@'.

func (*GenericDaoDynamodb) GdaoDeleteManyWithContext

func (dao *GenericDaoDynamodb) GdaoDeleteManyWithContext(ctx aws.Context, table string, filter godal.FilterOpt) (int, error)

GdaoDeleteManyWithContext is is AWS DynamoDB variant of GdaoDeleteMany.

func (*GenericDaoDynamodb) GdaoDeleteWithContext

func (dao *GenericDaoDynamodb) GdaoDeleteWithContext(ctx aws.Context, table string, bo godal.IGenericBo) (int, error)

GdaoDeleteWithContext is AWS DynamoDB variant of GdaoDelete.

func (*GenericDaoDynamodb) GdaoFetchMany

func (dao *GenericDaoDynamodb) GdaoFetchMany(table string, filter godal.FilterOpt, sorting *godal.SortingOpt, startOffset, numItems int) ([]godal.IGenericBo, error)

GdaoFetchMany implements godal.IGenericDao.GdaoFetchMany.

  • table name format: <table_name>[:<index_name>[:<refetch-from-table:true/false>]]:
  • table_name: name of the table to fetch data from.
  • index_name: (optional) name of the table's index (local or global) to fetch data from.
  • refetch-from-table: (optional) true/false - default: false; when fetching data from index, if 'false' only projected fields are returned,. if 'true' another read is made to fetch the whole item from table (additional read capacity is consumed!)
  • sorting will not be used as DynamoDB does not currently support custom sorting of queried items.

This function uses "scan" operation by default, which is expensive! To force "query" operation, prefix the table name with character @. (Available since v0.5.2) Furthermore, prefix the table name with character ! to query "backward" instead of the default "forward" mode ("query" mode must be used).

func (*GenericDaoDynamodb) GdaoFetchManyWithContext

func (dao *GenericDaoDynamodb) GdaoFetchManyWithContext(ctx aws.Context, table string, filter godal.FilterOpt, _ *godal.SortingOpt, startOffset, numItems int) ([]godal.IGenericBo, error)

GdaoFetchManyWithContext is AWS DynamoDB variant of GdaoFetchMany.

func (*GenericDaoDynamodb) GdaoFetchOne

func (dao *GenericDaoDynamodb) GdaoFetchOne(table string, keyFilter godal.FilterOpt) (godal.IGenericBo, error)

GdaoFetchOne implements godal.IGenericDao.GdaoFetchOne.

  • keyFilter: filter that matches exactly one item by key.

func (*GenericDaoDynamodb) GdaoFetchOneWithContext

func (dao *GenericDaoDynamodb) GdaoFetchOneWithContext(ctx aws.Context, table string, keyFilter godal.FilterOpt) (godal.IGenericBo, error)

GdaoFetchOneWithContext is is AWS DynamoDB variant of GdaoFetchOne.

func (*GenericDaoDynamodb) GdaoSave

func (dao *GenericDaoDynamodb) GdaoSave(table string, bo godal.IGenericBo) (int, error)

GdaoSave implements godal.IGenericDao.GdaoSave.

Note: due to the nature of AWS DynamoDB, this function does not return godal.ErrGdaoDuplicatedEntry.

func (*GenericDaoDynamodb) GdaoSaveWithContext

func (dao *GenericDaoDynamodb) GdaoSaveWithContext(ctx aws.Context, table string, bo godal.IGenericBo) (int, error)

GdaoSaveWithContext is is AWS DynamoDB variant of GdaoSave.

func (*GenericDaoDynamodb) GdaoUpdate

func (dao *GenericDaoDynamodb) GdaoUpdate(table string, bo godal.IGenericBo) (int, error)

GdaoUpdate implements godal.IGenericDao.GdaoUpdate.

Note: due to the nature of AWS DynamoDB, this function does not return godal.ErrGdaoDuplicatedEntry.

func (*GenericDaoDynamodb) GdaoUpdateWithContext

func (dao *GenericDaoDynamodb) GdaoUpdateWithContext(ctx aws.Context, table string, bo godal.IGenericBo) (int, error)

GdaoUpdateWithContext is is AWS DynamoDB variant of GdaoUpdate.

func (*GenericDaoDynamodb) GetAwsDynamodbConnect

func (dao *GenericDaoDynamodb) GetAwsDynamodbConnect() *dynamodb.AwsDynamodbConnect

GetAwsDynamodbConnect returns the '*dynamodb.AwsDynamodbConnect' instance attached to this DAO.

func (*GenericDaoDynamodb) SetAwsDynamodbConnect

func (dao *GenericDaoDynamodb) SetAwsDynamodbConnect(adc *dynamodb.AwsDynamodbConnect) *GenericDaoDynamodb

SetAwsDynamodbConnect attaches a '*dynamodb.AwsDynamodbConnect' instance to this DAO.

type GenericRowMapperDynamodb

type GenericRowMapperDynamodb struct {
	// ColumnsListMap holds mappings of {table-name:[list of key attribute names]}
	ColumnsListMap map[string][]string
}

GenericRowMapperDynamodb is a generic implementation of godal.IRowMapper for AWS DynamoDB.

Implementation rules:

  • ToRow : transform godal.IGenericBo "as-is" to map[string]interface{}.
  • ToBo : expect input is a map[string]interface{}, or JSON data (string or array/slice of bytes), transforms input to godal.IGenericBo via JSON unmarshalling.
  • ColumnsList : look up column-list from a 'columns-list map' (AWS DynamoDB is schema-free but key-attributes are significant) and returns it.
  • ToDbColName : return the input field name "as-is".
  • ToBoFieldName: return the input column name "as-is".

Available: since v0.2.0

func (*GenericRowMapperDynamodb) ColumnsList

func (mapper *GenericRowMapperDynamodb) ColumnsList(table string) []string

ColumnsList implements godal.IRowMapper.ColumnsList.

This function looks up column-list from a 'columns-list map' (AWS DynamoDB is schema-free but key-attributes are significant) and returns it.

func (*GenericRowMapperDynamodb) ToBo

func (mapper *GenericRowMapperDynamodb) ToBo(table string, row interface{}) (godal.IGenericBo, error)

ToBo implements godal.IRowMapper.ToBo.

This function expects input to be a map[string]interface{}, or JSON data (string or array/slice of bytes), transforms it to godal.IGenericBo via JSON unmarshalling. Field names are kept intact.

func (*GenericRowMapperDynamodb) ToBoFieldName added in v0.5.0

func (mapper *GenericRowMapperDynamodb) ToBoFieldName(_, colName string) string

ToBoFieldName implements godal.IRowMapper.ToBoFieldName.

This function returns the input column name "as-is".

func (*GenericRowMapperDynamodb) ToDbColName added in v0.5.0

func (mapper *GenericRowMapperDynamodb) ToDbColName(_, fieldName string) string

ToDbColName implements godal.IRowMapper.ToDbColName.

This function returns the input field name "as-is".

func (*GenericRowMapperDynamodb) ToRow

func (mapper *GenericRowMapperDynamodb) ToRow(_ string, bo godal.IGenericBo) (interface{}, error)

ToRow implements godal.IRowMapper.ToRow.

This function transforms godal.IGenericBo to map[string]interface{}. Field names are kept intact.

Jump to

Keyboard shortcuts

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