Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#508] Add Restore method for Orm, to recover soft deleted data #775

Merged
merged 7 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions contracts/database/orm/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@ import (
type EventType string

const (
EventRetrieved EventType = "retrieved"
EventCreating EventType = "creating"
EventCreated EventType = "created"
EventUpdating EventType = "updating"
EventUpdated EventType = "Updated"
EventSaving EventType = "saving"
EventSaved EventType = "saved"
// Create events
EventCreating EventType = "creating"
EventCreated EventType = "created"

// Update events
EventUpdating EventType = "updating"
EventUpdated EventType = "updated"

// Save events
EventSaving EventType = "saving"
EventSaved EventType = "saved"

// Delete events
EventDeleting EventType = "deleting"
EventDeleted EventType = "deleted"
EventForceDeleting EventType = "force_deleting"
EventForceDeleted EventType = "force_deleted"

// Restore events
EventRestoring EventType = "restoring"
EventRestored EventType = "restored"

// Retrieve events
EventRetrieved EventType = "retrieved"
)

type Event interface {
Expand Down
59 changes: 45 additions & 14 deletions contracts/database/orm/observer.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,61 @@
package orm

type Observer interface {
// Retrieved called when the model is retrieved from the database.
Retrieved(Event) error
// Creating called when the model is being created.
Creating(Event) error
// Created called when the model has been created.
Created(Event) error
// Updating called when the model is being updated.
Updating(Event) error
// Updated called when the model has been updated.
Updated(Event) error
// Saving called when the model is being saved.
Saving(Event) error
// Saved called when the model has been saved.
Saved(Event) error
// Deleting called when the model is being deleted.
Deleting(Event) error
// Deleted called when the model has been deleted.
Deleted(Event) error
// ForceDeleting called when the model is being force deleted.
ForceDeleting(Event) error
// ForceDeleted called when the model has been force deleted.
ForceDeleted(Event) error
}

type ObserverWithCreating interface {
// Creating called when the model is being created.
Creating(Event) error
}

type ObserverWithDeleting interface {
// Deleting called when the model is being deleted.
Deleting(Event) error
}

type ObserverWithForceDeleting interface {
// ForceDeleting called when the model is being force deleted.
ForceDeleting(Event) error
}

type ObserverWithRestored interface {
// Restored called when the model has been restored.
Restored(Event) error
}

type ObserverWithRestoring interface {
// Restoring called when the model is being restored.
Restoring(Event) error
}

type ObserverWithRetrieved interface {
// Retrieved called when the model is retrieved from the database.
Retrieved(Event) error
}

type ObserverWithSaved interface {
// Saved called when the model has been saved.
Saved(Event) error
}

type ObserverWithSaving interface {
// Saving called when the model is being saved.
Saving(Event) error
}

type ObserverWithUpdating interface {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't initiate these methods in the observer file. Add them according to requirement.

// Updating called when the model is being updated.
Updating(Event) error
}

type ModelToObserver struct {
Model any
Observer Observer
Expand Down
2 changes: 2 additions & 0 deletions contracts/database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ type Query interface {
Pluck(column string, dest any) error
// Raw creates a raw query.
Raw(sql string, values ...any) Query
// Restore restores a soft deleted model.
Restore(model ...any) (*Result, error)
// Rollback rolls back the changes in a transaction.
Rollback() error
// Save updates value in a database
Expand Down
28 changes: 0 additions & 28 deletions database/console/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,18 @@ import (

type DummyObserver struct{}

func (u *DummyObserver) Retrieved(event orm.Event) error {
return nil
}

func (u *DummyObserver) Creating(event orm.Event) error {
return nil
}

func (u *DummyObserver) Created(event orm.Event) error {
return nil
}

func (u *DummyObserver) Updating(event orm.Event) error {
return nil
}

func (u *DummyObserver) Updated(event orm.Event) error {
return nil
}

func (u *DummyObserver) Saving(event orm.Event) error {
return nil
}

func (u *DummyObserver) Saved(event orm.Event) error {
return nil
}

func (u *DummyObserver) Deleting(event orm.Event) error {
return nil
}

func (u *DummyObserver) Deleted(event orm.Event) error {
return nil
}

func (u *DummyObserver) ForceDeleting(event orm.Event) error {
return nil
}

func (u *DummyObserver) ForceDeleted(event orm.Event) error {
return nil
}
Expand Down
Loading
Loading