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

create_with_timestamps(context_fun, time, repo)

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 function
  • time: The DateTime to use for timestamps
  • repo: 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

update_with_timestamps(context_fun, time, repo)

Similar to create_with_timestamps/3 but only sets updated_at for updates.

with_timestamps(entity, time, repo)

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/updated
  • time: The DateTime to use for inserted_at and updated_at
  • repo: 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.

with_updated_at(entity, time, repo)

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 updated
  • time: The DateTime to use for updated_at
  • repo: 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