Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copyRecord #1976

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion IHP/ModelSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,4 +1125,23 @@ onlyWhereReferencesMaybe field referenced records = filter (\record -> get field
-- > |> isValid
--
isValid :: forall record. (HasField "meta" record MetaBag) => record -> Bool
isValid record = isEmpty record.meta.annotations
isValid record = isEmpty record.meta.annotations

-- | Copies all the fields (except the 'id' field) into a new record
--
-- Example: Duplicate a database record (except the primary key of course)
--
-- > project <- fetch projectId
-- > duplicatedProject <- createRecord (copyRecord project)
--
copyRecord :: forall record id. (Table record, SetField "id" record id, Default id, SetField "meta" record MetaBag) => record -> record
copyRecord existingRecord =
let
fieldsExceptId = (columnNames @record) |> filter (\field -> field /= "id")

meta :: MetaBag
meta = def { touchedFields = map (IHP.NameSupport.columnNameToFieldName . cs) fieldsExceptId }
in
existingRecord
|> set #id def
|> set #meta meta