/dynmgrm

GORM DynamoDB Driver

Primary LanguageGoMIT LicenseMIT

dynmgrm - GORM DynamoDB Driver

logo

Go Reference GitHub go.mod Go version (subdirectory of monorepo) GitHub release (latest by date) codecov Go Report Card GitHub License

dynmgrm is the driver to issue PartiQL Statement to DynamoDB with GORM⚡

Features

Supports the following PartiQL statements

  • Select
    • With Secondary Index
    • With begins_with function
    • With contains function
    • With size function
    • With attribute_type function
    • With MISSING operator
  • Insert
  • Update
    • With SET clause
      • With list_append function
        • ListAppend()
      • With set_add function
      • With set_delete function
    • With REMOVE clause
  • Delete
  • Create Table ※ proprietary PartiQL syntax by miyamo2/godynamo
  • Create GSI ※ proprietary PartiQL syntax by miyamo2/godynamo

Supports the following GORM features

  • Query

    • Select
    • Find
    • Scan
  • Update

    • Update
    • Updates
    • Save
  • Create

    • Create
  • Delete

    • Delete
  • Condition

    • Where
    • Not
    • Or
  • Table/Model

    • Table
    • Model ※ Combination with Secondary Index are not supported.
  • Transaction ※ Supports only Insert, Update, and Delete.

    • Begin
    • Commit
    • Rollback
    • Transaction
  • Migration

    • AutoMigrate
    • CurrentDatabase
    • FullDataTypeOf
    • CreateTable
    • DropTable
    • HasTable
    • GetTables
    • HasColumn
    • ColumnTypes
    • CreateIndex
    • DropIndex
    • HasIndex

Custom Clause

  • SecondaryIndex

Custom Serializer

  • dynamo-nested

What is about GORM Serializer?

Quick Start

Installation

go get github.com/miyamo2/dynmgrm

Usage

Tip

miyamo2/dynmgrm is recommended to be used in with miyamo2/sqldav.

miyamo2/sqldav provides Defined Type of slice/map that implements sql.Scanner and driver.Valuer.

These are as the equivalent to Set, List, Map and TypedList included in miyamo2/dynmgrm before v0.9.0.

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)
}

Contributing

Feel free to open a PR or an Issue.

However, you must promise to follow our Code of Conduct.

See here for more details on contributing.

License

dynmgrm released under the MIT License

Credits

Go gopher

The Go gopher was designed by Renee French. The design is licensed under the Creative Commons 3.0 Attributions license. Read this article for more details

Special Thanks

  • btnguyen2k/godynamo

    dynmgrm connects to database/sql by miyamo2/godynamo that forked from btnguyen2k/godynamo.

  • JetBrainsMono

    JetBrainsMono is used for the caption of the dynmgrm logo.