soft_delete

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2022 License: MIT Imports: 6 Imported by: 135

README

Soft Delete

https://gorm.io/docs/delete.html#Soft-Delete

import "gorm.io/plugin/soft_delete"

type User struct {
  ID        uint
  Name      string
  DeletedAt soft_delete.DeletedAt
}

// Query
SELECT * FROM users WHERE deleted_at = 0;

// Delete
UPDATE users SET deleted_at = /* current unix second */ WHERE ID = 1;
Specify Time Unit

We now support ms and ns timestamp when filling the deleted_at col, only need to specify tag gorm:"softDelete:milli" or gorm:"softDelete:nano".

type User struct {
  ID    uint
  Name  string
  DeletedAt soft_delete.DeletedAt `gorm:"softDelete:milli"`
  // DeletedAt soft_delete.DeletedAt `gorm:"softDelete:nano"`
}

// Query
SELECT * FROM users WHERE deleted_at = 0;

// Delete
UPDATE users SET deleted_at = /* current unix milli second or nano second */ WHERE ID = 1;

Flag Mode

flag mode will use 0, 1 to mark data as deleted or not, 1 means deleted

type User struct {
  ID    uint
  Name  string
  IsDel soft_delete.DeletedAt `gorm:"softDelete:flag"`
}

// Query
SELECT * FROM users WHERE is_del = 0;

// Delete
UPDATE users SET is_del = 1 WHERE ID = 1;

Mixed Mode

mixed mode will use 0, 1 to mark data as deleted or not, 1 means deleted, and store delete time

type User struct {
  ID        uint
  Name      string
  DeletedAt time.Time
  IsDel     soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
}

// Query
SELECT * FROM users WHERE is_del = 0;

// Delete
UPDATE users SET is_del = 1, deleted_at = /* current unix second */ WHERE ID = 1;

support mixed mode specify time unit, e.g. gorm:"softDelete:flag,DeletedAtField:DeletedAt" or gorm:"softDelete:flag,DeletedAtField:DeletedAt,DeletedAtFieldUnit:milli" or gorm:"softDelete:flag,DeletedAtField:DeletedAt,DeletedAtFieldUnit:nano".

type User struct {
  ID        uint
  Name      string
  DeletedAt int64
  IsDel     soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt,DeletedAtFieldUnit:milli"`
}

// Query
SELECT * FROM users WHERE is_del = 0;

// Delete
UPDATE users SET is_del = 1, deleted_at = /* current unix milli second second*/ WHERE ID = 1;

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FlagDeleted = 1
	FlagActived = 0
)

Functions

This section is empty.

Types

type DeletedAt

type DeletedAt uint

func (DeletedAt) DeleteClauses

func (DeletedAt) DeleteClauses(f *schema.Field) []clause.Interface

func (DeletedAt) QueryClauses

func (DeletedAt) QueryClauses(f *schema.Field) []clause.Interface

func (DeletedAt) UpdateClauses added in v1.0.3

func (DeletedAt) UpdateClauses(f *schema.Field) []clause.Interface

type SoftDeleteDeleteClause

type SoftDeleteDeleteClause struct {
	Field         *schema.Field
	Flag          bool
	TimeType      schema.TimeType
	DeleteAtField *schema.Field
}

func (SoftDeleteDeleteClause) Build

func (SoftDeleteDeleteClause) MergeClause

func (sd SoftDeleteDeleteClause) MergeClause(*clause.Clause)

func (SoftDeleteDeleteClause) ModifyStatement

func (sd SoftDeleteDeleteClause) ModifyStatement(stmt *gorm.Statement)

func (SoftDeleteDeleteClause) Name

func (sd SoftDeleteDeleteClause) Name() string

type SoftDeleteQueryClause

type SoftDeleteQueryClause struct {
	Field *schema.Field
}

func (SoftDeleteQueryClause) Build

func (SoftDeleteQueryClause) MergeClause

func (sd SoftDeleteQueryClause) MergeClause(*clause.Clause)

func (SoftDeleteQueryClause) ModifyStatement

func (sd SoftDeleteQueryClause) ModifyStatement(stmt *gorm.Statement)

func (SoftDeleteQueryClause) Name

func (sd SoftDeleteQueryClause) Name() string

type SoftDeleteUpdateClause added in v1.0.3

type SoftDeleteUpdateClause struct {
	Field *schema.Field
}

func (SoftDeleteUpdateClause) Build added in v1.0.3

func (SoftDeleteUpdateClause) MergeClause added in v1.0.3

func (sd SoftDeleteUpdateClause) MergeClause(*clause.Clause)

func (SoftDeleteUpdateClause) ModifyStatement added in v1.0.3

func (sd SoftDeleteUpdateClause) ModifyStatement(stmt *gorm.Statement)

func (SoftDeleteUpdateClause) Name added in v1.0.3

func (sd SoftDeleteUpdateClause) Name() string

Jump to

Keyboard shortcuts

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