store

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2025 License: MIT Imports: 10 Imported by: 0

README

Store 层

因为 Store 层代码相对不易变,所以,Store 层很少会随着项目迭代,衍生出 V2 版本,因此 Store 层只需要一个版本即可。

Store 层开发规范

  • 每一个数据库表对应一个 Store 层资源,资源的CURD代码按资源分文件保存;
  • 每个 Store 层资源方法均包括标准的CURD方法和扩展方法,例如:
// PostStore 定义了 post 模块在 store 层所实现的方法.
type PostStore interface {
    Create(ctx context.Context, obj *model.PostM) error
    Update(ctx context.Context, obj *model.PostM) error
    Delete(ctx context.Context, opts *where.Options) error
    Get(ctx context.Context, opts *where.Options) (*model.PostM, error)
    List(ctx context.Context, opts *where.Options) (int64, []*model.PostM, error)

    PostExpansion
}

// PostExpansion 定义了帖子操作的附加方法.
type PostExpansion interface{}
  • Store 层方法继承自 genericstore,如果需要自定义逻辑,可重新方法。

Documentation

Overview

nolint: dupl

nolint: dupl

Index

Constants

This section is empty.

Variables

View Source
var ProviderSet = wire.NewSet(NewStore, wire.Bind(new(IStore), new(*datastore)))

ProviderSet is a Wire provider set that declares dependency injection rules. It includes the NewStore constructor function to generate datastore instances. wire.Bind is used to bind the IStore interface to the concrete implementation *datastore, allowing automatic injection of *datastore instances wherever IStore is required.

View Source
var (

	// S is a global variable for convenient access to the initialized datastore
	// instance from other packages.
	S *datastore
)
View Source
var (
	SetterProviderSet = wire.NewSet(NewSecretSetter, wire.Bind(new(auth.TemporarySecretSetter), new(*secretSetter)))
)

Functions

func NewSecretSetter

func NewSecretSetter(store *datastore) *secretSetter

NewSecretSetter initializes a new secretSetter instance using the provided datastore.

func NewStore

func NewStore(db *gorm.DB) *datastore

NewStore initializes a singleton instance of type IStore. It ensures that the datastore is only created once using sync.Once.

Types

type IStore

type IStore interface {
	// DB returns the *gorm.DB instance of the Store layer, which might be used in rare cases.
	DB(ctx context.Context, wheres ...where.Where) *gorm.DB
	// TX is used to implement transactions in the Biz layer.
	TX(ctx context.Context, fn func(ctx context.Context) error) error
	// User returns an implementation of the UserStore.
	User() UserStore
	// Secret returns an implementation of the SecretStore.
	Secret() SecretStore
}

IStore defines the methods that the Store layer needs to implement.

type SecretExpansion

type SecretExpansion interface{}

SecretExpansion is an empty interface provided for extending the SecretStore interface. Developers can define secret-specific additional methods in this interface for future expansion.

type SecretStore

type SecretStore interface {
	// Create inserts a new Secret record into the store.
	Create(ctx context.Context, obj *model.SecretM) error

	// Update modifies an existing Secret record in the store based on the given model.
	Update(ctx context.Context, obj *model.SecretM) error

	// Delete removes Secret records that satisfy the given query options.
	Delete(ctx context.Context, opts *where.Options) error

	// Get retrieves a single Secret record that satisfies the given query options.
	Get(ctx context.Context, opts *where.Options) (*model.SecretM, error)

	// List retrieves a list of Secret records and their total count based on the given query options.
	List(ctx context.Context, opts *where.Options) (int64, []*model.SecretM, error)

	// SecretExpansion is a placeholder for extension methods for secrets,
	// to be implemented by additional interfaces if needed.
	SecretExpansion
}

SecretStore defines the interface for managing secret-related data operations.

type UserExpansion

type UserExpansion interface{}

UserExpansion is an empty interface provided for extending the UserStore interface. Developers can define user-specific additional methods in this interface for future expansion.

type UserStore

type UserStore interface {
	// Create inserts a new User record into the store.
	Create(ctx context.Context, obj *model.UserM) error

	// Update modifies an existing User record in the store based on the given model.
	Update(ctx context.Context, obj *model.UserM) error

	// Delete removes User records that satisfy the given query options.
	Delete(ctx context.Context, opts *where.Options) error

	// Get retrieves a single User record that satisfies the given query options.
	Get(ctx context.Context, opts *where.Options) (*model.UserM, error)

	// List retrieves a list of User records and their total count based on the given query options.
	List(ctx context.Context, opts *where.Options) (int64, []*model.UserM, error)

	// UserExpansion is a placeholder for extension methods for users,
	// to be implemented by additional interfaces if needed.
	UserExpansion
}

UserStore defines the interface for managing user-related data operations.

Jump to

Keyboard shortcuts

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