zoran119
zoran119

Reputation: 11327

how to implement soft deletes

Can anyone tell me what would be a good way to implement soft deletes? I can have a deleted property in my class, but my question is how to easily ignore instances which have deleted = true in my searches, listings and so on.

So, rather than saying Domain.findByDeleted(true) just get Domain.list() to ignore deleted instances, rather than saying Domain.findByPropertyAndDeleted('property', true) just say Domain.findByProperty('property').

Is there a nice way of doing this?

Upvotes: 3

Views: 1345

Answers (3)

Dónal
Dónal

Reputation: 187409

The hibernate filter plugin can automatically add the predicate deleted = false to every query that is performed for a particular domain class. However, my testing indicated that this plugin does not work with Grails 2.0.0.

Upvotes: 1

Victor Sergienko
Victor Sergienko

Reputation: 13495

We're used to override list(), get() and some more methods of domain classes. Now we can use syntax like A.delete(log: true)

On bootstrap, we do:

grailsApplication.domainClasses.each { GrailsDomainClass domainClassInfo ->
    def oldGormDelete = domainClassInfo.metaClass.getMetaMethod('delete', [] as Class[])
    assert oldGormDelete
    domainClassInfo.metaClass.delete = { Map params ->
        ...
        def result = oldGormDelete.invoke(delegate)
        ...
    }
}

Upvotes: 0

doelleri
doelleri

Reputation: 19702

I would recommend using a named query for this. Something like

static namedQueries = {
    notDeleted {
        ne 'deleted', true
    }
}

which you could use like Domain.notDeleted.list() or Domain.notDeleted.findByProperty(value)

Upvotes: 8

Related Questions