Exposed: [Feature request] soft delete support

Created on 21 Jan 2017  路  6Comments  路  Source: JetBrains/Exposed

Hi, would it be possible to have software delete implemented in this library?
Also created_at updated_at support would be nice.

sample implementations: https://gist.github.com/logrusorgru/82b002b8807253b2adef

documentation enhancement

Most helpful comment

@BugsBunnyBR , I'm not sure about wide usefulness of this feature. Besides you can easily implement it by yourself. Try something like this:

abstract class ExtendedIntIdTable(name: String ) : IntIdTable(name) {

    val createdAt = datetime("createdAt").default(DateTime.now())
    val updatedAt = datetime("updatedAt").nullable()
    val deletedAt = datetime("deletedAt").nullable()

}

abstract class ExtendedIntEntity(id: EntityID<Int>, table: ExtendedIntIdTable) : IntEntity(id) {
    val createdAt by table.createdAt
    var updatedAt by table.updatedAt
    var deletedAt by table.deletedAt

    override fun delete() {
        deletedAt = DateTime.now()
    }
}

abstract class ExtendedIntEntityClass<E:ExtendedIntEntity>(table: ExtendedIntIdTable) : IntEntityClass<E>(table) {

    init {
        EntityHook.subscribe { action -> 
            if (action.changeType == EntityChangeType.Updated) 
                action.toEntity(this)?.updatedAt = DateTime.now()
        }
    }
}                         

object FooTable : ExtendedIntIdTable("Foo") {
    val bar = varchar("bar", 255).nullable()
} 

class FooEntity(id: EntityID<Int>) : ExtendedIntEntity(id, FooTable) {

    val bar by FooTable.bar

    companion object : ExtendedIntEntityClass<FooEntity>(FooTable)
}

All 6 comments

@BugsBunnyBR , I'm not sure about wide usefulness of this feature. Besides you can easily implement it by yourself. Try something like this:

abstract class ExtendedIntIdTable(name: String ) : IntIdTable(name) {

    val createdAt = datetime("createdAt").default(DateTime.now())
    val updatedAt = datetime("updatedAt").nullable()
    val deletedAt = datetime("deletedAt").nullable()

}

abstract class ExtendedIntEntity(id: EntityID<Int>, table: ExtendedIntIdTable) : IntEntity(id) {
    val createdAt by table.createdAt
    var updatedAt by table.updatedAt
    var deletedAt by table.deletedAt

    override fun delete() {
        deletedAt = DateTime.now()
    }
}

abstract class ExtendedIntEntityClass<E:ExtendedIntEntity>(table: ExtendedIntIdTable) : IntEntityClass<E>(table) {

    init {
        EntityHook.subscribe { action -> 
            if (action.changeType == EntityChangeType.Updated) 
                action.toEntity(this)?.updatedAt = DateTime.now()
        }
    }
}                         

object FooTable : ExtendedIntIdTable("Foo") {
    val bar = varchar("bar", 255).nullable()
} 

class FooEntity(id: EntityID<Int>) : ExtendedIntEntity(id, FooTable) {

    val bar by FooTable.bar

    companion object : ExtendedIntEntityClass<FooEntity>(FooTable)
}

With this solution, if someone changes the data directly in the database it would not have the data updated. The best option would be to have it in the create table as in the gist.

I'm not sure that ON UPDATE current_timestamp syntax will work on MySQL. I guess it will require to create trigger for that.

Nice, I don't know about that.

https://github.com/JetBrains/Exposed/wiki/DAO#auto-fill-created-and-updated-columns-on-entity-change

Was this page helpful?
0 / 5 - 0 ratings

Related issues

power721 picture power721  路  3Comments

gertvdijk picture gertvdijk  路  4Comments

michele-grifa picture michele-grifa  路  4Comments

supertote picture supertote  路  3Comments

yuri-li picture yuri-li  路  3Comments