gormodel

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2020 License: MIT Imports: 3 Imported by: 0

README

gormodel

Maybe some useful toolkits for GORM.

Documentation

Overview

Package gormodel toolkits.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindContext

func BindContext(ctx context.Context, db *gorm.DB) (c context.Context)

BindContext bind db to context.Context.

func ExtractContext

func ExtractContext(ctx context.Context) (db *gorm.DB)

ExtractContext extracts db from context.Context.

Types

type Actions added in v0.2.0

type Actions interface {
	Creator
	Finder
	Updater
	Remover
}

Actions interface for CRUD

type Base

type Base struct {
}

Base operations helpers.

func (*Base) Create

func (b *Base) Create(ctx context.Context, i interface{}) error

Create action

func (*Base) FirstOrCreate added in v0.2.0

func (b *Base) FirstOrCreate(ctx context.Context, i interface{}) error

FirstOrCreate create if exists or return first match

func (*Base) Get added in v0.2.0

func (b *Base) Get(ctx context.Context, i interface{}) error

Get return record with first match.

func (*Base) GetByID

func (b *Base) GetByID(ctx context.Context, i interface{}, id ...uint) error

GetByID return records with given id.

func (*Base) Remove

func (b *Base) Remove(ctx context.Context, i interface{}) error

Remove record

func (*Base) UpdateAttrs added in v0.2.0

func (b *Base) UpdateAttrs(ctx context.Context, i interface{}, attrs map[string]interface{}) error

UpdateAttrs updates given attrs.

func (*Base) UpdateOrCreate added in v0.2.0

func (b *Base) UpdateOrCreate(ctx context.Context, i interface{}) error

UpdateOrCreate record

type Creator added in v0.2.0

type Creator interface {
	Create(context.Context, interface{}) error
	FirstOrCreate(context.Context, interface{}) error
}

Creator for create actions.

type Finder added in v0.2.0

type Finder interface {
	GetByID(context.Context, interface{}, ...uint) error
	Get(context.Context, interface{}) error
}

Finder for find actions.

type Model

type Model struct {
	ID        uint       `gorm:"PRIMARY_KEY" json:"id"`
	CreatedAt time.Time  `json:"createdAt"`
	UpdatedAt time.Time  `json:"updatedAt"`
	DeletedAt *time.Time `sql:"index" json:"-"`
}

Model likes gorm.Model but with json little camel-case keys.

Example
package main

import (
	"github.com/ipfans/gormodel"
)

func main() {
	type User struct {
		gormodel.Model

		Name string
	}
}
Output:

type Modeler added in v0.2.0

type Modeler interface {
	TableName() string
}

Modeler for table name.

type Remover added in v0.2.0

type Remover interface {
	Remove(context.Context, interface{}) error
}

Remover for remove actions.

type Updater added in v0.2.0

type Updater interface {
	UpdateOrCreate(context.Context, interface{}) error
	UpdateAttrs(context.Context, interface{}, map[string]interface{}) error
}

Updater for update actions.

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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