Skip to content

Instantly share code, notes, and snippets.

@murphybytes
Created November 8, 2016 16:26
Show Gist options
  • Save murphybytes/8bceef5d48f75519cdfedad2e4536a67 to your computer and use it in GitHub Desktop.
Save murphybytes/8bceef5d48f75519cdfedad2e4536a67 to your computer and use it in GitHub Desktop.
Impelment db traits
package kolide
import (
"time"
"github.com/WatchBeam/clock"
)
type Createable interface {
MarkCreated(clock.Clock)
}
// Createable contains common timestamp fields indicating create time
type CreateInfo struct {
CreatedAt time.Time `json:"-" db:"created_at"`
}
// MarkCreated sets timestamp, intended to be called when Createable record is
// initially inserted in database
func (ci *CreateInfo) MarkAsCreated(c clock.Clock) {
if ci.CreatedAt.IsZero() {
ci.CreatedAt = c.Now()
}
}
type Deleteable interface {
MarkDeleted(clock.Clock)
}
// Deleteable is used to indicate a record is deleted. We don't actually
// delete record in the database. We mark it deleted, records with Deleted
// set to true will not normally be included in results
type DeleteInfo struct {
DeletedAt *time.Time `json:"-" db:"deleted_at" gorm:"-"`
Deleted bool
}
// MarkDeleted indicates a record is deleted. It won't actually be removed from
// the database, but won't be returned in result sets.
func (d *DeleteInfo) MarkDeleted(c clock.Clock) {
t := c.Now()
d.DeletedAt = &t
d.Deleted = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment