Skip to content

Commit

Permalink
BeforeInsert and BeforeUpdate hooks replaced with BeforeAppendModel hook
Browse files Browse the repository at this point in the history
  • Loading branch information
k-karuna committed Sep 9, 2024
1 parent 2fbfcdc commit be6377a
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions internal/storage/token_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package storage

import (
"context"
"time"

"github.com/dipdup-io/starknet-metadata/internal/types"
"github.com/dipdup-net/indexer-sdk/pkg/storage"
"github.com/shopspring/decimal"
"github.com/uptrace/bun"
"time"
)

// TokenUpdateID - incremental counter
Expand Down Expand Up @@ -45,17 +44,16 @@ func (TokenMetadata) TableName() string {
return "token_metadata"
}

// BeforeInsert -
func (tm *TokenMetadata) BeforeInsert(ctx context.Context) (context.Context, error) {
tm.UpdatedAt = time.Now().Unix()
tm.CreatedAt = tm.UpdatedAt
tm.UpdateID = TokenUpdateID.Increment()
return ctx, nil
}

// BeforeUpdate -
func (tm *TokenMetadata) BeforeUpdate(ctx context.Context) (context.Context, error) {
tm.UpdatedAt = time.Now().Unix()
tm.UpdateID = TokenUpdateID.Increment()
return ctx, nil
// BeforeAppendModel -
func (tm *TokenMetadata) BeforeAppendModel(ctx context.Context, query bun.Query) error {
switch query.(type) {
case *bun.InsertQuery:
tm.UpdatedAt = time.Now().Unix()
tm.CreatedAt = tm.UpdatedAt
tm.UpdateID = TokenUpdateID.Increment()
case *bun.UpdateQuery:
tm.UpdatedAt = time.Now().Unix()
tm.UpdateID = TokenUpdateID.Increment()
}
return nil
}

0 comments on commit be6377a

Please sign in to comment.