volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

HasCreatedAt, HasUpdatedAt, HasDeletedAt in template #304

Closed saulortega closed 6 years ago

saulortega commented 6 years ago

Because the created_at, updated_at and deleted_at columns can be considered "special" columns, they are useful for some custom template actions.

So, it would be good if there were variables that indicate if these columns exist. That way, I can create templates like:

{{ if .HasDeletedAt }}
o.DeletedAt = null.NewTime(time.Now(), true)
o.Update(exec, "deleted_at")
{{ else }}
o.Delete(exec)
{{ end }}

That, considering the limitations of the template logic: I can not redefine a variable (unless I use a function)

aarondl commented 6 years ago

https://github.com/volatiletech/sqlboiler/blob/d7d6aa4c3ab274f606830afeabde5709aa566c35/boilingcore/templates.go#L273 https://github.com/volatiletech/sqlboiler/blob/d7d6aa4c3ab274f606830afeabde5709aa566c35/strmangle/sets.go#L3-L4

Example usage:

{{$hasDeletedAt := setInclude "deleted_at" (columnNames .Table.Columns)}}

I think this should work for your use case. Re-open if insufficient.

saulortega commented 6 years ago

Nice. :)

Thank you.