使用 sqlboiler 实现通用存储库模式 [关闭]

问题描述 投票:0回答:0

我有一张这样的桌子

product

CREATE TABLE IF NOT EXISTS "products" (
  "id"  BIGINT PRIMARY KEY,
  "sku"         TEXT        NOT NULL,
  "name"        TEXT        NOT NULL CONSTRAINT "name_check" CHECK ( "name" <> ''::TEXT ),
  "created_at"  TIMESTAMPTZ NOT NULL DEFAULT now(),
  "updated_at"  TIMESTAMPTZ NOT NULL DEFAULT now(),
  "deleted_at"  TIMESTAMPTZ
);

我使用

sqlboiler
为此表生成了ORM,并为
entity
(数据库表的ORM)创建了接口,如下所示:

// Entity interface for ORM model
type Entity interface {
    Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error)

    Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error)

    Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error

    Reload(ctx context.Context, exec boil.ContextExecutor) error

    Update(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) (int64, error)

    Upsert(ctx context.Context, exec boil.ContextExecutor, updateOnConflict bool, conflictColumns []string, updateColumns boil.Columns, insertColumns boil.Columns) error
}

然后我创建一个公共存储库

// Repository is an object that has implemented some data access method
type Repository[T entity.Entity] struct {
    db       *sql.DB
    newQuery func(mods ...qm.QueryMod) entity.Query[T]
}

// New function initialize a new Repository
func New[T entity.Entity](db *sql.DB, newQuery func(mods ...qm.QueryMod) entity.Query[T]) Repository[T] {
    return Repository[T]{
        db:       db,
        newQuery: newQuery,
    }
}

我希望我能像

productRepo := base.Repository[entity.Product]
一样使用它,但我不能。

entity.Product
不满足
entity.Entity
, 它的方法有指针接收器。

所以我只能用

productRepo := base.Repository[*entity.Product]
,有些情况会导致死锁错误。

如何删除此语句中的

pointer operator

go repository-pattern sqlboiler
© www.soinside.com 2019 - 2024. All rights reserved.