ZennerIoT / ex_audit

Ecto auditing library that transparently tracks changes and can revert them.
MIT License
368 stars 110 forks source link

feat: allow option `limit` in history/2 #64

Closed brunohkbx closed 3 years ago

narrowtux commented 3 years ago

Hi @brunohkbx. Thanks for the PR. I don't think a limit option makes sense for this function, except for when you pass render_struct: false. However, if you only need the version entries, it's simpler to just write the desired query yourself:

query = from v in MyApp.Version,
  where: v.entity_id == ^id,
  where: v.entity_schema == ^struct,
  order_by: [desc: v.recorded_at],
  limit: 5

MyApp.Repo.all(query)

Maybe it also makes sense if we have a utility function that generates that query for you from a given struct, to which you could then add further modifications such as limit, maybe like this:

query = ExAudit.history_query(my_struct)
query = limit(query, 5)
MyApp.Repo.all(query)