DemoGen.TimestampHelpers (DemoGen v0.2.0)
Helpers for setting timestamps on entities after they've been created via demo commands. Uses SQL directly.
Summary
Functions
Convenience function that calls a context function and sets demo timestamps.
Similar to create_with_timestamps/3 but only sets updated_at for updates.
Sets timestamps on an entity using direct SQL update.
Sets only the updated_at timestamp for update operations.
Functions
Convenience function that calls a context function and sets demo timestamps.
This combines calling your existing context function with timestamp setting in a single function call.
Parameters
context_fun: A zero-arity function that calls your existing context functiontime: The DateTime to use for timestampsrepo: The app Repo module
Examples
Instead of calling context function + timestamp helper separately:
with {:ok, item} <- TimestampHelpers.create_with_timestamps( fn -> Items.create_item(user, attrs) end, time, MyApp.Repo ) do {:ok, %{context | symbols: Map.put(symbols, :item, item)}} end
Similar to create_with_timestamps/3 but only sets updated_at for updates.
Sets timestamps on an entity using direct SQL update.
This function updates the inserted_at and updated_at fields of an entity to use the provided time, then returns the entity struct with the updated timestamp fields.
Parameters
entity: The Ecto struct that was just created/updatedtime: The DateTime to use for inserted_at and updated_atrepo: The app Repo Module
Examples
# In a demo command after calling your context function:
with {:ok, item} <- Items.create_item(user, attrs) do
updated_item = TimestampHelpers.with_timestamps(item, time, MyApp.Repo)
{:ok, %{context | symbols: Map.put(symbols, :item, updated_item)}}
end
# Works with any entity type:
updated_user = TimestampHelpers.with_timestamps(user, time, MyApp.Repo)
updated_org = TimestampHelpers.with_timestamps(org, time, MyApp.Repo)Returns
-> entity struct with updated inserted_at and updated_at fields.
Sets only the updated_at timestamp for update operations.
Use this for demo commands that update existing entities rather than create new ones.
Parameters
entity: The Ecto struct that was just updatedtime: The DateTime to use for updated_atrepo: The app Repo module
Examples
with {:ok, item} <- Items.update_item(item, attrs) do updated_item = TimestampHelpers.with_updated_at(item, time, MyApp.Repo) {:ok, %{context | symbols: Map.put(symbols, :item, updated_item)}} end